﻿/*!
* Codagenic cStore v1.0.x beta
* www.codagenic.com.au
*
* Copyright 2010, Codagenic
*
* Stable With: jQuery V1.5.1
* Media Module Requires Fancybox
*
* Date: Tue 05 Apr 15:55:04 2011 +1100
*/

var base = "";

cstore = {
    defaults: {
        baseMediaURI: "",
        displayCurrencyCode: true
    },
    addToShoppingCartControl: {
        init: function () {
            $(document).ready(function () {
                if (cstore.defaults.displayCurrencyCode) {
                    window.currencyCode = $('#cStorePriceDisplay .default').text().substring(0, 3);
                }
                cstore.colorPicker.init();
                cstore.sizePicker.init();
                // IE7 problem means just a normal drop and set the width rather than a drawer control
                $('#cstoreAddToCartControls .last select').css('width', '44px');
                //cstore.quantityPicker.init();
            });
        }
    },
    inventory: {
        check: function (sender) {
            var availabe = false;
            for (var i in attribute_colour[sender]['inventory']) {
                if (attribute_colour[sender]['inventory'][i]['qty'] > 0) {
                    availabe = true;
                    break;
                }
            }

            cstore.inventory.disableAddToCart(availabe);

        },
        disableAddToCart: function (available) {
            // work out what multivariate option is in use for google website optimizer
            var variation = "a";
            var nameEQ = "mv=";
            var ca = document.cookie.split(';');
            for (var i = 0; i < ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0) == ' ') c = c.substring(1, c.length);
                if (c.indexOf(nameEQ) == 0) variation = c.substring(nameEQ.length, c.length);
            }

            if (variation == "b") {
                if (!available) {
                    $(".addToCartPostback").hide();
                    $('#cstoreAddToCartControls .last').hide();
                    $('#btnUnavailable').show();
                    $(".rShoppingCartAddToCartButtons").removeClass("rShoppingCartAddToCartButtons").addClass("rShoppingCartAddToCartButtonsNoShip");
                } else {
                    $(".addToCartPostback").show();
                    $('#cstoreAddToCartControls .last').show();
                    $('#btnUnavailable').hide();
                    $(".rShoppingCartAddToCartButtonsNoShip").removeClass("rShoppingCartAddToCartButtonsNoShip").addClass("rShoppingCartAddToCartButtons");
                }
            }
            else {
                if (!available) {
                    $("#addToCartJS").hide();
                    $('#cstoreAddToCartControls .last').hide();
                    $('#btnUnavailable').show();
                    $(".rShoppingCartAddToCartButtons").removeClass("rShoppingCartAddToCartButtons").addClass("rShoppingCartAddToCartButtonsNoShip");
                } else {
                    $("#addToCartJS").show();
                    $('#cstoreAddToCartControls .last').show();
                    $('#btnUnavailable').hide();
                    $(".rShoppingCartAddToCartButtonsNoShip").removeClass("rShoppingCartAddToCartButtonsNoShip").addClass("rShoppingCartAddToCartButtons");
                }
            }            
        }
    },
    colorPicker: {
        init: function () {
            var source = $('#cstoreAddToCartControls .colour select');
            var selected = source.find("option[selected]");
            var options = $("option", source);
            var indicator = $('lblcolour').children('.indicator');
            source.after('<ul id="colourList"></ul>');
            window.activeColourIndex = 0;
            window.activeThumbIndex = 0;
            var ulColour = $('#colourList');
            var stringBuilder = "";

            options.each(function (i) {

                var bgAttribute = null;
                // priority for pattern colour swatch background
                if (attribute_colour[i]['patternColourUrl'] != '') {
                    bgAttribute = 'url(' + attribute_colour[i]['patternColourUrl'] + ') no-repeat 1px 1px';
                } else {
                    bgAttribute = attribute_colour[i]['hexCode'];
                }

                if (bgAttribute.toLowerCase() == "#fff" || bgAttribute.toLowerCase() == "#ffffff") {
                    bgAttribute = "url(/images/buttons/whiteSwatch.gif) no-repeat 2px 2px";
                }

                var lineclass = "cstoreAttr";

                if ($(this).val() == selected.val()) {
                    lineclass += " state-active";
                }

                stringBuilder += "<li><a id=\"colourAttr_" + i + "\" style=\"background:" + bgAttribute + ";\" href=\"#\" class=\"" + lineclass + "\" title=\"" + attribute_colour[i]['fullName'] + "\">" + attribute_colour[i]['title'] + "</a></li>";

            });

            ulColour.append(stringBuilder);

            ulColour.delegate('a', 'click', function () {
                var obj = $(this).attr('id').replace(/colourAttr_/g, '');
                $('#colourList .state-active').removeClass('state-active');
                $(this).parent().addClass('state-active');
                window.activeColourIndex = obj;
                source.val($(this).text());
                $("#lblcolour .indicator").html(attribute_colour[obj]['fullName']);

                source.change();
                cstore.media.parse(obj);
                cstore.sizePicker.parse(obj);
                cstore.inventory.check(obj);

                if ($('#sizeList > li .state-active').length == 0) {
                    cstore.sizePicker.clean(true);
                } else {
                    cstore.sizePicker.clean();
                }

                return false;
            });

            source.hide();
        }
    },
    sizePicker: {
        init: function () {

            //Replace Size Form Elements
            // Changed by Marcus - because there are potentially two ddls in the size section need a different way to get the element so do it by class
            var source = $('#cstoreAddToCartControls select.sizeNumbers');
            var selected = source.find("option[selected]");
            var options = $("option", source);
            source.after('<ul id="sizeList"></ul>');
            var ulSizes = $('#sizeList');
            var stringBuilder = "";

            //Check for one size fits all
            if (source.children('option:first').val() == 'EA') {
                source.parent().hide();
                source.parent().parent().find('.last').removeClass('last').addClass('panel1'); //Specific for emu
            }

            options.each(function (i) {

                var lineclass = "cstoreAttr";

                if ($(this).val() == selected.val()) {
                    lineclass += " state-active";
                }
                // plus sign needs to be replaced as does not conform with browser standards within the ID field, sizes can have plus signs (e.g. "18+")
                stringBuilder += "<li><a href=\"#\" id=\"sizeAttr_" + $(this).val().replace(/\+/g, "plus") + "\" class=\"" + lineclass + "\">" + $(this).text() + "</a></li>";
            });

            ulSizes.append(stringBuilder);

            ulSizes.delegate('a', 'click', function () {
                if (!$(this).hasClass('disabled')) {
                    //var obj = $(this).text(); // Old statement pre change
                    // Changed by Marcus - as there are now size conversions take the portion of the ID rather than the text as the text changes on region select
                    var obj = $(this).attr("id");
                    $("#lblsize .indicator").html($(this).text());
                    obj = obj.replace(/sizeAttr_/g, "").replace(/plus/g, "+");
                    $('#sizeList li .state-active').removeClass('state-active');
                    $(this).addClass('state-active');
                    $('#cstoreAddToCartControls select.sizeNumbers').val(obj);
                    $('#cstoreAddToCartControls select.sizeNumbers').change(obj);
                    cstore.price.set(obj);
                }
                return false;
            });

            source.hide();
            // Get and set the default colour .....
            var defaultColourIndex = 0;
            if (typeof defaultColour != 'undefined') {
                $('#cstoreAddToCartControls .colour select option').each(function () {
                    if ($(this).val() == defaultColour) {
                        defaultColourIndex = $(this).index();
                    }
                });
            }

            $('#colourList li:eq(' + defaultColourIndex + ') a').click();

            cstore.sizePicker.clean(true);

            // Now check for a region element to the size and initialise ....
            if ($('#cstoreAddToCartControls select.sizeRegions').length > 0) {
                cstore.sizeRegionPicker.init();
                cstore.sizePicker.rebuild();
                // We want to select the correct size for the user's current region if applicable (UK, EUR and/or Japan) ......
                var currentRegion = $(".currencyHolder a#regionSelector span:eq(0)").attr("class");
                if (currentRegion == "germany" || currentRegion == "japan" || currentRegion == "unitedkingdom") {
                    var regionIndex = 0;
                    switch (currentRegion) {
                        case "unitedkingdom":
                            regionIndex = 1;
                            break;
                        case "germany":
                            regionIndex = 2;
                            break;
                        case "japan":
                            regionIndex = 3;
                            break;
                    }
                    var source = $('#cstoreAddToCartControls select.sizeRegions');
                    var obj = $("#sizeRegionList li:eq(" + regionIndex + ") a").attr("id");
                    obj = obj.replace(/sizeAttr_/g, "");
                    var newval = obj;

                    $("#regionSelector span:first").removeClass();
                    $("#regionSelector span:first").addClass(newval);
                    $('#sizeRegionList li .state-active').removeClass('state-active');
                    $("#sizeRegionList li:eq(" + regionIndex + ") a").addClass('state-active');
                    source.val(obj);
                    source.change(obj);
                    // We now need to rebuild the sizes for the region ....
                    cstore.sizePicker.rebuild(true);
                }
            }
        },
        parse: function (sender) {
            var sizesArray = attribute_colour[sender]['inventory'];
            var sizesLength = sizesArray.length;
            var disabledLength = 0;
            $('#sizeList li a').removeClass('disabled');
            $('#sizeList li').each(function (i, e) {
                for (var x = 0; x < sizesArray.length; x++) {
                    // Changed by Marcus - as there are now size converions take the portion of the ID rather than the text as the text changes on region select
                    var obj = $('a', this).attr("id");
                    obj = obj.replace(/sizeAttr_/g, "").replace(/plus/g, "+");
                    //if (sizesArray[x]['sizeId'] == $('a', this).text()) { //Old statement pre change
                    if (sizesArray[x]['sizeId'] == obj) {
                        if (sizesArray[x]['qty'] <= 0) {
                            $('a', this).addClass('disabled');
                            disabledLength++;
                        }
                    }
                }
            });

            if (disabledLength == sizesLength) {
                $('#productColumn2 input.rShoppingCartAddToCartButton').attr({ 'disabled': true }).addClass('disabled');
            } else {
                $('#productColumn2 input.rShoppingCartAddToCartButton').attr({ 'disabled': false }).removeClass('disabled');
                cstore.sizePicker.clean();
            }
        },
        clean: function (sender) {
            sender = undefined ? null : sender;
            //Select First Available Size
            if ($('#sizeList li a.state-active').hasClass('disabled') || sender == true) {
                $('#sizeList li a').each(function () {
                    if (!$(this).hasClass('disabled')) {
                        $(this).click();
                        return false;
                    }
                });
            } else {
                $('#sizeList li a.state-active').click();
            }
        },
        rebuild: function (sender) {
            sender = undefined ? null : sender;
            // Called when we have changed the region - build the sizes applicable to the region using the conversion matrix
            var selectedRegion = $('#sizeRegionList li a.state-active').attr('id');
            selectedRegion = selectedRegion.replace(/sizeAttr_/g, "");
            // To get the new sizes we need to know the type of size (Men Women or Children) passed as the value of the first element of the region list
            var sizeType = $('#cstoreAddToCartControls select.sizeRegions option:first').val();
            $('#sizeList li').each(function (index) {
                var obj = $('a', this).attr("id");
                obj = obj.replace(/sizeAttr_/g, "").replace(/plus/g, "+");
                var warehouseSize = obj;
                var sizeRegion = warehouseSize;
                try {
                    sizeRegion = sizeShoeConversion[sizeType][selectedRegion][warehouseSize].conv;
                }
                catch (ex) {
                    // Nothing we have already set to the warehousing size
                }
                $('a', this).text(sizeRegion);
                if ($('a', this).hasClass("state-active")) {
                    $("#lblsize .indicator").html(sizeRegion);
                }
            });
        }
    },
    sizeRegionPicker: {
        init: function () {
            var source = $('#cstoreAddToCartControls select.sizeRegions');
            var selected = $('#cstoreAddToCartControls select.sizeRegions option:selected');
            var selectedText = selected.text();
            var options = $("option", source);

            source.after("<div id=\"sizeRegionSelector\" class=\"sizeRegionSelector\"><a class=\"trigger\" id=\"regionSelector\" href=\"#\"><span class=\"" + selectedText + "\"><img src=\"/Images/sprites/dropArrow.png\"></span></a><ul id=\"sizeRegionList\"></ul></div>");
            var ulSizeRegions = $('#sizeRegionList');
            stringBuilder = "";
            options.each(function (i) {
                var lineclass = "cstoreAttr";
                if ($(this).val() == selected.val()) {
                    lineclass += " state-active";
                    lineclass += " active";
                }
                stringBuilder += "<li><a href=\"#\" id=\"sizeAttr_" + $(this).text() + "\" class=\"" + lineclass + " trigger\"><span class=" + $(this).text() + ">&nbsp;</span></a></li>";
            });
            ulSizeRegions.append(stringBuilder);

            // Now replace the drop down with a drawer effect
            var $regionSelector = new Object;
            $regionSelector.item = $('#sizeRegionSelector');
            $regionSelector.target = $regionSelector.item.children('select');
            $regionSelector.selected = $('option:selected', $regionSelector.target);
            $regionSelector.drawer = $('#sizeRegionList');
            window.$regionSelector = $regionSelector; //Set

            $regionSelector.item.delegate('.trigger', 'click', function () {
                if ($regionSelector.drawer.is(':hidden')) {
                    cstore.sizeRegionPicker.show();
                    if (typeof window.sizeregiontimer != 'undefined') {
                        window.clearTimeout(window.sizeregiontimer);
                    }
                    window.sizeregiontimer = window.setTimeout("cstore.sizeRegionPicker.hide()", 900);
                } else {
                    cstore.sizeRegionPicker.hide();
                    if (typeof window.sizeregiontimer != 'undefined') {
                        window.clearTimeout(window.sizeregiontimer);
                    }
                }
                return false;
            });

            $regionSelector.item.delegate('#sizeRegionList', 'mouseenter mouseleave', function (e) {
                if (e.type == 'mouseleave') {
                    window.sizeregiontimer = window.setTimeout("cstore.sizeRegionPicker.hide()", 900);
                } else {
                    if (typeof window.sizeregiontimer != 'undefined') {
                        window.clearTimeout(window.sizeregiontimer);
                    }
                }
            });

            $regionSelector.item.delegate('#sizeRegionList > li a', 'click', function () {
                var obj = $(this).attr("id");
                obj = obj.replace(/sizeAttr_/g, "");
                var newval = obj;
                if ($regionSelector.selected.val() !== newval) {
                    $("#regionSelector span:first").removeClass();
                    $("#regionSelector span:first").addClass(newval);
                    $('#sizeRegionList li .state-active').removeClass('state-active');
                    $(this).addClass('state-active');
                    // Because the first element does not have match text and value becuase of a gender qualifer (e.g. "US-Mens:)
                    // IE fails on setting it - fix here (messy I know but that's IE for ya
                    if (obj == "US") {
                        $('#cstoreAddToCartControls select.sizeRegions').attr('selectedIndex', 0);
                    }
                    else {
                        $('#cstoreAddToCartControls select.sizeRegions').val(obj);
                    }
                    $('#cstoreAddToCartControls select.sizeRegions').change(obj);
                    if (typeof window.sizeregiontimer != 'undefined') {
                        window.clearTimeout(window.sizeregiontimer);
                    }
                    // We now need to rebuild the sizes for the region ....
                    cstore.sizePicker.rebuild(true);
                    cstore.sizeRegionPicker.hide();
                }
                return false;
            });
            source.hide();
        },
        show: function () {
            window.$regionSelector.drawer.slideDown({ duration: 400, easing: 'easeOutCubic' });
        },
        hide: function () {
            window.$regionSelector.drawer.slideUp({ duration: 400, easing: 'easeOutCubic' });
        }
    },
    quantityPicker: {
        init: function () {

            //Replace Quantity Form Elements to conform with image drop
            var source = $('#cstoreAddToCartControls .last select');
            var selected = $('#cstoreAddToCartControls .last select option:selected');
            var selectedText = selected.text();
            var options = $("option", source);

            source.after("<div id=\"quantitySelectorWrap\" class=\"quantitySelector\"><a class=\"trigger\" id=\"quanitySelector\" href=\"#\"><span>" + selectedText + "&nbsp;<img src=\"/Images/sprites/dropArrow.png\"></span></a><ul id=\"quantityList\"></ul></div>");
            var ulQuantity = $('#quantityList');
            stringBuilder = "";
            options.each(function (i) {
                var lineclass = "cstoreAttr";
                if ($(this).val() == selected.val()) {
                    lineclass += " state-active";
                    lineclass += " active";
                }
                stringBuilder += "<li><a href=\"#\" id=\"quanAttr_" + $(this).text() + "\" class=\"" + lineclass + " trigger\"><span class=\"quantityItem\">" + $(this).text() + "</span></a></li>";
            });
            ulQuantity.append(stringBuilder);

            // Now replace the drop down with a drawer effect
            var $quantitySelector = new Object;
            $quantitySelector.item = $('#quantitySelectorWrap');
            $quantitySelector.target = $quantitySelector.item.children('select');
            $quantitySelector.selected = $('option:selected', $quantitySelector.target);
            $quantitySelector.drawer = $('#quantityList');
            window.$quantitySelector = $quantitySelector; //Set

            $quantitySelector.item.delegate('.trigger', 'click', function () {
                if ($quantitySelector.drawer.is(':hidden')) {
                    cstore.quantityPicker.show();
                    if (typeof window.quantitytimer != 'undefined') {
                        window.clearTimeout(window.quantitytimer);
                    }
                    window.quantitytimer = window.setTimeout("cstore.quantityPicker.hide()", 900);
                } else {
                    cstore.quantityPicker.hide();
                    if (typeof window.quantitytimer != 'undefined') {
                        window.clearTimeout(window.quantitytimer);
                    }
                }
                return false;
            });

            $quantitySelector.item.delegate('#quantityList', 'mouseenter mouseleave', function (e) {
                if (e.type == 'mouseleave') {
                    window.quantitytimer = window.setTimeout("cstore.quantityPicker.hide()", 900);
                } else {
                    if (typeof window.quantitytimer != 'undefined') {
                        window.clearTimeout(window.quantitytimer);
                    }
                }
            });

            $quantitySelector.item.delegate('#quantityList > li a', 'click', function () {
                var obj = $(this).attr("id");
                obj = obj.replace(/quanAttr_/g, "");
                var newval = obj;
                if ($regionSelector.selected.val() !== newval) {
                    $("#quantitySelectorWrap span:first").html(newval + "&nbsp;<img src=\"/Images/sprites/dropArrow.png\">");
                    $('#quantityList li .state-active').removeClass('state-active');
                    $(this).addClass('state-active');
                    source.val(obj);
                    source.change(obj);
                    if (typeof window.quantitytimer != 'undefined') {
                        window.clearTimeout(window.quantitytimer);
                    }
                    cstore.quantityPicker.hide();
                }
                return false;
            });
            source.hide();
        },
        show: function () {
            window.$quantitySelector.drawer.slideDown({ duration: 400, easing: 'easeOutCubic' });
        },
        hide: function () {
            window.$quantitySelector.drawer.slideUp({ duration: 400, easing: 'easeOutCubic' });
        }

    },
    media: {
        init: function () {
            $(document).ready(function () {

                /*
                * To who ever tries to read this section of code im sorry!
                * I Only had 1 Hour to write it so I need to come back and neat'n it a bit.
                * Byron
                */

                var btnZoom = $('#scriptZoom');
                var btnClose = $('#scriptHideZoom');
                var zoomCanvas = $('#zoomedCanvas');

                btnZoom.bind('click', function () {
                    var src = $(this).attr('href');

                    $('#defaultImages .mainImage').fadeOut(600);
                    $('#cStoreCarPrevious').fadeOut(600);
                    $('#cStoreCarNext').fadeOut(600);
                    $('#cStore_carousel').fadeOut(600, function () {
                        btnZoom.parent().animate({ 'width': 977 }, {
                            duration: 600,
                            easing: 'easeOutExpo',
                            complete: function () {
                                btnZoom.parent().addClass('loading');
                                btnClose.show();
                                zoomCanvas.show();

                                var img = new Image();

                                $(img).load(function () {
                                    btnZoom.parent().removeClass('loading');
                                    $(this).css({ 'display': 'none' });
                                    zoomCanvas.children('.viewPort').append($(this));
                                    if ($(this).height() > 588) {

                                        var appendedImg = $(this);

                                        btnZoom.parent().animate({ 'height': 690 }, {
                                            duration: 460,
                                            easing: 'easeOutExpo',
                                            complete: function () {

                                                appendedImg.fadeIn(700, function () {
                                                    var viewPort = $('#zoomedCanvas .viewPort');
                                                    $('body').mousemove(function (e) {
                                                        var ytop = ((e.pageY - 132) / 620) * (viewPort.height() - 620);
                                                        viewPort.parent().scrollTop(ytop);
                                                    });
                                                });
                                            }
                                        });

                                    } else {
                                        $(this).fadeIn(700);
                                    }

                                }).error(function () {
                                    btnZoom.parent().removeClass('loading');

                                }).attr('src', src);

                                $('html').bind('click', function (e) {
                                    var tgt = $(e.target);
                                    if (!tgt.parents('#emuImage').length) {
                                        closeOver();
                                    }
                                });

                            }
                        });

                    });

                    $(this).hide();

                    return false;
                });




                var closeOver = function () {

                    zoomCanvas.fadeOut(600, function () {
                        $(this).children('.viewPort').html('').end().show();

                        if ($(this).parent().height() > 588) {

                            btnClose.parent().animate({ 'height': 588 }, {
                                duration: 450,
                                easing: 'easeOutExpo',
                                complete: function () {
                                    btnClose.parent().animate({ 'width': 538 }, {
                                        duration: 600,
                                        easing: 'easeOutExpo',
                                        complete: function () {
                                            $('#defaultImages .mainImage').fadeIn(600);
                                            $('#cStore_carousel').fadeIn(600);
                                            $('#cStoreCarPrevious').fadeIn(600);
                                            $('#cStoreCarNext').fadeIn(600);
                                            btnZoom.show();
                                            zoomCanvas.hide();
                                        }

                                    });
                                }
                            });

                        } else {

                            btnClose.parent().animate({ 'width': 538 }, {
                                duration: 600,
                                easing: 'easeOutExpo',
                                complete: function () {
                                    $('#defaultImages .mainImage').fadeIn(600);
                                    $('#cStore_carousel').fadeIn(600);
                                    btnZoom.show();
                                    zoomCanvas.hide();
                                }

                            });
                        }

                    });

                    btnClose.hide();

                    return false;
                }

                btnClose.bind('click', function (e) {
                    closeOver();
                    e.preventDefault();
                });
            });
        },
        set: function (sender, type) {
            window.activeThumbIndex = sender;

            $('#scriptZoom').attr({ 'href': attribute_colour[window.activeColourIndex]['zooms'][sender] });

            var thumbs = $('#thumbs');
            $('> li a.state-active', thumbs).removeClass('state-active');
            $('.media' + sender + ' a', thumbs).addClass('state-active');
            $('#defaultImages .mainImage img').fadeOut(800, function () {
                var loading = $(this).parent().parent();
                loading.addClass('loading');
                var newImgURI = base + attribute_colour[window.activeColourIndex]['mains'][sender];
                cstore.media.load($('#defaultImages .mainImage'), newImgURI, loading);
                $(this).remove();
            });


        },
        parse: function (sender) {

            var thumbs = attribute_colour[sender]['thumbs'];
            var thumbsLength = thumbs.length;
            var target = $('#thumbs');
            cstore.media.set(0, 'mains');
            $(target).fadeOut(500, function () {
                $('li', this).remove();
                for (i = 0; i < thumbsLength; i++) {
                    target.append('<li class="media' + i + '"><a href="javascript:cstore.media.set(' + i + ', &#39;mains&#39;);"><img src="' + base + thumbs[i] + '" /></a></li>');
                }
                $('> li:first a', target).addClass('state-active');
                // Replaced by a fade in the media load to stop the "flicker"
                //$(this).fadeIn(500);
                cstore.media.carousel.init();
            });
        },
        load: function (target, src, loading) {
            var $image = new Image();
            target.hide();
            $($image).load(function () {
                loading.removeClass('loading');
                $(target).append(this).fadeIn(800);
                $("ul#thumbs").fadeIn(500);
            }).error(function () {
                loading.removeClass('loading');
                $("ul#thumbs").fadeIn(500);
            }).attr('src', src);
        },
        carousel: {
            init: function () {
                var $_carousel = new Object();
                $_carousel.item = $('#cStore_carousel');
                $_carousel.container = $('#cStore_carousel').children('ul');
                $_carousel.slides = $_carousel.container.children('li');
                $_carousel.slideWidth = 70;

                $_carousel.container.width($_carousel.slides.length * $_carousel.slideWidth);
                if ($_carousel.slides.length > 6) {

                    // Only add the previous and next controls if they are not there already
                    if ($('#cStoreCarNext').length == 0 && $('#cStoreCarPrevious').length == 0) {
                        var strControls = '<a class="control prev" id="cStoreCarPrevious" href="#">Previous</a><a class="control next" id="cStoreCarNext" href="#">next</a>';
                        $_carousel.item.after(strControls);
                    }

                    $('#cStoreCarNext').bind('click', function () {
                        cstore.media.carousel.animate(-1);
                        return false;
                    });

                    $('#cStoreCarPrevious').bind('click', function () {
                        cstore.media.carousel.animate(1);
                        return false;
                    });

                    $_carousel.slides.filter(':last').detach().prependTo($_carousel.container);
                    $_carousel.container.css({ marginLeft: ($_carousel.slideWidth * -1) });

                    window.$_carousel = $_carousel;
                } else {

                    if ($_carousel.slides.length == 1) {
                        // Changed as removing the inner HTML breaks products with only one image for certain colour variation
                        $('li', $_carousel.item).remove();
                        //$_carousel.item.html('');
                    }
                    $_carousel.container.css("marginLeft", "0px");
                    $('#cStoreCarNext').detach();
                    $('#cStoreCarPrevious').detach();
                }
            },
            animate: function (direction) {
                var $_carousel = window.$_carousel; //Get

                if (!$_carousel.container.is(':animated')) {
                    $_carousel.container.animate({ marginLeft: '+=' + ($_carousel.slideWidth * direction) },
                    { easing: 'easeOutCubic', duration: 600, complete: function () {
                        if (direction < 0) {
                            $('#thumbs').children('li').filter(':first').detach().appendTo($_carousel.container);
                        } else {
                            $('#thumbs').children('li').filter(':last').detach().prependTo($_carousel.container);

                        }
                        $_carousel.container.css({ marginLeft: ($_carousel.slideWidth * -1) });
                    }
                    });
                }
            }
        }
    },
    price: {
        set: function (sizeId) {
            var price = null;
            var sale = null;
            var currency = null;

            for (var i = 0; i < attribute_colour[window.activeColourIndex]['inventory'].length; i++) {
                if (attribute_colour[window.activeColourIndex]['inventory'][i]['sizeId'] == sizeId) {
                    price = attribute_colour[window.activeColourIndex]['inventory'][i]['prices']['rrp'];
                    sale = attribute_colour[window.activeColourIndex]['inventory'][i]['prices']['sale'];
                    currency = attribute_colour[window.activeColourIndex]['inventory'][i]['prices']['currency'];
                }
            }

            if (sale.length <= 1) {
                //Regular
                $('#cStorePriceDisplay .price').html('<span>' + price + '</span>');
                $('#unitPrice').html('x ' + price + ' ' + currency);
            } else {
                //On sale
                $('#cStorePriceDisplay .price').html('<span class="strike">' + price + '</span>&nbsp;<span class="now">NOW ' + sale + '<br /></span>');
                $('#unitPrice').html('x ' + sale + ' ' + currency);
            }
            //Cufon.refresh();
        }
    },
    checkout: {
        init: function () {
            $(document).ready(function () {
                var accTriggers = $('#cStoreHolder .cStoreFormHeading');
                accTriggers.addClass('cStoreScript-active');
                $(':not(.state-closed)', accTriggers).addClass('state-open');
                accTriggers.click(function () {
                    $(this).next('.cStoreForm').slideToggle(500).end().toggleClass('state-open').toggleClass('state-closed');
                });

                if ($('#scriptToggleBilling .yes input:checked').val()) {
                    $('#cStoreBillingInnerForm').hide();
                }

                $('#scriptToggleBilling .no input').live('click', function () { $('#cStoreBillingInnerForm').slideDown(500); });
                $('#scriptToggleBilling .yes input').live('click', function () { $('#cStoreBillingInnerForm').slideUp(500); });

            });
        },
        ccvHelper: {
            init: function () {
                $(document).ready(function () {
                    var popup = $('#ccvHelpPopUp');
                    var info = $('#ccvHelpPopUp .ccvHelperInfo');

                    popup.bind('mouseenter mouseleave', function (e) {
                        if (e.type == 'mouseenter') {
                            info.fadeIn(300);
                        } else {
                            info.fadeOut(300);
                        }
                    });
                });
            }
        }
    }
};



