﻿/// <reference name="MicrosoftAjax.js" />
/// <reference path="~/scripts/jquery-1.3.2-vsdoc2.js" />

Type.registerNamespace("SearsPhotos.Common");

//*************************************
// SearsPhotos.Common.SessionExtender
//*************************************
SearsPhotos.Common.SessionExtender = function() {

    this._idleTimeBeforeIssueMessage = 0;
    this._modalBehaviorId = null;
    this._timeoutID = null;
    this._timerCallBack = null;
}

SearsPhotos.Common.SessionExtender.prototype = {

    start: SearsPhotos$Common$SessionExtender$start,
    restartTimer: SearsPhotos$Common$SessionExtender$restartTimer,
    silentExtend: SearsPhotos$Common$SessionExtender$silentExtend,
    onExtendSessionOKClicked: SearsPhotos$Common$SessionExtender$onExtendSessionOKClicked,
    _setTimerForExtendSessionPopup: SearsPhotos$Common$SessionExtender$_setTimerForExtendSessionPopup,
    _showModalPopup: SearsPhotos$Common$SessionExtender$_showModalPopup,
    _onTouchSessionSuccess: SearsPhotos$Common$SessionExtender$_onTouchSessionSuccess,
    _onTouchSessionFail: SearsPhotos$Common$SessionExtender$_onTouchSessionFail
}

function SearsPhotos$Common$SessionExtender$start(idleTimeBeforeIssueMessage, modalBehaviorId) {
    this._idleTimeBeforeIssueMessage = idleTimeBeforeIssueMessage;
    this._modalBehaviorId = modalBehaviorId;
    this._setTimerForExtendSessionPopup();
}

function SearsPhotos$Common$SessionExtender$silentExtend() {
    SearsPhotos.CommonService.TouchSession(this._onTouchSessionSuccess, this._onTouchSessionFail, false);
}

function SearsPhotos$Common$SessionExtender$onExtendSessionOKClicked() {

    var mpeBehaviorhandle = $find(SearsPhotos.Common.SessionExtender._staticInstance._modalBehaviorId);
    mpeBehaviorhandle.hide();

    $("#ProdConfig").css("visibility", "visible");

    var successDelegate = Function.createDelegate(this, this._onTouchSessionSuccess);
    var failDelegate = Function.createDelegate(this, this._onTouchSessionFail);

    SearsPhotos.CommonService.TouchSession(successDelegate, failDelegate);
}

function SearsPhotos$Common$SessionExtender$_setTimerForExtendSessionPopup() {

    this._timerCallBack = Function.createDelegate(this, this._showModalPopup);
    this._timeoutID = window.setTimeout(this._timerCallBack, this._idleTimeBeforeIssueMessage);
}

function SearsPhotos$Common$SessionExtender$_showModalPopup() {
    var mpeBehaviorhandle = $find(SearsPhotos.Common.SessionExtender._staticInstance._modalBehaviorId);
    mpeBehaviorhandle.show();

    $("#ProdConfig").css("visibility", "hidden");

    window.focus();
}

function SearsPhotos$Common$SessionExtender$restartTimer() {

    window.clearTimeout(this._timeoutID);
    this._setTimerForExtendSessionPopup();
}

function SearsPhotos$Common$SessionExtender$_onTouchSessionSuccess(res) {

    try {
        if (res.IsAuthenticated == false) {
            window.location = window.location;
        }
        else {
            this._setTimerForExtendSessionPopup();
        }
    } catch (e) {


    }
}

function SearsPhotos$Common$SessionExtender$_onTouchSessionFail(res) {

}


SearsPhotos.Common.SessionExtender._staticInstance = new SearsPhotos.Common.SessionExtender();

SearsPhotos.Common.SessionExtender.start = function(idleTimeBeforeIssueMessage, modalBehaviorId) {
    SearsPhotos.Common.SessionExtender._staticInstance.start(idleTimeBeforeIssueMessage, modalBehaviorId);
}

SearsPhotos.Common.SessionExtender.silentExtend = function() {
    SearsPhotos.Common.SessionExtender._staticInstance.silentExtend();
}

