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 
    });
    $('#' + 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);
}

function getHashTag(param) {
    var seperator = "/";
    var raw = window.location.hash.substring(1);
    var tags = raw.split(seperator);
    for (var i = 0; i < tags.length; i++) {
        var label = tags[i].split("=")[0];
        var value = tags[i].split("=")[1];
        if (label.toUpperCase() == param.toUpperCase())
            return value;
    }
}

function newHashTag(setParam, setValue) {
    window.location.hash = setParam + "=" + setValue;
}

function setHashTag(setParam, setValue) {
    var seperator = "/";
    var raw = window.location.hash.substring(1);
    var tags = raw.split(seperator);
    var newTags = new Array();
    var update = false;
    for (var i = 0; i < tags.length; i++) {
        var label = tags[i].split("=")[0];
        var value = tags[i].split("=")[1];
        if (label != null && label != "") {
            if (label == setParam) {
                update = true;
                newTags[newTags.length] = label + "=" + setValue;
            }
            else {
                newTags[newTags.length] = label + "=" + value;
            }
        }
    }
    if (!update) {
        newTags[newTags.length] = setParam + "=" + setValue;
    }
    window.location.hash = newTags.join(seperator);
}
$(document).ready(function () {
    $('#liAccessories,#accessoriesMenu').hover(function () {
        showAccessories();
    }, function () {
        hideAccessories();
    });
});
