﻿    var _gSessionId;
    var _gQty;
    var _gProductDetailId;
    var _gAllowedBackorder;
    var _gTitle;
    var _gRedirect;



    var _global_qty;
    var _global_profInstall;
    var _global_profInstallPrice;
    var _global_allowbackorder;
    var _global_sessionid_aspnet;
    var _global_productdetailid;
    var _global_stock;


    $(function() {


        $("a.BoxDetail").colorbox();


        //Quick Look Logic AND Function





        $("li.list_ItemListCss").hover(function() {

            var listbox = $(this);
            $(this).find("span.quicklook").mouseover(
                    function() {

                        RemoveTheLink($(listbox).attr("clientid"));

                    }
                    );
            AddTheLink($(this).attr("clientid"), $(this).attr("hidid"));

        }, function() {


            //            RemoveTheLink($("#" + $(this).attr("clientid")).val());


            //            $(this).find("span.quicklook").mouseover(
            //                    function() {

            //                        alert('memek');
            //                        RemoveTheLink($("#" + $(libox).attr("clientid")).val());

            //                    }
            //                    );



        });


        /*
        if (original.Height > original.Width)
        {
        targetH = targetSize;
        targetW = (int)(original.Width * ((float)targetSize / (float)original.Height));
        }
        else
        {
        targetW = targetSize;
        targetH = (int)(original.Height * ((float)targetSize / (float)original.Width));
        }
        
        */




        //Manupulate widget mainimage


        $("div").each(function(i, thediv) {
            if ($(thediv).attr("id").indexOf('PnlMain') != -1)
                $("#" + $(thediv).attr("id")).css("background", "#ffffff");

        });





        if ($('#CartInfoContainer').length > 0) {
            $('#CartInfoContainer').dialog({
                autoOpen: false,
                width: 650,
                height: 450,
                bgiframe: true,
                modal: true,
                resizable: false

            });
        }

        //        $(".list_ItemListCss").click(function() {

        //            window.location = $(this).find("[type='hidden']").val();

        //        });






        $("a.ACreate").click(function() {
            $('#CartInfoContainer').dialog('open');
        });
        $("a.ACreate").mouseover(function() {
            _gTitle = $(this).attr("title");
            $('#CartInfoContainer').attr("title", $(this).attr("title"));


            _gRedirect = 1;
        });
        $("a.ACreate").mouseout(function() {
            _gRedirect = 0;

        });
        $("#productList li").hover(
			function() { $(this).find('img.QuickLookImg:hidden').fadeIn(500); },
			function() { $(this).find('img.QuickLookImg:visible').fadeOut(500); }
			);
    });



