﻿//Use googles CDN to load js files.
//google.load("jquery", "1.3.2");
google.load("jqueryui", "1.7.2");

var resultCount;

///	<summary>
/// Initializes script styles.
/// <summary>
function initializeStyles() {
    $("#navigation a[href$='geoprogramsearch.aspx']").removeAttr("onmouseover").removeAttr("onmouseout")
            .children("img").attr("src", "images/nav_maps-h.gif");

    //Layout the filter options into toggle panels.
    $("#filters").addClass("ui-accordion ui-widget ui-helper-reset")
                .find("h3")
                .addClass("ui-accordion-header ui-helper-reset ui-state-default ui-corner-top ui-corner-bottom")
			    .prepend('<span class="ui-icon ui-icon-triangle-1-e"/>')
			    .click(function() {

			        $(this).toggleClass("ui-accordion-header-active").toggleClass("ui-state-active")
					    .toggleClass("ui-state-default").toggleClass("ui-corner-bottom")
				        .find("> .ui-icon").toggleClass("ui-icon-triangle-1-e").toggleClass("ui-icon-triangle-1-s")
				        .end().next().toggleClass("ui-accordion-content-active").toggle();

			        hidden = $(this).find("~ div").find("[id$='FiltersExpanded']");
			        hidden.val(((hidden.val().toLowerCase() == "true") ? "false" : "true"));

			        return false;
			    }).next().addClass("ui-accordion-content ui-helper-reset ui-widget-content ui-corner-bottom").hide();

    $("#h3FilterByTopic").click();
    $("[id$='topicFiltersExpanded']").val("true");

//    $("#h3FilterbyMajorAgency").click();
//    $("[id$='majorAgencyFiltersExpanded']").val("true");

    if (resultCount > 0) {
        $("#tableProgams").tablesorter({ sortList: [[0, 0]], cancelSelection: false });
    }

    $("#selectAllTopicFilters").click(function() {
        $("[id$='checkBoxTopicFilter']").each(function() { $(this).attr('checked', true) });

        return false;
    });

    $("#deselectAllTopicFilters").click(function() {
        $("[id$='checkBoxTopicFilter']").each(function() { $(this).attr('checked', false) });

        return false;
    });

    $("#selectAllMajorAgencyFilters").click(function() {
        $("[id$='checkBoxMajorAgencyFilter']").each(function() { $(this).attr('checked', true) });

        return false;
    });

    $("#deselectAllMajorAgencyFilters").click(function() {
        $("[id$='checkBoxMajorAgencyFilter']").each(function() { $(this).attr('checked', false) });

        return false;
    });

    $(".command").hover(function() {
        $(this).addClass("ui-state-hover");
    }, function() {
        $(this).removeClass("ui-state-hover");
    });
}

function initialize() {
    initializeStyles();

    $("#searchControls1").tabs();

    $("#tableProgramRecipients").tablesorter({ sortList: [[0, 0]] });

    //Attach enter key event.
    $("[id$='textBoxAddress']").keypress(function(e) {
        keyCode = e.which ? e.which : e.keyCode;

        if (keyCode == 13) {
            $(this).blur();
            e.preventDefault();

            WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions($("[id$='linkButtonFindPrograms']").attr("id").replace(/_/g, "$"), "", true, "", "", false, true));
        }
    });

    $("#mapVersion").click(function () {
        $("body").append(
                    "<form id=\"formMapVersion\" method=\"post\" action=\"geoprogramsearch.aspx\">"
                    + "<input type=\"hidden\" name=\"Address\" value=\"" + $("[id$='textBoxAddress']").val() + "\" \>"
                    + "<input type=\"hidden\" name=\"Radius\" value=\"" + $("[id$='dropDownListAddressRadius'] :selected").val() + "\" \>"
                    + "<input type=\"hidden\" name=\"TopicFilters\" value=\"" + getTopicFilters().join(',') + "\" \>"
                    + "<input type=\"hidden\" name=\"MajorAgencyFilters\" value=\"" + getMajorAgencyFilters().join(',') + "\" \>"
                    + "</form>");



        $("#formMapVersion").submit();

        return false;
    });
}

function getTopicFilters() {
    var filters = [];

    $("[id$='checkBoxTopicFilter']").each(function() {
        if ($(this).attr("checked"))
            filters.push($(this).parent().attr("valueId"));
    });

    return filters;
}

function getMajorAgencyFilters() {
    var filters = [];

    $("[id$='checkBoxMajorAgencyFilter']").each(function() {
        if ($(this).attr("checked"))
            filters.push($(this).parent().attr("valueId"));
    });

    return filters;
}

//Register event to begin intialization once google's CDN has provided scripts.
google.setOnLoadCallback(initialize);
