﻿
var $j = jQuery.noConflict();

$j(document).ready(function () {
    initjQuery();

    // Init Function
    function initjQuery() {

        SelectTabIfQueryStringIsPresent();

        // Remove the separator from the last footer menu link
        $j('.ftr_links li:last').addClass('ftr_last');

        // 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, .groupingBarText').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());
                }
            });
        });

        // initial state checking & highlight logic for any part of the site
        // contains filters. i.e. Training, WTB Vendors, WTB Individuals
        $j(".filterOptions").each(function () {
            var filterOptions = $j(this);
            var inputsChecked = $j("input:checked", filterOptions);
            if (inputsChecked.length == 0) {
                var firstInput = $j("input:first", filterOptions);
                var firstLI = $j("li:first", filterOptions);
                firstInput.attr("checked", true);
                firstLI.css("backgroundColor", "#E7F3C9");

            }
        });


        // 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).css("backgroundColor", "#E7F3C9");
            }
            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");
            $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 */
        $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?c=Entire%20Site&q=";
            var searchBox = $j(this);
            searchBox.find("input").keypress(function (e) {
                if (e.keyCode == 13) {
                    submitSearchValueFromTextBox(searchBox, baseURL);
                }
            });
            searchBox.find("a").click(function () {
                submitSearchValueFromTextBox(searchBox, baseURL);
            });
        });

        // Search the knowledge base
        $j(".searchKB").each(function () {
            var baseURL = "/search.aspx?c=Knowledge%20Base&q=";
            var searchBox = $j(this);

            searchBox.find("input").keypress(function (e) {
                if (e.keyCode == 13) {
                    window.location.href = baseURL + escape(searchBox.find("input").val());
                }
            });
            searchBox.find("a").click(function () {
                window.location.href = baseURL + escape(searchBox.find("input").val());
            });
        });

        submitSearchValueFromTextBox = function (searchBox, baseURL) {
            window.location.href = baseURL + escape(searchBox.find("input").val());
        }

        //Hide drop downs on body click
        $j('body').click(function () {
            $j('.dropdown_panel').each(function () { $j(this).css({ display: 'none' }); });
            $j('.dropdown_wrapper').find('.DownArrow').removeClass('DownArrow');
            $j('.dropdown_wrapper').css("z-index", "1");
        });

        // Used on support downloads
        $j('.show-toggle,.hide-toggle').click(function () {
            $j(this).parent().children().each(function () {
                $j(this).toggle();
            });
            return false;
        });

        // Used on support product pages for Multi-Lingual downloads.
        // TODO: refactor this so that it isn't so ugly that it is tied to the link's parent for its click value.
        $j(".pdf-button").parent().click(function () {
            var parent = $j(this).parents(".action");
            var selectedPdfUrl = $j(parent).find("select option:selected").attr("value");
            window.open(selectedPdfUrl, "Download", "scrollbars=no,menubar=no,height=600,width=800,resizable=yes,toolbar=no,status=no");
            return false;
        });

        function SelectTabIfQueryStringIsPresent() {
            var tabValue = SMART.Utilities.GetQueryStringValue("tab");

            if (tabValue.length > 0) {
                ResetSelectedTabs();
                SelectTab(tabValue);
            }
        }

        function ResetSelectedTabs() {
            $j("#tab-1").removeClass("selected");
        }

        function SelectTab(tabValue) {
            var tabToSelect = "#tab-" + tabValue;
            $j(tabToSelect).addClass("selected").click();
        }


        function ContactSupportSelectCountry() {
            var container = $j('#CountryContainer');
            container.children().each(function () { $j(this).hide(); });
            var CountryDropdown = $j(".ContactSupportCountrySelector option:selected");
            if (CountryDropdown) {
                var selectedCountry = CountryDropdown.val();
                if (selectedCountry) {
                    selectedCountry = selectedCountry.replace(" ", "");
                    var selected = $j("#" + selectedCountry);
                    if (selected) {
                        selected.show();
                    }
                }
            }
        }

        $j(".ContactSupportCountrySelector").change(function () { ContactSupportSelectCountry(); });
        $j(".ContactSupportCountrySelector").keyup(function () { ContactSupportSelectCountry(); });
        ContactSupportSelectCountry();

        // Fix the lengths on the footer if they need to go back where they were.
        var footerLength = 0;
        $j('ul.ftr_links').each(function () { footerLength += $j(this).text().length; });
        $j('ul.ftr_sector').each(function () { footerLength += $j(this).text().length; });
        if (footerLength < 100) {
            $j('ul.ftr_sector li').width('110px');
        }
    };
});

// New methods

String.prototype.namespace = function(separator) {
    var namespaces = this.split(separator || '.')
    var root = window;
    for (var i = 0, len = namespaces.length; i < len; i++) {
        root = root[namespaces[i]] = root[namespaces[i]] || {};
    }
    return root;
};


//Redirect for send to a friend 'go' button.. 
function SendToAFriendRedirect(url) {
    window.location.href = url + window.location.href;
}



