

var oldLink;

var oldSubLink;

//var oldLinkColor
//var DEFAULT_LINK_COLOR = "#17CCFA";
var DEFAULT_LINK_COLOR = "DarkBlue";
var SELECTED_LINK_COLOR = "black";

var DEFAULT_SUB_LINK_COLOR = "DarkBlue";
var SELECTED_SUB_LINK_COLOR = "blue";

//Default image for pages
var DEFAULT_PAGE_IMAGE = "walk.jpg";

var PAGE_IMAGES = new Object();
//image to use for each page, the left needs to match the name in the anchor on index page
PAGE_IMAGES["Welcome"] = DEFAULT_PAGE_IMAGE;
PAGE_IMAGES["About Us"] = "sam.jpg";
PAGE_IMAGES["Services"] = "blitz.jpg";
PAGE_IMAGES["Rates"] = "blitz_2.jpg";
PAGE_IMAGES["Contact Us"] = "nessa.jpg";

function setPageImage(pageName)
{
	var imageName = PAGE_IMAGES[pageName];
	if (!imageName || imageName == null)
	{
		imageName = DEFAULT_PAGE_IMAGE;
	}
	
	document.getElementById('pageTopImg').src = "images/"+imageName;
}

//This should be called from an anchor tag so that if the user doesn't have
//javascript enabled it will just display that page so it still works for them
function menuClick(link)
{
	//if browser should follow the link like a normal click
	var followLink = true;
	//alert("menuClick: link="+link+" window.top.framesDisabled="+window.top.framesDisabled);
	
	//hideRatesMenu();
	
	if (!link)
	{
		return followLink;
	}
	
	if (oldLink)
	{
		oldLink.style.color = DEFAULT_LINK_COLOR;
	}
	oldLink = link;
	link.style.color = SELECTED_LINK_COLOR;
	
	if (!window.top.framesDisabled)
	{
		var contentFrame = document.getElementById('contentFrame');
		if (contentFrame != null)
		{
			contentFrame.src=link.href + "?isframe=true";
			//content is loaded in frame, so don't follow the link
			followLink = false;
		}
	}
	else
	{
		//no frames, need to change to that URL instead of default which is to load in fram
		window.location.href = link.href + "?noFrames=true";
		followLink = false;
	}
	
	var newName = link.name;
	if (!newName | newName == "")
	{
		if (link.text)
		{
			newName = link.text;
		}
		else
		{
			//IE
			newName = link.innerHTML;
		}
	}
	//alert("menuClick: newName="+newName);
	
	//document.getElementById('pageName').textContent = newName;
	var pageName = document.getElementById('pageName');
	if (pageName.textContent)
	{
		pageName.textContent = newName;
	}
	else
	{
		//IE
		pageName.innerHTML = newName;
	}
	
	if (!window.top.framesDisabled)
	{
		//change the image for this page
		setPageImage(link.name);
		
		link.blur();
		
		adjustContentFrame();
		
		if (followLink == false && link.id && window.location.href.indexOf("?page=" + link.id) < 0)
		{
			var old = window.location.href;
			var sepIndex = old.indexOf("?");
			if (sepIndex > 0)
			{
				old = old.substring(0, sepIndex);
			}
			window.location.href = old + "?page="+link.id;
		}
	}
	
	//alert("menuClick: link="+link+" window.top.framesDisabled="+window.top.framesDisabled+" followLink="+followLink);
	
	//prevent default behaviour of going directly to the page
	return followLink;
}

function showPageByUrl(pageId)
{
	var ok = false;
	var contentFrame = document.getElementById('contentFrame');
	if (contentFrame != null)
	{
		contentFrame.src= pageId + ".html?isframe=true";
		ok = true;
	}
	
	adjustContentFrame();
	
	return ok;
}

function subMenuClick(link, newName)
{
	if (oldSubLink)
	{
		oldSubLink.style.color = DEFAULT_SUB_LINK_COLOR;
	}
	oldSubLink = link;
	link.style.color = SELECTED_SUB_LINK_COLOR;
	link.blur();
	
	
	if (newName && newName != null)
	{
		var pageName = document.getElementById('pageName');
		if (pageName.textContent)
		{
			pageName.textContent = newName;
		}
		else
		{
			//IE
			pageName.innerHTML = newName;
		}
	}
	
	return false;
}

function hideRatesMenu()
{
	//var ratesMenu = document.getElementById("ratesMenu");
//	ratesMenu.style.display = "none";
}

