var splitter = "2eb169485c5c68dc8e5908aa2444814c";
var uri_start = "jaax";
var xmlhttp
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
	try {
		xmlhttp=new ActiveXObject("Msxml2.XMLHTTP")
	} catch (e) {
		try {
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP")
		} catch (E) {
			xmlhttp=false
		}
	}
@else
	xmlhttp=false
@end @*/

if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp=false
	}
}

function myXMLHttpRequest() {
	var xmlhttplocal;
	try {
		xmlhttplocal= new ActiveXObject("Msxml2.XMLHTTP")
	} catch (e) {
		try {
			xmlhttplocal= new ActiveXObject("Microsoft.XMLHTTP")
		} catch (E) {
			xmlhttplocal=false;
		}
	}

	if (!xmlhttplocal && typeof XMLHttpRequest!='undefined') {
		try {
			var xmlhttplocal = new XMLHttpRequest();
		} catch (e) {
			var xmlhttplocal=false;
			alert('couldn\'t create xmlhttp object');
		}
	}

	return(xmlhttplocal);
}

function requestLoad(id, modulepos, returnpos) {
	xmlhttp.open('get', templatepath+'/scripts/moduleloader.php?Itemid='+id+'&positions='+modulepos+'&returnpos='+returnpos);
	xmlhttp.onreadystatechange = handleResponse;
	xmlhttp.send(null);
}

function handleResponse() {
	if(xmlhttp.readyState == 4){
		if (xmlhttp.status == 200){
			var response = xmlhttp.responseText;
			var update = new Array();

			if(response.indexOf(splitter) != -1) {
				update = response.split(splitter);
				changeText(update[0], update[1]);
			}
		}
	}
}

function changeText( div2show, text ) {
	// Detect Browser
	var IE = (document.all) ? 1 : 0;
	var DOM = 0;
	if (parseInt(navigator.appVersion) >=5) {DOM=1};
	// Grab the content from the requested "div" and show it in the "container"
	viewer = $(div2show);
	viewer.innerHTML = text;
}

function hackATag() {
	a = document.getElementsByTagName("A");
	
	if (a.length) {
		for (i = 0; i < a.length; ++i) {
			q = qualifiedURL(a[i]);
			if (q == 1) {
				a[i].onclick = function() {
					removeHighLight();
					requestChangeContent(this.href);
					currentURL = this.href.split("#")[0];
					return false;
				}
			}else if (q != 0)
			{
				a[i].onclick = function() {
					location.href = location.href.split("#")[0] + "#" + this.href.split("#")[1];
					currentURL = this.href.split("#")[0];
					return false;
				}
			}
		}
	}
}

function initJAjax() {
	ajaxwrap = $('ja-ajaxwrap');
	//janim = new Fx.Styles( 'ja-ajaxwrap', {duration: 1000} );
	
	// initialize our DHTML history
	
	dhtmlHistory.initialize();
	if (dhtmlHistory.isFirstLoad())
	{
		if (location.href.indexOf('#')>-1)
		{
			historyChange (location.href.split('#',2)[1],"");
		}
	}
	// subscribe to DHTML history change
	// events
	dhtmlHistory.addListener(historyChange);
	//alert(location.href);
	hackATag();
}

jaAddEvent( window, 'load', initJAjax );

function removeHighLight() {
	oldhighlight = document.getElementsByClassName("active");
	for (i = 0; i < oldhighlight.length; ++i) {
		oldhighlight[i].removeClass("active");
	}
}

function reHighLight(ids) {
	ids = ids.split(",");
	for (i = 0; i < ids.length; ++i) {
		temp = $('menu'+ids[i].trim());
		if (temp) {
			temp.addClass("active");
			temp.blur();
		}
	}
}

function requestChangeContent(url) {
	addHistory (url, "");
	jajaxAnimation('close');
	xmlhttp.open('post', url);
	xmlhttp.onreadystatechange = handleChangeContent;
	xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	xmlhttp.send('jajax=1');
}

function addHistory (url, data) {
	if (url.indexOf (livesite) == 0)
	{
		uri = uri_start+url.substr(livesite.length);
		dhtmlHistory.add(uri, "");
	}
}

function historyChange(newLocation, historyData) {
	//alert(newLocation);
	var historyMsg = historyData;
	if (newLocation == "complexObject") {
		historyMsg =JSON.stringify(historyData);
	}

	//alert(newLocation);
	if (newLocation.indexOf(uri_start) == 0)
	{
		url = livesite + newLocation.substr(uri_start.length);
		//alert(url);
		removeHighLight();
		requestChangeContent(url);
		currentURL = url.split("#")[0];
	}else if (!newLocation) {
		location.href = location.href.split('#')[0];
	}

}

