var splitter = "2eb169485c5c68dc8e5908aa2444814c";
var uri_start = "jaax";

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
	//alert(location.href);
	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() {
	if (!$$('.active')) return;
	$$('.active').each(function(el){
		el.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 addHistory (url, data) {
	if (url.indexOf (livesite) == 0)
	{
		uri = uri_start+url.substr(livesite.length);
		dhtmlHistory.add(uri, "");
	}
}

function historyChange(newLocation, historyData) {
	//alert(uri_start);
	//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);
		removeHighLight();
		//requestChangeContent(url);
		currentURL = url.split("#")[0];
	}else if (!newLocation) {
		location.href = location.href.split('#')[0];
	}

}

function requestChangeContent(url) {
	addHistory (url, "");
	jajaxAnimation('close');
	url = url + '&jajax=1';
	new Ajax(url,{method:'get',onComplete:handleChangeContent}).request(); 	
}

function handleChangeContent (request){
	var content = request.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";
		}
	} else {
		ajaxloading = $('ja-ajaxloading');
		ajaxloading.style.display = "none";
	}
}

function qualifiedURL(a) {
	if (!a) return 0;
	url = a.href.toLowerCase();
	if (url.indexOf(currentURL + "#") == 0) return url.split("#")[1];
	if (!url.indexOf('http:')!=0) url = livesite + url;

	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 (url.indexOf('rss') > -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;
	}
}
