﻿var isStillSearching = true;

function BindSearch(sFilter, x, y) {
    var elem = document.getElementById("goSearchChild");
    var elem2 = document.getElementById("goSearch");
    
    if (sFilter.length < 1) { elem2.style.display = 'none'; }
    else {
        var lastCategory = "";
        var divItem = "<div class=\"search_out\" onmouseover=\"this.className='search_hover'\" onmouseout=\"this.className='search_out'\">";
        var innerText = "";
        var closeButton = "<div class=\"SearchBuddyCloseWrapper\"><img onclick=\"close_SearchBuddy();\" src=\"/images/closeBtn.gif\" /></div>"; 
        var headerText = closeButton + "<div class='searchBuddyTitle'><h3>Featured Results:</h3></div>";
        headerText += "<div class='dividerHeader'><img src='/images/spacer.gif' />";
        
        for (var i = 0; i < psItems.length; i++) {
            if (psItems[i][1].substring(0, sFilter.length).toLowerCase() == sFilter.toLowerCase()) {
                if (psItems[i][0] != lastCategory) {
                    innerText += "</div>";
                    innerText += "<div class='categoryWrapper'>";
                    innerText += "<div class='categoryHeader'><span class='categoryHeaderTxt'>" + psItems[i][0] + "</span></div>";
                    lastCategory = psItems[i][0];
                }
                
                innerText += divItem;
                innerText += "<div class='searchItemWrapper'>";
                innerText += "<table cellpadding=0 cellspacing=0><tr>";

                if (psItems[i][3] != "") {
                    innerText += "  <td style='vertical-align:top;'>";
                    innerText += "  <img src=\"" + psItems[i][3] + "\" width='80' style='padding-right: 5px;' />";
                    innerText += "  </td>";
                }
                
                innerText += "  <td style='vertical-align:top;'>";
                innerText += "  <a class='searchBuddyLink' href=\"" + psItems[i][2] + "\">" + psItems[i][4] + "</a>";
                innerText += "  <div class='searchItemDescription'><span class='txt'>" + psItems[i][5] + "</span></div></td>";
                innerText += "  </tr>";
                innerText += "</table>";
                innerText += "</div></div>";
            }
        }
        
        if (innerText.length == 0) {
            innerText = "</div><div class='categoryWrapper'><div class='categoryHeader'><span class='categoryHeaderTxt'>No featured results found.</span></div>";
            innerText += "<div class='searchItemWrapper'><span class='txt'>Click the View Results button to view all search results.</span></div></div>";
        }
        
        innerText += "</div><div class='dividerHeader'><img src='/images/spacer.gif' /></div>";
        innerText += "<div style='position: relative; height: 32px; margin-top: 15px;'>";
        innerText += "<a href='/SearchResults.aspx?q=" + sFilter + "'>";
        innerText += "<img src='/images/viewAllResults.gif' alt='VIEW ALL RESULTS' style='float: right;' border='0' /></a></div>";

        if (elem.childNodes.length != 0) {
            elem.removeChild(elem.firstChild);
        }

        var resultsDiv = document.createElement("div");
        resultsDiv.innerHTML = headerText + innerText;

        elem.appendChild(resultsDiv);

        setTimeout("document.getElementById('goSearch').style.display = 'block';", 500);
        
        return false;
    }
}

function close_SearchBuddy() {
    isStillSearching = false;
    document.getElementById('goSearch').style.display = "none";
}
$(document).ready(function() {
    $("#txtSearch").keyup(function(e) {
        if (!e) e = window.event;
        if (e.keyCode == 13) {
            TBShowLoading();
            location.href = '/SearchResults.aspx?q=' + this.value;
        }
    });
    $("#txtSearch").keydown(function(e) {
        if (e.keyCode == 13) {
            TBShowLoading();
            location.href = '/SearchResults.aspx?q=' + this.value;
            return false;
        }
    });

    $(".searchIcon").click(function() {
    
        if (document.getElementById('txtSearch').value != 'Search Related' && document.getElementById('txtSearch').value != '')
        {
            TBShowLoading();
            location.href = '/SearchResults.aspx?q=' + document.getElementById('txtSearch').value;
            return false;
        }
        else 
        {            
            return false;
        }      

    });
});