// ------------------------ Ajax Engine ----------------------------------------


DailybeastAjax = {};
DailybeastAjax._count = 0;
DailybeastAjax._xArgs = {};

DailybeastAjax._xArray = {};


DailybeastAjax.makeAjaxCall = function(url) {
	
	DailybeastAjax._count++;
	
	DailybeastAjax._xArray[DailybeastAjax._count] = DailybeastAjax._xArgs;
	DailybeastAjax.doAjax(DailybeastAjax._count,url);
	
	 DailybeastAjax._xArgs = {};
	
}

	
DailybeastAjax.doAjax = function(countVar, url){
		
	//$('#ajaxhelper').animate({ opacity: .9999}, 0001, '', function() { 
		if(DailybeastAjax._xArray[countVar].loader){	
			DailybeastAjax.setLoaderOverlay(DailybeastAjax._xArray[countVar].returnDiv);
			DailybeastAjax._xArray[countVar].noloader = false;
		}
		var requestType = "POST";
		if(DailybeastAjax._xArray[countVar].requestType != null)
			requestType = DailybeastAjax._xArray[countVar].requestType;
		
		$.ajax({
			type:       requestType,
			url:        url,
			data:       DailybeastAjax._xArray[countVar].data,
			success:    function(p_sMsg) {
				
				//DailybeastAjax._tmp = DailybeastAjax._xArray[countVar];
				DailybeastAjax.onAjaxCallReturned(countVar, p_sMsg);
				
			}
		});

//	 });
	
}




DailybeastAjax.onAjaxCallReturned = function(countVar, p_sMsg) {
	
	
	if(DailybeastAjax._xArray[countVar].returnDiv){
		$(DailybeastAjax._xArray[countVar].returnDiv).html(p_sMsg);
	}
	
	DailybeastAjax.returnVar = p_sMsg;
	if(DailybeastAjax._xArray[countVar].callBack) {
		DailybeastAjax._xArray[countVar].callBack();
	}
	
	DailybeastAjax._xArray[countVar] = null;
}






DailybeastAjax.setLoaderOverlay = function( theElement ){
	var loadingImg = 'http://www.tdbimg.com/image/loading_ajax.gif';
	
	//$('#ajaxoverlay').remove();
	
	if( $(theElement).css('position') != 'absolute' )
		$(theElement).css('position','relative');
	
	padLeft = $(theElement).css('padding-left');
	padTop = $(theElement).css('padding-top');
	
	
	var l_nHeight = $(theElement).height();
	var l_nWidth = $(theElement).width();

	
	$(theElement).append(
		'<div id="ajaxoverlay" style="background:#fff;'+
		 			'width:'+l_nWidth+'px; '+
					'height:'+l_nHeight+'px; '+
		 			'position:absolute; '+
					'top:'+ padTop+'; '+
					'left:'+padLeft+'; '+
					'z-index:1200; '+
					'filter:alpha(opacity=75);-moz-opacity:.75;opacity:.75;'+
					'"></div> '
		);
	
	$(theElement).append(
		'<img id="ajaxloader" src="'+loadingImg+'" style="'+
		 			'position:absolute; '+
					'margin-top:'+ padTop+'; '+
					'margin-left:'+padLeft+'; '+
		 			'left:'+(l_nWidth - 51) / 2+'px; '+
					'top:'+(l_nHeight - 64) / 2+'px; '+
					'z-index:1201; '+
					'" /> '
		);

	
	
	//alert ( $(theElement).offset({border: true, padding: true})['left'])
	//alert ( $(theElement).offset()['left'])
	
	
	//var l_aOffset = $(theElement).offset({border: true, padding: true});
	
	//alert(l_nHeight);
	
	/*
	$('body').append( 
		 '<div id="ajaxoverlay" style="background:#000; '+
		 			'width:'+l_nWidth+'px; '+
					'height:'+l_nHeight+'px; '+
		 			'position:absolute; '+
					'top:'+l_aOffset['top']+'px; '+
					'left:'+l_aOffset['left']+'px;'+
					'"></div> '
	);
	*/
	
}