SearsPhotos.Common.SessionExtender.onExtendSessionOKClicked = function() {
    SearsPhotos.Common.SessionExtender._staticInstance.onExtendSessionOKClicked();
}

SearsPhotos.Common.SessionExtender.restartTimer = function() {
    SearsPhotos.Common.SessionExtender._staticInstance.restartTimer();
}

function updateCartTotal() {
    $.ajax({
        type: "POST",
        url: "UpdateCartTotal.asmx/Update",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            $("#cartTotal").text(msg.d);
        }
    });
}

SearsPhotos.Common.SessionExtender.registerClass("SearsPhotos.Common.SessionExtender");

if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

/***************************/
//@Author: Adrian "yEnS" Mato Gondelle
//@website: www.yensdesign.com
//@email: yensamg@gmail.com
//@license: Feel free to use it, but keep this credits please!					
/***************************/

//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) {
        $("#backgroundPopup").css({
            "opacity": "0.7"
        });
        $("#backgroundPopup").fadeIn("slow");
        $("#popupContact").fadeIn("slow");
        popupStatus = 1;
    }
}

//disabling popup with jQuery magic!
function disablePopup() {
    //disables popup only if it is enabled
    if (popupStatus == 1) {
        $("#backgroundPopup").fadeOut("slow");
        $("#popupContact").fadeOut("slow");
        popupStatus = 0;
    }
}

//centering popup
function centerPopup() {
    //request data for centering
    var windowWidth = document.documentElement.clientWidth;
    var windowHeight = document.documentElement.clientHeight;
    var popupHeight = $("#popupContact").height();
    var popupWidth = $("#popupContact").width();
    //centering
    $("#popupContact").css({
        "position": "absolute",
        "top": windowHeight / 2 - popupHeight / 2,
        "left": windowWidth / 2 - popupWidth / 2
    });
    //only need force for IE6

    $("#backgroundPopup").css({
        "height": windowHeight
    });

}

$(document).ready(function() {
    $(".moreinfoLink").click(function() {

        centerPopup();
        loadPopup();

    });

    $("#popupContactClose").click(function() {
        disablePopup();
    });


    $("#backgroundPopup").click(function() {
        disablePopup();
    });

    $("#popupContact").click(function() {
        disablePopup();
    });

    $(document).keypress(function(e) {
        if (e.keyCode == 27 && popupStatus == 1) {
            disablePopup();
        }
    });

});

//FAQ code
jQuery(document).ready(function() {


    $(".category").next("div").slideDown("slow");


    $(".category").click(function() {

        $(this).next("div").toggle("slow");

    });


    $(".question").click(function() {

        $(this).next("div").toggle("fast");

    });


    $(".category").hover(function() {

        $(this).removeClass("categorynohover");
        $(this).addClass("categoryhover");

    }, function() {

        $(this).removeClass("categoryhover");
        $(this).addClass("categorynohover");

    }
             );


    $(".question").hover(function() {

        $(this).addClass("categorynohover");

    }, function() {

        $(this).removeClass("categorynohover");

    }
             );


    $("#expandLink").click(function() {

        $("div [id*='ToggeableDiv_']").slideDown("slow");
        $("#collapseLink").css("display", "");
        $(this).css("display", "none");

    });

    $("#collapseLink").click(function() {

        $("div [id*='ToggeableDiv_']").slideUp("slow");
        $("#expandLink").css("display", "");
        $(this).css("display", "none");

    });
});


Type.registerNamespace("SearsPhotos.Products");

//*************************************
// SearsPhotos.Products.ProdConfig
//*************************************

//CALL BACK FUNCTIONS NEEDED FOR PROD CONFIG
function ProdConfig_viewCartCallback() {
    window.location = "../Cart.aspx";
}

function ProdConfig_cartItemSavedCallback() {
    window.location = "../Cart.aspx";
}

function ProdConfig_cartItemCancelledCallback() {
    window.location = "../Cart.aspx";
}

function ProdConfig_viewProjectsCallback() {
    window.location = "../Albums/Albums.aspx?t=projects";
}

function ProdConfig_serverTouchedCallback() {
    SearsPhotos.Common.SessionExtender.restartTimer();
}

function ProdConfig_userActiveCallback() {

}

