/*
Author:        Andrew Paul Simmons
Last Modified: 9.11.2006
Contact:       andrew-simmons@andrew-simmons.com
*/



window.onload = function()
{
	//Calls initializePage after the images
	// from the body blcok (the images required
	// for the page to be functional) are loaded.
	// uncomment this for image loader loadImages();
	// remove this for image loader
	initializePage();
}
initializePage = function()
{
	loadImages();
	initMenu();
	initIPageLinks();
	initContentFrame();
}
loadImages = function()
{
	var onLoadPreload = function()
	{
		// do nothing when the images are 
		// preloaded
	}
	new XMLImageLoader("indexImages.xml", 
							 {"preload":onLoadPreload});	
	/*		
	var onLoadHead = function()
	{
		var pageContent = document.getElementById("PageContainer");	
		pageContent.style.display = "block";
	}
	var onLoadBody = function()
	{
		var innerContent = document.getElementById("InnerContainer3");	
		innerContent.style.display = "block";
		var loadingAnimation = document.getElementById("BannerLoadingAnim");	
		loadingAnimation.style.display = "none";
		initializePage();

	}
	new XMLImageLoader("indexImages.xml", {"head":onLoadHead,"body":onLoadBody});	
	*/
}

initContentFrame = function()
{
	//Frame initialzed to have extra properties by
	// initializeFrame.js
 	this.contentFrame = document.getElementById("ContentFrame");

	
	this.contentFrame.onloadComplete = function()
	{

	}
   this.slideMenu.open();
   this.contentFrame.src = "iPages/home.html";
   this.slideMenu.open();	
}

initMenu = function()
{
	this.slideMenu = 
		new SlideMenu(document.getElementById("MenuContainer"),188);
		
	
	var path = "images/main/";
	var stateImage_urls =
		{
			"normalUp":     path + "Menu_Button.jpg",
			"normalDown":   path + "Menu_Button-MsDwn.jpg",
			"selectedUp":   path + "Menu_Button-Selected.jpg",
			"selectedDown": path + "Menu_Button-Selected-MsDwn.jpg"
		};
		
	this.menuButtons = 
		new MenuButtons(stateImage_urls,
							 Utils.createDelegate(this,this.loadContentFrame));
	
	this.menuButtons.add("home");
	this.menuButtons.setSelected("home");
	this.menuButtons.add("education");
	this.menuButtons.add("experience");
	this.menuButtons.add("contact");
	
}
function initIPageLinks()
{
	var makeIPageLink = function(link_id, pageName)
	{
		var iPageLink_el = document.getElementById(link_id);
		var loadContentFrame_func = this.loadContentFrame;
		iPageLink_el.onclick = function()
		{
			loadContentFrame_func(pageName)
		}
	}	
	makeIPageLink("TopLink1", "home");
	makeIPageLink("TopLink2", "education");
	makeIPageLink("TopLink3", "experience");
	makeIPageLink("TopLink4", "contact");
}

MenuButtons = function(stateImage_urls,defaultAction_func)
{
	/* 
		supported states:
	     normalUp, normalDown, selectedUp, selectedDown
	*/				
	this._stateImage_urls = stateImage_urls;	
	
	
	this._buttons = new Object();
	this.defaultAction_func = defaultAction_func;
}
MenuButtons.prototype.add = function(buttonId_str, buttonAction_func)
{
	this._buttons[buttonId_str] = new Object() 
	var p = this._buttons[buttonId_str];
	p._selected = false;
	p.documentElement = document.getElementById(buttonId_str);
	
	
	var b = p.documentElement;
	var toStateURL = this._stateImage_urls;
	var action_func = buttonAction_func || this.defaultAction_func;
	var setSelected_func = Utils.createDelegate(this,this.setSelected);
	p._mouseDown = false;
	
	b.onmousedown = function()
	{
		if(p._selected)
		{
			if(b.style.backgroundImage != "url("+ toStateURL["selectedDown"] +")")
			{
				b.style.backgroundImage = "url("+ toStateURL["selectedDown"] +")";
			}
		}
		else
		{
			if(b.style.backgroundImage != "url("+ toStateURL["normalDown"] +")")
			{
				b.style.backgroundImage = "url("+ toStateURL["normalDown"] +")";
			}
		}
			
		p._mousedown = true;
	}
	b.onmouseup = function()
	{
		
		p._mouseDown = false;
		if(!p._selected)action_func(buttonId_str);
		setSelected_func(buttonId_str);
	}
	
	b.onmouseout = function()
	{
		if(p._selected)
		{
			if(b.style.backgroundImage != "url("+ toStateURL["selectedUp"] +")")
			{
				b.style.backgroundImage = "url("+ toStateURL["selectedUp"] +")";
			}
		}
		else
		{
			if(b.style.backgroundImage != "url("+ toStateURL["normalUp"] +")")
			{
				b.style.backgroundImage = "url("+ toStateURL["normalUp"] +")";
			}
		}
			
		if(p._mousedown) 
			p._mousedown = false;
	}
	
	
}

