﻿$(document).ready(function() {
    //set sub-menu width
    SetOccasionsMenuWidth();

    $(".closePopup").click(function() {
        disablePopup();
    });


    $("#aRegistration").click(function() {
        $("#registration").hide();
        $("#login").show();
    });

    if (!customRevenuePath) {
        $("#aLogin").click(function() {
            $("#login").hide();
            $("#registration").show();
        });
    }
    //var _facebook_api = "b34ae814457980d2bbd9e723a81aed43";
    //var prefix_keys = ['base_domain_', 'fbsetting_']
    //var postfix_keys = ['', '_user', '_ss', '_session_key', '_expires']

    //var exp = "=;expires=Thu, 01 Jan 1970 00:00:00 GMT" + '; path=/';
    //for (i = 0; i < prefix_keys.length; i++) {
    //    document.cookie = prefix_keys[i] + _facebook_api + exp;
    //}
    //for (i = 0; i < postfix_keys.length; i++) {
    //     document.cookie = _facebook_api + postfix_keys[i] + exp;
    // }

    // FB.init("b34ae814457980d2bbd9e723a81aed43", _path + "xd_receiver.htm", { "forceBrowserPopupForLogin": true, "reloadIfSessionStateChanged": false });
});

function SetOccasionsMenuWidth() {
    var numberChildren = $("#occasions").contents().children().size();
    var columns = Math.ceil(numberChildren / 6);

    if (columns == 1) {
        //fix so its inline with tab
        $("#occasions").css("width", 122);
    }
    else {
        $("#occasions").css("width", 140 * columns);
    }
}

function AlignContent(divLeft, divCentre, divRight) {
    var totalHeight;
    var DivElements = new Array(3);
    
    DivElements[0] = document.getElementById(divLeft);
    DivElements[1] = document.getElementById(divCentre);
    DivElements[2] = document.getElementById(divRight);

    //get greatest total height
    totalHeight = GetGreatestHeight(DivElements);

    for (var i = 0; i < 3; i++) {
        SetNewElementHeight(DivElements[i], totalHeight);
    }    
}

function GetGreatestHeight(Elements) {
    var greatestHeight = 0;
    for (var i = 0; i < 3; i++) {
        var elementHeight = (Elements[i].offsetHeight + getOffset(Elements[i]));

        if (elementHeight > greatestHeight)
            greatestHeight = elementHeight;
    }
    return greatestHeight;
}

function SetNewElementHeight(Element, totalHeight) {
    var currentHeight = Element.offsetHeight + getOffset(Element);

    if (totalHeight > currentHeight) {
        //need to increate height
        Element.style.height = ((totalHeight - getOffset(Element))) + 'px';
    }
    else {
        Element.style.height = ((currentHeight - getOffset(Element))) + 'px';
    }
}

function getOffset(element) {
    var top = 0;
    while (element.offsetParent) {
        //add on this elements offset
        top += element.offsetTop;

        //search parent element
        element = element.offsetParent;
    }
    //add final elements offset/or add on base element as there is no parent
    top += element.offsetTop;
    return top;
}

var popupStatus = 0;
function loadPopup() {
    //loads popup only if it is disabled
    if (popupStatus == 0) {
        $("#popupFilter").css({
            "opacity": "0.7"
        });
        $("#popupFilter").fadeIn("slow");
        $("#popupContainer").fadeIn("slow");
        popupStatus = 1;
    }
}

function disablePopup() {
    //disables popup only if it is enabled
    if (popupStatus == 1) {
        $("#popupFilter").fadeOut("slow");
        $("#popupContainer").fadeOut("slow");
        popupStatus = 0;
    }
}

function centerPopup() {
    var windowWidth = $("#site-wrapper").width();
    var windowHeight = document.documentElement.clientHeight;
    var popupWidth = $("#popupContainer").width();
    //centering
    $("#popupContainer").css({
        "position": "absolute",
        "top": 100,
        "left": windowWidth / 2 - popupWidth / 2
    });
    //only need force for IE6

    $("#popupFilter").css({
        "height": windowHeight
    });

}


function showPopupType(show, hide) {
    
    $(hide).hide();
    $(show).show();
    
    loadPopup();
    centerPopup();
}