document.onclick = checkClick;
function checkClick(e)
{
    var target = (e && e.target) || (event && event.srcElement);
    var obj = document.getElementById('searchTip');
    var obj2 = document.getElementById('<%= txtSearchField.ClientID %>');
    if (target==obj2) {
        if (obj.style.display=='block')
            obj.style.display='block';
    } else {
        null; 
        if (checkParent(target))
        {
            if ($("#searchTip").val() != "")
            {
                $("#searchTip").slideDown("fast")
            }
        }
        else
        {
            null;
        }
    }
}
function checkParent(t){
    while(t.parentNode) {
        if(t==document.getElementById('searchTip')){
            return false
        }
        t=t.parentNode
    }
    return true
}
function hideSearch()
{
    $("#searchTip").slideUp("fast");
    
    $(".hsearchinput").attr('value','');
}


var keyHasBeenPressed = false;
function delayStartSearch(searchBox, userid)
{
    keyHasBeenPressed = true;
    setTimeout("startSearch('"+searchBox.value+"','"+userid+"');",1000);

}


function startSearch(searchValue, userid)
{   

    if(keyHasBeenPressed == true)
    {    
        if (searchValue.length > 0)
        {
            callSearch(searchValue, userid);
           
        }
        else
        {
            $("#searchTip").slideUp("fast");
        }
        keyHasBeenPressed = false;        
    }
          
    
}
function callSearch(q, uid)
{
    $("#searchTip").slideDown("fast");
    document.getElementById("searchTip").innerHTML ="<h2>Loading...</h2><ul><ul><li><ul><li><p><center><img alt=\"\" src=\"/images/loading.gif\"/></center></p></li></ul>";
    $.ajax({ type: "POST",  
         url: "/WebServices/topSearch.asmx/getResults",  
         dataType: "xml",  
         data: "searchQuery=" + q + "&uid=" + uid,  
         processData: false,  
         error: function(XMLHttpRequest, textStatus, errorThrown) { callSearchError(XMLHttpRequest,textStatus, errorThrown); },  
         success: function(xml) { callSearchFinish(xml, q); }  
    });
}

function callSearchFinish(xml, query)  
{  
    var strSearchTip = "";
    var prevCategory = "";
    var firstTime = true;
    var imagePath = $("imagePath", this).text();
    
    var ctr = 0;        
     $("#searchTip").slideDown("fast"); 
    $("FeedItemSummary", xml).each(function()  
    {  
        searchCategory = $("searchCategory", this).text();
        title = $("title", this).text();
        imagePath = $("imagePath", this).text();
        desc = $("desc", this).text();
        link = $("link", this).text();
        
        // new result set, close previous result set if necessary and start header for new result set
        if (prevCategory != searchCategory)
        {
            if (!firstTime)
            {
                // add closing tags to result set
                strSearchTip += "</ul></li></ul>";
            }
            else
            {
                strSearchTip = "<h2><a href=\"/Search/?q=" + query + "\">View all search results</a><a style=\"position:absolute;top:6px;right:10px;\" onclick=\"hideSearch();\"><img alt=\"X\" src=\"/images/theX2.gif\" width=\"23\" height=\"23\" /></a></h2>";
                firstTime = false;
            }
            //clean up search cat for search results page
            var sc = searchCategory.toLowerCase();
            sc = sc.replace(" ","");
            strSearchTip += "<ul><li><h3><a href=\"/Search/Default.aspx?q=" + query + "&cat=" + sc + "\">" + searchCategory + "</a></h3><ul>"
        
        }
        
        var strImageTag = "";
        var strImageClass = ""
        
                if (imagePath != "")
        {
            strImageTag = "<img alt=\"\" src=\"" + imagePath + "\" width=\"58\" height=\"58\" />";
            strImageClass = "class=\"gotimage\"";                   
        }
                
        // create result item
        strSearchTip += "<li onclick=\"location.href='" + link + "'\" " + strImageClass + " id=\"li-search-" + ctr + "\"><h4>" + strImageTag + title + "</h4>";        
        strSearchTip += "<p>" + desc + "</p></li>"        
                
        prevCategory = searchCategory;
        ctr++;
    });    
    if (ctr <= 0)
    {        
        strSearchTip += "<h2>No Results Found<a style=\"position:absolute;top:9px;right:32px;\" onclick=\"hideSearch();\"><img alt=\"X\" src=\"/images/TheX.jpg\" width=\"20\" height=\"20\" /></a></h2>";
    }
    else
    {
        // add closing tags to result set
        strSearchTip += "</ul><h2><a href=\"/Search/?q=" + query + "\">View all search results</a></h2>";        
    }    
    document.getElementById("searchTip").innerHTML = strSearchTip;
}  

/*
function callSearchFinish(xml, query)  
{  
    var strSearchTip = "";
    var prevCategory = "";
    var firstTime = true;
    
    var ctr = 0;
    
    $("FeedItemSummary", xml).each(function()  
    {  
        searchCategory = $("searchCategory", this).text();
        title = $("title", this).text();
        imagePath = $("imagePath", this).text();
        desc = $("desc", this).text();
        link = $("link", this).text();
        
        // new result set, close previous result set if necessary and start header for new result set
        if (prevCategory != searchCategory)
        {
            if (!firstTime)
            {
                // add closing tags to result set
                strSearchTip += "</ul>"
            }
            else
            {
                firstTime = false;
            }
        
            strSearchTip += "<h2>" + searchCategory + "</h2>"
            strSearchTip += "<ul>"
        }
        
        var strImageTag = "";
        var strImageClass = ""
        
        if (imagePath != "")
        {
            strImageTag = "<img alt=\"\" src=\"" + imagePath + "\" width=\"58\" height=\"58\" />";
            strImageClass = "class=\"gotimage\"";                   
        }
        
        // create result item
        strSearchTip += "<li style=\"height:58\" onclick=\"location.href='" + link + "'\" " + strImageClass + " id=\"li-search-" + ctr + "\"><h3><a href=\"" + link + "\">" + strImageTag + title + "</a></h3>";        
        strSearchTip += "<p>" + desc + "</p></li>"        
                
        prevCategory = searchCategory;
        ctr++;
    });

    if (ctr <= 0)
    {
        strSearchTip = "<ul><li><h3>No Results Found</h3></li>";
    }
    else
    {
        // add closing tags to result set
        strSearchTip += "</ul><h2><a href=\"/App/Search/?q=" + query + "\">View all search results</a></h2>";
    }
    
    document.getElementById("searchTip").innerHTML = strSearchTip;
}  
*/
function callSearchError(xmlObj,textStatus,errorThrown)  
{  
    var strSearchTip = "<ul><li><h3>No Results Found</h3></li></ul>";    
}  