MenuButtons.prototype.setSelected = function(buttonId_str)
{

	
	// make sure that all other buttons are no longer selected
	for(i in this._buttons)
	{

		if(i == buttonId_str) continue;
		this._buttons[i]._selected = false;
		if(this._buttons[i].documentElement.style.backgroundImage 
			!= "url("+ this._stateImage_urls["normalUp"] +")")
		{
			this._buttons[i].documentElement.style.backgroundImage = 
					"url("+ this._stateImage_urls["normalUp"] +")";
		}
	}
	if(this._buttons[buttonId_str].documentElement.style.backgroundImage != 
	           "url("+ this._stateImage_urls["selectedUp"] +")")
	{
	this._buttons[buttonId_str].documentElement.style.backgroundImage = 
	           "url("+ this._stateImage_urls["selectedUp"] +")";
	}
	this._buttons[buttonId_str]._selected = true;	
}

loadContentFrame = function(pageName)
{
	this.contentFrame.src = ("iPages/" + pageName + ".html");
}

/* Used for future flash interactive section */
startAnim2 = function()
{
	//Set how far we want to lower the content and menu bar area
	var expandDistance_num = 400; 
	
	/*
	  It is important to keep the document structure in mind here.
	  There are 3 nested divs of interest.  
	  
	  The first InnerContainer1 shows the left sidebar as a background and 
	  contains InnerContainer2. 
	  
	  InnerContainer2 shows the right side bar and contains InnerContainer3.
	  
	  InnerContainer3 holds the left menu and the right content area.
	  
	  The goal here is to lower InnerContaner3 and expand the height of 
	  InnerContainers 1 and 2 to accomidate the lowering of InnerContainer 3.
	 */
	  
	var leftSideBarContainer_div = document.getElementById("InnerContainer1");
	var rightSideBarContainer_div = document.getElementById("InnerContainer2");

	/*
		Here we need to calculate the initial height of InnerContainer2. However, 
		the height of the 3 InnerContainers is allowed to vary with the content.  
		So, the height is not a part of the style object. Hence, we must 
		use getElementPosition to get the position of this nonpositioned element.
		This is of no consequence because this calulation is only performed once
		before the animation begins.
		
		To find the height we subtract the height of InnerContainer1 from the height
		of the footer which sits directly below the bottom of InnerContainer1.
	*/
	var sideBarTop_num = Utils.getElementPosition(leftSideBarContainer_div).top;
	var footerTop_num = Utils.getElementPosition("Footer").top;
	var sideBarHeight_num = footerTop_num - sideBarTop_num;
	
	//It appears that the menu overlaps the bottom bar so we expand the side bars an iditional
	// few pixels.
	sideBarHeight_num += 4;
	
	var contentArea_div = document.getElementById("InnerContainer3");
	var contentYPos_num = 0;
	
	updateContentAreaPosition = function()
	{
		contentYPos_num += 1;
		sideBarHeight_num += 1;
		
		//expand the sidebars
		leftSideBarContainer_div.style.height = sideBarHeight_num + "px";
		rightSideBarContainer_div.style.height = sideBarHeight_num + "px";
		
		// move the content and menu down
		contentArea_div.style.top = contentYPos_num + "px";
		
		//stop when we are fully expanded
		if(contentYPos_num >= expandDistance_num)
		{
			clearInterval(intervalID);
		}
	}
	var intervalID = setInterval(updateContentAreaPosition,10,null);
}