/**
 * @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;	
	//$('#news').slideUp(500,function(){$('#left-side').hide(0);});
	//$('#news').hide(0);
	var data = $.get(url,{},function(response){
		//$('#news').stop(true,true).slideUp(0,function(){$('#left-side').hide(0);}).slideDown(500);
		var content = $(response).find('content').text();

		content = content.replace(/href="http:\/\/domkorkowy\.fr\.pl\/site\/n\/strefa-dystrybutora\//ig, 'href="');
		content = content.replace(/href="http:\/\/domkorkowy\.fr\.pl\/site\/n\/strefa-dystrybutora/ig, 'href="');
		content = content.replace(/href="http:\/\/domkorkowy\.fr\.pl\//ig, 'href="');

		$('#ACCcontent').html(content);
		//$('#news').show(0);
		document.title = ($(response).find('title').text());
		if($('#ACCsubmenu').length>0){
			$('#ACCsubmenu').html($(response).find('submenu').text());
		}
		
		href = window.location.href;
		href =  href.split('#');
		href=href[0];
		addr=url.split('get.php?n=');
		addr=addr[1];
		if(addr!=undefined){
			window.location.href=href+'#'+addr;
		}
		$.history.load(addr);
		lastPage=addr;
		$(response).find('module').each(function(){
			$('#'+$(this).attr('name')+'Div').html($(this).text());
		});
		loadingPage=false;
                if($('#news').height()==0){
                    var height=$('#ACCcontent').height()+10;
                    if(($('#left-side').height()+10)>height) height=($('#left-side').height()+10);
                    $('#news').height(height+10);
                }else{
                    $('#news').height('');
                }
	},"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{
			if(page!=lastPage){
				loadPage('./get.php?n='+page);
			}
		}
	}
	setTimeout('checkCurrentUrl();', 600);	
}


