﻿$(document).ready(function() {

    //  stop ajax cacheing
    $.ajaxSetup({
        cache: false
    });

});

$(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() {

    //  get the size options
    getSizeOptions();

    //  rebind the options if the colour is changed
    $("select.colour").change(function() {
        getSizeOptions();
    });

    //  finally get the initial price
    GetPrice();
}

function getSizeOptions() {
    var product = $(".productinfo input:first").val();
    var colour = $("select.colour").val();

    //  handle products with no colours
    if (colour == undefined) colour = 0;

    //  get sizes
    $(".productsizeoptions").load("/Assets/FearnleyCricket/WebServices/ProductOptions.aspx?product_id=" + product + "&colour_id=" + colour + " #ajax", "", function() {

        //  get the new price if an option is changed
        $(this).find("td.select input").click(function() {
            GetPrice();
        });
    });
}

function AddToBasket() {
    var colour = $("select.colour").val();
    var product = $(".productinfo input:first").val();
    var quantity = $(".quantity").val();
    var size = 0;
    var sizeCount = 0;

    //  handle products without colours
    if (colour == undefined) colour = 0;

    //  see if this product has sizes
    $(".productsizeoptions td.select").each(function() {
        sizeCount += 1;
        if ($(this).find("input").attr("checked")) size = $(this).find("input").val();
    });
    if (sizeCount > 0 && size == 0) {
        alert("Please select a size to add to your basket");
        
        return false;
    }

    //  build the JSON string.
    var JSON = "{" +
               " 'Product_ID' : '" + product + "' , " +
               " 'Colour_ID' : '" + colour + "' , " +
               " 'Size_ID' : '" + size + "' , " +
               " 'Quantity' : '" + quantity + "' , " +
               " 'ReturnUrl' : '" + location.href + "' " +
               "}";

    //  call the web service passing the product and options
    //  and then update the price.
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/Assets/FearnleyCricket/WebServices/FearnleyCricket.asmx/AddToBasket",
        data: JSON,
        dataType: "json",
        success: function (response) {
            var oResponse = eval("(" + response.d + ")");

            if (oResponse.Valid) {
                $("#addtobagmessage").html("<div class='message'>Your basket has been updated, the total is now " + oResponse.Data + " <b><a href='/basket.aspx'>Click here</a></b> to view your basket and checkout.</div>");
                $("span.baskettotal").text(oResponse.Data);
                $("span.items").text(oResponse.DataTwo);
            } else {
                alert("Sorry there was a problem adding the item to the basket, please try again.");
            }
        },
        error: function (response) {
            alert(response.responseText);
        }
    });
}

function GetPrice() {
    var colour = $("select.colour").val();
    var product = $(".productinfo input:first").val();
    var size = 0;
    var sizeCount = 0;

    //  handle products without colours
    if (colour == undefined) colour = 0;

    //  see if this product has sizes
    $(".productsizeoptions td.select").each(function() {
        sizeCount += 1;
        if ($(this).find("input").attr("checked")) size = $(this).find("input").val();
    });

    //  build the JSON string.
    var JSON = "{" +
               " 'Product_ID' : '" + product + "' , " +
               " 'Colour_ID' : '" + colour + "' , " +
               " 'Size_ID' : '" + size + "' " +
               "}";

    //  call the web service passing the product and options
    //  and then retrieve the price.
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/Assets/FearnleyCricket/WebServices/FearnleyCricket.asmx/GetPrice",
        data: JSON,
        dataType: "json",
        success: function(response) {
            var oResponse = eval("(" + response.d + ")");

            if (oResponse.Valid) {
                $("span.price").html(oResponse.Data);
            }
        },
        error: function(response) {
            alert(response.responseText);
        }
    });
}

function slideSwitch() {
    // Create Lander Slider
    var $active = $('#splash a.active');

    if ($active.length == 0) $active = $('.lander a:last');

    var $next = $active.next().length ? $active.next()
        : $('#splash a:first');


    $active.addClass('last-active');

    $next.css({ opacity: 0.0 })
        .addClass('active')
        .animate({ opacity: 1.0 }, 1000, function() {
            $active.removeClass('active last-active');
        });
}
