var thickboxMap = {};
var thickboxMeasure = {};
var thickboxPaint = {};
var thickboxHandle = {};

var scroll = {};
var sortProducts = {};
var recalc = {};
var remove = {};
var checkSTForm = {};
var checkSearch = {};

var applyVoucher = {};
var removeVoucher = {};

var screenHeight;

$(document).ready(function () {

    /* Thumbnails */
    $(".thumbLink").click(function () {
        var id_bits = this.id.split("_");
        for (i=1; i<=4; i++) {
            if (i == id_bits[2]) {
                $("div#prodPic"+i).show();
            } else {
                $("div#prodPic"+i).hide();
            }
        }
    });

    /* Quantity */
    $(".recalculate").click(function () {
        var qty_array = $("select#qty").val().split("|");
        $("#totalPrice").html("&pound;"+qty_array[1]);
    });

    recalc = function() {

        if ($('select#colour').length > 0) {
            var colour_array = $("select#colour").val().split("|");
            var surcharge = colour_array[1];
        }

        var qty_array = $("select#qty").val().split("|");

        if ($('select#colour').length > 0) {
            var surcharge_total = (parseFloat(colour_array[1])*parseFloat(qty_array[0]));
            total = (parseFloat(qty_array[1])+parseFloat(surcharge_total));
            total = total.toFixed(2);
        } else {
            total = qty_array[1];
        }
        $("#totalPrice").html("&pound;"+total);
    };
  
    $(".recalculateFurniture").click(function () {
        recalc();
    });

    remove = function(x,y) {
        window.open('/useful-beautiful-unique/shop/my-basket/remove/?x='+x+'&y='+y,'_self');
    }

    $("#where").change(function() {
        if (this.value == 'other') {
            $("#otherDiv").show();
        } else {
            $("#otherDiv").hide();
        }
    });

    $("#buttonBasket").click(function() {
        window.open('/useful-beautiful-unique/shop/my-basket/','_self');
    });

    $("#buttonEdit").click(function() {
        window.open('/useful-beautiful-unique/shop/order/delivery/','_self');
    });

    checkSTForm = function() {
        var terms = $("input#terms").attr('checked');
        if (terms == true) {
            return true;
        } else {
            alert("Please read and agree to our Terms and Conditions");
            return false;
        }
    }
 
    sortProducts = function(x) {
        window.open(x+$("#sort").attr('value')+'/','_self');
    };

    /* Scroll */
    scroll = function(x) {
        $.scrollTo(x,800);
    };

    /* Check Search */
    checkSearch = function() {
        var keys = $("#keywords").attr('value');
        if (keys.length >= 3) {
            return true;
        } else {
            alert('Please type a search word or phrase (at least 3 characters)');
            return false;
        }
    };

    /* Vouchers */
    applyVoucher = function() {
        if ($('input#voucher_code').val() != '') {
            window.location.href='/basket.php?do=voucher&code='+escape($('input#voucher_code').val());
        } else {
            alert('Please enter a voucher code');
        }
    };

    removeVoucher=function() {
        window.location.href='/basket.php?do=remove-voucher';
    };

    /* Thickbox Windows */
    
    thickboxMap = function() {
        screenHeight = parseInt($(window).height()*0.8);
        $('#thickbox_link_map').html('<a href="/delivery-map.html?KeepThis=true&TB_iframe=true&height='+screenHeight+'&width=800" title="Delivery Price Map" class="thickbox">delivery price map</a>');
        tb_init('#thickbox_link_map a.thickbox');
    };

    thickboxMeasure = function() {
        screenHeight = parseInt($(window).height()*0.8);
        $('#thickbox_link_measure').html('<a href="/measuring.html?KeepThis=true&TB_iframe=true&height='+screenHeight+'&width=800" title="Measuring for your furniture" class="thickbox">Measuring for your furniture</a>');
        tb_init('#thickbox_link_measure a.thickbox');
    };

    thickboxPaint = function() {
        screenHeight = parseInt($(window).height()*0.8);
        $('#thickbox_link_paint').html('<a href="/paint-options.html?KeepThis=true&TB_iframe=true&height='+screenHeight+'&width=800" title="Paint Options" class="thickbox">Bespoke paint options</a>');
        tb_init('#thickbox_link_paint a.thickbox');
    };

    thickboxHandle = function() {
        screenHeight = parseInt($(window).height()*0.8);
        $('#thickbox_link_handle').html('<a href="/handle-options.html?KeepThis=true&TB_iframe=true&height='+screenHeight+'&width=800" title="Handle Options" class="thickbox">Bespoke handle options</a>');
        tb_init('#thickbox_link_handle a.thickbox');
    };
    if(typeof tb_init == 'function'){
        thickboxMap();
        thickboxMeasure();
        thickboxPaint();
        thickboxHandle();
    }
    
    $(".fb-gallery a[rel=gallery]").fancybox();
    $('.fb-gallery h2 a').click(function(e){
        e.preventDefault();
        $(this).parents('.item').find('.itemPic a').click();
    });
    $('.fb-gallery-enlarge').click(function(e){
        e.preventDefault();
        var href=$(this).attr('href');
        $(".fb-gallery .itemPic a[href='"+href+"']").click();
    });
});

$(window).resize(function() {
    if(typeof tb_init == 'function'){
        thickboxMap();
        thickboxMeasure();
        thickboxPaint();
        thickboxHandle();
    }
});
