var xmlProducts;
var xmlTags;
var t;
var watermarkText = "Shop Search";
$(document).ready(function() {

    $(".SearchInput").focus(function() {
        if ($(".SearchInput").val() == watermarkText) {
            $(".SearchInput").val("");
        }
        $(".SearchInput").css({ "color": "#000000", "font-style": "normal" });
        clearTimeout(t);
        return true;
    });

    $(".SearchInput").blur(function() {
        if ($(".SearchInput").val() == "") {
            $(".SearchInput").val(watermarkText);
            $(".SearchInput").css({ "color": "#666" });
        }
        t = setTimeout("$('.SearchResults').slideUp()", 80);

    });

    $(".SearchInput").keydown(function(e) {
        if (e.which == 8) {
            SearchText = $(".SearchInput").val().substring(0, $(".SearchInput").val().length - 1);
        }
        else {
            SearchText = $(".SearchInput").val() + String.fromCharCode(e.which);
        }
        if (SearchText != "") {
            getResult(SearchText);
            $(".SearchResults").slideDown();
        } else {
            $(".SearchResults").slideUp();
        }
    });

    $.ajax({
        type: "GET",
        url: "xmlshopfeed",
        dataType: "xml",
        success: function(xml) {
            xmlProducts = xml;
        }
    });

});

function getResult(s) {
    var out = "";
    var e = "";
    var eArr = new Array();
    var outArr = new Array(512);
    var count = 0;
    var v = "";
    var vArr = new Array();
    var PIPE = new RegExp('\\|', 'g');

    $(".SearchResults").empty();

    if (s.length < 2) { return; }
    if (xmlProducts == null) {
        setTimeout("getResult('" + s + "')", 250);
    }

    $("products>product", xmlProducts).each(function() {
        var n = $("title", this).text().toUpperCase();
        var URL =  $("url", this).text();
        
        if ((n.indexOf(s.toUpperCase()) > -1)) 
        {
            e += "<div><a class='block' href='" + URL + "'>" + $("title", this).text().replace(PIPE, " ") + "</a></div>";

        }
    });

   
    if (e != "") {
        out += "<div class='SearchResultHeader'>PRODUCTS</div>";
        out += e;
    }

    $(".SearchResults").html(out);
}
