jQuery(document).ready(function () {

    $ = jQuery;

    // Economy View More / View Less Toggle
    $('.view-more-link').click(function () {
        if ($(this).prev().hasClass("view-less-text")) {
            $(this).addClass("view-more-link");
            $(this).removeClass("view-less-link");
            $(this).html("More");
            $(this).prev().addClass("hide-more-text");
        } else {
            $(this).addClass("view-less-link");
            $(this).removeClass("view-more-link");
            $(this).html("Less");
            $(this).prev().removeClass("hide-more-text");
            $(this).prev().addClass("view-less-text");
        }
    });

    $('.view-more-link').each(function () {
        $(this).removeClass("view-more-link-off");
    });

    $(".snippet-text-long").each(function () {
        $(this).addClass("hide-more-text");
    });

    // Search Focus Behavior
    jQuery('#query').focus(
		function () {
		    if (jQuery(this).val() == "Search")
		        jQuery(this).val("");
		}
	).blur(
		function () {
		    if (jQuery(this).val() == "")
		        jQuery(this).val("Search");
		}
	);

    // Subnav Dropdown Behavior
    $('.nav-item').hover(function () {
        $(this).addClass('nav-item-hover');
    },
		function () {
		    $(this).removeClass('nav-item-hover');
		});

    $('.nav-item-first').hover(function () {
        $(this).addClass('nav-item-first-hover');
    },
		function () {
		    $(this).removeClass('nav-item-first-hover');
		});

    $('.nav-item-last').hover(function () {
        $(this).addClass('nav-item-last-hover');
    },
		function () {
		    $(this).removeClass('nav-item-last-hover');
		});

    // Table hover info
    $(".header-text").hover(function () {
        $(this).siblings(".header-info").show();
    },
	    function () {
	        $(this).siblings(".header-info").hide();
	    });
    $(".header-icon").hover(function () {
        $(this).siblings(".header-info").show();
    },
	function () {
	    $(this).siblings(".header-info").hide();
	});

    /* Add span to Managed Content li 
    $(".managed-content ul li").each(function(){
    $(this).html("<span>" + $(this).html() + "</span>");
    });

    $(".managed-content ul ul li").each(function () {
    $(this).html("<span>" + $(this).html() + "</span>");
    });

    $(".managed-content ul ol li").each(function () {
    $(this).html("<span>" + $(this).html() + "</span>");
    });
    End of Adding span to Managed Content li */

    // Sidenav Search Box On Press Enter
    $('.site-search-box').keypress(function (e) {
        if (e.which == 13) {
            $('.submit-search-btn').click();
            return false;
        }
    });
    // End of Sidenav Search Box On Press Enter

    // Search browse toggle
    $("#product-category").click(function () {
        if (!($(this).hasClass("search-filter-header-item-active"))) {
            $(this).addClass("search-filter-header-item-active");
            $(this).closest(".search-filter-header").siblings(".economy-category").removeClass("category-active");
            $(this).siblings("#economy-category").removeClass("search-filter-header-item-active");
            $(this).closest(".search-filter-header").siblings(".product-category").addClass("category-active");
            $('input[id*="hfSelectedWindow"]').val("product-category");
        }
    });


    $("#economy-category").click(function () {
        if (!($(this).hasClass("search-filter-header-item-active"))) {
            $(this).addClass("search-filter-header-item-active");
            $(this).closest(".search-filter-header").siblings(".product-category").removeClass("category-active");
            $(this).siblings("#product-category").removeClass("search-filter-header-item-active");
            $(this).closest(".search-filter-header").siblings(".economy-category").addClass("category-active");
            $('input[id*="hfSelectedWindow"]').val("economy-category");
        }
    });


    var contentNavList = $(".content-nav-list-hide")
    contentNavList.css("display", "none");
    contentNavList.closest(".nav-box-sub-content").find(".nav-box-list-title").addClass("filter-list-hidden");

    // hide filter js warning
    $(".filter-info-warning").css("display", "none");

    // Select all Sidebar Filter Items 
    $(".nav-select-all").mousedown(function () {
        $(this).closest(".content-nav-list").find(".styled").attr("checked", true);
    });

    $(".nav-deselect-all").mousedown(function () {
        $(this).closest(".content-nav-list").find(".styled").attr("checked", false);
    });

    // Toggle Filter Sections
    //$("div.content-nav-header .nav-box-list-title").addClass("filter-list-hidden");
    //$("div.nav-box-sub-content .content-nav-list").hide();
    $("div.content-nav-header .nav-box-list-title").click(function () {
        $(this).parent().siblings(".content-nav-list").toggle(200);

        if ($(this).hasClass("filter-list-hidden"))
            $(this).removeClass("filter-list-hidden");
        else
            $(this).addClass("filter-list-hidden");
    });

    $("p.nav-select-all").click(function () {
        $(this).parent().siblings().find(".filter-item-list input").attr("checked");
    });

    // Add Plus Signs to Filter items with Sub Filter Items
    $('ul.filter-item-list label').each(function () {
        // get the id of the item
        var id = $(this).attr('name');

        // get the sub list of filters
        var subFilterList = $("." + id);

        // if the list belongs to this id
        if (subFilterList.hasClass(id)) {
            // if the list is on
            // expanded
            if (subFilterList.hasClass("sub-filter-list-on"))
                $(this).append("<span> - </span>");
            // otherwise, collapsed
            else
                $(this).append("<span> + </span>");

            // display the cursor as a pointer to make it look like a link
            $("ul.filter-item-list label[name=" + id + "]").css("cursor", "pointer");
        }
    });

    // Hide and Show Sub Filter Items
    $('.filter-item-list label').click(function () {
        var labelClass = $(this).attr('name');

        // if the associated ul is visible hide
        if ($(this).closest(".filter-item-list").find("ul." + labelClass + "").hasClass("sub-filter-list-on")) {
            $(this).closest(".filter-item-list").find("ul." + labelClass + "").removeClass("sub-filter-list-on");
            $(this).closest(".filter-item-list").find("ul." + labelClass + "").addClass("sub-filter-list-off");
            $(this).removeClass("clickedLabel-" + labelClass);
            $(this).find("span").replaceWith("<span> + </span>"); //Add plus sign
        }
        // otherwise show it
        else {
            $(this).addClass("clickedLabel-" + labelClass);
            $(this).closest(".filter-item-list").find("ul." + labelClass + "").addClass("sub-filter-list-on");
            $(this).closest(".filter-item-list").find("ul." + labelClass + "").removeClass("sub-filter-list-off");
            $(this).find("span").replaceWith("<span> - </span>"); // Add minus sign
        }
    });

    $('.sub-filter-list').each(function () {
        if (!$(this).hasClass("sub-filter-list-on")) {
            $(this).addClass("sub-filter-list-off");
        }
    });

});

