﻿function addToBasket(sender, pproductId, pquantity, setQuantity, closedialog) {

    if (pproductId == null) {
        var href = $(this).attr("href");

        pproductId = href.substring(href.lastIndexOf("product=") + 8) * 1;
    }

    var body = $('body');
    var dialog = $('<div class="ecom_add_popup"><div class="content"></div><div class="buttons"></div></div>');
    var blackout = $('<div></div>');

    var btnRemove = $('<a class="button arrow right" href="#"><span>Remove Item</span></a>');
    var btnContinue = $('<a class="button pink arrow right" href="#"><span>Continue Shopping</span></a>');
    var btnBasket = $('<a class="button blue ecomcontinue right" href="/Ecommerce/Basket/"><span>Goto Basket</span></a>');
    var ctlrQuantity = $('<span class="ctrlQuantity"><span class="label">Quantity:</span> <a class="button right" href="#"><span>-</span></a><input readonly="readonly" type="text" name="quantity" id="txtQuantity"/><a class="button2 right" href="#"><span>+</span></a></span>');
    var ctrlUnitPrice = $('<span class="ctrlUnitPrice"><span class="label">Unit Price:</span> <span class="value"></span></span>');
    var ctrlLineValue = $('<span class="ctrlLineValue"><span class="label">Line Value:</span> <span class="value"></span></span>');
    
    var content = dialog.find('.content');

    btnContinue.click(function() { dialog.remove(); blackout.remove(); return false; });
    btnRemove.click(function() { dialog.remove(); blackout.remove(); addToBasket(this, pproductId, 0, true, true); return false; });
    ctlrQuantity.find('.button').click(function() { dialog.remove(); blackout.remove(); addToBasket(this, pproductId, parseInt(ctlrQuantity.find('#txtQuantity').val()) + 1, true); return false; });
    ctlrQuantity.find('.button2').click(function() { dialog.remove(); blackout.remove(); addToBasket(this, pproductId, parseInt(ctlrQuantity.find('#txtQuantity').val()) - 1, true); return false; });
    
    dialog.css({
        'top': ($(window).scrollTop() + 50) + 'px',
        'left': (body.width() / 2 - 250) + 'px'
    });

    blackout.css({
        'position' : 'absolute',
        'top':'0px',
        'left':'0px',
        'width': body.width() + 'px',
        'height': body.height() + 'px',
        'background':'black',
        'z-index':'4000'
    });

    body.append(dialog);
    
    body.append(blackout);
    blackout.fadeTo(0, 0.0);
    blackout.fadeTo(200, 0.3);
    dialog.fadeIn(500);

    var requestdata = { product: pproductId };

    if (pquantity != null) {
        requestdata.quantity = pquantity;

        if (setQuantity != null && setQuantity != false) {
            requestdata.set = 1;
        }
    }
    else {
        if (sender) {
            var qty = $(sender.currentTarget).parent().find('input.qty');

            if (qty.length > 0)
                requestdata.quantity = qty.val();
        }
    }

    content.html('<span class="loading">Adding to basket...</span>');

    $.ajax(
        {
            type: "GET",
            url: "/ecommerce/add_to_basket.aspx",
            data: requestdata,
            dataType: "json",
            success: function(data) {

                $('.ecom_basket').show();

                if (data.totalitems == 0) {
                    $('.ecom_basket').text("");
                    $('.ecom_basket').hide();
                }
                else {
                    var itemtext = 'Items';

                    if (data.totalitems == 1) itemtex = "Item";

                    $('.ecom_basket').text("Your Basket: " + data.totalitems + " " + itemtext + "  £" + data.basket.Order_value);
                }



                if (closedialog == true) {
                    dialog.remove(); blackout.remove(); return true;
                }

                content.html('<h1>You have added to your basket:</h1><span class="prodimage"></span><h2></h2><span class="description"></span>');

                content.find('h2').text(data.product.Description);
                content.find('.prodimage').append('<img src="/Content/Product/Thumb/' + data.product.thumbImage + '" />');

                content.append(ctlrQuantity);
                content.append(ctrlUnitPrice);
                content.append(ctrlLineValue);

                ctlrQuantity.find('#txtQuantity').val(data.quantity);
                ctrlUnitPrice.find('.value').text("£" + data.unitprice);
                ctrlLineValue.find('.value').text("£" + data.linevalue);
                

                dialog.find('.buttons').append(btnBasket).append(btnContinue).append(btnRemove);
                // alert(data);



            },
            error: function(er, textStatus, errorThrown) { alert("There was a problem adding to your basket. Please try again"); }
        }
    );
    
    return false;

    return true;

    if (pproductid == null) {
        var href = $(this).attr("href");

        pproductId = href.substring(href.lastIndexOf("/p") + 2)*1;
    }

    if (pquantity == null) {
    
        var tmp = $(this).parent().find(".qty");
        if (tmp.val()) 
        {
            pquantity = tmp.val();
        }
        else 
        {
            pquantity = 1;
        }
    }

    var requestdata = { product: pproductId, quantity: pquantity }


        
    
    return false;
}

