﻿basket_daemon = function() { };

basket_daemon.prototype.service_path = '/ajax/basket.ashx?__v=' + Math.floor(Math.random()*11458741502938) + '&method=';

basket_daemon.prototype.product_add = function(productId, quantity, callback) {
    basket.product_add_with_consultation(productId, quantity, 0, callback);
};

basket_daemon.prototype.product_add_with_consultation = function(productId, quantity, hadConsultation, callback) {
    var q = quantity; if (arguments[1] == null) q = 1;
    $.ajax({
    type: "GET",
        url: this.service_path + 'add&pid=' + productId + '&qty=' + quantity + '&hadConsultation=' + hadConsultation,
        success: function() {
            basket.product_add_callback(callback);
        },
        error: function() {
            alert('error');
        }
    });
};

basket_daemon.prototype.product_add_callback = function() {
    basket.build_right_panel();
    eval(arguments[0])();
};

basket_daemon.prototype.product_update = function(productId, quantity, callback) {
    var q = quantity; if (arguments[1] == null) q = 1;
    $.ajax({
        type: "GET",
        url: this.service_path + 'update&pid=' + productId + '&qty=' + quantity,
        success: function() {
            basket.product_update_callback(callback);
        },
        error: function() {
            alert('error');
        }
    });
};

basket_daemon.prototype.product_update_callback = function() {
    basket.build_right_panel();
    eval(arguments[0])();
};

basket_daemon.prototype.product_remove = function(productId, callback) {
    $.ajax({
        type: "GET",
        url: this.service_path + 'remove&pid=' + productId,
        success: function() {
            basket.product_remove_callback(callback);
        },
        error: function() {
            alert('error');
        }
    });
};

basket_daemon.prototype.product_remove_callback = function() {
    basket.build_right_panel();
    eval(arguments[0])();
};

basket_daemon.prototype.build_right_panel = function() {
    $.ajax({
        type: "GET",
        url: this.service_path + 'shopping_cart_json',
        success: function() {
            basket.build_right_panel_callback(arguments[0]);
        }
    });
};

basket_daemon.prototype.build_right_panel_callback = function() {
    var info = eval("(" + arguments[0] + ")");
    var html = '<h3>SHOPPING BASKET</h3>';
    html += '<div class="basket-items">';
    html += '<p>' + info.basketItemCountText + '</p>';

    for (var i = 0; i < info.basketItems.length; i++) {
        html += '<div class="item">';
        html += '<div class="title">';
        html += info.basketItems[i].itemName;
        html += '</div>';
        html += '<div class="label">Quantity</div>';
        html += '<div class="value">';
        html += info.basketItems[i].itemQuantity;
        html += '</div>';
        html += '<div class="clear"></div>';
        html += '<div class="label">Unit Price</div>';
        html += '<div class="value">£';
        html += info.basketItems[i].itemPrice;
        html += '</div>';
        html += '<div class="clear"></div>';
        html += '</div>';
    }

    html += '<div class="total">';
    html += '<div class="label-sub">Subtotal</div>';
    html += '<div class="value-sub">';
    html += info.basketSubTotalPrice;
    html += '</div>';
    html += '<div class="clear"></div>';
    html += '<div class="label-sub">Delivery</div>';
    html += '<div class="value-sub">';
    html += info.basketDeliveryPrice;
    html += '</div>';
    html += '<div class="clear"></div>';
    html += '<div class="label-main">Total</div>';
    html += '<div class="value-main">';
    html += info.basketTotalPrice;
    html += '</div>';
    html += '<div class="clear"></div>';
    html += '</div>';
    html += '<a href="/basket/" class="arrow">View basket &amp; checkout</a>';
    /*html += '<div class="sub-total">Sub Total '
    html += info.basketSubTotalPrice;
    html += '</div>'

    html += '<div class="sub-total">Delivery '
    html += info.basketDeliveryPrice;
    html += '</div>'

    html += '<div class="total"><h2>Total: ';
    html += info.basketTotalPrice;
    html += '</h2></div>';
    html += '<div class="links"><a href="/basket/" class="arrow">View basket &amp; checkout</a></div>';
    html += '</div>';*/

    //$('#basket_right_panel').html(html).animate({ backgroundColor: '#b50000' }, 250).animate({ backgroundColor: '#e5e5e5' }, 250);
    $('#basket_right_panel').html(html);
};

$(function() {
    basket = new basket_daemon();
    basket.build_right_panel();
});