var sizeShoeConversion =
{
    "US-Women": {
        "US": {
            "3": { "conv": "3" },
            "4": { "conv": "4" },
            "5": { "conv": "5" },
            "6": { "conv": "6" },
            "7": { "conv": "7" },
            "8": { "conv": "8" },
            "9": { "conv": "9" },
            "10": { "conv": "10" },
            "11": { "conv": "11" },
            "12": { "conv": "12" },
            "13": { "conv": "13" },
            "14": { "conv": "14" },
            "15": { "conv": "15" }
        },
        "UK": {
            "3": { "conv": "1" },
            "4": { "conv": "2" },
            "5": { "conv": "3" },
            "6": { "conv": "4" },
            "7": { "conv": "5" },
            "8": { "conv": "6" },
            "9": { "conv": "7" },
            "10": { "conv": "8" },
            "11": { "conv": "9" },
            "12": { "conv": "10" },
            "13": { "conv": "11" },
            "14": { "conv": "12" },
            "15": { "conv": "13" }
        },
        "EU": {
            "3": { "conv": "33" },
            "4": { "conv": "34" },
            "5": { "conv": "35/36" },
            "6": { "conv": "37" },
            "7": { "conv": "38" },
            "8": { "conv": "39" },
            "9": { "conv": "40/41" },
            "10": { "conv": "42" },
            "11": { "conv": "43" },
            "12": { "conv": "44/45" },
            "13": { "conv": "46" },
            "14": { "conv": "47" },
            "15": { "conv": "48/49" }
        },
        "Japan": {
            "3": { "conv": "20" },
            "4": { "conv": "21" },
            "5": { "conv": "22" },
            "6": { "conv": "23" },
            "7": { "conv": "24" },
            "8": { "conv": "25" },
            "9": { "conv": "26" },
            "10": { "conv": "27" },
            "11": { "conv": "28" },
            "12": { "conv": "29" },
            "13": { "conv": "30" },
            "14": { "conv": "31" },
            "15": { "conv": "32" }
        }
    },
    "US-Women-Unisex": {
        "US": {
            "3": { "conv": "4" },
            "4": { "conv": "5" },
            "5": { "conv": "6" },
            "6": { "conv": "7" },
            "7": { "conv": "8" },
            "8": { "conv": "9" },
            "9": { "conv": "10" },
            "10": { "conv": "11" },
            "11": { "conv": "12" },
            "12": { "conv": "13" },
            "13": { "conv": "14" },
            "14": { "conv": "15" }
        },
        "UK": {
            "3": { "conv": "2" },
            "4": { "conv": "3" },
            "5": { "conv": "4" },
            "6": { "conv": "5" },
            "7": { "conv": "6" },
            "8": { "conv": "7" },
            "9": { "conv": "8" },
            "10": { "conv": "9" },
            "11": { "conv": "10" },
            "12": { "conv": "11" },
            "13": { "conv": "12" },
            "14": { "conv": "13" }
        },
        "EU": {
            "3": { "conv": "34" },
            "4": { "conv": "35/36" },
            "5": { "conv": "37" },
            "6": { "conv": "38" },
            "7": { "conv": "39" },
            "8": { "conv": "40/41" },
            "9": { "conv": "42" },
            "10": { "conv": "43" },
            "11": { "conv": "44/45" },
            "12": { "conv": "46" },
            "13": { "conv": "47" },
            "14": { "conv": "48/49" }
        },
        "Japan": {
            "3": { "conv": "21" },
            "4": { "conv": "22" },
            "5": { "conv": "23" },
            "6": { "conv": "24" },
            "7": { "conv": "25" },
            "8": { "conv": "26" },
            "9": { "conv": "27" },
            "10": { "conv": "28" },
            "11": { "conv": "29" },
            "12": { "conv": "30" },
            "13": { "conv": "31" },
            "14": { "conv": "32" }
        }
    },
    "US-Men": {
        "US": {
            "3": { "conv": "3" },
            "4": { "conv": "4" },
            "5": { "conv": "5" },
            "6": { "conv": "6" },
            "7": { "conv": "7" },
            "8": { "conv": "8" },
            "9": { "conv": "9" },
            "10": { "conv": "10" },
            "11": { "conv": "11" },
            "12": { "conv": "12" },
            "13": { "conv": "13" },
            "14": { "conv": "14" }
        },
        "UK": {
            "3": { "conv": "2" },
            "4": { "conv": "3" },
            "5": { "conv": "4" },
            "6": { "conv": "5" },
            "7": { "conv": "6" },
            "8": { "conv": "7" },
            "9": { "conv": "8" },
            "10": { "conv": "9" },
            "11": { "conv": "10" },
            "12": { "conv": "11" },
            "13": { "conv": "12" },
            "14": { "conv": "13" }
        },
        "EU": {
            "3": { "conv": "34" },
            "4": { "conv": "35/36" },
            "5": { "conv": "37" },
            "6": { "conv": "38" },
            "7": { "conv": "39" },
            "8": { "conv": "40/41" },
            "9": { "conv": "42" },
            "10": { "conv": "43" },
            "11": { "conv": "44/45" },
            "12": { "conv": "46" },
            "13": { "conv": "47" },
            "14": { "conv": "48/49" }
        },
        "Japan": {
            "3": { "conv": "21" },
            "4": { "conv": "22" },
            "5": { "conv": "23" },
            "6": { "conv": "24" },
            "7": { "conv": "25" },
            "8": { "conv": "26" },
            "9": { "conv": "27" },
            "10": { "conv": "28" },
            "11": { "conv": "29" },
            "12": { "conv": "30" },
            "13": { "conv": "31" },
            "14": { "conv": "32" }
        }
    },
    "US-Children": {
        "US": {
            "7": { "conv": "7" },
            "8": { "conv": "8" },
            "9": { "conv": "9" },
            "10": { "conv": "10" },
            "11": { "conv": "11" },
            "12": { "conv": "12" },
            "13": { "conv": "13" },
            "1": { "conv": "1" },
            "2": { "conv": "2" },
            "3": { "conv": "3" },
            "4": { "conv": "4" }
        },
        "UK": {
            "7": { "conv": "6" },
            "8": { "conv": "7" },
            "9": { "conv": "8" },
            "10": { "conv": "9" },
            "11": { "conv": "10" },
            "12": { "conv": "11" },
            "13": { "conv": "12" },
            "1": { "conv": "13" },
            "2": { "conv": "1" },
            "3": { "conv": "2" },
            "4": { "conv": "3" }
        },
        "EU": {
            "7": { "conv": "23" },
            "8": { "conv": "24" },
            "9": { "conv": "25/26" },
            "10": { "conv": "27" },
            "11": { "conv": "28" },
            "12": { "conv": "29/30" },
            "13": { "conv": "31" },
            "1": { "conv": "32" },
            "2": { "conv": "33/34" },
            "3": { "conv": "35" },
            "4": { "conv": "36" }
        },
        "Japan": {
            "7": { "conv": "12" },
            "8": { "conv": "13" },
            "9": { "conv": "14" },
            "10": { "conv": "15" },
            "11": { "conv": "16" },
            "12": { "conv": "17" },
            "13": { "conv": "18" },
            "1": { "conv": "19" },
            "2": { "conv": "20" },
            "3": { "conv": "21" },
            "4": { "conv": "22" }
        }
    }
}; 


    


