/**
 * @author Michal
 */

var lastPage=undefined;
var loadingPage=false;


$.ajaxSetup({
    cache: false
});

$(document).ready(function(){
    $('#loadingDiv').hide();
    $('#ajaxErrorDiv').hide();
    $('#loadingDiv').ajaxStart(function(){
        $(this).show();
    });

    $('#loadingDiv').ajaxStop(function(){
        $(this).hide();
    });
    
    $(document).ajaxError(function(){
        $('#ajaxErrorDiv').show();
        setTimeout("$('#ajaxErrorDiv').hide(500);", 1000);
    });

    $.history.init(function($){});
    Navi.show_modified();
    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());
        }
		
        href = window.location.href;
        href =  href.split('#');
        href = href[0];
        addr = url.split('get.php?n=');
        addr = addr[1];

        $.history.load(addr);
        lastPage=addr;

        //google analytics
        //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;
        Navi.show_modified();
    },"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);
}

checkCurrentUrl();


