//global ajaxCalls array which saves informations for each ajaxcall:
// ajaxCalls[iceId]['waitstatus'] //for handling the waitingmessage effect: 
																		//nowait -> no waitmessage shown
																		//busy ->  waitmessage effect is active -> we must wait
																		//ready -> waitmessage effect is ready => new content can be shown
//ajaxCalls[iceId]['active'] // 0 or 1 -> We only make new Ajaxcall to server is active=0 (1 indicates that the ajaxcall for this iceId is not ready)
//ajaxCalls[iceId]['dceId']	 // effecthandling: if a waitmessage should be shown this id is neccessary (dceId is foldout and the waitmessage is generated for the parentId of this dceId)
	
 			
		
	var ajaxCalls=new Array();
	
	function xajaxCall(iceId,params,displayWait,dceId) {
		if (!loadedAll) {
			return;
		}
		if (danpfeAjaxDebug) alert('xajax Call iceId:'+iceId+' Params:'+params);
		if (!ajaxCalls[iceId]) {
			ajaxCalls[iceId]=new Array();                
		}
    	if (ajaxCalls[iceId]['active']==1) {
    		return;
    	}
    	ajaxCalls[iceId]['active']=1;  
    	ajaxCalls[iceId]['waitstatus']='nowait';
    	if (displayWait && dceId) {
    		ajaxCalls[iceId]['waitstatus']='busy';
    		ajaxCalls[iceId]['dceId']=dceId;	      
        	var myEffect = new fx.Height(dceId , {duration: 500, onComplete: function () {displayWaitInContainer(iceId)}});
       		myEffect.toggle();		       		               
    	} 
    	//Scroll to the contentid if given:
    	if (dceId) {
       		// var myScrollEffect = new fx.Scroll();
	     	// myScrollEffect.scrollTo(dceId); 
	    }
      
    	var arguments=new Array();
    	arguments[0]=iceId;
    
    	for(var i=1;i <= params.length;i++) {
    		//if a form should be posted:
    		if (params[i-1].substring(0,5)=='POST:') {
    			arguments[i]=xajax.getFormValues(params[i-1].substring(5));			
			}else {				
    			arguments[i]=params[i-1];
    		}	
    	}
    	//return xajax.call("handleXajaxRequest", arguments, 1, iceId);
    	return xajax.call(fe_user, arguments, 1,iceId);
    	// window.setTimeout("response('"+contentid+"')",5000);	
  	}
	


	function getParameter(Obj){
			if(!Obj.options)return;
			Opt = Obj.options;
			if(typeof(Opt) == 'object' && Opt.length) {
			for(var i = 0; i<Opt.length; i++){
					if(Opt[i].selected == true)
					return Opt[i].value;
			}
			}
	}
	

	function checkForAjax() {
		if(window.XMLHttpRequest)  {
		 // typeof XMLHttpRequest
		 return true;
		}
		else if (window.ActiveXObject) {
		 return true;
		}
		else {
		 return false;
		}
	}

var loadControl=new Array();
var loadControlLoops=0;
var loadControlState=true;
var loadedAll=false;


//loads a JavaScript on Demand... 
// optional you can pass a functionname which is used to check if the script was loaded
function loadJavaScript(file,checkvar) {
if (checkvar != '') {
	loadControl.push(checkvar);
	loadControlState=false;
}
var head = document.getElementsByTagName("head")[0];
		script = document.createElement('script');	
		script.type = 'text/javascript';
		script.src = file;
		head.appendChild(script)   	
	//document.write('<script type="text/javascript" src="'+file+'" ></script>');
}

// check (500ms*100 => 50sec) if the loaded JavaScripts are ready
// if they are ready the function calls start();

function waitAndStart() {
	var check=true;        	
	for(i=0;i<loadControl.length;i++) {      	
			eval('if (!self.'+loadControl[i]+') check=false');		
		}

	if (check==false ) {      			
		if (loadControlLoops<10) {
			loadControlLoops++;
			window.setTimeout('waitAndStart()','500');	
		}
		else {
			//load fails... :-(
			if (danpfeAjaxDebug) alert('loading script fails!');
		}
	}
	else {
		
		start();
	}      	
}



    
function start() {
 	loadedAll=true;
 	if (danpfeAjaxDebug) alert('all scripts loaded');
 	substituteLinks();
 	//substituteReplaces();
}

    



