// JavaScript Document

var lastKey = null;

$(document).ready(function() {  
	$('#dropBox').keyup(function() {
		lastKey = parseInt((new Date().getTime()) / 1000);
		if (document.getElementById('dropBox').value.length <= 2) $('#dropdown').hide();
		else {
			setTimeout("generateVenues()", 500);
			$("#searchIcon").removeClass("hidden");
		}
	});
	$('#noJS').hide();
	document.searchMore.venue.value = "";
	document.searchMore.form_start.value = "";
	document.searchMore.form_end.value = "";
	document.searchMore.form_search.value = "";
	$('#formStart').datepicker({
		showAnim: "slideDown",
		dateFormat: "mm-dd-yy",
		minDate: 0,
		numberOfMonths: 2
	});
	$('#formEnd').datepicker({
		showAnim: "slideDown",
		dateFormat: "mm-dd-yy",
		minDate: 0,
		numberOfMonths: 2
	});
});

$(window).load(function() {
	if (window.location.hash != "" && window.location.hash != "#") {
		var urlArray = window.location.hash.split("/");
		urlArray[0] = urlArray[0].substring(1);
		var tempStr = urlArray[urlArray.length-1];
		
		var expand;
		if (/\d{2,5}/.test(tempStr)) expand = tempStr;
		else expand = null;
		
		var page;
		for (var i = 0; i < urlArray.length; i++) if (/p=/.test(urlArray[i])) page = urlArray[i].substring(2);
		else page = null;

		if (urlArray[0] == "today") loadEvents("today", page, expand);
		else if (urlArray[0] == "tomorrow") loadEvents("tomorrow", page, expand);
		else if (urlArray[0] == "weekend") loadEvents("weekend", page, expand);
		else {
			for (var i = 0; i < urlArray.length; i++) {
				$('#searchMore').show();
				if (urlArray[i].substring(0,3) == "loc") document.searchMore.venue.value = urlArray[i].substring(4);
				else if (urlArray[i].substring(0,5) == "start") document.searchMore.form_start.value = urlArray[i].substring(6);
				else if (urlArray[i].substring(0,3) == "end") document.searchMore.form_end.value = urlArray[i].substring(4);
				else if (urlArray[i].substring(0,6) == "search") document.searchMore.form_search.value = urlArray[i].substring(7);
				else if (urlArray[i].substring(0,3) == "cat") document.searchMore.form_cat.value = urlArray[i].substring(4);
			}
			loadEvents("other", page, expand);
		}
	} else loadEvents("today");
});

function removeButtons() {
	$('#today').removeClass("buttonSelect");
	$('#tomorrow').removeClass("buttonSelect");
	$('#weekend').removeClass("buttonSelect");
	$('#more').removeClass("buttonSelect");
}

function loadEvents(day, p, expand) {
	removeButtons();
	$('#eventList').html("<img src='/images/icons/ajax.gif'> <b>Loading...</b>");
	var url = "";
	if (day == "other") {
		url += "loc=" + document.searchMore.venue.value + "&formStart=" + document.searchMore.form_start.value + "&formEnd=" + document.searchMore.form_end.value + "&formSearch=" + document.searchMore.form_search.value + "&formCat=" + document.searchMore.form_cat.value;
		window.location.hash = "loc=" + document.searchMore.venue.value + "/start=" + document.searchMore.form_start.value + "/end=" + document.searchMore.form_end.value + "/search=" + document.searchMore.form_search.value + "/cat=" + document.searchMore.form_cat.value;
	}
	else {
		url += "simple="+day;
		window.location.hash = day;
	}
	if (p) {
		url += "&p=" + p;
		window.location.hash += "/p=" + p;
	}
	$.ajax({
		type: "GET",
		url: "/pages/93/ajax.php",
		data: url,
		success: function(msg) {
			$('#eventList').html(msg);
			$('#eventList').slideDown(500);
			if (day != "other") {
				if (day == "today") $('#today').addClass("buttonSelect");
				else if (day == "tomorrow") $('#tomorrow').addClass("buttonSelect");
				else $('#weekend').addClass("buttonSelect");
			}
			if (expand) expandEvent(expand);
		}
	});
}

function showSearch() { $('#searchMore').slideDown(250); }

function expandEvent(id) {
	$('#event'+id).slideDown(250);
	var urlArray = window.location.hash.split("/");
	var tempStr = urlArray[urlArray.length-1];
	if (/\d{2,5}/.test(tempStr)) window.location.hash = window.location.hash.substring(0,window.location.hash.length - (urlArray[urlArray.length-1].length + 1));
	window.location.hash += "/" + id;
	$.ajax({
		type: "GET",
		url: "/pages/93/ajax.php",
		data: "expand=" + id
	});
	$.scrollTo('#event'+id, 500);
}

function generateVenues() {
	var nowTime = parseInt((new Date().getTime()) / 1000);
	if (nowTime >= lastKey) {
		var text = document.getElementById('dropBox').value;
		var url = "text="+text;
		if (usingIE) url += "&ie";
		$.ajax({
			type: "GET",
			url: "/pages/93/ajax.php",
			data: url,
			success: function(msg) {
				$('#dropdown').html(msg);
				$('#dropdown').show();
				$('#searchIcon').addClass("hidden");
			}
		});
	}
}
  
function fillBox(text) {
	document.getElementById('dropBox').value = text;
	$('#dropdown').hide();
	$('#dropdown').html("<img src='/images/icons/ajax.gif'> <b>Loading...</b>");
}

function addEndDate() {
	$("#dateAdd").addClass("hidden");
	$("#endDate").removeClass("hidden");
	document.searchMore.form_end.focus();
}
