﻿function EISearch () {
    this.resultsList = $("#eiSearchResults");
    this.resultsPanel = $("#eiSearchResultsPanel");
    this.inputForm = $("#eiInputForm");
    this.previousButton = $("#eiSearchPreviousButton");
    this.nextButton = $("#eiSearchNextButton");
    this.goButton = $("#eiSearchGoButton");
    
    var searchObj = this;
    
    this.inputForm.keydown(function(e) {
        // alert(e.which);
        if (e.which == 13) {
            searchObj.submitSearch(this.value);
            e.stopPropagation(); 
            e.preventDefault();
        } else if (e.which == 9 && !e.shiftKey && searchObj.resultsShowing) {
            e.stopPropagation();
            e.preventDefault();
            searchObj.goButton[0].focus();
        } else if (e.which == 27) {
            searchObj.hideResults();
        }
    });
    
    this.inputForm.keyup(function(e) {
        searchObj.updateSearch(this.value);
    });
    
    this.goButton.keydown(function(e) {
        if (e.which == 9 && e.shiftKey) {
            searchObj.inputForm[0].focus();
            e.stopPropagation();
            e.preventDefault();
        }
    });
    
    this.goButton.click(function(e) {
        searchObj.submitSearch(searchObj.inputForm[0].value);
        e.stopPropagation();
        e.preventDefault();
    });
    
    this.previousButton.click(function(e) {
        searchObj.prevPage();
        e.stopPropagation();
        e.preventDefault();
    });
    
    this.nextButton.click(function(e) {
        searchObj.nextPage();
        e.stopPropagation();
        e.preventDefault();
    });
    
    this.inputForm.click(function(e) {
        if (this.value.length > 0) {
            searchObj.showResults();
        }
        e.stopPropagation();
        e.preventDefault();
    });
    
    this.resultsPanel.keydown(function(e) {
        if (e.which == 27) {
            searchObj.hideResults();
        }
    });

    
    this.resultsPanel.click(function(e) {
        if (e.target.nodeName != "A") {
            e.stopPropagation();
            e.preventDefault();
        }
    });
    
    $(document).click(function(e) {
        searchObj.hideResults();
    });
    
}

EISearch.prototype = {
    resultsPanel : null,
    resultsList : null,
    inputForm : null,
    previousButton : null,
    nextButton : null,
    goButton : null,
    
    resultsShowing : false,
    nextSearch : null,
    currentSearch : null,
    searchInProgress : false,
    pagStart : 0,
    pagEnd : 0,
    pagTotal : 0,
    pagStep : 10,
    
    submitSearch : function (searchString) {
        location.href = "/energyissues/search/?q=" + escape(searchString);
    },
    
    updateSearch : function (searchString) {
        if (this.searchInProgress) {
            this.nextSearch = searchString;
        } else {
            if (searchString != this.currentSearch) {
                if (searchString != null && searchString != '') {
                    this.currentSearch = searchString;
                    this.searchInProgress = true;
                    var searchObj = this;
                    $.ajax({
                        type: "GET",
                        url: "/webHandlers/EISearchHandler.ashx",
                        data: "q=" + escape(searchString),
                        success: function (xmlObj) {
                            searchObj.handleAJAXResponse(xmlObj);
                        }
                    });
                } else {
                    this.hideResults();
                }
            }
        }
    },
    
    handleAJAXResponse : function(xmlObj) {
        this.populateResults(xmlObj);
        this.searchInProgress = false;
        if (this.nextSearch != null) {
            this.updateSearch(this.nextSearch);
            this.nextSearch = null;
        }
    },
    
    populateResults: function(xmlObj) {
        xmlChildren = xmlObj.getElementsByTagName("pageref");
        var numEntries = 0;
        var ulNode = document.createElement("ul");
        for (i=0; i<xmlChildren.length; i++) {
            var childNode = xmlChildren[i];
            if (childNode.nodeType == 1) {
                if(childNode.attributes.getNamedItem("fullpath")!= null) {
                    var liNode = document.createElement("li");
                    var anchorNode = document.createElement("a");
                    liNode.appendChild(anchorNode);

                    var hrefAttribute = document.createAttribute("href");
                    hrefAttribute.value = childNode.attributes.getNamedItem("fullpath").value;
                    anchorNode.attributes.setNamedItem(hrefAttribute);

                    // var classAttribute = document.createAttribute("class");
                    // classAttribute.value = childNode.attributes.getNamedItem("color").value;
                    // anchorNode.attributes.setNamedItem(classAttribute);

                    anchorNode.appendChild(document.createTextNode(childNode.attributes.getNamedItem("title").value));
                    ulNode.appendChild(liNode);
                    numEntries++;
                }
            }
        }
        
        this.resultsList.empty();
        if (numEntries > 0) {
            this.pagTotal = numEntries;
            this.pagStart = 0;
            this.pagEnd = Math.min(this.pagTotal, this.pagStart + this.pagStep);
            this.resultsList.append(ulNode);
            this.updatePagination();
            this.showResults();
        } else {
            this.pagTotal = this.pagStart = this.pagEnd = 0;
            this.updatePagination();
        }
    },
    
    updatePagination : function() {
        this.resultsList.find("li").css("display", "none").slice(this.pagStart, this.pagEnd).css("display", "block");
        
        if (this.pagTotal > 0) {
            $("#eiPaginationStart").text(this.pagStart + 1);
            $("#eiPaginationEnd").text(this.pagEnd);
            $("#eiPaginationTotal").text(this.pagTotal);
            $("#eiSearchPreviousButton").css("display", (this.pagStart == 0 ? "none" : "block"));
            $("#eiSearchNextButton").css("display", (this.pagEnd == this.pagTotal ? "none" : "block"));
            $("#eiSearchPagination").css("display", "block");
            $("#eiSearchPaginationNotFound").css("display", "none");
        } else {
            $("#eiSearchPagination").css("display", "none");
            $("#eiSearchPaginationNotFound").css("display", "block");
        }
    },
    
    nextPage : function() {
        if (this.pagEnd < this.pagTotal) {
            this.pagStart = this.pagEnd;
            this.pagEnd = Math.min(this.pagTotal, this.pagStart + this.pagStep);
            this.updatePagination();
        }
    },

    prevPage : function() {
        if (this.pagStart > 0) {
            this.pagStart = Math.max(0, this.pagStart - this.pagStep);
            this.pagEnd = Math.min(this.pagTotal, this.pagStart + this.pagStep);
            this.updatePagination();
        }
    },
    
    showResults : function() {
        this.resultsShowing = true;
        this.resultsPanel.fadeIn(500);
    },

    hideResults : function() {
        this.resultsShowing = false;
        this.resultsPanel.fadeOut(500);
    }
};

$().ready(function() {

    // Wire up EI Search
    eiSearchInstance = new EISearch();
    
    // Close chart thickboxes on any click
    $(".tbChartImage").find("a").click(function(e) {
        tb_remove();
    });
});