function displayWaitInContainer(iceId) { 
   if (ajaxCalls[iceId]['dceId']) {
     var waiter;
   	 contentid=ajaxCalls[iceId]['dceId'];           	 
   	   if (!ajaxCalls[iceId]['waitmessage']) {
   	     //first call for iceId=> create waiter div
      	   waiter=document.createElement('div');      	   		      	   
      	   waiter.innerHTML='<div class="feajax-message">loading <img src="'+danpfeAjaxPath+'img/spinner.gif"></div>';
      	   // waiter.setAttribute('class','message');      	       	  
      	   waiter.setAttribute('style','visibility:hidden;');
      	   //waiter.id=iceId;
      	   ajaxCalls[iceId]['waitmessage']=waiter;
      	   contentidobj=$(contentid);
      	   containerid=contentidobj.parentNode.id;		      	        	   
      	   $(containerid).appendChild(waiter);
    			}
    			else {
    			 waiter=ajaxCalls[iceId]['waitmessage'];
    			 waiter.innerHTML='<div class="feajax-message">loading <img src="'+danpfeAjaxPath+'img/spinner.gif"></div>';
    			}
  	   var myEffect = new fx.Opacity(waiter , {duration: 500, onComplete: function() { ajaxCalls[iceId]['waitstatus']='ready'; }}); // alert('set to ready'+ajaxCalls[iceId]['waitstatus']);
   	   myEffect.hide();	       	  
   	   myEffect.toggle();       	   
   	  
   }      
}
function finishAjaxRequest(iceId) { 
if (danpfeAjaxDebug) alert('xajax Call is ready for ICE:'+iceId+' waitstatus'+ajaxCalls[iceId]['waitstatus']);     
	if (ajaxCalls[iceId]['waitstatus']=='nowait') {
	  ajaxCalls[iceId]['active']=0;
	}
	else {     
		 hideWaitAndShowCounter=0;  	   		     	  
		hideWaitAndShow(iceId);
	}      
}

// hides The waitmessage and after that shows the content again
// (waits till effectstate is ready)
var hideWaitAndShowCounter=0;
function hideWaitAndShow(iceId) {    
	 //alert('testdamit warte '+ajaxCalls[iceId]['waitstatus']+'iceId'+iceId);       
	       
 if (ajaxCalls[iceId]['waitstatus']!='nowait') {
 
 		//we must wait for finishing effects...before Hiding the waitmessage again
     if (ajaxCalls[iceId]['waitstatus']!='ready') { 
      hideWaitAndShowCounter++;
      if (hideWaitAndShowCounter<5) {
  	  	window.setTimeout(function () { hideWaitAndShow(iceId);} ,'500');	
  	  }
  	  else {
  	  	if (danpfeAjaxDebug) alert('error hideWaitAndShow');
  	  }
  	 }
  	 else {        	
  		ajaxCalls[iceId]['waitmessage'].innerHTML='<div class="feajax-message">finished</div>';
  		 var myEffect = new fx.Opacity(ajaxCalls[iceId]['waitmessage'] , {duration: 500, onComplete:function () {foldDownContentForICE(iceId)}});	       	   
       	 myEffect.toggle();
       }
	}
}
//Finishs the waitermessage effect and sets ajaxCalls[iceId]['active']=0
function foldDownContentForICE(iceId) {
	//	if (danpfeAjaxDebug) alert('foldDownContentForICE ICE:'+ajaxCalls[iceId]['dceId']);           
		 var myEffect = new fx.Height(ajaxCalls[iceId]['dceId'] , {duration: 500, onComplete: function () {  ajaxCalls[iceId]['active']=0; }});      	
	 myEffect.toggle();      	
	
}




function substituteLinks() { 
	if (danpfeAjaxDebug) alert('start   substituteLinks');   
	var a = document.getElementsByTagName("a");
		for (var i = 0; i < a.length; i++) 
		{
			var o = a[i];
		
				
		//	var r = /ajaxsl-?(\w*)?/.exec(o.id); if (r)
			
			if (Element.hasClassName(o, 'ajaxsl')) //o.getAttribute('class',0)=='ajaxsl'
			{
				//if (r[1]) {
				//	var href=$(o.id).getAttribute('href',0);				
					o.setAttribute('href','javascript: '+o.getAttribute('onclick',0));
						
				//	$(o.id).setAttribute('onclick','ajaxCall("'+r[1]+'","'+href+'")');
					  if (danpfeAjaxDebug) alert('ajaxsl simplelink substituted. Href set to # automatically'+o.getAttribute('href',0));
				//}			
			}
		}
}  


function substituteReplaces() {      
	var a = document.getElementsByTagName("*");
		for (var i = 0; i < a.length; i++) 
		{
			var o = a[i];
			/*
			if (o.id != '')
				alert(o.id);
			*/
				
			var r = /ajax-?(\w*)?/.exec(o.id);
			if (r)
			{
				if (r[1]) {
					var attr=$(o.id).getAttribute(r[1],0);
					if( attr == 'null' || attr == 'undefined') {
						continue;
					}
					/*
					var href=$(o.id).getAttribute('href',0);				
					$(o.id).setAttribute('href','');
					$(o.id).setAttribute('onclick','xajaxCall("'+r[1]+'","'+href+'")');
					*/
				}			
			}
		}
}
function isFunction(a)
	 {			 
	      return (typeof a == 'function');
	 }           


function doEffect(effect,id) {
	// var myEffect = new fx.Height(id , {duration: 500});      	
	// var myEffect = new fx.Opacity('root' , {duration: 500});      	
	// myEffect.custom(1, 0.5);
	// myEffect.toggle(); 
	
}