function showRatesMenu()
{
//	var ratesMenu = document.getElementById("ratesMenu");
//	//alert("showRatesMenu: ratesMenu="+ratesMenu);
//	ratesMenu.style.display = "";
//	
//	startSubMenuAnimation();
}

//var DEFAULT_CONTENT_FRAME_HEIGHT = 430;
var DEFAULT_CONTENT_FRAME_HEIGHT = 600;
var MIN_CONTENT_FRAME_HEIGHT = 300;
//This is the height of stuff above and under contentFrame
//TODO: calc from adding other stuff
var DEFAULT_OTHER_HEIGHT = 310;
var FOOTER_HEIGHT = 35;
var SPACING = 20;

//This should be called any time a new page is loaded
//or when window is resized
function adjustContentFrame()
{
	var contentFrame = document.getElementById('contentFrame');
	
	//alert("contentFrame="+contentFrame);
	
	if (contentFrame)
	{
		var newHeight = null;
		
		if (window.innerHeight)
		{
			newHeight = window.innerHeight;
		}
		else if (document.documentElement && document.documentElement.clientHeight)
		{
			newHeight = document.documentElement.clientHeight;
		}
		else if (document.body && document.body.clientHeight)
		{
			newHeight = document.body.clientHeight;
		}
		
		if (newHeight && (newHeight - DEFAULT_OTHER_HEIGHT) > MIN_CONTENT_FRAME_HEIGHT)
		{
			newHeight = newHeight - DEFAULT_OTHER_HEIGHT - SPACING;
		}
		else
		{
			newHeight = DEFAULT_CONTENT_FRAME_HEIGHT;
		}
		
		var newHeightPx = newHeight+"px";
		if (contentFrame.style.height != newHeightPx)
		{
			//alert("changing contentFrame height from "+contentFrame.style.height+" to "+newHeight);
			contentFrame.style.height = newHeightPx;
			
			var sepMenuContent = document.getElementById('sepMenuContent');
			if (sepMenuContent)
			{
				sepMenuContent.style.height = (newHeight + FOOTER_HEIGHT) + "px";
			}
			
			var page = document.getElementById('page');
			if (page)
			{
				page.style.height = (newHeight + DEFAULT_OTHER_HEIGHT - FOOTER_HEIGHT) + "px";
			}
			
			/*var shadow = document.getElementById('shadow');
			if (shadow)
			{
				shadow.style.height = (newHeight + DEFAULT_OTHER_HEIGHT - FOOTER_HEIGHT) + "px";
			}*/
		}
	}
}

var animationCount = 0;
//number of animation steps
var animationStop = 20;
//delay in milliseconds between animation steps
var animationStep = 25;
function startSubMenuAnimation()
{
	var subMenu = document.getElementById("subMenu");
	//subMenu.style.background = "yellow";
	subMenu.style.color = "red";
	
	animationCount = 0;
	animateSubMenu();
}

function animateSubMenu()
{
	animationCount++;
	
	var subMenu = document.getElementById("subMenu");
	subMenu.style.height = animationCount * 5 + "px";
	
	if (animationCount < animationStop)
	{
		//set timer for next step this
		self.setTimeout("animateSubMenu()", animationStep);
	}
	else
	{
		stopSubMenuAnimation();
	}
}

function stopSubMenuAnimation()
{
	var subMenu = document.getElementById("subMenu");
	subMenu.style.background = "";
	subMenu.style.height = "";
	
	animationCount = 0;
}

//variable for current animation
var animation;

function startAnimation(elementName, steps, delayMillisPerStep)
{
	//alert("elementName="+elementName+" steps="+steps+" delayMillisPerStep="+delayMillisPerStep+" animation="+animation);
	
	//stop old animation if any
	if (animation)
	{
		stopAnimation();
	}
	//new object for animation
	animation = new Object();
	//number of animation steps
	animation.steps = steps;
	//animation.color = "red";
	//animation.border = "medium solid red";
	animation.border = "medium solid lightBlue";
	//animation.border = "medium solid red";
	animation.borderHide = "medium solid white";
	
	animation.textColorFlash = "red";
	animation.textColorNormal = "DarkBlue";
	
	
	//step number counter
	animation.count = 0;
	animation.num = 0;
	//delay in milliseconds between animation steps
	animation.delayMillisPerStep = delayMillisPerStep;
	//find the element using its name
	animation.elementName = elementName;
	animation.element = document.getElementById(elementName);
	if (animation.element)
	{
		//animation.element.style.color = "red";
		//animation.element.style.border = animation.borderHide;
		animate();
	}
}

