$("#server_status").ready(function(){loadBlock("#server_status", "server_status")});
$.History.bind(function(hash) {
    if(hash == '') 
        hash = 'main';
        
    if(hash == 'map')     
    {
        hash = 'main';
        $(document).ready(function(){
            window.setTimeout(showMap, 400);
        });
    }
    loadBlock("#page", "page_" + hash);
});

$(document).ready(function(){
    if ($.History.getHash() == '')
        $.History.go('');
});

function loadBlock(target, name)
{
    var cache = localStorage[name];
    var cdata;
    try
    {
        cdata    = $.parseJSON(cache);
    }
    catch(e)
    {
    }
    
    if (cdata)
    {
        //Y-m-d H:i:s
        var now = new Date();
        var snow = now.getFullYear() + "-" + (now.getMonth() < 9 ? "0" : "") + (now.getMonth() + 1) + "-" + 
            (now.getDate() < 9 ? "0" : "") + now.getDate() + " " + (now.getHours() < 9 ? "0" : "") + now.getHours() + ":" + 
            (now.getMinutes() < 9 ? "0" : "") + now.getMinutes() + ":" + 
            (now.getSeconds() < 9 ? "0" : "") + now.getSeconds();
        if (cdata.expire > snow)
        {
            $(target).fadeOut(400, function()
                {
                    $(target).html(cdata.value);
                    $(target).fadeIn(400);
                });
            return;
        }
    }

    $(target).fadeOut(400, function()
    {
        $(target).empty().html('<img class="loading" src="/loading.gif" />');
        $(target).fadeIn(400, function() {   
            $.getJSON("index.php", {"block": name}, function(data){
                localStorage.setItem(name, data);
                $(target).fadeOut(400, function()
                {
                    localStorage[name] = JSON.stringify(data);
                    $(target).html(data.value);
                    $(target).fadeIn(400);
                });
            });
        });
    });    
}

function loadCP(page)
{
    loadCPEx(page, {});
}

function loadCPEx(page, d)
{
    var r = {"block": "page_cp", "act":page};
    for (prop in d) { 
        //if (prop in b) { continue; }
        r[prop] = d[prop];
    }
    
    $("#page").fadeOut(400, function()
    {
        $("#page").empty().html('<img class="loading" src="/loading.gif" />');
        $("#page").fadeIn(400, function() {   
            $.getJSON("index.php", r, function(data){
                    $("#page").fadeOut(400, function()
                    {
                        $("#page").html(data.value);
                        $("#page").fadeIn(400);
                    });
            });    
        });
    });
}

function appendBlock(target, name)
{
    $.getJSON("index.php", {"block": name}, function(data){
            $(target).fadeOut(400, function()
            {
                $(target).append(data.value);
                $(target).fadeIn(400);
            });
    });    
}

function SetIFrameSource(cid, url)
{
    var myframe = document.getElementById(cid);
    if(myframe !== null)
    {
        if(myframe.src){
            myframe.src = url; }
        else if(myframe.contentWindow !== null && myframe.contentWindow.location !== null){
            myframe.contentWindow.location = url; }
        else{ myframe.setAttribute('src', url); }
    }
}

var map = 0;

function showMap()
{
    $("#map_container").css('display', 'block');
    $("#map_container").hide();
    $("#map_container").html("<iframe name=\"map_frame\" id=\"map_frame\" width=\"966\" height=\"732\" src=\"\"></iframe><div id=\"close\"></div>");
    $("#map_frame").hide();
    $("#movie").hide();
    $("#map_frame").load(function() {
        $("#map_frame").fadeIn(400);
    });
    $("#map_container").fadeIn(1000, function() {      
        //$("#map_frame").attr('src', "pomm/index.php");
        SetIFrameSource("map_frame", "pomm/index.php");
        map = 1;
    });
    
    $("body").bind("click", function(e) {
        if (map)
        {
            $("#map_container").fadeOut(400);
	    $("#movie").show();
        }
        
        map = 0;
    });
    $("#close").bind("click", function(e) {
        if (map)
        {
            $("#map_container").fadeOut(400);
            $("#movie").show(); 
        }
        
        map = 0;
    });

}

