var menuItems=new Array("Home", "About", "Location", "Scooters", "Service", "Contact");
var imageObjects = new Array();
var images = new Array("./img/common/bg.jpg", "./img/common/logo.jpg", "./img/common/magnify.gif",
		"./scooters/img/HY150T-36_sm.jpg", "./scooters/img/HY150T-36.jpg",
		"./scooters/img/HY150T-4_sm.jpg", "./scooters/img/HY150T-4.jpg",
		"./scooters/img/HY150T-4B_sm.jpg", "./scooters/img/HY150TB-4.jpg",
		"./scooters/img/HY150T-4BLOG_sm.jpg", "./scooters/img/HY150T-4BLOG.jpg",
		"./scooters/img/HY50T-10_F2_sm.jpg", "./scooters/img/HY50T-10_F2.jpg",
		"./scooters/img/HY50T-10_SY_sm.jpg", "./scooters/img/HY50T-10_SY.jpg",
		"./scooters/img/HY50T-3_FC2_sm.jpg", "./scooters/img/HY50T-3_FC2.jpg",
		"./scooters/img/HY50T-8SPORTY_sm.jpg", "./scooters/img/HY50T-8SPORTY.jpg",
		"./accessories/backpacks/img/EPO-L033.jpg", "./accessories/backpacks/img/EPO-L019.jpg",
		"./accessories/covers/img/170T.jpg", 
		"./accessories/helmets/flip_face_helmet_JX-A111_sm.jpg", "./accessories/helmets/flip_face_helmet_JX-A111.jpg",
		"./accessories/helmets/flip_face_helmet_JX-B213_sm.jpg", "./accessories/helmets/flip_face_helmet_JX-B213.jpg",
		"./accessories/helmets/flip_face_helmet_JX-B216_sm.jpg", "./accessories/helmets/flip_face_helmet_JX-B216.jpg"		
		);
var imageFileNames = new Array();

function printHeader(){
	var out =		
		"<table>" +
			"<tr>" +					
				"<td id=\"logoTd\" align=\"center\" width=\"477px\">" +	
					"<img src=\"img\/common\/logo.jpg\" height=\"77px\" width=\"300px\" alt=\"logo\"> <br>"+
					"<span id=\"motto\">&quot;Helping the Earth AND Your Wallet.&quot;<\/span>" +
				"<\/td>" +				
			"<\/tr>" +
			"<tr>" +
			"<td align=\"center\" valign=\"bottom\"><font size=\"4px\"><b><i>Phone:<\/i> 720-222-3421<\/b><\/font><\/td>" +
			"<\/tr>" +
		"<table>";	
	return out;
}

function mainTable () {	
	runJS("dimensions.js");
	var width = screen.width - (menuButtonWidth * 2) - (menuXoffset * 2) - 120;	
	var out = " <table id=\"mainTable\" name=\"mainTable\" style = " + 
		 "\"border: 1px solid black; " +
		 "width: " + width + "px; " +
		 "height: " + menuHeight + "px; " +
		 "caption-side:bottom; " +
		 "background-color:white; " +
		 "text-align:center\">";	
	alert("out = " + out);
	document.write(out);
}

function menu(pageName)	{	
	runJS("dimensions.js");
	alert("menu button height = " + menuButtonHeight + ", menuItems len = " + menuItems.length);
	var menuHeight = (menuButtonHeight * menuItems.length) + 107;
	alert("menu height = " + menuHeight);
	var out = "<div id=\"floatdiv\" style=\"" +
		"position:absolute; " +
		"width:" + (menuButtonWidth) + "px;height:" + menuHeight + "px;left:0px;top:0px;" + 
		"padding:0px;background:#0000FF;" +
		"border:1px solid #2266AA\">" +
		menuTable(pageName) +
		"<\/div>";
	alert(out);
	document.write(out);
}

function runJS(script_filename) {	
    var html_doc = document.getElementsByTagName('head').item(0);
    var js = document.createElement('script');
    js.setAttribute('language', 'javascript');
    js.setAttribute('type', 'text/javascript');
    js.setAttribute('src', script_filename);
    html_doc.appendChild(js);
    return false;
}

