$(function() {
    // Show / Hide bag
    $("#TopBarBagLink").live('click', function() {
        targetBag = $(this).attr("href");
        if ($(this).parent().hasClass("open")) {
            $(targetBag).slideUp("slow", function() {
                $(this).parent().removeClass("open");
            });
        }
        else {
            $(targetBag).slideDown("slow");
            $(this).parent().addClass("open");
        }
        return false;
    });

    // Custom Select and checkboxes
    if (!($.browser.msie && $.browser.version == "6.0")) {
        $("form input").filter(":checkbox,:radio").checkbox();
        $("select").sb({ fixedWidth: true });
        var prm = Sys.WebForms.PageRequestManager.getInstance();
        prm.add_endRequest(function() {
            $("select").sb({ fixedWidth: true });
            $("form input").filter(":checkbox,:radio").checkbox();
        });
    }

    // Disabled items should be grey
    $("li.disabled .item .text").css("color", "gray");

    // Project Detail - Lightbox
    $("#product-detail a#product-shot").colorbox();

    // Product Detail - Zoom
    $("#product-detail a#zoom").click(function() {
        var text = $(this).html();
        var target = $(this).attr("href");
        if (text == "Disable Zoom") {
            $(target).next().remove();
            $(target).parent("#wrap").css("z-index", "auto");
            $(this).html("Zoom");
        }
        else {
            $(".cloud-zoom").CloudZoom({ zoomWidth: '594', zoomHeight: '391', adjustX: 21 });
            $(this).html("Disable Zoom");
        }
        return false;
    });

    // Product Detail - Thumbnail slider
    var thumbnailsTotalWidth = 0;
    $("#thumbnails ul li").each(function() {
        thumbnailsTotalWidth += $(this).outerWidth(true);
    });
    $("#thumbnails ul").css("width", thumbnailsTotalWidth);
    $("#thumbnails-slider").slider({
        animate: true,
        change: handleSliderChange,
        slide: handleSliderSlide
    });
    $("#thumbnails-slider").find('.ui-slider-handle').wrap('<div class="slider-scrollbar-helper"></div>').parent();
    if (thumbnailsTotalWidth > $("#thumbnails").outerWidth(false)) {
        $("#thumbnails-slider").css("visibility", "visible");
    }

    // Product Detail - Complete The Look slider
    var completeLookTotalHeight = 0;
    $("#complete-the-look ul > .item").each(function() {
        completeLookTotalHeight += $(this).outerHeight(true);
    });
    $("#complete-the-look-slider").slider({
        animate: true,
        orientation: 'vertical',
        value: 100,
        change: handleCTLSliderChange,
        slide: handleCTLSliderSlide
    });
    $("#complete-the-look-slider").find('.ui-slider-handle').wrap('<div class="slider-scrollbar-helper"></div>').parent();
    if (completeLookTotalHeight > $("#complete-the-look").outerHeight(false)) {
        $("#complete-the-look-slider").css("display", "block");
    }

    // Product Detail - Thumbnail switcher
    $("#thumbnails a").click(function() {
        // Read URLs of main and zoomed image from hyperlinks, assign them
        var imageURL = $(this).attr("href");
        var originalImageURL = $(this).attr("orighref");
        $("#product-shot img").attr("src", imageURL);
        $("#product-shot").attr("href", originalImageURL);
        // Sort out zoom options
        var zoomTarget = $("#product-detail a#zoom").attr("href");
        var zoomText = $("#product-detail a#zoom").html();
        // if Zoom is enabled - disable it
        if (zoomText == "Disable Zoom") {
            $(zoomTarget).next().remove();
            $(zoomTarget).parent("#wrap").css("z-index", "auto");
            $("#product-detail a#zoom").html("Zoom");
        }
        return false;
    });

    // Promotions Scrollable
    var promotionWidth = $("#promotions .inner ul li").width();
    var promotionWrapperWidth = $("#promotions .inner").width();
    var visibleItems = Math.floor(promotionWrapperWidth / promotionWidth) + 1;
    $("#promotions .inner").jCarouselLite({
        btnNext: "#promotions a.next",
        btnPrev: "#promotions a.prev",
        //circular: false,
        circular: true,
        visible: visibleItems
    });

    // Disabling text selection on elements with .no-select
    $.extend($.fn.disableTextSelect = function() {
        return this.each(function() {
            if ($.browser.mozilla) {//Firefox
                $(this).css('MozUserSelect', 'none');
            } else if ($.browser.msie) {//IE
                $(this).bind('selectstart', function() { return false; });
            } else {//Opera, etc.
                $(this).mousedown(function() { return false; });
            }
        });
    });
    $('.no-select').disableTextSelect(); //No text selection on elements with a class of 'noSelect'

    if ($("#collections").size() > 0) {
        $("body").addClass("carouselbg");
    }

    // Collections Scrollable
    //$("#collections .inner").jCarouselLite({
    //    btnNext: "#collections a.next",
    //    btnPrev: "#collections a.prev",
    //    circular: true,
    //    visible: 3,
    //		speed: 180
    //});

    if ($.browser.msie && parseInt($.browser.version, 10) == "7") {
        var itemLi = 1;
        $("#collections .inner ul li").each(function() {
            var itemLiIndex = 2000 - itemLi;
            $(this).css("z-index", itemLiIndex.toString());
            itemLi++;
        });
    }


    $("#collections .inner").ultimateCarousel({
        btnPrev: "#collections a.prev",
        btnNext: "#collections a.next",
        visible: 2,
        infinite: true
    });

    var bodyWidth = $("#footer").width();
    $("#collections").css("width", bodyWidth);

    $(window).resize(function() {
        var bodyWidth = $("#footer").width();
        $("#collections").css("width", bodyWidth);
    });
});

function handleCTLSliderChange(e, ui) {
    var maxScroll = $("#complete-the-look").attr("scrollHeight") - $("#complete-the-look").height();
    $("#complete-the-look").animate({ scrollTop: (100 - ui.value) * (maxScroll / 100) }, 1000);
}

function handleCTLSliderSlide(e, ui) {
    var maxScroll = $("#complete-the-look").attr("scrollHeight") - $("#complete-the-look").height();
    $("#complete-the-look").attr({ scrollTop: (100 - ui.value) * (maxScroll / 100) });
}

function handleSliderChange(e, ui) {
    var maxScroll = $("#thumbnails").attr("scrollWidth") - $("#thumbnails").width();
    $("#thumbnails").animate({ scrollLeft: ui.value * (maxScroll / 100) }, 1000);
}

function handleSliderSlide(e, ui) {
    var maxScroll = $("#thumbnails").attr("scrollWidth") - $("#thumbnails").width();
    $("#thumbnails").attr({ scrollLeft: ui.value * (maxScroll / 100) });
}
