jQuery.noConflict();
// global vars :

//var ef;

//list of scripts already added
var ardfilesadded="";
var ardfilesbeingloaded="";
var ardfilesloadflag=false;
var ardscripts=null;
//window
var win =null; 

jQuery(document).ready(function () {
	//alert('onload');
    //ef=new evalFunc();
	// we handle googlemaps
	jQuery("script").each(function f() {
		if (jQuery(this).attr('src')!=undefined) ardfilesadded+="["+jQuery(this).attr('src')+".js]";
	});
	jQuery("link").each(function f() {
		if (jQuery(this).attr('href')!=undefined) ardfilesadded+="["+jQuery(this).attr('href')+".css]";
	});
	// post scripts;
	if (ardscripts!=null) {
		for (var i=0; i<ardscripts.length; i++) {
			eval(ardscripts[i]);
		}
	}
});

function ardLoadHourGlass() {
	jQuery("body").append('<div id="ardhourglass">&nbsp;</div>'); 
	jQuery("body").append('<div id="ardoverlay">&nbsp;</div>');
	jQuery("#ardoverlay").height(jQuery(document).height());
	jQuery("#ardhourglass").css('top',jQuery(document).scrollTop());
	jQuery("#ardoverlay").width(jQuery(document).width());
	jQuery("#ardhourglass").click(function () {ardUnloadHourGlass();});
	jQuery("#ardoverlay").click(function () {ardUnloadHourGlass();});
}

function ardUnloadHourGlass() {
	jQuery("#ardhourglass").remove();
	jQuery("#ardoverlay").remove();
	//jQuery("body").click(function() {alert('u');});
}

function ardActionForm(formid,method,actionUrl,actionPanel) {
	ardLoadHourGlass();
	var datastr = jQuery("#"+formid).serialize();
	//alert(datastr);
	jQuery.ajax({
		type: method,
		url: actionUrl,
		data: datastr,
		success: function(msg){
			eval("response="+msg);
			if (actionPanel!=null && actionPanel!='') response.actionPanel=actionPanel;
			ardHandleResponse(response);
			ardUnloadHourGlass();
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			ardHandleErrorResponse(XMLHttpRequest,textStatus,errorThrown);
			ardUnloadHourGlass();
		}
	});
	return false;
}

function ardActionLink(actionUrl,actionPanel) {
	ardLoadHourGlass();
	jQuery.ajax({
		type: "GET",
		url: actionUrl,
		data: "",
		success: function(msg){
			if (msg!='') {
				eval("response="+msg);
				if (actionPanel!=null && actionPanel!='') response.actionPanel=actionPanel;
				ardHandleResponse(response);
			}
			ardUnloadHourGlass();
		},
		error: function (XMLHttpRequest, textStatus, errorThrown) {
			ardHandleErrorResponse(XMLHttpRequest,textStatus,errorThrown);
			ardUnloadHourGlass();
		}

	});
	return false;
}
function  ardHandleErrorResponse(XMLHttpRequest,textStatus,errorThrown) {
	//alert(textStatus+' '+errorThrown);
	ardDialog('Une erreur s\'est produite !',textStatus+' '+errorThrown);
	/*jQuery('#ard-dialog').attr('title','Une erreur s\'est produite !');
	jQuery('#ard-dialog').html(textStatus+' '+errorThrown);
	jQuery('#ard-dialog').dialog();
	*/
	/*Ext.Msg.show({
		title:'Une erreur s\'est produite !',
		msg:textStatus+' '+errorThrown,
		buttons: Ext.Msg.OK,
		icon:Ext.Msg.ERROR
		}
	);*/
 }

function ardSleep(millis) {
    var notifier = new EventNotifier();
    setTimeout(notifier, millis);
    notifier.wait();
} 

