$(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;
    });

    // Disabling text selection on elements with .no-select
    $(function() {
        $.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'
    });

    // 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
    var oColorBox = $("#product-detail a#product-shot");
    if (oColorBox.length > 0) {
        oColorBox.colorbox();
    }

    // Product Detail - Thumbnail slider
    var thumbnailsTotalHeight = 0;
    $("#thumbnails ul li").each(function() {
        thumbnailsTotalHeight += $(this).outerHeight(true);
    });
    $("#thumbnails-slider").slider({
        animate: true,
        orientation: 'vertical',
        value: 100,
        change: handleSliderChange,
        slide: handleSliderSlide
    });
    $("#thumbnails-slider").find('.ui-slider-handle').wrap('<div class="slider-scrollbar-helper"></div>').parent();
    if (thumbnailsTotalHeight > $("#thumbnails").outerHeight(false)) {
        $("#thumbnails-slider").css("visibility", "visible");
    }

    // Product Detail - Complete The Look slider
    var completeLookTotalWidth = 0;
    $(".product-footer #complete-the-look-container ul > .item").each(function() {
        completeLookTotalWidth += $(this).outerWidth(true);
    });
    $(".product-footer #complete-the-look-container ul.row").css("width", completeLookTotalWidth + "px");
    $(".product-footer #complete-the-look-slider").slider({
        animate: true,
        change: handleCTLSliderChange,
        slide: handleCTLSliderSlide
    });
    $(".product-footer #complete-the-look-slider").find('.ui-slider-handle').wrap('<div class="slider-scrollbar-helper"></div>').parent();
    if (completeLookTotalWidth > $(".product-footer #complete-the-look-container").outerWidth(false)) {
        $(".product-footer #complete-the-look-slider").css("display", "block");
    }

    // Basket - Complete The Look slider
    var completeLookTotalHeight = 0;
    $("#shopping-bag #complete-the-look ul li").each(function() {
        completeLookTotalHeight += $(this).outerHeight(true);
    });
    $("#complete-the-look-basket-slider").slider({
        animate: true,
        orientation: 'vertical',
        value: 100,
        change: handleCTLBagSliderChange,
        slide: handleCTLBagSliderSlide
    });
    $("#complete-the-look-basket-slider").find('.ui-slider-handle').wrap('<div class="slider-scrollbar-helper"></div>').parent();
    if (completeLookTotalHeight > $("#shopping-bag #complete-the-look").outerHeight(false)) {
        $("#complete-the-look-basket-slider").css("display", "block");
    }

    // Product Detail - Thumbnail switcher
    $("#thumbnails a").click(function() {
        // Read URL of main and large image from hyperlink, assign them
        var imageURL = $(this).attr("href");
        var originalImageURL = $(this).attr("orighref");
        $("#main-image img").attr("src", imageURL);
        $("#product-shot").attr("href", originalImageURL);
        return false;
    });

    // 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'

    var bodyWidth = $("#footer").width();
    $("#collections").css("width", bodyWidth);

    $(window).resize(function() {
        var bodyWidth = $("#footer").width();
        $("#collections").css("width", bodyWidth);
    });

    solutionsHeight();
    homeHeight();
    $(window).resize(function() {
        solutionsHeight();
        homeHeight();
    });

    // Solutions Scrollable
    $("#solutions").ultimateCarousel({
        btnPrev: "#solutions a.prev",
        btnNext: "#solutions a.next",
        visible: 2,
        infinite: false,
        zoomFeature: true,
        speed: "400",
        itemResize: false
    });

    $("#solutions ul li a.image").each(function() {
        var imagePath = $(this).children("img").attr("src").match(/[^\.]+/);
        $(this).prepend("<img class='blurred' src='" + imagePath + "-blurred.jpg' />");
    });

    // ** Style dropdown boxes **
    
    // Dropdown boxes that do not auto postback
    /*$('select:not(.auto-postback)').styledSelect();
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_endRequest(function() {
        $('select:not(.auto-postback)').styledSelect();
    });*/

    // Dropdown boxes that auto postback
    /*$('select.auto-postback').styledSelect({autoPostback: true});
    var prm = Sys.WebForms.PageRequestManager.getInstance();
    prm.add_endRequest(function() {
        $('select.auto-postback').styledSelect({ autoPostback: true });
    });*/
    
});

function handleCTLSliderChange(e, ui) {
    var maxScroll = $("#complete-the-look-container").attr("scrollWidth") - $("#complete-the-look-container").width();
    $("#complete-the-look-container").animate({ scrollLeft: ui.value * (maxScroll / 100) }, 1000);
}

function handleCTLSliderSlide(e, ui) {
    var maxScroll = $("#complete-the-look-container").attr("scrollWidth") - $("#complete-the-look-container").width();
    $("#complete-the-look-container").attr({ scrollLeft: ui.value * (maxScroll / 100) });
}

function handleCTLBagSliderChange(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 handleCTLBagSliderSlide(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("scrollHeight") - $("#thumbnails").height();
    $("#thumbnails").animate({ scrollTop: (100 - ui.value) * (maxScroll / 100) }, 1000);
}

function handleSliderSlide(e, ui) {
    var maxScroll = $("#thumbnails").attr("scrollHeight") - $("#thumbnails").height();
    $("#thumbnails").attr({ scrollTop: (100 - ui.value) * (maxScroll / 100) });
}


function solutionsHeight() {
	var wrapperHeight = $("body").height();
	var headerHeight = $("#header").height();
	var primaryNavHeight = $("#primary-nav").height();
	var secondaryNavHeight = $("#secondary-nav").height();
	var calcContentHeight = wrapperHeight - (headerHeight + primaryNavHeight + secondaryNavHeight) - 32;
	
	if(calcContentHeight > 730) {
		$("#solutions, #solutions ul, #solutions ul li").css({ "height": calcContentHeight + "px" });
	}
}



function homeHeight() {
    if ($('body').hasClass("home")) {
        var wrapperHeight = $("body").height();
        var headerHeight = $("#header").height();
        var primaryNavHeight = $("#primary-nav").height();
        var contentPadding = $('#content').css('padding-bottom').replace("px", "");
        var calcContentHeight = wrapperHeight - (headerHeight + primaryNavHeight) - contentPadding;

        if (calcContentHeight > 671) {
            $("body.home #content").css({ "height": calcContentHeight + "px" });
        }
    }
}
