﻿$(function() {
    //  Create Product Tabs
    var tabContainers = $('div.tabs div.tabcontainer div.bottom > div');

    $('div.tabs ul.tabNavigation a').click(function() {
        tabContainers.hide().filter(this.hash).show();

        $('div.tabs ul.tabNavigation a').removeClass('selected');
        $(this).addClass('selected');

        return false;
    }).filter(':first').click();
});

function createCustomiseAndBuy() {

    //  this builds the functionality for the customise and buy options
    //  the price is recalculated on each option
    //  change and retrieved via web service

    //  call the update when select boxes are changed or inputs clicked.
    $(".options select").change(function () {
        updateCustomiseAndBuy(false);
    });

    //  create add to basket function
    $(".add").click(function() {
        updateCustomiseAndBuy(true);
    });

    //  call it on start up.
    updateCustomiseAndBuy(false);
}

//  the updateCustomiseAndBuy function is called whenenever a user changes an option 
//  in the customise and buy panel
function updateCustomiseAndBuy(bAddToBasket) {
    var product = $(".productid input:first").val();
    var options = "";
    var quantity = $(".quantity select").val();
    var notes = $(".assemblynotes textarea").val();

    //  get options.
    $(".productoptions select").each(function () {
        options = options + "'" + $(this).val() + "' , ";
    });
    if (options.length > 0) options = options.substr(0, options.length - 3);

    //  build the JSON string.
    var JSON = "{" +
               " 'Product_ID' : '" + product + "' , " +
               " 'ProductOptions' : [ " + options + " ] , " +
               " 'Quantity' : '" + quantity + "' , " +
               " 'Notes' : '" + notes + "' , " +
               " 'AddToBasket' : '" + bAddToBasket + "' " +
               "}";

    //  call the web service passing the product, options, accessories, delivery and
    //  and then update the price.
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/Assets/SeagullFittings/WebServices/SeagullFittings.asmx/CustomiseAndBuy",
        data: JSON,
        dataType: "json",
        success: function (response) {
            var oResponse = eval("(" + response.d + ")");

            //  update prices.

            if (oResponse.DataTwo == "0.00") {
                $(".pricing").hide();
                $(".noprice").show();
            } else {
                $(".totalprice").html(oResponse.Data);
                $(".RRP").html("RRP: &pound;" + oResponse.DataFive);
                $(".savings").html("Save: <span>&pound;" + oResponse.DataSix + "</span>");

                $(".noprice").hide();
                $(".pricing").show();
            }

            if (bAddToBasket) {
                window.location = "/Basket.aspx"
            }

        },
        error: function (response) {

            //  if an error has occured display for debugging purposes.
            alert(response.responseText);
        }
    });
}