function handleChangeContent() {
	if(xmlhttp.readyState == 4){
		if (xmlhttp.status == 200){
			var response = xmlhttp.responseText;

			content = response.split(splitter, 2);
			if (content.length >= 2) {
				params = content[0];
				arr = params.split('&');
				arrparams = new Array();
				for(i=0;i<arr.length;i++){
					temp = arr[i].split('=',2);
					eval('var '+temp[0].trim()+'=\''+escape(temp[1].trim())+'\';');
				}
				outputhtml = content[1];
				reHighLight(unescape(ids));
				document.title = unescape(title);
				//alert(unescape(title));
			} else {
				outputhtml = content[0];
			}
			var IE = (document.all) ? 1 : 0;
			var DOM = 0;
			if (parseInt(navigator.appVersion) >=5) {DOM=1};
			// Grab the content from the requested "div" and show it in the "container"
			viewer = $('ja-ajaxwrap');
			viewer.innerHTML = outputhtml;
			jajaxAnimation('open');
			//Remove adding script 
			RemoveAddingScript();
			//Add new script
			js = viewer.getElementsByTagName ('script');
			for (i=0; i<js.length; i++)
			{
				var script_id = "ja_script_" + i;
				if (js[i].src)
				{
					AddScriptIntoHEAD (js[i].src, script_id, 0);
				}else {
					AddScriptIntoHEAD (js[i].text, script_id, 1);
				}
			}
			hackATag();
		}
	}
}

var ajaxwrap;
var loadingHeight = 100;
var janim;
var currentURL = location.href.split("#")[0];

function jajaxAnimation(param) {
	if (param == 'close') {
		ajaxloading = $('ja-ajaxloading');
		ajaxloading.style.display = "block";
		if (window.ActiveXObject) {
			ajaxloading.style.position = "absolute";
			ajaxloading.style.top = (window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop) + 10 + "px";
			ajaxloading.style.left = (document.body ? document.body.clientWidth : document.documentElement.clientWidth) - ajaxloading.offsetWidth - 10 + "px";
		} else {
			ajaxloading.style.position = "fixed";
			ajaxloading.style.top = "10px";
			ajaxloading.style.left = (document.body ? document.body.clientWidth : document.documentElement.clientWidth) - ajaxloading.offsetWidth - 10 + "px";
		}
		//~ ajaxwrap.setOpacity(0.3);
		//~ $('ja-ajaxloading').style.top = ((screen.height - $('ja-headerwrap').offsetHeight) / 2) + "px";
		//~ janim.custom({'opacity': [1,0.5], 'height': [ajaxwrap.offsetHeight, 0]});
		//~ janim.options.onComplete = function() {
			//~ ajaxwrap.innerHTML = '<div id="ja-containerwrap"><div id="ja-container-f" style="text-align: center"><div id="ja-mainbody-f"><div id="ja-contentwrap"><div id="ja-content"><img src="'+ templatepath +'/scripts/loading.gif" alt="Loading" border="0" /><br />Loading...</div></div></div></div></div>';
		//~ }
	} else {
		ajaxloading = $('ja-ajaxloading');
		ajaxloading.style.display = "none";
		//~ ajaxwrap.setOpacity(1);
		//~ janim.options.onComplete = Class.empty;
		//~ janim.custom({'height': [loadingHeight, ajaxwrap.scrollHeight], 'opacity': [0.5,1]});
	}
}

function qualifiedURL(a) {
	if (!a) return 0;
	url = a.href;
	if (url.indexOf(currentURL + "#") == 0) return url.split("#")[1];

	if (!url || url.indexOf(livesite) != 0) return 0;
	if (url.indexOf(livesite + '/admin') == 0) return 0;
	if (url.indexOf('jos_change_template') > -1) return 0;
	
	if (a.onclick) return 0;
	
	return 1;
}


function AddScriptIntoHEAD(script_src, script_id, addSrc){
	var head = document.getElementsByTagName('head').item(0);
	//create:
	script = document.createElement('script');
	script.type = 'text/javascript';
	if (!addSrc)
	{
		script.src = script_src;
	}else script.text = script_src ;
	script.id = script_id;
	//alert(script.text);
	head.appendChild(script);
}

function RemoveAddingScript(){
	i=0;
	while (1)
	{
		var head = document.getElementsByTagName('head').item(0);
		var script_id = "ja_script_" + i;
		var this_script_id = document.getElementById(script_id);
		if(this_script_id) head.removeChild(this_script_id);
		else return;
	}
}