//Handle the updatepanel asynchronize issue
    function pageLoad(sender, args) {

        //Product listing hight manupulation
        /*$("#productList li").each(function(i, theli) {


            var imgIn = $(theli).find("img");

            if ($(theli).find("img").height() > ($(theli).height() * .60)) {
                var desireH = $(theli).height() * .60;
                //Set reduced height

                imgIn.height(desireH);
                imgIn.width(imgIn.width() * .60);

                //Check width
                if (imgIn.width() > $(theli).width()) {

                    imgIn.width($(theli).width());
                    imgIn.heigh(imgIn.height() * ($(theli).width() / imgIn.width()));
                }
            }
            else if ($(theli).find("img").width() > $(theli).width()) {

                imgIn.width($(theli).width());
                imgIn.heigh(imgIn.height() * ($(theli).width() / imgIn.width()));
                if ($(theli).find("img").height() > ($(theli).height() * .60)) {
                    imgIn.height($(theli).height() * .60);
                    imgIn.width(imgIn.width() * .60);
                }

            }



        });*/
        if (args.get_isPartialLoad()) {
            

        }

    }



      
        function PopUpShopCartPutData(_sessionId, _imgSrc, _lblDescContent, _productdetailid) {
            //alert(_sessionId + " " + _imgSrc + " " + _lblDescContent);
            // alert($("#ctl00_ContentPlaceHolder1_ImagePopCrontrol1_productId").val());
           
            $("#ctl00_ContentPlaceHolder1_ImagePopCrontrol1_imgCartPopUp").attr("src", _imgSrc);
            $("#ctl00_ContentPlaceHolder1_ImagePopCrontrol1_lblCartDescription").html(_lblDescContent);
            $("#ctl00_ContentPlaceHolder1_ImagePopCrontrol1_productId").val(_productdetailid);     
        }

    function PopUpShopCart(_sessionId, _txtClientId) {
        alert($("#" + _txtClientId).val());
    }
    function QuickAddToCart(_sessionId, _txtAddQtyId, _productId, _allowedbackorder) {
        //alert(_sessionId + " \n" + _txtAddQtyId + " \n" + _productId);
        _gSessionId = _sessionId;
        _gProductDetailId = $("#" + _productId).val();
        _gAllowedBackorder = _allowedbackorder;     
        var _qty = $("#" + _txtAddQtyId).val();
        var _pdid = $("#" + _productId).val();
        if (_qty == "")
            _qty = 1;
        else if (isNaN(_qty)) {
            jAlert('You have entered an invalid number on quantity', 'Invalid Number','warningboxid');
            return false;
        }
        _gQty = _qty;
        EverythingIpod.wsEverythingIpod.AddToCartLogic(_qty, _pdid, _allowedbackorder, AddToCartLogicHandler);



        return false;
        
    
    }
    function AddToCartLogicHandler(ev)
    {
        if (ev.toString() == "0") {
            EverythingIpod.wsEverythingIpod.CheckAndAddToCart(_gSessionId, _gQty, _gProductDetailId, CheckAndAddToCartHandler);
        }
        else if (ev.toString() == "1") {
        EverythingIpod.wsEverythingIpod.GetItemInStock(_gProductDetailId, GetItemInStockHandler);
        }
        else {
            EverythingIpod.wsEverythingIpod.GetItemInStockAllowed(_gProductDetailId, _gQty, GetItemInStockAllowedHandler);
        }


    }
    function GetItemInStockAllowedHandler(ev) {

        $("#ctl00_ContentPlaceHolder1_ImagePopCrontrol1_lblAllowedHeader").html(ev.toString());

        $('#ContainerAllow').attr("title", _gTitle);
        $('#ContainerAllow').dialog({
            autoOpen: false,
            width: 650,
            bgiframe: true,
            modal: true,
            resizable: false,

            buttons: {
                "Choose Option 1": function() {
                    EverythingIpod.wsEverythingIpod.AddToCartNotAllowBackorder(_gSessionId, _gProductDetailId, CheckAndAddToCartHandler);  

                },
                "Choose Option 2": function() {
                EverythingIpod.wsEverythingIpod.AddToCartAllowBackorder(_gSessionId, _gProductDetailId, _gQty, CheckAndAddToCartHandler);

                },
                "Cancel": function() {
                    $(this).dialog("close");
                }
            }

        });
        $('#ContainerAllow').dialog('open');
        
        
    }
    
    function GetItemInStockHandler(ev) {
        $("#ctl00_ContentPlaceHolder1_ImagePopCrontrol1_lblNotAllowHeader").html(ev.toString());
        $('#ContainerNotAllow').attr("title", _gTitle);
        $("#ContainerNotAllow").dialog({
            autoOpen: false,
            width: 650,
            bgiframe: true,
            modal: true,
            resizable: false,
            buttons: {
                "Processed to Checkout": function() {
                EverythingIpod.wsEverythingIpod.AddToCartNotAllowBackorder(_gSessionId, _gProductDetailId, CheckAndAddToCartHandler);  
                },
                "Cancel": function() {
                    $(this).dialog("close");
                }
            }


        });
        $("#ContainerNotAllow").dialog('open');
    } 
  
    function CheckAndAddToCartHandler(ev) {
        if (ev != "none") {
            window.location = "AddedPageSuccess.aspx" + ev.toString(); 
       }
   }

   function redirecttoproductdetails(recordid, device, category) {
        if(_gRedirect == 0)
       window.location = 'productdetails.aspx?recordid=' + recordid + '&device=' + device + '&category=' + category;

}