var count = 1;
// Styled Dropdown Behavior
function createDropDown(select, dropdown) {
	var source = $(select);
	var destination = $(dropdown);
	var selected = source.find("option:selected");  // get selected <option>
	var options = $("option", source);  // get all <option> elements
	// create <dl> and <dt> with selected value inside it
	$(destination).append('<dl class="select-dropdown"></dl>')
	$(destination).find(".select-dropdown").append('<dt class="option-selected"><a href="javascript:void(0);" class="option-selected-link option-selected-link-' + count + '">' + selected.text() + 
			'<span class="option-value">' + selected.val() + 
			'</span>&nbsp;</a></dt>')
	$(destination).append('<dd class="select-options"><ul class="select-options-list"></ul></dd>')
	// iterate through all the <option> elements and create UL
	options.each(function(){
			$(destination).find(".select-options-list").append('<li class="option"><a href="javascript:void(0);" class="option-link option-link-' + count +'">' + 
					$(this).text() + '<span class="option-value">' + 
					$(this).val() + '</span></a></li>');
	});
	$(".option-link-" + count + "").click(function() {
	    var text = $(this).html();
    $(destination).find(".option-selected-link").html(text);
    $(".select-options").hide();
    source.val($(this).find(".option-value").html())
	});
	$(".option-selected-link-" + count + "").click(function() {
		$(destination).find(".select-options").toggle();
	});
	$(document).bind('click', function(e) {
		var $clicked = $(e.target);
		if (!($clicked.parents().hasClass("select-box-dropdown") || $clicked.parents().hasClass("product-box-dropdown") || $clicked.parents().hasClass("pagination-box-filter") || $clicked.parents().hasClass("pagination-bottom-box-filter")))
		    $(".select-options").hide();
    });

    count++;
}

