
var blocked=0;
var allowSuxess=0;
var ajaxData='';
var lastTarget = '';
var lastHref= '';
var lastInAnimation= '';
var ajaxTryCount=0;
var reloadBody=0;
var bodyData;
var Debug =1;

//$.getScript('/assets/js/jquery.history.js');

var xhr=null;
function ajax_abort() {
	if (xhr!=null) {
		xhr.abort();
		xhr=null;
	}
}

function debug(msg){
	if (window.console && window.console.info && Debug==1) {
		console.info(msg);
	}
}

function convAjaxUrl(href) {
	var h=location.hostname; // $_SERVER[HTTP_HOST];
	h='http://'+h+'/';
	var q=href.replace(h,'');
	var v=q.split('&');
	if (v[0].substr(0,1)=='/') v[0]=v[0].substr(1);
	v[0]=(encodeURIComponent) ? encodeURIComponent(v[0]) : escape(v[0]);
	q=v.join('&');
	
	return h+'index.php?q='+q;
}

function pageload(hash) {
	if(hash && jQuery.historyCurrentHash.substr(1)==hash) {
		//alert(hash+"\r\n"+jQuery.historyCurrentHash.substr(1));
	}
}



function doAjaxStart(target,InAnimation) {
	//debug("blocked="+blocked);
	if(blocked!=1) {
		blocked=1;
		$(target).html('<div class="loader" >&nbsp;</div>');
		$(target).children().fadeIn(100);
		allowSuxess=1;
	}
}

function doAjaxSuccess(data,target,InAnimation) {
	if(allowSuxess==1){
		var timeToExecAnimation=1;
		if(window.CmsAjaxBeforeAnimation){
			var tmp=InAnimation.split("|");	
			if(tmp[1]=="slow")tmp[1]=300;
			if(tmp[1]=="fast")tmp[1]=100;
			var timeToExecAnimation=CmsAjaxBeforeAnimation(ajaxData,target,tmp[1]);
		}
		//alert(timeToExecAnimation);
		var tmp=InAnimation.split("|");
		
		if(target!="#body"){
			switch (tmp[0]){
				case "fadeIn":
					$(target).fadeOut(tmp[1],function(){
						$(target).html(ajaxData);
						setTimeout(function(){$(target).fadeIn(tmp[1],function(){return true;});},timeToExecAnimation);
					});
				break;
				case "show":
					$(target).hide(tmp[1],function(){
						$(target).html(ajaxData);
						setTimeout(function(){$(target).show(tmp[1],function(){return true;});},timeToExecAnimation);
					});
				break;
				case "slideDown":
					$(target).slideUp(tmp[1],function(){
						$(target).html(ajaxData);
						//alert("data ok");
						setTimeout(function(){$(target).slideDown(tmp[1],function(){return true;});},timeToExecAnimation);
					});
				break;
				case "theme":
					//alert("continue");
					//continueSwitchTheme(tmp[1]);
					break;
				default:
					alert("animation "+tmp[0]+"is not defined");
					return false;
				break;
			}
		}else{
			target='body';
			reloadBody=1;
			bodyData=ajaxData.split("eval")[0];
		}
		
		
		if(window.CmsAjaxComplete){
			if(tmp[1]=="slow")tmp[1]=300;
			if(tmp[1]=="fast")tmp[1]=100;
			CmsAjaxComplete(ajaxData,target,tmp[1],lastHref);
		}
		
		allowSuxess=0;
		blocked=0;
		return true;
	}else{
		if(!data && !ajaxData){ajaxData=data="<h1>Error loading page:"+lastHref+"<a href='"+lastHref+"'>retry</a></h1>";}
		setTimeout("doAjaxSuccess('','"+target+"','"+InAnimation+"');",10);
		
	}
}

