﻿$(document).ready(function () {
    /**** EVENT DETAIL POPUP HANDLERS ****/
    //CLOSING POPUP
    $(".LoginDetailsPopupClose").click(function () { disablePopup(); });

    //Click out event!
    $(".backgroundPopup").click(function () { disablePopup(); });

    $("#RegisterSwitchLink").click(function () { _CLICKEDBUTTON = "register"; loadPopup(); });
    $("#SignInSwitchLink").click(function () { _CLICKEDBUTTON = "login"; loadPopup(); });

    //Press Escape event!
    $(document).keypress(function (e) {
        if (e.keyCode == 27 && popupStatus == 1)
            disablePopup();
    });
    
    $(window).resize(function(){
	    if(popupStatus == 1)
	    {
            setOverlayDimensionsToCurrentDocumentDimensions();
	        centerPopup();
	    }
    });
    
    /**** END EVENT DETAIL POPUP HANDLERS ****/
});

//SETTING UP OUR POPUP  
//0 means disabled; 1 means enabled;  
var popupStatus = 0;
//loading popup with jQuery magic!
function loadPopup(id) {
    _DEFAULTBUTTON = "GUEST";
    $("#UserName").focus();
    if (id) {
        $(".backgroundPopup").css({ "opacity": "0.5" });
        centerPopup();
        setOverlayDimensionsToCurrentDocumentDimensions();

        if ($.browser.msie) {
            $(".backgroundPopup").show();
            $("#" + id).show();
        }
        else {
            $(".backgroundPopup").fadeIn("slow");
            $("#" + id).fadeIn("slow");
        }

        popupStatus = 1;
    }
    else {
        //switch (_CLICKEDBUTTON) {
        //    case "checkout":
        $(".page-title").show();
        $(".returning-wrapper-details").hide();
        $(".returning-wrapper").show().addClass("left");
        $(".new-wrapper-details").hide();
        $(".new-wrapper").show();
        //        break;
        //    case "register":
        //        $(".page-title").hide();
        //        $(".returning-wrapper-details").hide();
        //        $(".returning-wrapper").hide();
        //        $(".new-wrapper-details").show();
        //        $(".new-wrapper").show();
        //        break;
        //    case "login":
        //        $(".page-title").hide();
        //        $(".returning-wrapper-details").show();
        //        $(".returning-wrapper").show().removeClass("left");
        //        $(".new-wrapper-details").hide();
        //        $(".new-wrapper").hide();
        //        break;
        //}

        //loads popup only if it is disabled
        if (popupStatus == 0) {
            $(".backgroundPopup").css({ "opacity": "0.5" });
            setOverlayDimensionsToCurrentDocumentDimensions()

            if ($.browser.msie) {
                $(".backgroundPopup").show();
                $(".LoginDetailsPopup").show();
            }
            else {
                $(".backgroundPopup").fadeIn("slow");
                $(".LoginDetailsPopup").fadeIn("slow");
            }
            centerPopup();
            popupStatus = 1;
        }
    }
}

function setOverlayDimensionsToCurrentDocumentDimensions() {
    $('.backgroundPopup').width($(document).width());
    $('.backgroundPopup').height($(document).height());
}

//disabling popup with jQuery magic!
function disablePopup() {
    _DEFAULTBUTTON = "";
    $("#UserName").blur();
    //disables popup only if it is enabled
    if (popupStatus == 1) {
        if ($.browser.msie) {
            $("#header a#link_home").css({ "z-index": "100" });
            $(".backgroundPopup").hide();
            $(".LoginDetailsPopup").hide();
            $(".NutritionPopup").hide();
        }
        else {
            $(".backgroundPopup").fadeOut("slow");
            $(".LoginDetailsPopup").fadeOut("slow"); //, function () { $("#header a#link_home").css({ "z-index": "100" }); });
            $(".NutritionPopup").fadeOut("slow");
        }
        popupStatus = 0;
    }

    try
    {
        ClearErrors();
    }
    catch(e) {}
}

//centering popup  
function centerPopup() {
    //request data for centering  
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $(".LoginDetailsPopup").height();
    var popupWidth = $(".LoginDetailsPopup").width();
    var npopupHeight = $(".NutritionPopup").height();
    var npopupWidth = $(".NutritionPopup").width();
    //centering
    $(".LoginDetailsPopup").css({
        "_position": "absolute",
        "position": "fixed",
        /*"background-color": "white",*/
        "top": windowHeight / 2 - popupHeight / 2,
        "left": windowWidth / 2 - popupWidth / 2
    });
    $(".NutritionPopup").css({
        "_position": "absolute",
        "position": "fixed",
        /*"background-color": "white",*/
        "top": windowHeight / 2 - npopupHeight / 2,
        "left": windowWidth / 2 - npopupWidth / 2
    });
    //only need force for IE6
    $(".backgroundPopup").css({ "height": windowHeight });
}

