var zipCode     = "";
var pageloc     = "";
var lastHash    = "";
var hashHistory = new Array();
if (typeof homePage === "undefined")
	var homePage = "#checklists";
window.addEvent('domready', function(){	
	linkify();
	periodical = hashWatch.periodical(500);
});

var linkify = function()
{
	$$(document.links).filter(function(el) {
		if (el.rel == "toggler")
		{
			el.addEvent('mouseover', function(el)
			{
				if (el.target.tagName == "EM")
					el.target.parentNode.parentNode.style.backgroundColor = "#000";
				else
					el.target.parentNode.style.backgroundColor = "#000";
			});
			el.addEvent('mouseout', function(el)
			{
				if (el.target.tagName == "EM")
					el.target.parentNode.parentNode.style.backgroundColor = "";
				else
					el.target.parentNode.style.backgroundColor = "";
			});
		}
		if (el.href.replace("#", "") == el.href && el.rel != "lightbox" && el.target != "_blank" && el.rel != 'document' && el.rel != 'norewrite' && !el.href.contains('tour') && !el.href.contains('mailto'))
			el.href = "/#" + el.href.replace(window.location.protocol + "//" + window.location.host + "/", "").replace(/\//g, "|");

	});
};

var hashWatch = function()
{
//	effects['outside_canvas'].start("width", 0);
	newhash = window.location.hash.split(":");
	if (pageloc != newhash[1])
	{
		pageloc = newhash[1];
		scrollPage();
	}
	newhash = newhash[0];
	if ((newhash != '') && (newhash == lastHash))
		return;
	if ((newhash == '#home') && (homePage != '#home'))
	{
		window.location = homePage;
		return;
	}
	if (newhash == "" && (window.location.pathname == '' || window.location.pathname == '/' || window.location.pathname == '/index.php'))
	{
		if ($('index-main'))
		{
			if (homePage != '#home')
			{
				window.location = homePage;
				return;
			}
			newhash = homePage;
			currentPage = loadingPage = homePage.replace('#','');
			toggleFlash(1);
			resetLinks(this);
			effects['content-box-right'].start("width", 0);
			mapTabHide();
			effects['dashboard-box'].start("width", 0);
			$('content-box-right').innerHTML = "";
			$('dashboard-box').innerHTML = "";
			if ($('tour-div'))
			{
				effects['tour-div'].start("width", 0);
				$('tour-div').innerHTML = "";
			}
			backgroundSwap();
		}
	}
	if (lastHash != newhash && newhash.replace("#", "") != '')
	{
		closeVideoBox();
		if (newhash != "#home")
		{
			toggleFlash(0);
			hashHistory[hashHistory.length] = newhash.replace("#", "");
			resetLinks(this);
			newPage = newhash.replace("#", "");
			if (!newPage.contains("step") && !currentPage.contains("step") && !newPage.contains("directory-search") && !currentPage.contains("directory-search") || !newPage.contains('directory-search') && !newPage.contains("step") || !currentPage || currentPage == 'home')
			{
				effects['content-box-right'].start("width", 0);
				effects['dashboard-box'].start("width", 0);
				$('content-box-right').innerHTML = "";
				$('dashboard-box').innerHTML = "";
				if ($('tour-div'))
				{
					effects['tour-div'].start("width", 0);
					$('tour-div').innerHTML = "";
				}
			}

			// When moving away from the dashboard page, make sure to remove it
			// It can interfere with other pages that have the same element id's
			if ((currentPage == 'dashboard') && (newPage != 'dashboard'))
			{
				effects['dashboard-box'].start("width", 0);
				$('dashboard-box').innerHTML = "";
			}

			if (newPage.contains("step") && (currentPage.contains("topic") || currentPage.contains("step")) || newPage.contains("directory-search") && currentPage.contains("directory-search"))
				softLoadPage(newPage, 'content-box-right');
			else if (newPage == 'dashboard' && $('login-box').innerHTML.contains("Hello"))
				loadPage(newPage, 'dashboard-box');
			else
				loadPage(newPage, 'content-box-right');
			mapTabHide();
		} else if (currentPage != "home") {
			if ($('index-main'))
			{
				hashHistory[hashHistory.length] = newhash.replace("#", "");
				resetLinks(this);
				newPage = newhash.replace("#", "");
				effects['content-box-right'].start("width", 0);
				mapTabHide();
				effects['dashboard-box'].start("width", 0);
				$('content-box-right').innerHTML = "";
				$('dashboard-box').innerHTML = "";
				if ($('tour-div'))
				{
					effects['tour-div'].start("width", 0);
					$('tour-div').innerHTML = "";
				}
				currentPage = loadingPage = "home";
				backgroundSwap();
				checkForAd();
				toggleFlash(1);
			}
		} else /* currentPage == "home" */ {
			toggleFlash(1);
		}
	}
	lastHash = newhash;
	if (newhash != '')
		if (pageloc && window.location.hash != newhash + ":" + pageloc)
			window.location.hash = newhash + ":" + pageloc;
		else if (!pageloc && window.location.hash != newhash)
			window.location.hash = newhash;
}

var isValidZipCode = function(zip)
{
	if (typeof zip !== "string")
		return false;
	if (zip.length != 5)
		return false;
	if (zip != zip.replace(/\D/g,''))
		return false;
	return true;
}
var zipcodeChanged = function(zip) {
   if (!isValidZipCode(zip))
		return;
	if (zipCode==zip)
		return;
	zipCode = zip;
	if (isValidZipCode(zipCode))
		$$(document.links).filter(function(el) {
			if (el.rel == "service")
				el.href = "/#directory-search|" + el.id.replace("-service", "") + "|" + zipCode;
		});
	if (isValidZipCode(zipCode))
		$$(document.getElementsByTagName("input")).filter(function(el) {
			if (el.id.replace("zipcode", "") != el.id || el.name.replace("zipcode", "") != el.name)
				el.value = zipCode;
		});
}

