﻿var $j = jQuery.noConflict();

$j(document).ready(function() {
    initjQuery();

    // Init Function
    function initjQuery() {

        // Remove the separator from the last footer menu link
        $j('.ftr_links li:last').addClass('ftr_last');

        $j(".clickableDiv").homePageCarouselLinks();
        // Apply clickable link to the classes rc_blueBtn and rc_whiteBtn, where the rc_whiteBtn is not one of the more/less toggles
        ApplyClickableLinkToClass($j(".rc_blueBtn, .rc_whiteBtn:not(.More)"));
        $j(".scriptShowOnLoad").removeClass("scriptShowOnLoad");

        $j('.rc_bottom').each(function() {
            var boxDiv = $j(this).parent();
            if (boxDiv.outerWidth() > 0 && $j(this).css('width') != 'auto') {
                $j(this).css('width', $j('.content', boxDiv).outerWidth());
            }
        });

        $j('.show-first-bar-only .rc_collapseBar:not(:first)').each(function() {
            ToggleContent($j(this), '');
        });

        $j('.close-grouping-when-first-shown .rc_collapseBar').each(function() {
            ToggleContent($j(this), '');
        });

        $j('.collapseAll .rc_collapseBar').each(function() {
            ToggleContent($j(this), '');
        });

        $j('#filters-tab').css({ cursor: 'pointer' });
        $j('#filters-tab').click(function() {
            //collapse-off
            var downArrows = $j('.downarrow_inline', $j('#filters-tab'));
            var rightArrows = $j('.rightarrow_inline', $j('#filters-tab'));
            downArrows.removeClass('downarrow_inline').addClass('rightarrow_inline');
            rightArrows.removeClass('rightarrow_inline').addClass('downarrow_inline');
            $j('#filters-tab-content').toggle();

            $j('.rc_bottom', $j('#filters-tab-content')).each(function() {
                var boxDiv = $j(this).parent();
                if (boxDiv.outerWidth() > 0) {
                    $j(this).css('width', $j('.content', boxDiv).outerWidth());
                }
            });
        });


        // Training - Clicking and unclicking logic
        // Go through each of the columns and apply a click event handler to each of the checkboxes
        $j(".filterOptions").each(function() {

            var currentFilter = $j(this);
            // Attach an event handler to the the first checkbox in each of the columns, i.e. the "All XXX"
            var firstCheckBox = $j("input:first", this);
            firstCheckBox.click(function() {
                if ($j(this).attr("checked") === true) {
                    HighlightCheckbox($j(this), true);

                    $j("input:not(:first)", currentFilter).each(function() {
                        HighlightCheckbox($j(this), false);
                    });

                    TurnOtherCheckboxesOff($j(this));
                }
                else {
                    HighlightCheckbox($j(this), false);
                    TurnOnTheOtherCheckboxsIfAllIsUnselected($j(this));
                }
            });
            $j("input", currentFilter).each(function() {
                if ($j(this).attr("checked") === true) {
                    HighlightCheckbox($j(this), true);
                }
            });

            // Attach an event handler to all the other items in the column
            $j("input:not(:first)", currentFilter).click(function() {
                if ($j(this).attr("checked") === true) {
                    HighlightCheckbox($j(this), true);

                    firstCheckBox.each(function() {
                        HighlightCheckbox($j(this), false);
                    });

                    TurnOffTheAllCheckbox($j(this));
                }
                else {
                    TurnOnTheAllCheckboxIfNoOtherCheckboxesAreChecked($j(this));
                    HighlightCheckbox($j(this), false);
                }
            });
        });

        function HighlightCheckbox(currentCheckbox, isSelected) {
            var parentListElement = $j(currentCheckbox).parent().parent();

            if (isSelected === true) {
                $j(parentListElement).animate({ backgroundColor: '#E7F3C9' }, 300);
            }
            else {
                $j(parentListElement).css("backgroundColor", "transparent");
            }
        }

        function TurnOffTheAllCheckbox(currentCheckbox) {
            var parent = $j(currentCheckbox).parents(".filterOptions");
            $j(parent).find("input:first").removeAttr("checked");
        }

        function TurnOtherCheckboxesOff(currentCheckbox) {
            var parent = $j(currentCheckbox).parents(".filterOptions");
            $j(parent).find("input:not(:first)").removeAttr("checked");
        }

        function TurnOnTheAllCheckboxIfNoOtherCheckboxesAreChecked(currentCheckbox) {
            var parent = $j(currentCheckbox).parents(".filterOptions");
            var siblingCheckboxes = $j(parent).find("input:not(:first):checked");

            if (siblingCheckboxes.size() === 0) {
                $j(parent).find("input:first").attr("checked", true);

                HighlightCheckbox($j(parent).find("input:first"), true);
            }
        }

        function TurnOnTheOtherCheckboxsIfAllIsUnselected(currentCheckbox) {
            var parent = $j(currentCheckbox).parents(".filterOptions");
            var siblingCheckboxes = $j("input:not(:first)", parent).each(function() {
                $j(this).attr("checked", true);
                HighlightCheckbox($j(this), true);
            }); ;
        }

        /* When javascript is enabled, show the show all view, otherwise it will be hidden */
        var children = $j(".show-hide-more").each(function() {
            var child = $j(this).children().get(1);
            $j(child).removeAttr("class");
        });

        /* The javascript to toggle Show all rows */
        $j(".show-hide-more").click(function(event) {
            var hiddenText = $j(this).find(".hide");
            var shownText = $j(this).find("span:not(.hide)");

            $j(hiddenText).removeAttr("class");
            $j(shownText).attr("class", "hide");

            $j(this).siblings('.extra').toggle();

            event.preventDefault();
        });
        
        $j('.RightDisabled a, .LeftDisabled a').click(function(){return false;});
        
        
        // General search
        $j(".search").each(function() {
            var baseURL = "/search.aspx?q=";
            var searchBox = $j(this);
            searchBox.find("a").click(function(){
                submitSearchValueFromTextBox(searchBox, baseURL);
            });
        });
        
        submitSearchValueFromTextBox = function(searchBox, baseURL) {
            var textbox = searchBox.find("input").val();
            window.location = baseURL + textbox;
        }

        $j('body').click(function() {
            $j('#dd_gallery-menu').css({ display: 'none' });
        });
        
        // Used on support downloads
        $j('.show-toggle,.hide-toggle').click(function(){
            $j(this).parent().children().each(function(){
                $j(this).toggle();
            });
            return false;
        });
    };
});
