﻿
/* =====================================================================
Created By: Jordan Miller - LexJet Corporation
   Summary:	
  Includes: jquery.lj.add.to.basket-1.0.js
    Change: 2010-12-13 - Fixed display in Opera
=========================================================================
CSS

HTML

Example Call: 
$(".jquery-add-to-basket").addToBasket({});

*/
jQuery.addToBasket = function (obj, options) {


    try {
        var _obj = $(obj);
        var _qty = 1;
        var _sku = _obj.attr("sku");
        var _skuid = _obj.attr("skuid");
        var _qtyObj = (_obj.attr("qtyfieldid").length > 0) ? $(obj).closest(".buy-wrapper").find("#" + _obj.attr("qtyfieldid")) : null;

        //Replicate form posting functionality of pressing the enter key. 
        _qtyObj.keypress(function (event) {
            if (event.keyCode == "13") {
                event.preventDefault();
                $(_obj).trigger("click");
            }
        });

        //Get every object except the DEAD ones
        $(_obj).not(".dead").unbind("click").click(function () {
            if (_qtyObj != null) {
                //Figure out if the Quantity Object is a
                //TextField or DropDown. If it is, reset the quantity
                //to its value and continue
                if (_qtyObj.attr("tagName") == "INPUT") {
                    var value = _qtyObj.val().replace(/^\s\s*/, '').replace(/\s\s*$/, '');
                    var intRegex = /^\d+$/;
                    if (intRegex.test(value)
                    && value > 0)
                        _qty = value;
                    else {
                        alert("Quantity is invalid! Only positive numbers are allowed.");
                        return false;
                    }
                }
            }
            return addToBasket(obj)
        });

        function addToBasket(obj) {
            var redirectToBasket = options.RedirectToBasket;
            var data = "{'skuId': '" + _skuid + "', 'quantity': '" + _qty + "'}";
            var adding = $(obj).clone();

            //DEBUGGING ONLY
            //$("#new_specialinstructions").val(data);
            $.ajax({
                type: "POST",
                url: options.WebService,
                data: data,
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                async: true,
                beforeSend: function () {
                    //Remove any other existing status windows
                    //in the sell-wrapper
                    $(".sell-wrapper .flyout-wrapper").remove();

                    //Changes the Button so it cannot be pressed twice
                    //and the user knows something is happening. 
                    adding.attr("ID", $(obj).attr("ID") + "_adding");
                    adding.attr("href", SHOPPING_CART_URL);
                    adding.unbind('click');
                    adding.addClass("dead"); //flags the object so we don't accidently reset it. 
                    if (adding.attr("tagName") == "A") {
                        adding.find("IMG").attr("src", THEME_IMAGE_PATH + "btn_149_add_to_cart_adding.png");
                    } else if (adding.attr("tagName") == "INPUT") {
                        adding.attr("src", THEME_IMAGE_PATH + "btn_149_add_to_cart_adding.png");
                    }
                    $(obj).hide();
                    $(obj).after(adding);
                },
                success: function (msg) {
                    if (msg.d.length == 0)
                        redirectToBasket = true;
                    else {
                        //Update the Quick Basket.
                        updateQuickBasket();
                        //change the button icon to in basket display
                        if (adding.attr("tagName") == "A") {
                            adding.find("IMG").attr("src", THEME_IMAGE_PATH + "btn_149_in_cart_off.png");
                        } else if (adding.attr("tagName") == "INPUT") {
                            adding.attr("src", THEME_IMAGE_PATH + "btn_149_in_cart_off.png");
                        }

                        //Find and update any other links that may also be on the page. 
                        $("[skuid = '" + _skuid + "']").each(function () {
                            $(this).addClass("dead"); //flags the object so we don't accidently reset it. 
                            $(this).unbind("click");  //Remove the click event
                            $(this).attr("href", SHOPPING_CART_URL);
                            if ($(this).attr("tagName") == "A") {
                                $(this).find("IMG").attr("src", THEME_IMAGE_PATH + "btn_149_in_cart_off.png");
                            }
                            else if ($(this).attr("tagName") == "INPUT") {
                                $(this).attr("src", THEME_IMAGE_PATH + "btn_149_in_cart_off.png");
                            }
                        });

                        //The WebService will return the Status Window. Append it to the sell-wrapper for display in the UI.
                        $(obj).closest(".add-to-basket").prepend(msg.d.toString()).find(".jquery-close-window").click(function () {
                            $(this).closest(".flyout-wrapper").remove(); //Bind the buttons
                        });

                        //Reset the hovering mechanism
                        setImageHover();
                    }
                }
                , error: function (xhr, msg, e) {
                    alert(xhr.responseText);
                }
            });

            return redirectToBasket;
        }
    }
    catch (e) {
        //Ignore errors and let the add-to-cart hard coded url do the work. 
    }
}

jQuery.fn.addToBasket = function(options) {
    options = options || {};
    options.RedirectToBasket = options.RedirectToBasket || false;
    options.WebService = options.WebService || "../webservices/basketwebservice.asmx/AddItemToBasketBySkuId";

    this.each(function() {
        var obj = this;
        new jQuery.addToBasket(obj, options);
    });
    return this;
}