function menuTable(pageName) {
	runJS("dimensions.js");	
	var i = 0;
	var menuHeight = menuButtonHeight * (menuItems.length);	
	var out = "<table id=\"menuTable\" cellpadding=\"0px\" name=\"menuTable\" width=\"" + menuButtonWidth + "px\"" +
			" height=\"" + menuButtonHeight + "\">" +
		"<tr><td id=\"menuTitle\" name=\"menuTitle\" height=\"" + menuButtonHeight + "px\">" + "MENU<\/td><\/tr>";
		for(i = 0; i < menuItems.length; i++)	{
			alert("menuItem = " + menuItems[i]);
			var currentButtonFileName = getImageObject(menuItems[i].toLowerCase()) + "Green.gif";
			var otherButtonFileName =	getImageObject(menuItems[i].toLowerCase()) + "Red.gif";
			if(pageName.toLowerCase == menuItems[i].toLowerCase())
				buttonFile = currentButtonFileName;
			else
				buttonFile = otherButtonFileName;
			out += "<tr><td class=\"menuTd\" id=\"" + menuItems[i].toLowerCase() + "\" name=\"" + menuItems[i].toLowerCase() + "\">";
			alert("fname = " + buttonFile);			 
			out += "<A HREF=\"" + menuItems[i].toLowerCase() + ".html\">" + 
					"<img src=\"" + buttonFile	+										
					"\" height = \"" + menuButtonHeight + "px\"\/>"
			+ "<\/A>" + "<\/td>" + "<\/tr>";
		}
		out +=	"<\/table>";
		alert("out1 = " + out);
	return out;
}

function loadImages()	{
	if(imageObjects.length == 0)	{	
		var i = 0;
		var c = 0;
		for(i = 0; i < images.length; i++)	{			
			imageObjects[i] = new Image();
			imageObjects[i].src = images[i];	
			//alert("src = " + images[i].substring(images[i].lastIndexOf("/")+1));
		}
	}
}

function getImageObjectByFileName(fileName)	{	
	for(i = 0; i < imageObjects.length; i++)	{
		//alert("test02 " + imageObjects[i].src.substring( imageObjects[i].src.lastIndexOf("/")+1));
		if(imageObjects[i].src.substring( imageObjects[i].src.lastIndexOf("/")+1) == fileName)	{			
			return imageObjects[i];			
		}		
	}
	imageObjects[imageObjects] = new Image();
	imageObjects[imageObjects].src = findImagePathByFileName(fileName);	
	return imageObjects[imageObjects.length-1].src;
}

function findImagePathByFileName(fileName)	{
	for(i = 0; i < images.length; i++)	{
		if(images[i].substring(lastIndexOf("/")+1) == fileName)	{
			return images[i];
		}
		alert("Image " + fileName + " not found in images array in main.js.");
	}
	
}

function getImageName(imageUrl)	{
	return imageUrl.substr(imageUrl.lastIndexOf("/")+1, imageUrl.length);
}

function beginsWith(originalString, stringToSearch)	{
	if(originalString.length < stringToSearch.length ||
		strcmp(originalString.substr(0, stringToSearch.length), stringToSearch) != 0)
		return false;
	return true;
}

function endsWith(originalString, stringToSearch)	{
	if(originalString.length < stringToSearch.length ||
			strcmp( originalString.substr( (originalString.length - stringToSearch.length), originalString.length), stringToSearch) != 0) {	
		//alert("orStr = " + originalString + 
			//	", searchStr = " + stringToSearch + 
				//		", substr = " + originalString.substr((originalString.length - stringToSearch.length), originalString.length));
			return false;
	}
		return true;
}

function strcmp ( str1, str2 ) {
    return ( ( str1 == str2 ) ? 0 : ( ( str1 > str2 ) ? 1 : -1 ) );
}

function getImageObject(fileName)	{
	var i = 0;	
	for(i = 0; i < menuImageObjects.length; i++)	{		
		if(endsWith(menuImageObjects[i].src, fileName) )	{
			//alert("TEST2: " + getImageName(menuImageObjects[i].src) + ", " + fileName );			
			return menuImageObjects[i].src;
		}		
	}
	alert("File " + fileName + " is not found.");
	return null;	
}


