﻿$(function() {

    $("#searchT").hint();

    /* search bar in header */

    
    var i = -1;
    var href = "";
    $("#searchT").keyup(function(e) {
        //            var idx = $("#header-search-autocomplete a").index("a.focus");
        //             alert(idx);
        $(".focus").removeClass("focus");
        var val = "";

        if (e.which == 38) { // up arrow
            $("#header-search-autocomplete a:eq(" + parseInt(i - 1) + ")").addClass("focus");
            val = $("#header-search-autocomplete a:eq(" + parseInt(i - 1) + ")").text();
            href = $("#header-search-autocomplete a:eq(" + parseInt(i - 1) + ")").attr("href");
            i--;
        } else if (e.which == 40) { // down arror
            $("#header-search-autocomplete a:eq(" + parseInt(i + 1) + ")").addClass("focus");
            val = $("#header-search-autocomplete a:eq(" + parseInt(i + 1) + ")").text();
            href = $("#header-search-autocomplete a:eq(" + parseInt(i + 1) + ")").attr("href");
            i++;
        }

        if (val != "") $("#searchT").val(val);
        return true;
    });


    $("#searchT").keypress(function(e) {
        if (e.which == 0) return false;
        //alert(e.which);

        $("#header-search-autocomplete").hide();
        $("#header-search-autocomplete").html("");

        //             $("#header-search-autocomplete").load(
        //                        "/search #xsltsearch_results"
        //                        , { 'limit':'5', 'search': $(this).val() }
        //                        , function() {                            
        //                            $("#header-search-autocomplete").slideDown();
        //                        }
        //              );



        var ret = $(this).val();
        if (ret.length > 2) {
            $.get("/search",
            { "search": ret
                , beforeSend: function() { to = setTimeout(function() { alert('time out!'); }, 100) }
                , success: function() { clearInterval(to); }
                , cache : 'true'
            },
            function(data) {
                ret = $("#xsltsearch_results", data).html();

                         //alert(ret);
        var rett = "";
        $("a", ret).each(function(i) {
        if (i > 2) return false;
        rett += "<a class='autocomplete-a' href='" + $(this).attr("href") + "'>" + $(this).html() + "<\/a><br \/>";
        });

                         $("#header-search-autocomplete").html(rett);
        
                        if ( $.browser.msie ) {
                            if ($.browser.version != '6.0')
                                $("#header-search-autocomplete").slideDown();
                        }

                        

                     }
        );
        } else { $("#header-search-autocomplete").slideUp(); }


        if (e.which == 13) {

            if (href != "") { document.location = href; return false; }

            document.location = "/search?search=" + $(this).val();
            //$.facebox(ret);
            return false;
        }



    });

});

function test(data, textStatus) {
    //alert(data);
    //$("#header-search-autocomplete").slideDown();
}