/***************************/
/* Used for upsell popup window effects in OrderItem */
/***************************/

//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;

//loading popup with jQuery magic!
function loadPopup() {
    //loads popup only if it is disabled
    if (popupStatus == 0) {
        $("#div_upsell_background").css({
            "opacity": "0.7"
        });
        $("#div_upsell_background").fadeIn("slow");
        $("#div_upsell_popup").fadeIn("slow");

        updateMinimumQtys();
        
        popupStatus = 1;
    }
}

//disabling popup with jQuery magic!
function disablePopup() {
    //disables popup only if it is enabled
    if (popupStatus == 1) {
        $("#div_upsell_background").fadeOut("slow");
        $("#div_upsell_popup").fadeOut("slow");
        popupStatus = 0;
    }
}

//centering popup
function centerPopup() {
    //request data for centering
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#div_upsell_popup").height();
    var popupWidth = $("#div_upsell_popup").width();
    //centering
    $("#div_upsell_popup").css({
        "position": "absolute",
        "top": windowHeight / 2 - popupHeight / 2,
        "left": windowWidth / 2 - popupWidth / 2
    });
    //only need force for IE6

    $("#div_upsell_background").css({
        "height": windowHeight
    });

}

var numOpen = 0;

function openUpsell(divOpenId, divClosedId, divDetailsId) {
    if (numOpen == 0) {
        $('#div_upsell_popup').animate({ 'height': '895px' }, "fast", function() {
            $('#' + divDetailsId).slideToggle("normal");
        });
        $('#' + divOpenId).css({ "display": "" });
        $('#' + divClosedId).css({ "display": "none" });
    }
    else {
        $('#' + divDetailsId).slideToggle("normal");
        $('#' + divOpenId).css({ "display": "" });
        $('#' + divClosedId).css({ "display": "none" });
    }
    numOpen++;
}

function closeUpsell(divOpenId, divClosedId, divDetailsId) {

    if (numOpen <= 1) {
        $('#' + divDetailsId).slideToggle("normal", function() {
            $('#div_upsell_popup').animate({ 'height': '555px' }, "fast");
            $('#' + divClosedId).css({ "display": "" });
            $('#' + divOpenId).css({ "display": "none" });
        });
    }
    else {
        $('#' + divDetailsId).slideToggle("normal", function() {
            $('#' + divClosedId).css({ "display": "" });
            $('#' + divOpenId).css({ "display": "none" });
        });
    }

    numOpen--;
}

function updateMinimumQtys() {
    var itemQty = 0;

    $('.txt_Quantity').each(function() {
        if ($(this).val() != '') {            
            itemQty += parseInt($(this).val());
        }
    });

    $('.upsellQty').each(function() {
        $(this).val(itemQty);
        $(this).blur();
    });
}

function AddToCartBtnClick() {

    if (hasGiveaway != undefined && hasGiveaway) {
        return true;
    }
    if (numUpsells > 0 && (!$.browser.msie || ($.browser.msie && parseInt($.browser.version) >= 7))) {
        if (numErrors == 0)
            loadPopup();
        else
            ReEnableButton();
        return false;
    }
    else return true;
}


//CONTROLLING EVENTS IN jQuery
$(document).ready(function () {
    //LOADING POPUP
    //Click the button event!
    
    $("#ctl00_cphBody_ctl00_btnAddToCart").click(function () {

        //        alert('ahhh!');

        //        if (hasGiveaway != undefined && hasGiveaway) {
        //            return true;
        //        }
        //        if (numUpsells > 0 && (!$.browser.msie || ($.browser.msie && parseInt($.browser.version) >= 7))) {
        //            if (numErrors == 0)
        //                loadPopup();
        //            else
        //                ReEnableButton();
        //            return false;
        //        }
        //        else return true;
        return AddToCartBtnClick();
    });

    $("#cphBody_ctl00_btnAddToCart").click(function () {
        return AddToCartBtnClick();
    });

    //CLOSING POPUP
    //Click the x event!
    //$("#popupContactClose").click(function() {
    //    disablePopup();
    //});
    //Click out event!
    //$("#div_upsell_background").click(function() {
    //    disablePopup();
    //});
    //Press Escape event!
    $(document).keypress(function (e) {
        if (e.keyCode == 27 && popupStatus == 1) {
            disablePopup();
            ReEnableButton();
        }
    });
    //loadPopup();

});
