﻿/// <reference path="json2.js" />
/// <reference path="date.format.js" />
/// <reference path="intellisense/jquery.intellisense.js" />
/// <reference path="intellisense/jquery.ajaxdotnet.intellisense.js" />


$(document).ready(function() {
    $('.actionbtn').bind('click', function(evt) {
        evt.preventDefault();
        var Id = $(this).attr("id");
        var price = parseFloat($(this).parents("li.shopproduct").find('ul li.details p.price').text().replace('Price : £', ''));
        addtocart(Id, price);
        return false;
    });
    $('.actionbtn2').bind('click', function(evt) {
        evt.preventDefault();
        var Id = $(this).attr("id");
        var price = parseFloat($(this).parents(".details").find('p.price').text().replace('Price : £', ''));
        addtocart(Id, price);
        return false;
    });

});

function addtocart(item,price) {
    var Item = {};    
    var Cart = new Object();
    Cart.carts = [];

    Item.Id = item;
    Item.Quantity = 1;
    Cart.carts[0] = Item;
    notice();
    $.ajax({
        type: "POST",
        url: "/shopping-basket.aspx/add",
        data: JSON.stringify(Cart),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(obj) {

            if (obj == "success") {

                var items = parseInt($('#lblItems').text().replace('(', '').replace(')', ''));
                var subtotal = parseFloat($('#lblsubtotaltext').text().replace('(£', '').replace(')', ''));
                $('#lblItems').text('(' + (items + 1) + ')')
                $('#lblsubtotaltext').text('(£' + (subtotal + price).toFixed(2) + ')')

                if ($('ul.headerbasket li.proceed').length == 0) {
                    $('ul.headerbasket').append('<li class="proceed"><a title="Proceed to checkout" href="/shopping-basket.aspx">Proceed to checkout</a></li>');
                }



            }
        }
    });


}

//function AddProceed() {
//    headerbasket
//    
//    <li class="proceed"><a title="Proceed to checkout" href="/shipping-address.aspx">Proceed to checkout</a></li>
// }

function notice(title) {
  
    jQuery.noticeAdd({
        inEffect: { opacity: 'show' }, // in effect
        inEffectDuration: 500, 			// in effect duration in miliseconds
        stayTime: 1000, 			// time in miliseconds before the item has to disappear
        text: "Updating Cart", 				// content of the item
        stay: false, 			// should the notice item stay or not?
        type: 'success' 			// could also be error, succes,notice
    });
}