function doAjax(dtarget,href,InAnimation,obj){
	if(window.CmsAjaxStart){
		CmsAjaxStart(dtarget,href,obj);
	}
	lastTarget = dtarget;
	lastHref = href;
	lastInAnimation=InAnimation;
	
	var tmp=InAnimation.split("|");
	
	ajax_abort();
	if(tmp[0]=="theme"){
		doThemeSwitch=0;
		switchTheme(tmp[1]);
		xhr = $.ajax({
				type: "POST",
				url:href,
				data:"target=#body",
				dataType:"html",
				success:function(data){
					ajaxData=data;doAjaxSuccess(ajaxData,dtarget,InAnimation);
				}
		});
	}else{
		//debug("ready to execute ajax for href:"+href+" for target:"+dtarget);
		doAjaxStart(dtarget,InAnimation);
		//debug("ajax now oeo");
		xhr = $.ajax({
				type: "POST",
				url:convAjaxUrl(href),
				data:"target="+dtarget,
			 	cache: false,
				dataType:"html",
				success:function(data){
					//debug("we have an ajax success");
					ajaxData=data;
					doAjaxSuccess(ajaxData,dtarget,InAnimation);
					//$.historyLoad(encodeBase64(dtarget+'$'+href+'$'+InAnimation));
				}
			});
	}
	
	return true;
}

var doThemeSwitch=0;

function setData(data){
	ajaxData=data;
}

function switchTheme(theme){
	var height=getPageSize()[1];
	$('body').css({'position':'absolute','height':'100%','width':'100%','top':'0px','left':'0px','margin':'0px','padding':'0px'});
	if($.browser.mozilla)$('body').css({'overflow':'-moz-overflow-vertical'});
	var width=document.body.clientWidth;
	if(!$("#curentTheme").html())$('body').append("<div onclick='return false;' id='curentTheme' style=cursor:wait;z-index:10000000;position:absolute;top:0px;left:0px;width:"+width+"px;height:"+height+"px;overflow:hidden;'><div style='position:relative;'></div></div>");
	$("#cViewloader").remove();
	$("#cloader").remove();
	$("#curentTheme").css({'display':'block','opacity':'0','backgroundColor':$('body').css('backgroundColor'),'backgroundImage':$('body').css('backgroundImage'),'backgroundPosition':$('body').css('backgroundPosition'),'backgroundRepeat':$('body').css('backgroundRepeat')});
	$("#curentTheme").fadeTo('fast',0.8,function(){
		doThemeSwitch=1;
		$.getScript('/themes/'+theme+'/layout.js',function(){});
	});
}
function continueSwitchTheme(theme){
	allowSuxess=1;
	var layerTC=$('body').children('div[@id!="curentTheme"]').length;
	var layerT=0;
	$('body').children('div[@id!="curentTheme"]').fadeOut("slow",function(){
		$(this).remove();
		layerT+=1;
		if(layerTC==layerT){
			setTimeout(function(){
			$("#curentTheme").fadeTo('fast',1,function(){
				var done=0;
				var cnt=$('head>link').length;
				var currentTheme=$("#core_screen").attr('href').split('/')[2];
				$('head>link').each(function(){
					done++;
					$(this).attr('href',$(this).attr('href').replace("/"+currentTheme+"/","/"+theme+"/"));
					if(done==cnt){
						$('body').append(ajaxData);
						ajaxData='';
						
						setTimeout(function(){$("#curentTheme").fadeOut(1000,function(){showLayers();setAjax();});},2000);
					}
				});
			});
			},200);
		}
	});
	//$('body').children('div@[id!="curentTheme"]').remove();
	doThemeSwitch=0;
}
function show(theme) {
	if(doThemeSwitch==0){
		allowSuxess=1;
		if(reloadBody==1){
			$('body').html(bodyData);
		}
		if(window.showLayers)showLayers();
	}
	else {
		continueSwitchTheme(theme);
	}
}


function getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
};


function clearBox(obj,obj2){
	$(obj).fadeOut(300,function(){
		$(obj2).fadeOut(100,function(){
			$(obj).remove();
			$(obj2).remove();
		});
	});
}