/*

	Locality: GeoWeb Directory
	Copyright (c) 2008, Vincent Kenney
	All rights reserved.

	Redistribution and use in source and binary forms, with or without modification, are NOT premitted without the express written consent of the Author.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*/

var mapstract = null;
var directorySearchable = false;

$(document).ready(function(){
	// badass onliner to kill the browser anchor jump
	$('a.anchor').remove().prependTo('body');

	var faqexp = new RegExp("^.*?faq(.*)");
	var faqmatch = location.href.match(faqexp);
	if(!faqmatch){
		if($("#map").length > 0){
			mapstract = new Mapstraction('map','google');
			mapstract.setCenterAndZoom(new LatLonPoint(43.10,-89.4), 11);
			mapstract.addSmallControls();
		}
	}
	$("#list").hide();


	$("a.returnDirLink").hide();
	$("a.dirLink").click(function(){
		directorySearchable = false;
		$("#dir-items").html($("a[@name=directory-" + $(this).text() + "]").parent().next().html());
		//return false;
	});
	$(":input[@name=searchText]").keyup(function(e){
		LocalSearch();
	});
	PlotCompanys(null, null, null);
	checkHash();
});

function LocalSearch(){
	if(!directorySearchable){
		$("#dir-items").html($("#list").html());
		$("#dir-items dt").hide();
		$("#dir-items p.bom-heading-p").hide();
		$(".returnDirLink").hide();
		$("#dir-items").append("<p id=\"no-results\">No Results.</p>");
		directorySearchable = true;
	}
	$("#dir-items ul").hide();
	$("#dir-items ul li").hide();
	$("#dir-items dd").hide();
	var term = $(":input[@name=searchText]").attr("value");
	//alert(term);
	var regexp = new RegExp("^.*?" + term + ".*?", "i");
	$("#no-results").show();
	$(".dirItem").each(function(){
		//alert($(this).text());
		//$(this).show();
		if($(this).text().match(regexp)){
			//alert($(this).text());
			$(this).show();
			$(this).parent().show();
			$(this).parent().parent().show();
			$(this).parent().parent().parent().show();
			$("#no-results").hide();
		}
	});
}

function checkHash(){
	var catexp = new RegExp("^.*?category-list(.*)");
	var compexp = new RegExp("^.*?company-list(.*)");
	var indexexp = new RegExp("^.*?index(.*)");
	var faqexp = new RegExp("^.*?faq(.*)");
	var faqmatch = location.href.match(faqexp);
	var catmatch = location.href.match(catexp);
	var compmatch = location.href.match(compexp);
	var indexmatch = location.href.match(indexexp);
	if(indexmatch != null){
		$("#site-nav > p > a:first").addClass('current-page');
	}else if(catmatch != null){
		$("#site-nav > p > a:eq(1)").addClass('current-page');
	}else if(compmatch != null){
		$("#site-nav > p > a:eq(2)").addClass('current-page');
	}else if(faqmatch != null){
		$("#site-nav > p > a:eq(3)").addClass('current-page');
	}
	var hashexp = new RegExp("^.*?#(.*)");
	var match = location.href.match(hashexp);
	if(match != null){
		//alert(match[1]);
		pageload(match[1]);
	}
}

function pageload(hash){
	if(hash){
		$("#dir-items").html($("a[@name=" + hash + "]").parent().next().html());
	}else{
	}
}

function PlotCompanys(responseText, textStatus, xhr){
	if(mapstract == null){
		return;
	}
	mapstract.removeAllMarkers();
	if(responseText != null){
		$("div.dirItemBody").hide();
	}
	var autoCenter = false;
	$("div.company").each(function(){
		var lat = $(this).find("li.lat:first").text();
		var lon = $(this).find("li.lon:first").text();
		var icon = $(this).find("li.medal:first").text();
		var info = $(this).find(".locality-info:first").html();
		var label = $(this).find(".locality-label:first").text();
		var marker = new Marker(new LatLonPoint(lat, lon));
		if(label != null){
			marker.setLabel(label);
		}
		if(info != null){
			var address = $(this).find("li.street:first").text();
			var csz = $(this).find("li.city-state-zip:first").text();
			if(address != null && csz != null){
				info += "<br /><br /><form action=\"http://maps.google.com/maps\" method=\"get\" target=\"_blank\"> Driving Directions From:<input type=\"text\" name=\"saddr\" /> <input type=\"hidden\" name=\"daddr\" value=\"" + address + ", " + csz + "\" /> <input type=\"submit\" value=\"Get Directions\" /> </form>";
			}
			marker.setInfoBubble(info);
		}
		if(icon != ""){
			marker.setIcon(icon);
		}
		mapstract.addMarker(marker);						
		autoCenter = true;
	});
	$("li.lat").hide();
	$("li.lon").hide();
	if(responseText != null || autoCenter){
		mapstract.autoCenterAndZoom();
	}	
}