function animate()
{
	if (animation)
	{
		animation.count++;
	
		//animation.element.style.height = animation.count + "px";
		//animation.element.style.fontWeight = "bold";
		
		animation.num++;
		if (animation.num > 4) animation.num = 1;
//		if (animation.num == 1)
//		{
//			animation.element.style.borderLeft = animation.borderHide;
//			animation.element.style.borderTop = animation.border;
//		}
//		else if (animation.num == 2)
//		{
//			animation.element.style.borderTop = animation.borderHide;
//			animation.element.style.borderRight = animation.border;
//		}
//		else if (animation.num == 3)
//		{
//			animation.element.style.borderRight = animation.borderHide;
//			animation.element.style.borderBottom = animation.border;
//		}
//		else if (animation.num == 4)
//		{
//			animation.element.style.borderBottom = animation.borderHide;
//			animation.element.style.borderLeft = animation.border;
//		}
//		else
//		{
//			animation.num = 0;
//			animation.element.style.border = animation.borderHide;
//		}
//		
		if ((animation.count % 2) == 1)
		{
			animation.element.style.color = animation.textColorFlash;
		}
		else
		{
			animation.element.style.color = animation.textColorNormal;
		}
		
	
		if (animation.count < animation.steps)
		{
			//set timer for next step this
			self.setTimeout("animate()", animation.delayMillisPerStep);
		}
		else
		{
			stopAnimation();
		}
	}
}

function stopAnimation()
{
	if (animation)
	{
		animation.element.style.background = "";
		animation.element.style.height = "";
		animation.element.style.border = null;
		animation.element.style.color = null;
	
		animation = null;
	}
}


var initDone = false;
function init()
{
	//alert("init: starting  mainPageLoaded="+mainPageLoaded+" window.mainPageLoaded="+window.mainPageLoaded);
	
	//mainPageLoaded = true;
	
	var pageLoaded = false;
	var page = GetParam("page");
	if (page != null && page.length > 0)
	{
		var pageLink = document.getElementById(page);
		if (pageLink != null)
		{
			//alert("page="+page+" pageLink="+pageLink);
			menuClick(pageLink);
			pageLoaded = true;
		}
		else
		{
			pageLoaded = showPageByUrl(page); 
		}
	}
	
	if (pageLoaded == false && initDone == false
			&& !window.top.framesDisabled)
	{
		var welcomeLink = document.getElementById('welcome');
		if (!welcomeLink)
		{
			showPageByUrl("welcome");
		}
		else
		{
			menuClick(welcomeLink);
		}
	}
	
	//window.onresize = function() {adjustContentFrame();};
	window.onresize = adjustContentFrame;
	
	initDone = true;
	
	//startAnimation("lost", 20, 1000);
	startAnimation("lost", 20, 500);
	
	//alert("init: done page="+page+" mainPageLoaded="+mainPageLoaded);
	
	//googleStuff();
}

function googleStuff()
{
	var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
	document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));

	try {
	var pageTracker = _gat._getTracker("UA-11425510-1");
	pageTracker._trackPageview();
	} catch(err) {}
}

/*function menuClick(linkId)
{
	//alert("hi "+linkId);
	//var link = document.getElementById(linkId);
	//alert("link="+link.style.color);
	//link.style.color = 'red';
	//link.style.backgroundcolor = 'blue';
}*/


function buttonHover(e)
{
	//alert("hi "+e);

	if (e)
	{
		//e.src="images/"+e.id+"_hover.png";
		e.src="images/"+e.id+"_hover.png";
	}
}


function buttonUnHover(e)
{
	//alert("hi "+e);

	if (e)
	{
		//e.src="images/"+e.id+".png";
		e.src="images/"+e.id+".png";
	}
}


function buttonPressed(e)
{
	if (e)
	{
		e.src="images/"+e.id+"_pressed.png";
	}
}

function GetParam(name)
{
	if (name != null)
	{
		var urlAndQuery = window.location.href.split("?");
		
		//alert("href="+window.location.href+" urlAndQuery="+urlAndQuery+" len="+urlAndQuery.length);
		
		if (urlAndQuery.length > 1)
		{
			var query = urlAndQuery[1];
			var nameLower = name.toLowerCase();
			
			var params = query.split("&");
			
			//alert("query="+query+" nameLower="+nameLower+" params="+params+" len="+params.length);
			
			for (var i=0;i<params.length;i++)
			{
				var paramValue = params[i].split("=");
				if (paramValue.length == 2 && nameLower == paramValue[0])
				{
					return paramValue[1];
				}
			}
		}
	}
	
	return null;
}

