function formatTime(totalseconds) {
    if (totalseconds < 0)
        totalseconds = 0;
    var minutes = parseInt(totalseconds / 60);
    var seconds = totalseconds - (minutes * 60);
    if (seconds < 10) seconds = "0" + seconds;
    return minutes + ":" + seconds;
}

function displayPopup(title, content, height, width, closeButton,redirectHome) {
    var rand = Math.floor(Math.random() * 1000);
    $('form:first').append('<div id="popup' + rand + '">' + content + '</div>');
    $('#popup' + rand).dialog({
        autoOpen: false,
        height: height,
        width: width,
        modal: true,
        title: title,
        draggable: false,
        resizable: false,
        close: function () {
            $(this).remove();
        }
    });
    if (redirectHome) {
        $('#popup' + rand).dialog("option", "buttons", { "Ok": function() { window.location = './'; } });
    }
    else {
        if (closeButton) {
            $('#popup' + rand).dialog("option", "buttons", { "Ok": function() { $(this).dialog("close"); } });
        }
    }
    $('#popup' + rand).dialog("open");
}

function displayContentPopup(width, height, page) {
    var rand = Math.floor(Math.random() * 1000);
    var name = 'popup' + rand;
    $('form:first').append('<div id="popup' + rand + '"></div>');
    $('#' + name).dialog("option", "title", "Loading...");
    $('#' + name).html('<img src="/images/ajax.gif" alt="Loading" />');
    $('#' + name).dialog("open");
    $('#' + name).dialog({
        width: width,
        height: height,
        autoOpen: true,
        close: function () { $(this).remove(); },
        title: 'Loading...',
        draggable: false,
        resizable: false,
        modal: true
    });
    $('#' + name).load('/content/popup/' + page + '.htm', function () {
        $('#' + name).dialog("option", "title", $('#' + name + ' h1').html());
        $('#' + name + ' h1').remove();
    });
}

function createCookie(name, value, hours) {
    if (hours) {
        var date = new Date();
        date.setTime(date.getTime() + (hours * 60 * 60 * 1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else var expires = "";
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var ca = document.cookie.split(';');
    var nameEQ = name + "=";
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') c = c.substring(1, c.length); //delete spaces
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

var accessoriesTimer;
var jmTimer;
var gcTimer;

function showAccessories() {
    clearTimeout(accessoriesTimer);
    $('#accessoriesMenu').show();
}

function hideAccessories() {
    accessoriesTimer = setTimeout(function () { $('#accessoriesMenu').hide(); }, 100);
}

function showJM() {
    clearTimeout(jmTimer);
    $('#jmMenu').show();
}

function hideJM() {
    jmTimer = setTimeout(function () { $('#jmMenu').hide(); }, 100);
}

function showGC() {
    clearTimeout(gcTimer);
    $('#gcMenu').show();
}

function hideGC() {
    gcTimer = setTimeout(function () { $('#gcMenu').hide(); }, 100);
}

$(document).ready(function () {

    $('#liAccessories,#accessoriesMenu').hover(function () {
        showAccessories();
    }, function () {
        hideAccessories();
    });

    $('#liJM,#jmMenu').hover(function () {
        showJM();
    }, function () {
        hideJM();
    });

    $('#liGC,#gcMenu').hover(function () {
        showGC();
    }, function () {
        hideGC();
    });

});


