var errorPre = '<center><span><p style="font: 3em arial, verdana, sans-serif;">Grats, you broke it!</p><p style="font: 2em arial, verdana, sans-serif;">Error: ';
var errorPost = '</p></span><br><br><br><br><br><br><br><img src="img/creeper.png"></center> ';

function loadError(x, e) {
    if (x.status === 0) {
        return ('You are offline!!\n Please Check Your Network.');
    } else if (x.status === 404) {
        return ('Requested URL not found.');
    } else if (x.status === 500) {
        return ('Internel Server Error.');
    } else if (e === 'parsererror') {
        return ('Error.\nParsing JSON Request failed.\n' + x.responseText);
    } else if (e === 'timeout') {
        return ('Request Time out.');
    } else {
        return ('Unknow Error.\n' + x.responseText);
    }
}
function reload(args) {
    var url = "page.php?";
    if (args.length) {
        url += args;
    } else {
        url += "s=news";
    }
	url += "&h="+$(window).height();
    
	
    
    $('#link').html(window.location.href);
    $.ajax({
        url: url,
        cache: false,
        dataType: "html",
        timeout: 10000,
        success: function (data) {
            $("#pagecontent").html(data);
        },
        error: function (x, e) {
            $("#pagecontent").html(errorPre + loadError(x, e) + errorPost);
            Cufon.replace('p');
        }
    });
}
function load(args) {
    var url = "page.php?";
    if (args.length) {
        url += args;
        lasthash = args.substr(2);
        window.location.hash=args.substr(2);
    } else {
        url += "s=news";
    }
	url += "&h="+$(window).height();
    
	
    
    $('#link').html(window.location.href);
    $.ajax({
        url: url,
        cache: false,
        dataType: "html",
        timeout: 10000,
        success: function (data) {
            $("#pagecontent").html(data);
        },
        error: function (x, e) {
            $("#pagecontent").html(errorPre + loadError(x, e) + errorPost);
            Cufon.replace('p');
        }
    });
}
function loadNoHashUpdate(args) {
    var url = "page.php?";
    if (args.length) {
        url += args;
    } else {
        url += "s=news";
    }
	url += "&h="+$(window).height();
    $.ajax({
        url: url,
        cache: false,
        dataType: "html",
        timeout: 10000,
        success: function (data) {
            $("#pagecontent").html(data);
        },
        error: function (x, e) {
            $("#pagecontent").html(errorPre + loadError(x, e) + errorPost);
            Cufon.replace('p');
        }
    });
}
function page(what) {
    load("s=" + what);
}

$(function () {
    var dlgPos = [50, 200];
    var bgImgSrc = 'render/gfx3.php?x=' + $(window).width() + '&y=2048';
    $('body').css('background', 'url(' + bgImgSrc + '),url(\'render/bedrock.php\')');
    $('body').css('background-repeat', 'repeat-x, repeat');
    $('body').css('background-position', 'top left, top left');
    Cufon.replace('.link'); 
    Cufon.replace('td');
    $('.textwidget').keyup(function (event) {
        if (event.keyCode === 13) {
            event.preventDefault();
            $('#loginform').submit();
        }
    });
    // show a simple loading indicator
    $(document).ajaxStart(function () {
        $("#loader").show();
    }).ajaxStop(function () {
        $("#loader").hide();
    }).ajaxError(function (a, b, e) {
        $("#loader").hide();
    });
    
  $("#a-stylesheet").html(title == "1200" ? "normal" : "wide");
  $('#a-stylesheet').click(function() {
    var now = $("#a-stylesheet").html();
    setActiveStyleSheet(now == "normal" ? "1000" : "1200");
    $("#a-stylesheet").html(now == "normal" ? "wide" : "normal");
  });
	setInterval("checkhash();", 400);
        loadServerStatus();
});

var calls = 1;
var lasthash;
function checkhash() {
    if (window.location.hash.length > 0) {
        var obj = window.location.hash.substr(1);
        if(obj){
            if(obj!=lasthash){
                lasthash = obj;
                if (obj.length > 0) {
                    reload("s="+obj);
                }
            }
        }
    }
    if (calls++ % 100 == 0) {
        loadServerStatus();
    }
}
function loadServerStatus() {
    $.ajax({
        url: "serverstatus.php?p=1",
        cache: false,
        dataType: "html",
        timeout: 10000,
        success: function (data) {
            $("#serverstatus").html(data);
            Cufon.replace('#serverstatus');
        },
        error: function (x, e) {
            $("#serverstatus").html("");
        }
    });
    $.ajax({
        url: "serverstatus.php?p=2",
        cache: false,
        dataType: "html",
        timeout: 10000,
        success: function (data) {
            $("#serverstatus2").html(data);
            Cufon.replace('#serverstatus2');
        },
        error: function (x, e) {
            $("#serverstatus2").html("");
        }
    });
}

