var CurLoad = {id: 'cursor_loader'};

function $T() {
	var a = [];
	$A(arguments).each(function(i) {
		a.push($A(document.getElementsByTagName(i)));
	}); return(a = a.flatten());
};

function ajax_url(url,target){
	new Ajax.Updater(target,url,{
		onCreate  : function() {
			CurLoad.show ? CurLoad.show() : Element.show(CurLoad.id);
		},
		onComplete: function() {
			CurLoad.hide ? CurLoad.hide() : Element.hide(CurLoad.id);
		},
		asynchronous:true, evalScripts:true		
	});
}

function ajax_update(className,action,vars,target){
	args = 'args[]='+vars.join('&args[]=');
	var url = 'index.php?ajax=true&mod='+className+'&action='+action+'&'+args;
	ajax_url(url,target);
}

function ajax_onceToggle(className,action,vars,target){
	var objTarget;
	
	if (objTarget = $(target)){	
		if (objTarget.innerHTML.length<100){
			if (objTarget.innerHTML.replace(/^\s+|\s+$/g,"").length==0){
				ajax_update(className,action,vars,target);
				if (objTarget.style.display=='none') expandToggle(target);
			}
		}
		
		else expandToggle(target);
	}
}
function ajax_request(className,action,vars,options){

	//setLoading(document.body);

	args = 'args[]='+vars.join('&args[]=');
	var url = 'index.php?mod='+className+'&action='+action+'&'+args;
	var settings = new Object();
	if (options) settings = options;
	settings.onCreate  = function() {
			CurLoad.show ? CurLoad.show() : Element.show(CurLoad.id);
		};
	settings.onComplete= function() {
			CurLoad.hide ? CurLoad.hide() : Element.hide(CurLoad.id);
		};
	settings.asynchronous=true;
	settings.evalScripts=true;
	
	new Ajax.Request(url, settings);
}



var LoaderObs = {
	initialize: function() {
		Element.insert($T('body')[0] ,{ bottom: '<div id="'+CurLoad.id+'"><img src="img/loader_c.gif" /></div>'});	
		$(CurLoad.id).setStyle({opacity: 0.8});
		Event.observe(document, 'mousemove', LoaderObs.dostuff);
	},
	
	dostuff: function(event) {
		$(CurLoad.id).setStyle({ 'left': (Event.pointerX(event)+15)+'px', 'top': (Event.pointerY(event)+15)+'px'});		
	},
	
	stopObs: function() {
		Event.stopObserving(document, 'mousemove', LoaderObs.dostuff);
	}
}

Event.observe(window,'load',function(){ LoaderObs.initialize();},false);