//  STINKYFEETEVENTS
//  Fields JS
//  Copr. 2008
var search_terms = "Search events... ";
var event_city = "City";

var abbrevs = new Array("AK","AL","AR","AZ","CA","CO","CT","DC","DE","FL","GA","HI","IA","ID","IL","IN","KS","KY","LA","MA","MD","ME","MI","MN","MO","MS","MT","NC","ND","NE","NH","NJ","NM","NV","NY","OH","OK","OR","PA","RI","SC","SD","TN","TX","UT","VA","VT","WA","WI","WV","WY");
var names = new Array("Alaska","Alabama","Arkansas","Arizona","California","Colorado","Connecticut","Washington, D.C.","Delaware","Florida","Georgia","Hawai`i","Iowa","Idaho","Illinois","Indiana","Kansas","Kentucky","Louisiana","Massachusetts","Maryland","Maine","Michigan","Minnesota","Missouri","Mississippi","Montana","North Carolina","North Dakota","Nebraska","New Hampshire","New Jersey","New Mexico","Nevada","New York","Ohio","Oklahoma","Oregon","Pennsylvania","Rhode Island","South Carolina","South Dakota","Tennessee","Texas","Utah","Virginia","Vermont","Washington","Wisconsin","West Virginia","Wyoming");

var preselected, loadThese;
$(document).ready(function(){
//alert($("body").attr("id"));
	if ($("body").attr("id") == "search") {
		if (location.search.substring(0,4) == "?map") { setTimeout(openMap(),250); }
		if ($("#event_states").val() != "") {
			$("#loc-city").hide(); $("#loc-states").show(); }
		if ($("#search_terms").val() == "" || $("#search_terms").val() == search_terms) {
			$("#search_terms").css("color","#888"); $("#search_terms").val(search_terms); }
		if ($("#event_city").val() == "" || $("#event_city").val() == event_city) {
			$("#event_city").css("color","#888"); $("#event_city").val(event_city); }
		if ($("#event_states").val() != "") {
			$("#loc-states").show(); $("#loc-city").hide(); }
		$("#search_terms").focus(function(){ setInput($(this)); });
		$("#search_terms").blur(function(){ setInput($(this)); });
		$("#event_city").focus(function(){ setInput($(this)); });
		$("#event_city").blur(function(){ setInput($(this)); });
		$("form:first").submit(function(){ clearInputs($(this)); });
		$("#event_multiple_states").click(function(){ openMap(); });
		$("#event_multiple_states_edit").click(function(){ openMap(); });
		$("#event_multiple_states_reset").click(function(){
			$("#event_states").val(""); $("#loc-states").hide(); $("#loc-city").fadeIn(); return false;
		})
		
	} else if ($("body").attr("id") == 'home' || $('body').attr('class') == 'map') {
		$("#cancelform").click(function(){ iscancel(); });
		$("#cancelform1").click(function(){ iscancel1(); });
		$("#finishedbtn").click(function(){ isFinished(); });
		/*$(document).keyup(function(e){
			//var k = e.keyCode || window.event.keyCode;
			var k = e.keyCode;
			if (k == 13) { isFinished(); }
		});*/

		loadMap();

	}
	
});

$(window).load(function() {
	$(".loading").remove();
	$("#right").css("overflow","auto");
	if ($("#states li").length == 0) {
		$("#instructions").fadeIn();
	} else { $("#states").fadeIn(); }
});

function setInput(ipt) {
	var tval = ipt.attr('id');
	if (ipt.val() == "") { ipt.css("color","#888"); ipt.val(window[tval]); }
	else if (ipt.val() == window[tval]) { ipt.val(""); ipt.css("color","#000"); }
}

function clearInputs(form) {
	form.find("input").each(function(i){ var tval = $(this).attr('id');
		if ($(this).val() == window[tval]) { $(this).val("") } });
}

function openMap() {
	//alert(document.getElementById("search12").action);
	document.getElementById("search12").action="../mapsearch/?action=search";
	//alert(document.getElementById("search12").action);
	document.getElementById("search12").submit();
}

function loadMap() {
	
	// Preload the map background first
	var mapImg = new Image(700,550);
	mapImg.src= "mapsearch/img/map.gif";

	var loadImg = new Image(700,550);
	loadImg.src= "mapsearch/img/loading.gif";

	setTimeout(function() {
	    for (i = 0; i < abbrevs.length; i++) { loadState(i); }
	    if (opener != null) { 
	        if (opener.loadThese.length > 0) {
	            $("#instructions").hide();
	            for (i = 0; i < opener.loadThese.length; i++) {
	                $("#" + opener.loadThese[i]).addClass("selected");
	                var stateno = $(abbrevs).index(opener.loadThese[i]);
	                $("#states ul").append("<li class='" + opener.loadThese[i] + "'>" + names[stateno] + "</li>").fadeIn();
	            } opener.loadThese = null;
	            $("#states").fadeIn();
	        }
	    }
	}, 500);
	
}

function loadState(no) {
	
	var st = new Image();
	st.src= "mapsearch/img/"+abbrevs[no]+".png";

	$("#"+abbrevs[no]+"_outline").mouseover(function(){ stateHover(abbrevs[no]); });
	$("#"+abbrevs[no]+"_outline").mouseout(function(){ stateUnhover(abbrevs[no]); });
	$("#"+abbrevs[no]+"_outline").click(function(){ stateClick(abbrevs[no],no); });
	
}

function stateHover(abbrev) {
	//alert(abbrev);
	$("#"+abbrev).css("background-position","0 -150px");
}

function stateUnhover(abbrev) {
	$("#"+abbrev).css("background-position","0 0");
}

function stateClick(abbrev,no) {
	if ($("#states").is(":hidden")) {
		$("#instructions").hide();
		$("#states").fadeIn();
	}
	
	if ($("#"+abbrev).hasClass("selected")) {
		$("#"+abbrev).removeClass("selected")
		$("li."+abbrevs[no]).fadeOut().remove();
		if ($("#states li").length == 0) {
			$("#states").hide(); $("#instructions").fadeIn(); }
	} else {
		$("#"+abbrev).addClass("selected");
		$("#states ul").append("<li class='"+abbrevs[no]+"'>"+names[no]+"</li>").fadeIn();
	};	
}

function isFinished() {
	if ($("#states li").length != 0) {
		var states = new Array();
		$("#states li").each(function(){
		states[states.length] = $(this).attr("class"); });
		document.getElementById("event_states").value=states;
	} else { document.getElementById("event_states").value=""; };
	document.getElementById("frm1").submit();
}
function iscancel() {
	window.location='search/';
}
function iscancel1() {	
	window.location='index.php';
}
