﻿String.prototype.format = function () {
    var pattern = /\{\d+\}/g;
    var args = arguments;
    return this.replace(pattern, function (capture) {
        return args[capture.match(/\d+/)];
    });
}

$(function () {

    /**** Search Page ****/

    //handle clicks on any of the page links
    //load search results and display them.
    $('#search ul.pages').click(function (e) {
        fn = function (e) {
            e.preventDefault();
            $('#search #Page').val(parseInt($(this).html()));
            LoadSearchResults(DisplayResult);
        };

        if ($(e.target).is('a'))
            fn.call(e.target, e);
    });

    //handle form submission via ajax
    $('#search form').submit(function (e) {
        e.preventDefault();
        LoadSearchResults(DisplayResult);
    });

    //reset the current page to 1 and submit the form when items per page is changed
    $('#search #Count').change(function () {
        $('#search #Page').val(1);
        $('#search form').submit();
    });

    //reset the page when the query is changed
    $('#search #Query').change(function () {
        $('input#Page').val(1);
    });

    //Load new results only if the query is not empty
    function LoadSearchResults(success) {
        if ($('#search #Query').val() != '') {
            var data = $('#search form').serialize();
            $.post('/search/json', data, success);
        }
    }

    //update the page elements based on the results of the query.
    function DisplayResult(d) {

        //display the list of results
        var listHtml = '';
        for (var i in d.Results) {
            var result = d.Results[i];
            listHtml += "<li><h3><a href='{0}'>{1}</a></h3><div>{2}</div></li>".format(result.Url, result.Title, result.Summary);
        }
        $('#search ul.results').html(listHtml);

        //update the search info
        if (d.HasResults) {
            $('#search .results-found').show();
            $('#search .results-not-found').hide();
            $('#search .results-found span.start').html(d.RangeStart);
            $('#search .results-found span.end').html(d.RangeEnd);
            $('#search .results-found span.total').html(d.TotalResults);
        }
        else {
            $('#search .results-found').hide();
            $('#search .results-not-found').show();
            $('#search .results-not-found span.query').html(d.Query);
        }

        //create paging nav
        $('#search ul.pages li').remove();
        if (d.NumPages > 1) {
            var currFormat = "<li>{0}</li>";
            var otherFormat = "<li><a href=''>{0}</a></li>";
            for (var i = 1; i <= d.NumPages; i++) {
                var format = i == d.Page ? currFormat : otherFormat;
                $('#search ul.pages').append(format.format(i)).show('slow');
            }
        }
    }

    /**** Collapsible Plugin Call ****/
    $(".Collapsible").collapsible({
        headSelector: ".Collapsible-Header",
        bodySelector: ".Collapsible-Body",
        duration: 0,
        defaultOpen: false,
        openClass: "Collapsible-Open",
        closeClass: "Collapsible-Closed"
    });

    /**** Press Releases ****/
    $("#press-release-list .show-all").click(function (e) {
        $("#press-release-list .Collapsible").collapsible("show");
        e.preventDefault();
    });
    $("#press-release-list .hide-all").click(function (e) {
        $("#press-release-list .Collapsible").collapsible("hide");
        e.preventDefault();
    });

    //show the section specified by the hash if defined.  otherwise show the first section.
    if (window.location.hash != '') {
        var selectCategory = window.location.hash.substring(1).replace(/\+/g, " ").toLowerCase();
        $("#press-release-list h3 a").each(function () {
            if ($(this).html().toLowerCase() == selectCategory)
                $(this).click();
        });
    }
    else
        $("#press-release-list .Collapsible .Collapsible-Header:first").click();

    /* tab pages */
    $(window).hashchange(function () {
        // Select the tab, if any, whose id is embedded in the url hash.  Not using the tab id itself so the browser doesn't
        // scroll the document down to the targeted tab.
        var tabHash = location.hash.match(/^#tab\.(.+)$/);
        if (tabHash)
            $(".ygTab li").filter(function () {
                return this.id == tabHash[1];
            }).click();
    });
    $(window).hashchange();
});

$(window).load(function ()
{
    /* size landing page boxes - this MUST happen after images are loaded (so this does not belong in $(document).ready() */
    var max = 0;
    $('.shadowBoxContent').each(function () {
        var h = $(this).height();
        if (h > max) max = h;
    }).each(function () {
        $(this).height(max);
    });
});
