// create a namespace
var easyAjax = {};

easyAjax.configure = function (action, payload) {
	switch (action){
		case 'index':
		payload.type = 'POST';
		payload.url = '/index.php';
		payload.async = true;
		payload.cache = false;
		payload.contentType = 'application/x-www-form-urlencoded';
		break;
		case 'admin':
		payload.type = 'POST';
		payload.url = '/admin/ajax_admin_index.php';
                payload.async = true;
		payload.cache = false;
		payload.contentType = 'application/x-www-form-urlencoded';
            break;
		case 'update':
		payload.type = 'POST';
		payload.url = '/index.php';
		break;
		case 'delete':
		payload.type = 'POST';
		payload.url = '/index.php';
		break;
	        case 'load':
		payload.type = 'GET';
		payload.url = '/index.php';
		payload.async = true;
		payload.cache = false;
		payload.contentType = 'application/x-www-form-urlencoded';
		break;
		}
	easyAjax.callAjax(payload);	
};

easyAjax.callAjax = function (payload) {
	var dataType = (payload.dataType !== undefined) ? payload.dataType : 'json';
	$.ajax({
		
		type: payload.type,
		url: payload.url,
		data: payload.data,
		dataType: dataType,
		async: payload.async,
		cache: payload.cache,
		contentType: payload.contentType,
		// Global beforeSend wrapper with user defined function
		beforeSend: function (){
			if (typeof(payload.beforeSend)=== 'function'){
				payload.beforeSend();
				}
			},
			// Global success wrapper with user defined function
		success: function (data, status) {
				if(data.errorCode === 0 || (typeof data.errorCode === 'undefined')){
					if (typeof(payload.success) === 'function'){
						payload.success(data, status);
					}
				} else {
					alert(data.errorMessage);
				}
			},
		complete: function () {
				if (typeof(payload.complete) === 'function'){
						payload.complete();
				}
			}
	});
};

var Display=function(data){
	$("span.price_for_change").each(function(){
		var Currency=this.innerHTML;
		var num_ready = Number(Currency.replace(/[^0-9\.]+/g,""));
		var new_value = num_ready*data['ratio'];
		if(data['decimal_places']){
			new_value = (new_value.toFixed(data['decimal_places'])).toString();
		} else {
			new_value = new_value.toString();	
		}
		if(!data['decimal_places']){
			data['decimal_places'] = 0;
		}
		if(data['thousands_point']){
			if(new_value.length>3+(1+parseInt(data['decimal_places']))){
				if(data['decimal_places']==0){
					var decimal_point_index=new_value.length;
				} else {
					decimal_point_index=new_value.indexOf(data['decimal_point']);
				}
				var end_substr=new_value.substring(decimal_point_index);
				var front_substr=new_value.substring(0,decimal_point_index);
				var front_substr = front_substr.replace(/\B(?=(?:\d{3})+(?!\d))/g, data['thousands_point']);
				new_value = front_substr + end_substr;
                                }
		}
		new_value = data['symbol_left']+new_value+data['symbol_right'];
		this.innerHTML = new_value;
	});
}
easyAjax.changeCurrency = function (sendData) {
	var bundle = {
	data:
	{
	'currency_id': sendData.currency_id,
	'changing_currency': sendData.changing_currency,
	'func': 'change_currency'
	},
	success: function(data){
		if(data==false){
			alert('error');
		} else {
			var title = sendData.elem.text();
			var background = sendData.elem.css('background-image');
			$('.drop_title', sendData.dropdown).attr('index', sendData.currency_id).css('background-image', background);
			$('.drop_title>em', sendData.dropdown).text(title);
			Display(data);
		}
		sendData.dropdown.css('height','22px');
		$('div.list_wrapper', sendData.dropdown).css({'visibility':'hidden','border-top':'none'});
	}
	};
	easyAjax.configure('index', bundle);
};






