/**
 * @author Michal
 */

var lastPage=undefined;
var loadingPage=false;


$.ajaxSetup({
	cache: false
});

$(document).ready(function(){
	$('#loadingDiv').ajaxStart(function(){
		$(this).show();
	});
	
	$('#loadingDiv').ajaxStop(function(){
		$(this).hide();				
	});

	$.history.init(function($){});

        lastPage=undefined;
        checkCurrentUrl();

        
});

function loadPage(url){
	loadingPage=true;	

	var data = $.get(url,{},function(response){

		var content = $(response).find('content').text();

		$('#ACCcontent').html(content);

		document.title = ($(response).find('title').text());
		if($('#ACCsubmenu').length>0){
			$('#ACCsubmenu').html($(response).find('submenu').text());
		}
		
		if($('#pasek').length>0){
			$('#pasek').html($(response).find('pasek').text());
		}
		
		href = window.location.href;
		href =  href.split('#');
		href = href[0];
		addr = url.split('get.php?n=');
		addr = addr[1];

		$.history.load(addr);
		lastPage=addr;
		var gaurl = url.replace("./get.php","/site").replace(/\?/ig,"/").replace(/=/ig,"/").replace(/&/ig,"/");

		//_gaq.push(['_trackPageview',gaurl]);

		$(response).find('module').each(function(){
			$('#'+$(this).attr('name')+'Div').html($(this).text());
		});
		loadingPage=false;
	},"xml");

	return false;
}

function checkCurrentUrl(){
	if(loadingPage==false){
		href = window.location.href;
		href =  href.split('#');
		page = href[1];
		url=href[0];
		url = url.split("site/");
		url = url[0];
		
		if(page==undefined || page==null || page==''){
				setTimeout('checkCurrentUrl();', 600);
				return false;
		}else{
			page = page.replace(/%26/ig,'&').replace(/%3D/ig,'=');
			if(page!=lastPage){
				loadPage('./get.php?n='+page);
			}
		}
	}
	setTimeout('checkCurrentUrl();', 600);	
}