function CheckInventoryAndLogic(_sessionid_aspnet, _qtyId, _replacementId, _productdetailid, _allowbackorder, _stock) {

    /*_sessionid_aspnet, _qty, _productdetailid, _replacement,
    _replacement_cost, _backorder*/
    _global_qty = $("#" + _qtyId).val();
    _global_sessionid_aspnet = _sessionid_aspnet;

    if (_replacementId != "") {
        _global_profInstall = $("#" + _replacementId).val().split('|')[0];
        _global_profInstallPrice = $("#" + _replacementId).val().split('|')[1];
    }
    else {
        _global_profInstall = "noinstallation";
        _global_profInstallPrice = "0";
    }
    _global_allowbackorder = _allowbackorder;
    _global_productdetailid = _productdetailid;
    _global_stock = _stock;


    if (_global_qty == "")
        _global_qty = 1;
    else if (isNaN(_global_qty)) {
        jAlert('You have entered an invalid number on quantity', 'Invalid Number', 'warningboxid');
        return false;
    }




    //_qty, _productdetailid, _allowBackorder
    MyIpodStore.MyIpodStore.AddToCartLogic(_global_qty, _global_productdetailid, _global_allowbackorder, AddToCartLogicHandler);

}
function AddToCartLogicHandler(ev) {


    /* string _sessionid, string _sessionid_aspnet, 
    string _qty, string _productdetailid,string _replacement, 
    decimal _replacement_cost, string _backorder*/



    if (ev.toString() == "0") {

        MyIpodStore.MyIpodStore.CheckAndAddToCart(0, _global_sessionid_aspnet, _global_qty,
                            _global_productdetailid, _global_profInstall, _global_profInstallPrice, 'no', CheckAndAddToCartHandler);
    }
    else if (ev.toString() == "1") {
        $("#BackOrderNotification").html("We have only " + _global_stock + " in stock<br /> do you want add all to your cart?");
        $("#BackOrderNotification").attr("title", "In stock Notification");
        $("#BackOrderNotification").dialog({
            autoOpen: false,
            width: 650,
            bgiframe: true,
            modal: true,
            resizable: false,
            buttons: {
                "Processed to Checkout": function() {
                    MyIpodStore.MyIpodStore.CheckAndAddToCart(0, _global_sessionid_aspnet, _global_stock,
                            _global_productdetailid, _global_profInstall, _global_profInstallPrice, 'yes', CheckAndAddToCartHandler);
                },
                "Cancel": function() {
                    $(this).dialog("close");
                }
            }
        });
        $("#BackOrderNotification").dialog('open');

        //MyIpodStore.MyIpodStore.GetItemInStock(_gProductDetailId, GetItemInStockHandler);
    }
    else {
        var _OptionText = "Please choose the following options: <br /> Option 1: split orders: 1st " + _global_stock + " qty and 2nd " + (_global_qty - _global_stock) + " qty";
        _OptionText += "<br /> Option 2: Ship  " + _global_stock + " qty";

        $("#BackOrderNotification").html("We have only " + _global_stock + " in stock<br /> do you want add all to your cart?");
        $("#BackOrderNotification").attr("title", "In stock Notification");
        $("#BackOrderNotification").dialog({
            autoOpen: false,
            width: 650,
            bgiframe: true,
            modal: true,
            resizable: false,
            buttons: {
                "Choose Option 1": function() {
                    MyIpodStore.MyIpodStore.CheckAndAddToCart(0, _global_sessionid_aspnet, _global_qty,
                            _global_productdetailid, _global_profInstall, _global_profInstallPrice, 'yes', CheckAndAddToCartHandler);

                },
                "Choose Option 2": function() {
                    MyIpodStore.MyIpodStore.CheckAndAddToCart(0, _global_sessionid_aspnet, _global_stock,
                            _global_productdetailid, _global_profInstall, _global_profInstallPrice, 'yes', CheckAndAddToCartHandler);
                },
                "Cancel": function() {
                    $(this).dialog("close");
                }
            }
        });
        $("#BackOrderNotification").dialog('open');

    }
    return false;
}
function AddNumberUp(_addnumberup) {

    if (isNaN($("#" + _addnumberup).val())) {
        jAlert('You have entered an invalid number on quantity', 'Invalid Number', 'warningboxid');
        return false;
    }
    var _upQty = $("#" + _addnumberup).val();
    if (_upQty == "")
        _upQty = 1;
    _upQty++
    $("#" + _addnumberup).val(_upQty);




}

function AddNumberDown(_addnumberdown) {
    if (isNaN($("#" + _addnumberdown).val())) {
        jAlert('You have entered an invalid number on quantity', 'Invalid Number', 'warningboxid');
        return false;
    }
    var _upQty = $("#" + _addnumberdown).val();
    if (_upQty == "")
        _upQty = 1;
    _upQty--

    if (_upQty < 0)
        _upQty = 0;
    $("#" + _addnumberdown).val(_upQty);

}
function CheckAndAddToCartHandler(ev) {
    if (ev.toString() == "0")
        window.location = "ShoppingCart.aspx";
    else
        alert(ev);

}
function GoToHearitFirst(recid) {
    window.location = recid;

}

function RemoveTheLink(liId) {

  
            $("#" + liId).unbind("click");
     
    

}
function AddTheLink(liId, cId) {

    // $("#" + liId).click(function() { alert('ttt'); });
    if ($("#" + liId).length > 0) {
       
         
            $("#" + liId).click(function() {
                window.location = $("#" + cId).val();
            });
   
    }
}
