var xmlEvents;
var xmlTags;
var t;
$(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": "#999999", "font-style": "italic" });
        }
        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: "eventsfeed",
        dataType: "xml",
        success: function(xml) {
            xmlEvents = xml;
        }
    });

    $.ajax({
        type: "GET",
        url: "filters",
        dataType: "xml",
        success: function(xml) {
            xmlTags = xml;
        }
    });

});
			
function getResult(s) {
	var out = "";
	var e = "";
	var l = "";
	var v = "";
	var c = "";
	$(".SearchResults").empty();
	
	$("filters>categories>category", xmlTags).each(function(){
		var n = $("name", this).text().toUpperCase();
		if (n.indexOf(s.toUpperCase()) >-1) {
			c+= "<div><a href='" + resultsPage + "?c=" + $("name", this).text() + "' title='" + n + "'>";
			c+=  $("name", this).text().toUpperCase();
			c+= "</a></div>";
		}
	});
	if(c != "") {
		out +="<div class='SearchResultHeader'>CATEGORIES</div>";
		out +=c;
	}
	
	$("filters>levels>level", xmlTags).each(function(){
		var n = $("name", this).text().toUpperCase();
		if (n.indexOf(s.toUpperCase()) >-1) {
			l+= "<div><a href='" + resultsPage + "?l=" + $("name", this).text() + "' title='" + n + "'>";
			l+=  $("name", this).text().toUpperCase();
			l+= "</a></div>";
		}
	});	
	if(l != "") {
		out +="<div class='SearchResultHeader'>PACKAGE LEVELS</div>";
		out+=l;
	}
	$("filters>venues>venue", xmlTags).each(function(){
		var n = $("name", this).text().toUpperCase();
		if (n.indexOf(s.toUpperCase()) >-1) {
			v+= "<div><a href='" + resultsPage + "?v=" + $("name", this).text() + "' title='" + n + "'>";
			v+=  $("name", this).text().toUpperCase();
			v+= "</a></div>";
		}
	});
	if(v != "") {
		out +="<div class='SearchResultHeader'>VENUES</div>";
		out+=v;
	}
	
	$("events>event", xmlEvents).each(function(){
		var n = $("name", this).text().toUpperCase();
		if (n.indexOf(s.toUpperCase()) >-1) {
			e+= "<div><a href='" + $("url", this).text() + "' title='" + n + "'>";
			e+=  $("name", this).text().toUpperCase();
			e+= "</a></div>";
		}
	});
	if(e != "") {
		out +="<div class='SearchResultHeader'>EVENTS</div>";
		out+=e;
	}

		$(".SearchResults").html(out);
}