

var xmlreqs 	= new Array(); 
var xmlreqCount = 0;
var ajaxWaitingElement;
var ajaxWaitType = 'prefix';

function ajax_do (element, functie, params, object, waitingAnimation, doUrchinTracker) {
	if(waitingAnimation) {
		if(waitingAnimation == true) {
			startWaiting(element);
		} else {
			startWaiting(waitingAnimation, true);
		}
	} else {
		startWaiting();
	}

	var query = 'object=' + object + '&element=' + element + '&functie=' + functie + '&' + params;
	var url = "/_BWF/ajax/ajax.php"; 
    
	doXmlHttpRequest(url, query, doUrchinTracker, element);
}

function ajax_do_ssl (element, functie, params, object, waitingAnimation, doUrchinTracker) {
	if(waitingAnimation) {
		if(waitingAnimation == true) {
			startWaiting(element);
		} else {
			startWaiting(waitingAnimation, true);
		}
	} else {
		startWaiting();
	}

	var query = 'object=' + object + '&element=' + element + '&functie=' + functie + '&' + params;
	var url = "/_BWF/ajax/ajax.php"; 

	doXmlHttpRequest(url, query, doUrchinTracker, element);
}

/*	Do an actual ajax request (used by ajax_do)
	functions manages an array of xmlHtpp objects, which is needed when multiple ajax requests overlap
	the xmlHtpp objects in array are recycled after they have returned their ajax data (so size of array will be max simultanious ajax calls since page refresh)
*/
function doXmlHttpRequest(url, query, doUrchinTracker, element) {
	var pos = -1; 
	for (var i=0; i<xmlreqs.length; i++) { 
		if (xmlreqs[i].freed == 1) { 
			pos = i; 
			break; 
		} 
	}
	if (pos == -1) { 
		pos = xmlreqs.length; 
		xmlreqs[pos] = new createXMLHttpRequest(1); 
	} 
	if (xmlreqs[pos].xmlhttp) { 
		xmlreqs[pos].freed = 0;
		if(doUrchinTracker) {
			xmlreqs[pos].doUrchinTracker = true;
		} else {
			delete xmlreqs[pos].doUrchinTracker;
		}
		if(element) {
			xmlreqs[pos].element = element;
		}
	    xmlreqs[pos].xmlhttp.open("POST", url, true);
	    xmlreqs[pos].xmlhttp.onreadystatechange = function() {handleStateChange(pos)};
	    xmlreqs[pos].xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	    xmlreqs[pos].xmlhttp.send(query);	
	    xmlreqCount++;
	}
}

/* create an ajax data socket dependent on browser (used by doXmlHttpRequest) */
function createXMLHttpRequest(freed) {
	this.freed = freed;
	this.xmlhttp = false;
	  try{
	    /* Opera 8.0+, Firefox, Safari */
	    this.xmlhttp = new XMLHttpRequest();
	  } catch (e){
	    /* Internet Explorer Browsers */
	    try{
	      this.xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	    } catch (e) {
	      try{
	        this.xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	      } catch (e){
	        /* Ajax is not supported */
	        alert("Your browser does not support Ajax");
	        return false;
	      }
	    }
	  }

}


/* this function is called when the ajax call returns */ 
function handleStateChange(pos) {
    if(xmlreqs[pos].freed == 0 && xmlreqs[pos].xmlhttp.readyState == 4 && xmlreqs[pos].xmlhttp.status == 200) {
		finishWaiting();
		eval(xmlreqs[pos].xmlhttp.responseText);
		if(xmlreqs[pos].doUrchinTracker) {
			var tracker = location.pathname+location.search+"_ajax" + (location.hash).replace(/\//g,'_').replace(/#/,'_');
			tracker = tracker;
			_gaq.push(['_trackPageview', tracker]);
		}
		xmlreqs[pos].freed = 1; 
		if(xmlreqs[pos].element) {
			var div = document.getElementById(xmlreqs[pos].element);
			var x = div.getElementsByTagName("script"); 
			for(var i=0;i<x.length;i++) {
				eval(x[i].text);
			}
		}
    }
}

function startWaiting(waitingElement, waitingElementSeperate){
	document.body.className = 'waitAjax';
	if(waitingElement) {
		if(ajaxWaitType == "prefix") {
			document.getElementById(waitingElement).innerHTML = "<p style='text-align: center;'><br/><img src='../_BWF/images/loadingAnimation.gif' alt='loading'></p><br/><br/>" + document.getElementById(waitingElement).innerHTML;
		} else {
			document.getElementById(waitingElement).innerHTML = "<p style='text-align: center;'><br/><img src='../_BWF/images/loadingAnimation.gif' alt='loading'></p>";
		}
	}
	if(waitingElementSeperate) {
		ajaxWaitingElement = waitingElement;
	}
}

function finishWaiting(){
	document.body.className = 'readyAjax';
	if(ajaxWaitingElement && ajaxWaitingElement != '') {
		document.getElementById(ajaxWaitingElement).innerHTML = "";
		ajaxWaitingElement = null;
	}	
}

/* will parse part after hash in URL. no input required (for ajax deeplink/bookmark support) */
function parseHashURL() {
	if(location.hash.slice(1)!='') {
		return parseHashString(location.hash.slice(1));
	} else {
		return false;
	}
}

/* will parse a string (assumed to be part after hash of a URL) and execute the command (e.g. for ajax back button and deeplink/bookmark support). */
function parseHashString(string) {
	if(string && string != '') {
		var data = string.split('/');
		/* data[0] will always be '' because string begins with / */
		if(data.length == 2) {
			doHashString(data[1]);
			return true;
		}
		if(data.length == 3) {
			doHashString(data[1], data[2]);
			return true;
		}
		if(data.length == 4) {
			doHashString(data[1], data[2], data[3]);
			return true;
		}
		if(data.length == 5) {
			doHashString(data[1], data[2], data[3], data[4]);
			return true;
		}				
	}
	return false;
}

/* based on input a string is added after the current URL with hash */
function addHistoryEntry(p1, p2, p3, p4) {
	if(window.dhtmlHistory) {
	 	var string = '/'+encodeURIComponent(decodeURIComponent(p1));
	 	if(p2) {
	 		string = string + '/'+encodeURIComponent(decodeURIComponent(p2));
	 	}
	 	if(p3) {
	 		string = string + '/'+encodeURIComponent(decodeURIComponent(p3));
	 	}
	 	if(p4) {
	 		string = string + '/'+encodeURIComponent(decodeURIComponent(p4));
	 	}
	 	dhtmlHistory.add(string);
	 }
}

/* only if RSH is included window.dhtmlHistory exists */
if(window.dhtmlHistory) {
	/* what will be defined here is what is executed in case back button is pressed, string will be the string defined in addHistoryEntry (that what is behind hash in URL) */
	var rshListener = function(string, historyData) {
		parseHashString(string);
	};
	
	window.dhtmlHistory.create({ 
		toJSON: function(o) {
			return $.toJSON(o);
		},
		fromJSON: function(s) {
			return $.evalJSON(s);
	}
	});
	
	window.onload = function(){
		window.dhtmlHistory.initialize();
		window.dhtmlHistory.addListener(rshListener);
	};

}