function ardDialog(title,body,js) {
	//params=eval(json);
	jQuery('#ard-dialog').dialog('destroy');
	jQuery('#ard-dialog').attr('title',title);
	jQuery('#ard-dialog').html(body);
	//jQuery('#ard-dialog').ardjs=js;
	jQuery('#ard-dialog').dialog({zIndex:5000,bgiframe: true,autoOpen:false,resizable:false,height:140,modal: true,overlay: {backgroundColor: '#000',opacity: 0.5},buttons: {'Confirmer': function() {jQuery(this).dialog('close');eval(js);},'Annuler': function() {jQuery(this).dialog('close');}}});
	jQuery('#ard-dialog').dialog('open');
}
function ardHandleResponse(response) {
    ardfilesloadflag=false;
	// We dynamically load required css && js.
	if (response.libraries!=null) {
		for (var i=0; i<response.libraries.length; i++) {
			ardautoload(response.libraries[i]);
		}
	}
	// action Panel replacement
	if (response.actionPanel!=null) jQuery("#"+response.actionPanel).replaceWith(response.content);
	// hide
	if (response.hide!=null) {
		for (var i=0; i<response.hide.length; i++) {
			jQuery(response.hide[i]).hide();
		}
	}
	// show
	if (response.show!=null) {
		for (var i=0; i<response.show.length; i++) {
			jQuery(response.show[i]).show();
		}
	}	
	// post scripts;
	if (response.scripts!=null && !ardfilesloadflag) {
		for (var i=0; i<response.scripts.length; i++) {
			eval(response.scripts[i]);
		}
	}

	if (response.innerHTML!=null) {
		for (var i=0; i<response.innerHTML.length; i++) {
			jQuery(response.innerHTML[i][0]).replaceWith(response.innerHTML[i][1]);
		}
	}
	if (response.outerHTML!=null) {
		for (var i=0; i<response.outerHTML.length; i++) {
			jQuery(response.outerHTML[i][0]).replaceWith(response.outerHTML[i][1]);
		}
	}
}

function ardautoload(file) {
 filename=file.substring(0,file.lastIndexOf("."));
 ext=file.substring(file.lastIndexOf(".")+1);
 ardcheckloadjscssfile(filename,ext);
}

function ardcheckloadjscssfile(filename, filetype){
 if (ardfilesadded.indexOf("["+filename+"."+filetype+"]")==-1){
	ardloadjscssfile(filename, filetype);
	ardfilesloadflag=true;
	ardfilesadded+="["+filename+"."+filetype+"]"; //List of files added in the form "[filename1],[filename2],etc"
}
 /*
 else
  alert("file : "+filename +"." + filetype + ", already added!")
*/
}
var an=0;
function ardloadjsfileloaded(){
	filename=this.getAttribute("src");
	if (ardfilesbeingloaded==undefined) ardfilesbeingloaded="";
	//an++;
	
	if (ardfilesbeingloaded.indexOf("["+filename+"]")>=0) {
		ardfilesbeingloaded=ardfilesbeingloaded.replace("["+filename+"]","");
		//alert('t:'+filename+', tfbl:'+ardfilesbeingloaded+',a:'+an+',tl:'+ardfilesbeingloaded.length)
		if (ardfilesbeingloaded.length==0) {
			//alert("all files loaded : "+filename);
			// post scripts;
			if (response.scripts!=null) {
				for (var i=0; i<response.scripts.length; i++) {
					eval(response.scripts[i]);
				}
			}
		}
	}
	//if (ardfilesbeingloaded.length==0) alert("aall files loaded : "+filename);
    //alert('f:'+filename+'i:'+ardfilesbeingloaded.indexOf("["+filename+"]")+', fbl:'+ardfilesbeingloaded+',a:'+an+',l:'+ardfilesbeingloaded.length);
}

function ardloadjscssfile(filename, filetype){
 if (filetype=="js"){ //if filename is a external JavaScript file
  var script=document.createElement('script');
  ardfilesbeingloaded+="["+filename+"]";
  script.setAttribute("type","text/javascript");
  script.setAttribute("src", filename);
  script.onreadystatechange= function () {

    //alert('lf:'+filename+',fa:'+ardfilesadded);
    //if (this.readyState == 'complete') ardloadjsfileloaded();
  }
  script.onload= ardloadjsfileloaded;
  if (typeof script!="undefined")
  document.getElementsByTagName("head")[0].appendChild(script)
 }
 else if (filetype=="css"){ //if filename is an external CSS file
  var link=document.createElement("link")
  link.setAttribute("rel", "stylesheet")
  link.setAttribute("type", "text/css")
  link.setAttribute("href", filename)
  if (typeof link!="undefined")
  document.getElementsByTagName("head")[0].appendChild(link)
 }
 
}

function ardunloadjscssfile(filename, filetype){
 var targetelement=(filetype=="js")? "script" : (filetype=="css")? "link" : "none" //determine element type to create nodelist from
 var targetattr=(filetype=="js")? "src" : (filetype=="css")? "href" : "none" //determine corresponding attribute to test for
 var allsuspects=document.getElementsByTagName(targetelement)
 for (var i=allsuspects.length; i>=0; i--){ //search backwards within nodelist for matching elements to remove
  if (allsuspects[i] && allsuspects[i].getAttribute(targetattr)!=null && allsuspects[i].getAttribute(targetattr).indexOf(filename)!=-1)
   allsuspects[i].parentNode.removeChild(allsuspects[i]) //remove element by calling parentNode.removeChild()
 }
}

// Ext
function ardExtWindow(json) {
	if (win!=null) {
		win.close();
		win=null;
	}
	if(win==null){
       	win = new Ext.Window(json);
		win.show(this);
       }   
}
