﻿var gsa_search = null;
gsa = function(o) {
    o = o || {};
    var SearchURL = o.SearchURL || "/Popups/GSASearch.aspx";
    var GSAHiddenBaseURL = o.GSAHiddenBaseURL || "#GSAHiddenBaseURL";
    $("div.GSABox").each(function() {
        var box = $(this);
        var GSALoading = box.find("div.GSALoading");
        var res = box.find("div.GSASearchResults");
        var searchInput = box.find("div.GSASearchBox input.GSASearchInput");
        var searchButton = box.find("div.GSASearchBox input.GSASearchButton");
        function activateLinks(responseText, textStatus, XMLHttpRequest) {
            GSALoading.fadeOut(100);
            var list = res.find("ul.GSAResultsList");
            if (list.size() == 0) {
                res.html("No results found");
                return;
            }
            res.find("div.GSAPagingControls").each(function() {
                var GSABaseURL = getValue(GSAHiddenBaseURL);
                function getUrl(startIndex, pageSize) {
                    return GSABaseURL + "&start=" + startIndex + "&num=" + pageSize;
                }
                var root = $(this);
                var startIndex = parseInt(root.attr("startindex"));
                var endIndex = parseInt(root.attr("endindex"));
                var totalCount = parseInt(root.attr("totalcount"));
                var pageSize = parseInt(root.attr("pagesize"));
                var prevStart = startIndex - 1 - pageSize;
                if (prevStart < 0) prevStart = 0;
                var lastStart = parseInt(totalCount / pageSize) * pageSize;
                var nextStart = startIndex - 1 + pageSize;
                if (nextStart > totalCount) nextStart = lastStart;
                root.find("a.GSAFirstPage").click(function() { loadUrl2(getUrl(0, pageSize)); });
                root.find("a.GSAPrevPage").click(function() { loadUrl2(getUrl(prevStart, pageSize)); });
                root.find("a.GSANextPage").click(function() { loadUrl2(getUrl(nextStart, pageSize)); });
                root.find("a.GSALastPage").click(function() { loadUrl2(getUrl(lastStart, pageSize)); });
                root.find("select.GSAPageSizeCombo").change(function() {
                    var newPageSize = parseInt(this.options[this.selectedIndex].value);
                    loadUrl2(getUrl(0, newPageSize));
                });
                root.find("select.GSAPageIndexCombo").change(function() {
                    var pageIndex = parseInt(this.options[this.selectedIndex].value);
                    loadUrl2(getUrl(pageIndex * pageSize, pageSize));
                });
            });

        }
        function doSearch() {
            var text = searchInput.val();
            if (text) {
                GSALoading.fadeIn(100);
                res.load(SearchURL + "?searchq=" + escape(text), activateLinks);
            }
        }
        gsa_search = doSearch;
        function hasValue(elem) {
            if (getValue(elem))
                return true;
            return false;
        }
        function getValue(elem) {
            var n = res.find(elem);
            if (n.size() > 0)
                n = n[0].value;
            else
                n = false;
            return n;
        }
        function loadUrl(elem) {
            var n = getValue(elem);
            if (n) {
                GSALoading.fadeIn(100);
                res.load(SearchURL, { searchurl: n }, activateLinks);
            }
        }
        function loadUrl2(url) {
            GSALoading.fadeIn(100);
            res.load(SearchURL, { searchurl: url }, activateLinks);
        }
        function checkForEnter(event) {
            if (event.keyCode == 13) {
                doSearch();
                event.preventDefault();
                return false;
            }
        }
        searchInput.keydown(checkForEnter);
        searchButton.click(doSearch);
        doSearch();
    });
}
$.fn.advancedFilters = function(d) {
    if (d == 'expand') {
        $('a.expand-filters').css("display", "none");
        $('a.collapse-filters').css("display", "block");
        setSessionCookie("directoryadvfilters", "1");
    }
    else {
        $('a.expand-filters').css("display", "block");
        $('a.collapse-filters').css("display", "none");
        setSessionCookie("directoryadvfilters", "0");
    }
}
$.fn.applyTextDefault = function(tooltip) {
    var default_value = tooltip;
    if (tooltip == this[0].value)
        $(this).addClass("idleField");

    $(this).focus(function() {
        if (this.value == default_value) {
            this.value = '';
            $(this).removeClass("idleField");
        }
    });
    $(this).blur(function() {
        if (this.value == '') {
            $(this).addClass("idleField");
            this.value = default_value;
        }
    });
}
