﻿function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}
function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}
function eraseCookie(name) {
    createCookie(name, "", -1);
}

function displayOnlinePlayers(x, y, key) {
    var d = $('#dOnlineUsers');
    if (d.css("display") == "none") {
        $("#dStatsGeneral").fadeOut("fast").delay(300).empty();
        d.delay(300).fadeIn("fast");
    }
    else if (d.css("display") == "block") {
        d.fadeOut("fast", function () { $(this).delay(300).empty(); });
        return;
    }
    $.getJSON("hndlr.ashx?m=onl&t=" + key, function (data) {
        var d = $("#dOnlineUsers");
        var c = 0;
        $.each(data, function (key, val) {
            if (c == data.length - 1) {
                d.append(val);
            }
            else {
                d.append(val + " - ");
            }
            c++;
        });
        d.css("left", x - 180 + 'px');
        d.css("top", y + 15 + 'px');
    });
}

function displayStatsg(x, y, key) {
var d = $('#dStatsGeneral');
    if (d.css("display") == "none") {
        $("#dOnlineUsers").fadeOut("fast").delay(300).empty(); 
        d.delay(300).fadeIn("fast");
    }
    else if (d.css("display") == "block") {
        d.fadeOut("fast", function () { $(this).delay(300).empty(); });
        return;
    }
    $.getJSON('hndlr.ashx?m=stg&t=' + key, function (data) {
        var d = $("#dStatsGeneral");
        var c = 0;
                
        $.each(data[0], function (key, val) {
            if (c == data[0].length - 1) {
                d.append('<b>' + key + '</b>: ' + val);
            }
            else {
                d.append('<b>' + key + '</b>: ' + val + "<br />");
            }
            c++;
        });
        d.css("left", x - 180 + 'px');
        d.css("top", y + 15 + 'px');
    });
}

function cashFormat(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}

function pstats(k) {
    $.getJSON('hndlr.ashx?m=pst&t=' + k, function (data) {
        $.each(data[0], function (key, val) {
            if (key == "health") {
                $("span[id*='" + key + "']").text(val + '%');
            }
            else if (key == "cash") {
                $("span[id*='" + key + "']").text('$' + cashFormat(val));
            }
            else if (key == "newMails") {
                if (val == 0) {
                    $("img[id*='imgMsg']").attr("title", "");
                    $("img[id*='imgMsg']").attr("src", "../images/no_messages.gif");
                }
                else {
                    $("img[id*='imgMsg']").attr("title", "1+");
                    $("img[id*='imgMsg']").attr("src", "../images/new_message_" + val + ".gif");
                }
            }
            else {
                $("span[id*='" + key + "'][class='usrinfovalue']").text(val);
            }
        });
    });
}

function updateHeightWidth() {
    var fullw = $(window).width();
    var fullh = $(window).height();

    if (fullw < 1022) {
        if (fullw < 700 && $("div[id*='layoutWarning']").length == 0) {
            var d = document.createElement("div")
            jQuery(d).addClass("TMNErrorFont").attr("id", "layoutWarning");
            jQuery(d).text("It appears you are using a mobile device, or low screen resolution. If you encounter problems, please try an alternative layout from ");
            var h = document.createElement("a");
            jQuery(h).attr("href", "layout.aspx").text("this page");
            jQuery(d).append(h);
            $("#divContentIn").prepend(d);
        }
        fullw = 1000;
    }

    var headerHeight = $('#divHeader').height();
    var statsBarHeight = $('#divStats').height();
    var menuWidth = $('#divMenu').width();
    $('#divContent').width((fullw - menuWidth - 5) + 'px');
    $('#pageContainer').width(fullw + 'px');
    $('#divContent').height((fullh - headerHeight - statsBarHeight) + 'px');
    $('#divMenu').height((fullh - headerHeight - statsBarHeight) + 'px');
    $('#pageContainer').height(fullh + 'px');
}

function forwardOrNot() {
    return true;
}

function createBoxes() {
    var maxW = Math.ceil($(window).width() / 2);
    var maxH = Math.ceil($(window).height() / 2);
    var overLay = document.createElement("div");
    jQuery(overLay).attr("id", "overlay").css({ position: 'absolute', left: '0', top: '0', bottom: '0', right : '0', opacity: '0', zIndex: '2', backgroundColor: '#000000' }).append(document.createTextNode(' '));
    var imgBox = document.createElement("div");
    jQuery(imgBox).attr("id", "imgBox").css({ position: 'absolute', zIndex: '3', backgroundColor: '#737373', padding: '1px', opacity: '0', textAlign: 'center' });
    jQuery('body').prepend(imgBox).prepend(overLay);
}
function showBoxes() {
    $("#overlay").fadeTo(300, 0.6);
    $("#imgBox").fadeTo(300, 0.9);
    $("#overlay").click(function () { $("#imgBox").fadeOut(300).delay(300).queue(function () { $(this).remove(); }); $("#overlay").fadeOut(300).delay(300).queue(function () { $(this).remove(); }); })
    $("#imgBox").click(function () { $("#imgBox").fadeOut(300).delay(300).queue(function () { $(this).remove(); }); $("#overlay").fadeOut(300).delay(300).queue(function () { $(this).remove(); }); })
}
function displayImg(myImage) {
    createBoxes(); 
    var imgOb = document.createElement("img");
    var imgLink = document.createElement("a");
    jQuery(imgLink).attr("href", jQuery(myImage).attr("src")).attr("target", "_blank").text("Click here to open full-sized image").css("color", "#000000");
    var maxW = Math.ceil($(window).width() / 2);
    var maxH = Math.ceil($(window).height() / 2);
    jQuery(imgOb).attr("id", "bigImg").attr("src", jQuery(myImage).attr("src"));
    jQuery(imgOb).css("max-width", (Math.max(maxH, maxW)) + "px").css("max-height", (Math.max(maxH, maxW)) + "px");
    $("#imgBox").css("left", Math.ceil(maxW / 2) + 'px').css("top", Math.ceil(maxH / 2) + 'px')
    $("#imgBox").append(imgOb).append(document.createElement('br')).append(imgLink);
    jQuery(imgOb).fadeTo(300, 1);
    showBoxes();
}

function displayClone(cloneId) {
    createBoxes();
    var d = $("#" + cloneId);
    var t = $("#imgBox");
    var nt = document.createElement("div");
    var maxW = Math.floor($(window).width() / 2);
    var maxH = Math.floor($(window).height() * 0.85);
    nt = document.getElementById(cloneId).cloneNode(true);
    jQuery(t).css("top", (Math.floor($(window).height() * 0.1)) + 'px').css("padding", "5px");
    jQuery(t).append(nt);
    jQuery(t).css("left", (Math.ceil(($(window).width() / 2) - (jQuery(nt).width() / 2))) + 'px');
    if (jQuery(nt).height() > maxH) {
        jQuery(nt).css("height", maxH + "px").css("overflow-y", "scroll");
    }
    showBoxes();
}

function getY(oElement) {
    var iReturnValue = 0;
    while (oElement != null) {
        iReturnValue += oElement.offsetTop;
        oElement = oElement.offsetParent;
    }
    return iReturnValue;
}

function getX(oElement) {
    var iReturnValue = 0;
    while (oElement != null) {
        iReturnValue += oElement.offsetLeft;
        oElement = oElement.offsetParent;
    }
    return iReturnValue;
}
