﻿    function addSearchSuggestions(lis,tbid)
    {
        lis
            .attr("tbID",tbid)
            .mouseover(function(){
                $(this).css("background-color","#dedede");
            })
            .mouseout(function(){
                $(this).css("background-color","");
            })
            .click(function(){
                $("#"+$(this).attr("tbID")).val($(this).text());
                $(this).parent().remove();
            })
            .each(function(i){
                $("#"+$(this).attr("tbID")+"_suggestions").prepend($(this));
            })
            ;
    }
    var currentsearchid = null;
    var searchtext = null;
    $(document).ready( function() { 
        $(".suggestSearchBox").focus(function(){
            if($(this).val()=="Search Choosik")
            {
                $(this).val("");
            }
        }).
        blur(function(){
            if($(this).val()=="")
            {
                $(this).val("Search Choosik");
            }
        })
        .keypress( function(e){
            if (e.keyCode==13) // enter key submit 'go' button
            {
                $(this).parent().next().click();
                return false;
            }
        })
        .parent().next().click(function(){
            var searchval = escape($(this).prev().find(".suggestSearchBox").val());
            while(searchval.indexOf("%")>=0)
                searchval = searchval.replace("%","_percentsign_").replace(".", "_period_");
            gotoUrl(APP_ROOT+"search/"+searchval);
            return false;
        });
        /*
        $(".suggestSearchBox")
            .keypress( function(e){
                if (e.keyCode==13) // enter key - don't want to submit
                {
                    var input = $(this).next();
                    if(input.attr("class")=="suggestions")
                    {
                        input = input.next();
                    }
                    if(input.val()!="-1")
                        return false;
                }
            })
            .keyup( function(e) { 
                var input = $(this).next();
                var suggestions = null;
                if(input.attr("class")=="suggestions")
                {
                    suggestions = input;
                    input = input.next();
                }
                if(e.keyCode==40) //down arrow key
                {
                    if(suggestions==null || suggestions.length==0)
                    {
                        
                        var text = $(this).val();
                        if(text.length>1)
                        {
                            currentsearchid = $(this).attr('id');
                            $.get(APP_ROOT+"Ajax/SearchSuggestions.aspx",
                           { text: text, count: "10" },
                           function(data){
                            var $text = $("#"+currentsearchid);
                            var $data = $(data);
                            if($data.find("#requestText").length>0)
                            {
                                $text.after("<ul class='searchsuggestions' id='"+currentsearchid+"_suggestions'></ul>");
                                suggestions = $text.next();
                                suggestions.width($text.width()+22).css("left",findPosX(document.getElementById(currentsearchid))+"px").css("top",($text.offset().top+$text.height()+5)+"px");
                                
                                addSearchSuggestions($data.find("li"),currentsearchid);
                            }
                           }
                         );
                              
                         }
                    }
                    else
                    {
                        var oldval = parseInt(input.val());
                        var newval = oldval+1;
                        
                        if(newval<suggestions.children("li").length)
                        {
                            input.val(newval);
                            suggestions.children(":eq("+newval+")").css("background-color","#dedede");
                            if(oldval!=-1)
                                suggestions.children(":eq("+oldval+")").css("background-color","");
                        }
                    }
                }
                else if(e.keyCode==38) //up arrow key
                {
                    if(suggestions!=null && suggestions.length>0)
                    {
                        var oldval = parseInt(input.val());
                        var newval = oldval-1;
                        if(newval>=-1)
                        {
                           input.val(newval);
                            if(newval!=-1)
                                suggestions.children(":eq("+newval+")").css("background-color","#dedede");
                            else
                            {
                                if(suggestions!=null)
                                    suggestions.remove();
                            }
                            suggestions.children(":eq("+oldval+")").css("background-color","");
                        }
                    }
                }
                else if (e.keyCode==13) // enter key
                {
                    try //exception if enter with no selection
                    {
                        var val = parseInt($(this).next().next().val());
                        if(val!=-1)
                        {
                            $(this).val(suggestions.children(":eq("+val+")").text());
                            input.val(-1);
                        }
                        if(suggestions!=null)
                            suggestions.remove();
                        return false;
                    }catch(e){}
                }
                else
                {
            
                    input.val(-1);
                    if(suggestions!=null)
                        suggestions.remove();
                    var text = $(this).val();
                    if(text.length>1)
                    {
                        currentsearchid = $(this).attr('id');
                        $.get(APP_ROOT+"Ajax/SearchSuggestions.aspx",
                           { text: text, count: "10" },
                           function(data){
                            var $text = $("#"+currentsearchid);
                            var $data = $(data);
                            if($data.find("#requestText").length>0)
                            {
                                $text.after("<ul class='searchsuggestions' id='"+currentsearchid+"_suggestions'></ul>");
                                suggestions = $text.next();
                                suggestions.width($text.width()+22).css("left",findPosX(document.getElementById(currentsearchid))+"px").css("top",($text.offset().top+$text.height()+5)+"px");
                                
                                addSearchSuggestions($data.find("li"),currentsearchid);
                            }
                           }
                         );
                          
                     }
                 }
            } )
            .blur(function(){
                setTimeout("$('#"+$(this).attr('id')+"_suggestions').remove();",200);
                
            })
            .after("<input type='hidden' value='-1' class='selectedIndex' />")
            ;
            
           
            */
    });