function ProdConfig_itemsAddedToCartCallback() {
    updateCartTotal();
}

function ProdConfig_analyticsEnabled() {
    return true;
}

SearsPhotos.Products.ProdConfig = function() {



}

SearsPhotos.Products.ProdConfig.prototype = {
    changeCategory: SearsPhotos$Products$ProdConfig$changeCategory

}

function SearsPhotos$Products$ProdConfig$changeCategory(categoryCode) {
    var obj = swfobject.getObjectById("ProdConfig");

    if (obj != null) {
        obj.ProdConfig_changeProductCategory(categoryCode);
    }
}




SearsPhotos.Products.ProdConfig.changeCategory = function(categoryCode) {

    SearsPhotos.Products.ProdConfig._staticInstance.changeCategory(categoryCode);
}



SearsPhotos.Products.ProdConfig.registerClass("SearsPhotos.Products.ProdConfig");
SearsPhotos.Products.ProdConfig._staticInstance = new SearsPhotos.Products.ProdConfig();


Type.registerNamespace("SearsPhotos.Products");

$PageScript = SearsPhotos.Products.BuildPictureGift = function() { }

SearsPhotos.Products.BuildPictureGift.initialize = function SearsPhotos$Products$BuildPictureGift$initialize(ezpAlbumFeedUrl, projectId, ddlProdListElem, txtOrderQuantityElem, hlSaveForLaterElem, onProductChangedHandler) {

    $PageScript._EZPrintApp;
    $PageScript._isFirstTemplateLoaded = false;
    $PageScript._products;
    $PageScript._ddlProdListElem = ddlProdListElem;
    $PageScript._txtOrderQuantityElem = txtOrderQuantityElem;
    $PageScript._hlSaveForLaterElem = hlSaveForLaterElem;
    $PageScript._onProductChangedHandler = onProductChangedHandler;
    $PageScript._EZPrintConfig = {
        elementId: 'appDiv',
        width: '100%',
        height: 530,
        sku: $PageScript._getSelectedSku(),
        transparent: true,
        addToCartCallback: $PageScript._onEZPAddToCartComplete,
        readyCallback: $PageScript._onEZPAppReady,
        loadCallback: $PageScript._onEZPProjectLoaded,
        saveCallback: $PageScript._onEZPSave,
        errorCallback: $PageScript._onEZPError,
        templateLoadCallback: $PageScript._onEZPTemplateLoaded,
        mediaPickerSources: [{ id: '1',
            title: '1',
            iconUri: '',
            collectionsUri: ezpAlbumFeedUrl}],
            singleMediaUploadEnabled: false
        };

        //register methods to load products
        SearsPhotos.Products.BuildPictureGiftService.GetProducts($PageScript._onGetProductsSuccess, $PageScript._onGetProductsFailure);

        //if there is a project id to load, add it to the config params
        if (projectId != "")
            $PageScript._EZPrintConfig.projectId = projectId;

        $PageScript._EZPrintApp = ezp.apps.createTemplateApp($PageScript._EZPrintConfig);
    }

    SearsPhotos.Products.BuildPictureGift._showAddToCartDialog = function SearsPhotos$Products$BuildPictureGift$_showAddToCartDialog() {

        $("#divAddingItem").show();
        $("#divItemAdded").hide();
        $("#divSavingItem").hide();
        $("#divItemSaved").hide();

        $find("modalPopupExtenderBID").show();
    }

    SearsPhotos.Products.BuildPictureGift._hideAllDialogs = function SearsPhotos$Products$BuildPictureGift$_hideAllDialogs() {
        $("#divAddingItem").hide();
        $("#divItemAdded").hide();
        $("#divSavingItem").hide();
        $("#divItemSaved").hide();
        //hide the modal
        $PageScript._hideDialog();
    }

    SearsPhotos.Products.BuildPictureGift._showItemAddedDialog = function SearsPhotos$Products$BuildPictureGift$_showItemAddedDialog() {
        $("#divAddingItem").hide();
        $("#divSavingItem").hide();
        $("#divItemSaved").hide();
        $("#divItemAdded").show();

        //show for 3 seconds then quickly fade
        window.setTimeout("new function(){$('#divItemAdded').fadeOut('def', $PageScript._hideDialog);}", 1000);

    }

    SearsPhotos.Products.BuildPictureGift._showSavingItemDialog = function SearsPhotos$Products$BuildPictureGift$_showSavingItemDialog() {

        $("#divAddingItem").hide();
        $("#divItemAdded").hide();
        $("#divSavingItem").show();
        $("#divItemSaved").hide();

        $find("modalPopupExtenderBID").show();
    }

    SearsPhotos.Products.BuildPictureGift._showItemSavedDialog = function SearsPhotos$Products$BuildPictureGift$_showItemSavedDialog() {
        $("#divAddingItem").hide();
        $("#divItemAdded").hide();
        $("#divSavingItem").hide();
        $("#divItemSaved").show();

        window.setTimeout("new function(){$('#divItemSaved').fadeOut('def', $PageScript._hideDialog);}", 1000);
    }

    SearsPhotos.Products.BuildPictureGift._hideDialog = function SearsPhotos$Products$BuildPictureGift$_hideDialog() {
        $find("modalPopupExtenderBID").hide();
    }

    SearsPhotos.Products.BuildPictureGift._onAddProductToCartSucceeded = function SearsPhotos$Products$BuildPictureGift$_onAddProductToCartSucceeded(res) {
        $PageScript._showItemAddedDialog();
        $PageScript._extendSession();
    }

    SearsPhotos.Products.BuildPictureGift._onAddProductToCartFail = function SearsPhotos$Products$BuildPictureGift$_onAddProductToCartFail(error) {

        $PageScript._hideAllDialogs();
        alert(error.get_message());
    }


    SearsPhotos.Products.BuildPictureGift._onEZPTemplateLoaded = function SearsPhotos$Products$BuildPictureGift$_onEZPTemplateLoaded() {

        if (!$PageScript._isFirstTemplateLoaded) {
            $PageScript._isFirstTemplateLoaded = true;

            var productChangedDelegate = Function.createDelegate($PageScript, $PageScript._onProductChanged);
            $addHandler($PageScript._ddlProdListElem, 'change', productChangedDelegate);

            var onSaveForLaterClickedDelegate = Function.createDelegate($PageScript, $PageScript._onSaveForLaterClicked);
            $addHandler($PageScript._hlSaveForLaterElem, 'click', onSaveForLaterClickedDelegate);

            $PageScript._ddlProdListElem.disabled = false;
            $PageScript._hlSaveForLaterElem.disabled = false;

        }
    }

    SearsPhotos.Products.BuildPictureGift._onSaveForLaterClicked = function SearsPhotos$Products$BuildPictureGift$_onSaveForLaterClicked() {
        $PageScript._showSavingItemDialog();
        $PageScript._EZPrintApp.save();
    }

    SearsPhotos.Products.BuildPictureGift._onEZPAddToCartComplete = function SearsPhotos$Products$BuildPictureGift$_onEZPAddToCartComplete(projectId, productSku, thumbUrl) {

        if (Page_ClientValidate()) {

            var info = $PageScript._EZPrintApp.getProjectInfo();

            $PageScript._showAddToCartDialog();

            var selProduct = $PageScript._getSelectedProduct();

            var input = new SearsPhotos.Products.AddProductToCartParamsDTO();

            input.ProductId = selProduct.ProductId;
            input.ProjectId = projectId;
            input.SKU = productSku;
            input.ThumbnailPath = thumbUrl;
            input.OrderQty = $PageScript._getOrderQty();

            SearsPhotos.Products.BuildPictureGiftService.AddProductToCart(input, $PageScript._onAddProductToCartSucceeded, $PageScript._onAddProductToCartFail, this);
            updateCartTotal();
        }
    }

    SearsPhotos.Products.BuildPictureGift._onSaveProductForLaterFail = function SearsPhotos$Products$BuildPictureGift$_onSaveProductForLaterFail(error) {
        $PageScript._hideAllDialogs();
        alert(error.get_message());
    }


    SearsPhotos.Products.BuildPictureGift._onEZPError = function SearsPhotos$Products$BuildPictureGift$_onEZPError(error) {
        alert(error.get_message());
    }

    SearsPhotos.Products.BuildPictureGift._onSaveProductForLaterSucceed = function SearsPhotos$Products$BuildPictureGift$_onSaveProductForLaterSucceed(res) {
        $PageScript._showItemSavedDialog();
        $PageScript._extendSession();
    }

    SearsPhotos.Products.BuildPictureGift._onEZPSave = function SearsPhotos$Products$BuildPictureGift$_onEZPSave(projectId, thumbUrl) {
        var message = new SearsPhotos.Products.SaveProductForLaterParamsDTO();
        var prod = $PageScript._getSelectedProduct();

        message.ProductId = prod.ProductId;
        message.ProjectId = projectId;
        message.ThumbnailPath = thumbUrl;

        SearsPhotos.Products.BuildPictureGiftService.SaveProductForLater(message, $PageScript._onSaveProductForLaterSucceed, $PageScript._onSaveProductForLaterFail, this);
    }




    SearsPhotos.Products.BuildPictureGift._getSelectedSku = function SearsPhotos$Albums$_OrderSpecialty$getSelectedSku() {
        var list = $PageScript._ddlProdListElem;
        var sku = list.options[list.options.selectedIndex].value;
        return sku;
    }

    SearsPhotos.Products.BuildPictureGift._getSelectedProduct = function SearsPhotos$Products$BuildPictureGift$_getSelectedProduct() {
        return $PageScript._getProduct($PageScript._getSelectedSku());
    }

    SearsPhotos.Products.BuildPictureGift._getProduct = function SearsPhotos$Products$BuildPictureGift$_getProduct(sku) {
        for (var i = 0; i < $PageScript._products.length; i++) {

            if ($PageScript._products[i].SKU == sku)
                return $PageScript._products[i];
        }

        return null;
    }

    SearsPhotos.Products.BuildPictureGift._getOrderQty = function SearsPhotos$Products$BuildPictureGift$_getOrderQty() {
        return $PageScript._txtOrderQuantityElem.value;
    }


    SearsPhotos.Products.BuildPictureGift._onGetProductsSuccess = function SearsPhotos$Products$BuildPictureGift$_onGetProductsSuccess(res) {
        $PageScript._products = res;
        $PageScript._setProductDetailsPanel(null);
    }

    SearsPhotos.Products.BuildPictureGift._onProductChanged = function SearsPhotos$Products$BuildPictureGift$_onProductChanged() {
        var product = $PageScript._getSelectedProduct();
        $PageScript._setProductDetailsPanel(product);
        $PageScript._EZPrintApp.setSku(product.SKU);

        //extend session
        SearsPhotos.Products.BuildPictureGiftService.Touch();
        $PageScript._extendSession();

        if ($PageScript._onProductChangedHandler != null) {
            $PageScript._onProductChangedHandler();
        }
    }

    SearsPhotos.Products.BuildPictureGift._onGetProductsFailure = function SearsPhotos$Products$BuildPictureGift$_onGetProductsFailure(err) { }


    SearsPhotos.Products.BuildPictureGift._onEZPProjectLoaded = function SearsPhotos$Products$BuildPictureGift$_onEZPProjectLoaded() {


    }

    SearsPhotos.Products.BuildPictureGift._extendSession = function SearsPhotos$Products$BuildPictureGift$_extendSession() {
        SearsPhotos.Common.SessionExtender.silentExtend();
    }

    SearsPhotos.Products.BuildPictureGift._setProductDetailsPanel = function SearsPhotos$Products$BuildPictureGift$_setProductDetailsPanel(product) {

        if (product == null) {
            product = $PageScript._getProduct($PageScript._getSelectedSku());
        }

        $get("prodDescription").innerHTML = product.Description;
        $get("prodPrice").innerHTML = product.Price;
        $get("prodImageDimensions").innerHTML = product.ImageDimensions;
        $get("prodProductDimensions").innerHTML = product.ProductDimensions;
    }

    SearsPhotos.Products.BuildPictureGift._onEZPAppReady = function SearsPhotos$Products$BuildPictureGift$_onEZPAppReady() {


    }

    if (typeof (Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();

