/********************************************
	script: StatefulNavBar.js
	author: Keith J. Frank
	date: 10/20/05
	version: 2.0
	
	NOTES:
	Requires "blank.gif" (10x10 transparent gif) in image directory 
********************************************/
function StatefulNavBar(argsObj)
{//argsObj  {containerId: "existingDiv", parentId:"navBlock"}
	this.containerId = argsObj.containerId;
	this.container = document.getElementById(this.containerId);
	this.parentId = argsObj.parentId;
	this.parentDiv = document.createElement("div");
	this.parentDiv.setAttribute("id", this.parentId);
	this.parentDiv.style.position = "absolute";
	this.parentDiv.style.width = this.container.offsetWidth + "px";
	this.parentDiv.style.zIndex = 0;
	this.orientation = "horizontal";
	this.container.appendChild(this.parentDiv);
	//create buttonManager			
	this.buttonManager = new Array();
	this.buttonManager.selectedButton = null;
	this.buttonManager.selectButton = function(buttonObj)
	{
		var button = buttonObj
		for(var i = 0; i < button.manager.length; i++)
		{
			button.manager[i].baseImage.style.visibility = "visible";
			button.manager[i].textLayer.className = button.cssClass.base;
			button.manager[i].selected = false;
			document.body.style.cursor = "default";
		}
		
		button.baseImage.style.visibility = "hidden";
		button.textLayer.className =  button.cssClass.selected;
		button.trigger.style.cursor = "default";
		this.selectedButton = buttonObj;
		button.selected = true;
		button.action();
	}
}

StatefulNavBar.prototype.addButton = function(buttonObj)
{
	buttonObj.manager = this.buttonManager;// handle back to button Manager from button
	this.buttonManager[this.buttonManager.length] = buttonObj;
	this.parentDiv.appendChild(buttonObj.baseImage);
	this.parentDiv.appendChild(buttonObj.selectedImage);
	this.parentDiv.appendChild(buttonObj.textLayer);
	this.parentDiv.appendChild(buttonObj.trigger);
	if(this.orientation == "horizontal")
	{
		var left = 0;
		for(var i = 1; i < this.buttonManager.length; i++)
		{
			left += parseInt(this.buttonManager[i-1].width);
		}
			
		buttonObj.setLeft(left);
		
	}
	else if(this.orientation == "vertical")
	{
		var top = 0;
		for(var i = 1; i < this.buttonManager.length; i++)
		{
			top += parseInt(this.buttonManager[i-1].height);
		}	
		buttonObj.setTop(top);
	}
	
}

StatefulNavBar.prototype.setOrientation = function(orientation)
{//orientation  "horizontal" or "vertical"
//instanceName.setOrientation("vertical");
	this.orientation = orientation;
}

StatefulNavBar.prototype.getOrientation = function()
{
	return this.orientation;
}
	

function Button(argsObj)
{//argsObj   {frame:"", image:{path:"", name:"", type:""}, cssClass:"", textAlign:"", height:"", width:"", text:"", action:function}
	this.selected = false;
	this.iWindow = argsObj.frame;//required param for IE 5 on Mac REMOVE when no longer supported
	this.height = argsObj.height;
	this.width = argsObj.width;
	this.text = argsObj.text;
	this.textAlign = (argsObj.textAlign) ? argsObj.textAlign : "center" ;
	this.image = argsObj.image;
	this.cssClass = new Object(); 
	this.cssClass.base = argsObj.cssClass;
	this.cssClass.selected = this.cssClass.base + "Selected";
	this.action = argsObj.action;
	this.actionParam = argsObj.actionParam;
						
	this.baseImage = document.createElement("img");
	this.baseImage.setAttribute("src", this.image.path + this.image.name + "." + this.image.type);
	this.baseImage.setAttribute("height", this.height);
	this.baseImage.setAttribute("width", this.width);
	this.baseImage.setAttribute("border", "0");
	this.baseImage.style.position ="absolute";
	this.baseImage.style.visibility ="visible";
	this.baseImage.style.display = "block";
	this.baseImage.style.margin = "0px";
	this.baseImage.style.zIndex = 1;
			
	this.selectedImage = document.createElement("img");
	this.selectedImage.setAttribute("src", this.image.path + this.image.name + "_selected." + this.image.type);
	this.selectedImage.setAttribute("height", this.height);
	this.selectedImage.setAttribute("width", this.width);
	this.selectedImage.setAttribute("border", "0");
	this.selectedImage.style.position ="absolute";
	this.selectedImage.style.visibility ="visible";
	this.selectedImage.style.display = "block";
	this.selectedImage.style.margin = "0px";
	this.selectedImage.style.zIndex = 0;
	
	this.textLayer = document.createElement("div");
	this.textLayer.className = this.cssClass.base;
	this.textLayer.style.position ="absolute";
	this.textLayer.style.visibility ="visible";
	this.textLayer.innerHTML = this.text;
	this.textLayer.style.width = this.width + "px";
	this.textLayer.style.zIndex = 2;
	this.textLayer.style.backgroundColor = "transparent";
	this.textLayer.style.textAlign = this.textAlign;
	
	this.trigger = document.createElement("img");
	this.trigger.setAttribute("src", this.image.path + "blank.gif");
	this.trigger.style.position ="absolute";
	this.trigger.style.visibility ="visible";
	this.trigger.style.display = "block";
	this.trigger.style.height = this.height + "px";
	this.trigger.style.width = this.width + "px";
	this.trigger.style.zIndex = 3;
	this.trigger.button = this; //need handle back to button obj from trigger
	
	
	this.trigger.onmouseover = function()
	{
		if(!this.button.selected)
		{
			this.button.baseImage.style.visibility = "hidden";
			this.button.textLayer.className =  this.button.cssClass.selected;
			//need to catch error thrown by IE 5.5 and set cursor to "hand"
			try
			{
				this.button.trigger.style.cursor = "pointer";
			}
			catch(e)
			{
				this.button.trigger.style.cursor = "hand";
			}
		}
	} 
	
	this.trigger.onmouseout = function()
	{
		if(!this.button.selected)
		{
			this.button.baseImage.style.visibility = "visible";
			this.button.textLayer.className = this.button.cssClass.base;
			this.button.trigger.style.cursor = "default";
		}
	}
	
	this.trigger.onclick = function()
	{
		if(!this.button.selected) this.button.manager.selectButton(this.button);
	}
}

Button.prototype.setLeft = function(leftInt)
{
	this.left = leftInt + "px";
	this.baseImage.style.left = this.left;
	this.selectedImage.style.left = this.left;
	this.textLayer.style.left = this.left;
	this.trigger.style.left = this.left;
}

Button.prototype.getLeft = function()
{
	return parseInt(this.left);
}

Button.prototype.setTop = function(topInt)
{
	this.top = topInt + "px";
	this.baseImage.style.top = this.top;
	this.selectedImage.style.top = this.top;
	this.textLayer.style.top = this.top;
	this.trigger.style.top = this.top;
}

Button.prototype.getTop = function(topInt)
{
	return parseInt(this.top);
}
