$(document).ready(function () {

    $('#squery').focus(function () {
        $(this).val('');
    });
    $('#srch').val('');

    $('a.printer').click(function () {
        print();
    });

    $('#banner-close').click(function () {
        $(this).parents('#banner').slideUp('fast')
    });


    // Slideshow Functions



    // Increment Index and show slide
    function nextSlide() {
        slindex++;
        if (slindex > $("#slideshow li").size() - 1) { slindex = 0; }
        showSlide(slindex);
        t = setTimeout(nextSlide, 8000);
    }

    // Kill last slide and show slide. Update Navigation.
    function showSlide(i) {
        $("#slideshow li").hide();
        $("#slideshow li:eq(" + i + ")").fadeIn('fast');
        $("#slidectrl a").css("background-position", "0 0");
        $("#slidectrl a:eq(" + i + ")").css("background-position", "0 -14px");
        slindex = i;
    }

    // Slideshow Navigation
    $("#slidectrl a").click(function () {
        i = $(this).text();
        showSlide(i);
        clearTimeout(t);
        t = setTimeout(nextSlide, 8000);
        return false;
    });

    //Slideshow Init
    var t;
    slindex = -1;
    nextSlide();

    // heroshow Functions



    // Increment Index and show slide
    function nextHeroSlide() {
        hslindex++;
        if (hslindex > $("#heroshow li").size() - 1) { hslindex = 0; }
        showHeroSlide(hslindex);
        ht = setTimeout(nextHeroSlide, 8000);
    }

    // Kill last slide and show slide. Update Navigation.
    function showHeroSlide(i) {
        $("#heroshow li").hide();
        $("#heroshow li:eq(" + i + ")").fadeIn('fast');
        $("#heroctrl li").removeClass("on");
        $("#heroctrl li:eq(" + i + ")").addClass("on");
        hslindex = i;
    }

    // Slideshow Navigation
    /*$("#heroctrl a").click(function(){
    i = $(this).attr("rel");
    showHeroSlide(i);		
    clearTimeout(ht);
    ht = setTimeout(nextHeroSlide, 8000);
    return false;
    });*/

    //Slideshow Init
    var ht;
    hslindex = -1;
    nextHeroSlide();

    //Checkout - Shipping same as billing
    (function () {
        $("#checkout-auto-shipping").css('display', 'block');
        var checkbox = $("input#shipping-same-billing");
        if (checkbox.length) {
            updateFieldPair = function (e) {
                e.data.shippingField.val($(this).val());
            }
            checkbox.change(function () {
                var originalArr = ["#fn", "#ln", "#address", "#city", "#country", "#postcode", "#phone"];
                var shippingArr = ["#shipping-fn", "#shipping-ln", "#shipping-address", "#shipping-city", "#shipping-country", "#shipping-postcode", "#shipping-phone"];
                if ($(this).is(":checked")) {
                    for (var i = 0; i < originalArr.length; i++) {
                        var originalField = $(originalArr[i]);
                        var shippingField = $(shippingArr[i]);
                        shippingField.attr("disabled", true);
                        shippingField.val(originalField.val());

                        originalField.bind('change.autoshipping', { shippingField: shippingField }, updateFieldPair);
                    }
                } else {
                    for (var i = 0; i < originalArr.length; i++) {
                        var originalField = $(originalArr[i]);
                        var shippingField = $(shippingArr[i]);
                        shippingField.removeAttr("disabled");

                        originalField.unbind('change.autoshipping');
                    }
                }
            });
        }
    })();

    //Product page - gallery
    (function () {
        $('.shop-product-individual-gallery').each(function () {
            var galleryObj = {
                $this: $(this),
                numItems: 3,
                currentPage: 0
            }
            galleryObj.thumbList = $('.shop-product-individual-thumblist', galleryObj.$this);
            galleryObj.thumbListItems = $('li', galleryObj.thumbList);
            galleryObj.thumbListWidth = $('li', galleryObj.thumbList).length * $('li', galleryObj.thumbList).outerWidth(true)
            galleryObj.thumbList.width(galleryObj.thumbListWidth);
            galleryObj.leftControl = $('.shop-product-individual-leftcontrol a', galleryObj.$this);
            galleryObj.rightControl = $('.shop-product-individual-rightcontrol a', galleryObj.$this);
            galleryObj.maxPages = Math.ceil(galleryObj.thumbListItems.length / 3);

            galleryObj.thumbListItems.each(function (i) {
                $(this).bind('click.productgallery', { index: i, galleryObj: galleryObj }, function (e) {
                    e.preventDefault();
                    switchToIndex(e.data.index, e.data.galleryObj);
                });
            });

            if (galleryObj.maxPages > 1) {
                galleryObj.leftControl.bind('click.productgallery', { galleryObj: galleryObj }, previousClick);

                galleryObj.rightControl.bind('click.productgallery', { galleryObj: galleryObj }, nextClick);
            } else {
                galleryObj.leftControl.hide();
                galleryObj.rightControl.hide();
            }

            scrollThumb(0, galleryObj);
        });

        function switchToIndex(index, galleryObj) {
            scrollThumb(Math.floor(index / galleryObj.numItems), galleryObj);
            $('.shop-product-individual-images-main img', galleryObj.$this).attr('src', '');
            $('.shop-product-individual-images-main img', galleryObj.$this).attr('src', galleryObj.thumbListItems.eq(index).children('a').attr('href'));
            scrollControlStates();
        }

        function scrollThumb(page, galleryObj) {
            if (page < 0) {
                page = 0;
            } else if (page > (galleryObj.maxPages - 1)) {
                page = galleryObj.maxPages - 1;
            }

            galleryObj.thumbList.stop(true);
            var thumboffset = galleryObj.thumbListItems.eq(page * galleryObj.numItems).position().left;
            galleryObj.thumbList.animate({ left: -thumboffset }, 700, 'swing');

            galleryObj.currentPage = page;

            if (galleryObj.maxPages > 1) {
                galleryObj.leftControl.stop(true);
                galleryObj.rightControl.stop(true);
                if (page == 0) {
                    galleryObj.leftControl.fadeOut(400);
                    galleryObj.rightControl.fadeIn(400);
                } else if (page == galleryObj.maxPages - 1) {
                    galleryObj.leftControl.fadeIn(400);
                    galleryObj.rightControl.fadeOut(400);
                } else {
                    galleryObj.leftControl.fadeIn(400);
                    galleryObj.rightControl.fadeIn(400);
                }
            }
        }

        function previousClick(e) {
            e.preventDefault();
            scrollThumb(e.data.galleryObj.currentPage - 1, e.data.galleryObj);
        }

        function nextClick(e) {
            e.preventDefault();
            scrollThumb(e.data.galleryObj.currentPage + 1, e.data.galleryObj);
        }
    })();


    $(document).ready(function () {
        $(".expandable-list").each(function () {
            var $this = $(this);
            $this.children('ul').each(function () {
                var $this = $(this)
                $this.children('li').removeClass('open').eq(0).addClass('open');
                $this.children('li').each(function (i) {
                    var $this = $(this);
                    if (i === 0) {
                        $this.children('ul').show();
                    } else {
                        $this.children('ul').hide();
                    }
                    $this.children('a').click(function (e) {
                        e.preventDefault();
                        var $this = $(this);
                        var isOpen = $this.parent('li').hasClass('open');
                        if (isOpen) {
                            $this.parent('li').removeClass('open');
                            $this.siblings('ul').hide();
                        } else {
                            $this.parent('li').addClass('open');
                            $this.siblings('ul').show();
                        }
                    })
                })
            })
        })
    });
});
