/* Open external links in a new browser window */
$(function () {
    var externalLinks = $('a[rel~="external"]')

    if (externalLinks == null) {
        return;
    }

    externalLinks.each(
        function (index) {
            this.target = '_blank';
            this.title = 'External website';
        }
    );
});

/* Search Box default text */
$(function () {
    var searchBox = $(".searchtextbox");

    if (searchBox == null) {
        return;
    }

    var searchBoxDefaultText = searchBox.siblings('label#defaulttext').text();
    if (searchBox.attr("value") == "") {
        searchBox.val(searchBoxDefaultText);
    }

    searchBox.focus(
        function () {
            var searchBoxDefaultText = searchBox.siblings('label#defaulttext').text();
            if ($(this).val() == searchBoxDefaultText) {
                $(this).val("");
            }
        }
    );

    searchBox.blur(
        function () {
            var searchBoxDefaultText = searchBox.siblings('label#defaulttext').text();
            if ($(this).val() == "") {
                $(this).val(searchBoxDefaultText);
            }
        }
    );
});

function sfHover(elementId) {
    var element = document.getElementById(elementId)
    if (element != null) {
        var sfEls = element.getElementsByTagName("li");
        for (var i = 0; i < sfEls.length; i++) {
            sfEls[i].onmouseover = function () {
                this.className += " sfhover";
            }
            sfEls[i].onmouseout = function () {
                this.className = this.className.replace(new RegExp(" sfhover\\b"), "");
            }
        }
    }
}

/* Photo Slider */
$(function () {
    // Handle multiple web parts on the page
    var imageRotators = $("div.imagerotator-container");

    if (imageRotators == null) {
        return;
    }

    $("div.imagerotator-container").each(
        function (index) {
            // Remove scroll SharePoint add magically
            $(this).closest('div.ms-WPBody').css('overflow', 'hidden');

            // Handle 0 height
            if ($(this).height() == 0) {
                var p = $(this).parent();

                while (p.height() == 0) {
                    p = p.parent();

                    if (p == undefined) {
                        break;
                    }
                }

                $(this).height(p.height());
            }

            // Container size for this web part
            var containerWidth = $(this).width();
            var containerHeight = $(this).height();

            // Set container width to all items
            $("div.imagerotator-item", this).each(
                function (index) {
                    $(this).width(containerWidth);
                    $(this).height(containerHeight);

                    // Show background of the label bar if the image contains a title
                    var labelTitle = $("div.imagerotator-itemlabel > span.center", this);
                    if (labelTitle.length > 0 && jQuery.trim($(labelTitle[0]).text()).length > 0) {
                        $("div.imagerotator-itemlabel", this).removeClass('hidden-onload');
                    }
                }
            );

            // Load settings
            var interval = $('input[name="interval"]', this).val() * 1000;
            var isVertical = $(this).hasClass('vertical');

            // Only scroll when there are multiple items
            var countOfPhotos = $("div.imagerotator-item", this).length
            if (countOfPhotos > 1) {
                $("div.imagerotator", this).scrollable({
                    circular: true,
                    mousewheel: false,
                    keyboard: false,
                    vertical: isVertical
                }).navigator().autoscroll({
                    interval: interval,
                    autostart: true
                });

                // Show navigation images
                $("div.back", this).removeClass('hidden-onload');
                $("div.forward", this).removeClass('hidden-onload');
            }

            // Resize canvas width when initial size is exceeded
            var itemsContainer = $("div.imagerotator-items", this);
            var initialWidth = itemsContainer.width();
            var initialHeight = itemsContainer.height();

            if (!isVertical && (countOfPhotos * containerWidth) > initialWidth) {
                itemsContainer.width((countOfPhotos + 1) * containerWidth);
            }

            if (isVertical && (countOfPhotos * containerHeight) > initialHeight) {
                itemsContainer.height((countOfPhotos + 1) * containerHeight);
            }
        }
    );
})

/* Text overflow */
$(function () {
    $(".text-overflow").textOverflow();
})

/* Show allowed item in program finder refinement */
$(function () {
    var filters = $("ul.nyw-ref-filters");

    filters.each(
        function (index) {
            var count = $(this).next()[0].children[0].value;
            $(this).hideextra({ maxItems: count });
        }
    );
});

/* Prevent ClientSide error because we don't include the OOB methods */
    function OnChildItem() { }
    function ContextInfo() { }
    function GetUrlKeyValue() { }
    function EnsureSelectionHandler() { }
    function OnChildColumn() { }
    function OnFocusFilter() { }
    function ToggleAllItems() { }
    function OnClickFilter() { }
    function PopMenuFromChevron() { }
    function IMNRegisterHeader() { }
    function OnItem() { }
    function OnLink() { }
    function VerifyHref() { }
    function DispEx() { }
    function IMNImageOnClick() { }
    function HideListViewRows() { }
    function commonShowModalDialog() { }
    function STSNavigate() { }
    var g_ViewIdToViewCounterMap = new Array();
    var g_ctxDict = new Array();
    function EnsureSelectionHandlerOnFocus() { }
