/****************************************
 Dynamic Menu System: 
****************************************/

menuArray = new Array()

//menuItem Object Constructor
function menuItem(num1, num2, num3, title, url, target, end){
	this.num1 = num1
	this.num2 = num2
	this.num3 = num3
	this.title = title
	this.url = url
	if(target){
		this.target = target
	}
	if(end){
		this.end = end
	}
	
	return this
}

//Add menuItem to menuArray

function makeMenu(num1, num2, num3, title, url, target, end){
	obj = new menuItem(num1, num2, num3, title, url, target, end)
	menuItemCount = menuArray.length
	menuArray[menuItemCount] = obj
	if(obj.end){
		buildTable()
	}
}
/*******************************
IE 5.0 doesn't like the "push" method

function makeMenu(num1, num2, num3, title, url, target, end){
	obj = new menuItem(num1, num2, num3, title, url, target, end)
	menuArray.push(obj)
	if(obj.end){
		buildTable()
	}
}*/

// Get the current URL
function getURL(){
	var url = window.location
	url = url.toString()
	tempPath = "http://www.asianescapes.com"
	tempVar = tempPath.length;
	if(url.indexOf("www.asianescapes.com")){
		url = url.substring(tempVar, url.length)
	}
	return url
}

// Match the current URL to the URLs in the menu items
function matchURL(){
	currURL = getURL()
	for(i = 0; i < menuArray.length; i++){
		// match the current location to menuArray
		if(currURL == menuArray[i].url){
			saveMenu(menuArray[i].num1, menuArray[i].num2, menuArray[i].num3)
		}
	}		
}

// Build navigation - HTML Table
function buildTable(){
	menu=""
	matchURL()
	checkMenuState()
	topCount=-1
	subCount=-1
	sub2Count=-1
	menu+="<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\" width=\"200\">"
	for(i = 0; i < menuArray.length; i++){
		// Check for target
		target=""
		if(menuArray[i].target){
			target="target=\""+menuArray[i].target+"\""
		}		
		//////////////////////////////
		// Top level menu items //
		//////////////////////////////
		if(menuArray[i].num2 == -1){
			topCount++
			subCount=-1
			highlight=['','']
			if(level1 == topCount && level2 == -1 && level3 == -1)highlight = ['<b>', '</b>']
			
			// Check to see if this is the last item in the menu
			if(menuArray[i].end == 1){
				bgimg="images/level0_nochild.gif"
			}else{
				// Check to see if the next item is a Top level menu item
				if (menuArray[i+1].num2 == -1)bgimg="images/level0_nochild.gif"
					
				// Check to see if the next item is a Sub level menu item
				if (menuArray[i+1].num2 > -1)bgimg="images/level0_regular.gif"
				
				// Check to see if the current section is selected
				if (menuArray[i].num1 == level1 && menuArray[i+1].num2 > -1)bgimg="images/level0_round.gif"
			}
			menu+="\n  <tr>\n    <td width=\"200\" height=\"20\" background=\""+bgimg+"\"><div STYLE=\"padding:2px 5px 2px 10px\">"+highlight[0]+"<a href=\""+menuArray[i].url+"\" class=\"clSlideLinks\" onClick=\"saveMenu("+menuArray[i].num1+","+menuArray[i].num2+","+menuArray[i].num3+");\""+target+">"+menuArray[i].title+"</a>"+highlight[1]+"</div>\n    </td>\n  </tr>"
		}
		
		//////////////////////////////
		// Sub level menu items //
		/////////////////////////////
		if(menuArray[i].num1 == level1 && menuArray[i].num2 > -1 && menuArray[i].num3 == -1){
			subCount++
			sub2Count=-1
			highlight=['','']
			if(level1 == topCount && level2 == subCount && level3 == -1)highlight = ['<b>', '</b>']
						
			// Check to see if this is the last item in the menu
			if(menuArray[i].end == 1){
				bgimg="images/level1_regular.gif"
			}else{
			
				// Check to see if the next item is a Top level menu item
				if (menuArray[i+1].num2 == -1)bgimg="images/level1_regular.gif"
				
				// Check to see if the next item is a Sub level menu item
				if (menuArray[i+1].num2 > -1)bgimg="images/level1_regular.gif"
				
				// Check to see if the next item is a Sub2 level menu item
				if (menuArray[i+1].num3 > -1)bgimg="images/level1_sub.gif"
				
				// Check to see if the current section is selected
				if (menuArray[i].num1 == level1 && menuArray[i].num2 == level2 && menuArray[i+1].num3 > -1)bgimg="/images/level1_round2.gif"
			}
			menu+="\n  <tr>\n    <td width=\"200\" height=\"20\" background=\""+bgimg+"\"><div STYLE=\"padding:2px 5px 2px 15px\">"+highlight[0]+"<a href=\""+menuArray[i].url+"\" class=\"clSlideSubLinks\" onClick=\"saveMenu("+menuArray[i].num1+","+menuArray[i].num2+","+menuArray[i].num3+");\""+target+">"+menuArray[i].title+"</a>"+highlight[1]+"\n    </td>\n  </tr>"
		}
		
		///////////////////////////////
		// Sub2 level menu items //
		//////////////////////////////
		if(menuArray[i].num3 > -1 && menuArray[i].num1 == level1 && menuArray[i].num2 == level2){
			sub2Count++
			highlight=['', '']
			if(level1 == topCount && level2 == subCount && level3 == sub2Count)highlight=['<b>', '</b>']
			menu+="\n  <tr>\n    <td width=\"200\" height=\"20\" background=\"/images/level2_regular.gif\"><div STYLE=\"padding:2px 5px 2px 20px\">"+highlight[0]+"<a href=\""+menuArray[i].url+"\" class=\"clSlideSub2Links\" onClick=\"saveMenu("+menuArray[i].num1+","+menuArray[i].num2+","+menuArray[i].num3+");\""+target+">"+menuArray[i].title+"</a>"+highlight[1]+"\n    </td>\n  </tr>"
		}
	}
	menu+="\n</table>"
}

// Cookie functions
function SetCookie(sName, sValue){
	date = new Date()
	path="/"
	document.cookie = sName + "=" + escape(sValue)+"; path=" + path
}

function getCookie(sName){
	var aCookie = document.cookie.split("; ")
	for (var i=0; i < aCookie.length; i++){
		var aCrumb = aCookie[i].split("=")
		if (sName == aCrumb[0])
		return unescape(aCrumb[1])
	}
	return null
}

function killCookies(){
	path="/"
	document.cookie = "level1=-1; path="+path
	document.cookie = "level2=-1; path="+path
	document.cookie = "level3=-1; path="+path
}

// Save the menu state
function saveMenu(var1,var2,var3){
	SetCookie("level1",var1)
	SetCookie("level2",var2)
	SetCookie("level3",var3)
}

function checkMenuState(homePage){
	if(!getCookie("level1") || !getCookie("level2") || !getCookie("level3"))saveMenu(-1, -1, -1)
	level1=getCookie("level1")
	level2=getCookie("level2")
	level3=getCookie("level3")
}