//Version 1.4.1.20091030 (fix for safari overflow bug)
// Held over from Pool 2
function menuBar(menuBarName){
	/*********************************************************VARIABLES**/
	//	Menu Bar Display Variables (DivPlus style array)
	this.barStyle = {		width: '90%',
							left: '5%',
							height: 100,
							top: -102,
							//backgroundColor: [0,128,128],
							//backgroundImage: "bg.png",
							//html: "<img src='media/bgbig.png' width='100%' height='100' />",
							//backgroundColor: [0,128,128],
							//borderWidth: 2,
							//borderColor: [0,100,100],
							//opacity: .85,		//This is removed because IE's opacity filter hides any children of this div that are outside the div's area (the tab)
							zindex: 1000 };

	this.bgStyle = {		width: '90%',
							left: '5%',
							height: 100,
							top: -102,
							backgroundColor: [0,0,0],
							//backgroundImage: "bg.png",
							//html: "<img src='media/bgbig.png' width='100%' height='100' />",
							borderWidth: 2,
							borderColor: [128,128,128],
							opacity: .35,		//This is removed because IE's opacity filter hides any children of this div that are outside the div's area (the tab)
							zindex: 999 };

	//	Button Display Variables (DivPlus style array)
	this.buttonStyle = {	buttonsVertical: 4,				//	Note that these are not an actual DivPlus argument,
							buttonSpacing: 4,				//	but are just being stored in this array.  This is the
															//	number of buttons we want stacked in each column and
															//	the space between each button.
							height: 14,
							width: 83,
							backgroundColor: [0,180,180],
							container: menuBarName,
							textAlign: "center",
							fontSize: 12,
							lineHeight: 14,
							cursor: "pointer",
							borderWidth: 1,
							borderColor: [0,255,255],
							onmousedown: "this.returnFalse;",
							onmouseover: menuBarName+".buttons[this.id].setBackgroundColor([0,50,50]);"+menuBarName+".buttons[this.id].setColor([255,255,255]);",
							onmouseout: menuBarName+".buttons[this.id].setBackgroundColor([0,180,180]);"+menuBarName+".buttons[this.id].setColor([0,0,0]);",
							zindex: 2 };					//	top and left are assigned on button creation
	
	this.buttonClass = new Array();							//	buttonClass defines the special attributes given
															//	to a specific type of button.  If no buttonClass
															//	exists, the default will be used.  THIS OVERRIDES
															//	EVEN HARDCODED BUTTON PROPERTIES
    
    this.buttonClass.codePool={	backgroundColor: [217,99,217],
    							color: [178,230,255],
								borderColor: [255,115,255],
								//onmouseover: menuBarName+".buttons[this.id].setBackgroundColor([0, 61,204]);"+menuBarName+".buttons[this.id].setColor([255,255,255]);",
								//onmouseout: menuBarName+".buttons[this.id].setBackgroundColor([217,99,217]);"+menuBarName+".buttons[this.id].setColor([0,0,0]);"};			
								onmouseover: menuBarName+".buttons[this.id].setBackgroundColor([0, 61,204]);",
								onmouseout: menuBarName+".buttons[this.id].setBackgroundColor([217,99,217]);"};								
	this.buttonClass.pool = {	backgroundColor: [217,99,217],
								borderColor: [255,115,255],
								color: [255,255,156],
								//onmouseover: menuBarName+".buttons[this.id].setBackgroundColor([0,100,100]);"+menuBarName+".buttons[this.id].setColor([255,255,255]);",
								//onmouseout: menuBarName+".buttons[this.id].setBackgroundColor([217,99,217]);"+menuBarName+".buttons[this.id].setColor([0,0,0]);"};
								onmouseover: menuBarName+".buttons[this.id].setBackgroundColor([0,100,100]);",
								onmouseout: menuBarName+".buttons[this.id].setBackgroundColor([217,99,217]);"};
	this.buttonClass.add = {	backgroundColor: [0,180,180],
								borderColor: [0,255,255],
								onmouseover: menuBarName+".buttons[this.id].setBackgroundColor([0,100,100]);"+menuBarName+".buttons[this.id].setColor([255,255,255]);",
								onmouseout: menuBarName+".buttons[this.id].setBackgroundColor([0,180,180]);"+menuBarName+".buttons[this.id].setColor([0,0,0]);"};								
	this.buttonClass.nav = {	backgroundColor: [128,255,128],
								borderColor: [128,255,255],
								onmouseover: menuBarName+".buttons[this.id].setBackgroundColor([0,100,0]);"+menuBarName+".buttons[this.id].setColor([255,255,255]);",
								onmouseout: menuBarName+".buttons[this.id].setBackgroundColor([128,255,128]);"+menuBarName+".buttons[this.id].setColor([0,0,0]);"};
	this.buttonClass.help = {	backgroundColor: [255,180,180],
								borderColor: [255,255,180],
								onmouseover: menuBarName+".buttons[this.id].setBackgroundColor([100,50,0]);"+menuBarName+".buttons[this.id].setColor([255,255,255]);",
								onmouseout: menuBarName+".buttons[this.id].setBackgroundColor([255,180,180]);"+menuBarName+".buttons[this.id].setColor([0,0,0]);"};
	this.buttonClass.graph = {	backgroundColor: [255,215,153],
								borderColor: [255,215,0],
								onmouseover: menuBarName+".buttons[this.id].setBackgroundColor([102,102,0]);"+menuBarName+".buttons[this.id].setColor([255,255,255]);",
								onmouseout: menuBarName+".buttons[this.id].setBackgroundColor([255,215,153]);"+menuBarName+".buttons[this.id].setColor([0,0,0]);"};

	//	Button Headers Display Variables (DivPlus style array)
	this.buttonHeaderStyle = {  height: this.barStyle.height,
								width: 20,
								container: menuBarName,
								zindex: 1 };

																						
	//	Tab Display Variables (DivPlus style array)
	this.tabStyle = {		height: 24,
							width: 150,
							top: this.barStyle.height + 2,
							right: 50,
							//backgroundColor: [0,100,100],
							color: [255,255,255],
							container: menuBarName,
							textAlign: "center",
							fontSize: 16,
							lineHeight: 24,
							cursor: "pointer",
							html: "Options",
							//borderWidth: {left: 2, right: 2, bottom: 2},
							//borderColor: [0,100,100],
							onmousedown: "this.returnFalse;",
							onmouseover: menuBarName+".tabBG.setBackgroundColor([0,0,0]);"+menuBarName+".tab.setColor([255,255,255]);",
							onmouseout: menuBarName+".tabBG.setBackgroundColor([100,100,100]);"+menuBarName+".tab.setColor([255,255,255]);",
							onmouseup: "if("+menuBarName+".slideIndex==0) {"+menuBarName+".slideControl=setInterval('"+menuBarName+".slide()',10); "+menuBarName+".stopFish();}",
							zindex: -1 };

	//	Tab Display Variables (DivPlus style array)
	this.tabBGStyle = {		height: 24,
							width: 150,
							top: this.barStyle.height + 2,
							right: 50,
							backgroundColor: [100,100,100],
							opacity:  .35,
							color: [255,255,255],
							container: menuBarName,
							textAlign: "center",
							fontSize: 16,
							lineHeight: 24,
							cursor: "pointer",
							//html: "Open Menu",
							borderWidth: {left: 2, right: 2, bottom: 2},
							borderColor: [100,100,100],
							//onmousedown: "this.returnFalse;",
							//onmouseover: menuBarName+".tab.setBackgroundColor([0,50,50]);"+menuBarName+".tab.setColor([255,255,255]);",
							//onmouseout: menuBarName+".tab.setBackgroundColor([0,100,100]);"+menuBarName+".tab.setColor([255,255,255]);",
							//onmouseup: "if("+menuBarName+".slideIndex==0) {"+menuBarName+".slideControl=setInterval('"+menuBarName+".slide()',10); "+menuBarName+".stopFish();}",
							zindex: -2 };

							
	//  Slider Display Variables (DivPlus style array)
	this.sliderChannelStyle = {		height: 4,
									width: 220,
									top: 0,
									left: 0,
									borderWidth: 1,
									borderColor: [0,70,70],
									zindex: 1};
	
	this.sliderButtonStyle = {		height: 14,
									width: 10,
									top: (this.sliderChannelStyle.height / 2) - 14,
									left: 110,
									backgroundColor: [0,180,180],
									borderWidth: 1,
									borderColor: [0,255,255],
									dragger:{ dragConstraints: "h", dragLimits:{left: 0, right: this.sliderChannelStyle.width - 10}},
									onmouseup: "return false;",			//This is only a placeholder
									onmousedown: "this.returnFalse();", //This is not
									onmouseover: "this.style.backgroundColor = 'rgb(0,50,50)'",
									onmouseout: "this.style.backgroundColor = 'rgb(0,180,180)'",
									zindex: 2};
									
	//  Docked Item Display Variables (DivPlus style array)
	this.dockedStyle = {	top: this.barStyle.height,
							right: 0,						//This is only a placeholder
							height: 20,
							width: 120,
							backgroundColor: [0,120,120],
							color: [255,255,255],
							container: menuBarName,
							textAlign: "center",
							fontSize: 14,
							lineHeight: 20,
							cursor: "pointer",
							html: "Docked Item",
							borderWidth: {left: 2, bottom: 2},
							borderColor: [0,120,120],
							onmousedown: "this.returnFalse();",
							onmouseover: "this.style.backgroundColor='rgb(0,50,50)'; this.style.color='rgb(255,255,255)';",
							onmouseout: "this.style.backgroundColor='rgb(0,120,120)'; this.style.color='rgb(255,255,255)';",
							//onmouseup: "if("+menuBarName+".slideIndex==0) "+menuBarName+".slideControl=setInterval('"+menuBarName+".slide()',10);",
							onmouseup: "this.destroy()",
							zindex: 0 };
							
							
	//	Selector Display Variables (DivPlus style array)
	this.selectorStyle = {	height: 55,
							width: 240,
							top: 40,
							left: 20,
							container: menuBarName,
							fontSize: 12,
							overflow: "auto",
						//	lineHeight: 20,
							zindex: 1 };
	this.selectorButtonStyle = {	selectorOffset: 2,
									height: 14,
									width: 100,
									left: 20,
									top: 20,
									container: menuBarName,
									fontSize: 12,
									lineHeight: 14,
									zindex: 1,
									backgroundColor: [0,180,180],
									borderColor: [0,255,255],
									color: [0,0,0],
									borderWidth: 1,
									textAlign: "center",
									cursor: "pointer",
									html: "Add Criterion",
									onmouseover: menuBarName+".selectors[this.id].setBackgroundColor([0,50,50]);"+menuBarName+".selectors[this.id].setColor([255,255,255]);",
									onmouseout: menuBarName+".selectors[this.id].setBackgroundColor([0,180,180]);"+menuBarName+".selectors[this.id].setColor([0,0,0]);"};
									
	this.selectorClass = new Array();
	
	this.selectorButtonClass = new Array();

	//	Generic Display Variables (DivPlus style array)
	//	Since this is kind of a catch-all, it is expect that most properties will be defined in the supplemental
	//	DivPlus array passed to newGeneric.  This is intended for things like label text.
	this.genericStyle = {	height: 20,
							width: 200,
							container: menuBarName,
							color: [255,255,255],
							fontSize: 14,
							lineHeight: 20,
							zindex: 1 };

								
	//	Other Class Variables
	this.menuName = menuBarName;							//	Name of the menu bar
	this.buttons = new Array();								//	Stores the buttons
	this.buttons.helpText = new Array();					//	Stores help text associated with the buttons
	this.buttonNumber = 0;									//	Number of buttons
	this.buttonHeaders = new Array();						//	Stores the button headers
	this.buttonHeaderNumber = 0;							//	Number of button headers
	this.selectors = new Array();							//	Stores the selectors
	this.selectorNumber = 0;								//	Number of selectors
	this.generics = new Array();							//	Stores generic divs
	this.sliders = new Array();								//	Stores the sliders
	this.dockedPanels = new Array();						//	Stores the docked panels
	this.menuOpen = 0;										//	Is the menu open?
	this.slideSpeed = 5;									//	Number of pixels to move the menu on each iteration
	this.slideControl = "";									//	Placeholder for the sliding interval
	this.slideIndex = 0;									//	Current position of the menu slide
	
	
	/*********************************************************END VARIABLES**/
	
	//	Build the bar
	this.barStyle.divID = menuBarName;
	this.menu = new DivPlus(this.barStyle);
	this.menuBG = new DivPlus(this.bgStyle);
	this.tabStyle.divID = menuBarName+"tab";
	this.tab = new DivPlus(this.tabStyle);
	this.tab.element.returnFalse = function(){ return false; };
	this.tabBG = new DivPlus(this.tabBGStyle);
	this.tabBG.element.returnFalse = function(){ return false; };
	this.slidePositions = Math.line({	begin: {x:this.barStyle.left, y:this.barStyle.top}, 
										end: {x:this.barStyle.left, y: 0},
										steps: (this.barStyle.top * -1) / this.slideSpeed});
	if(browsers.saf) this.menu.setHeight(130);		//Added to fix Safari overflow bug
}

menuBar.prototype.newDockedPanel = function(panelId){
	var panel = panelManager[panelId];
	panel.style.display = "none";
	var currentDockedStyle = new Array();
	for(property in this.dockedStyle){
		currentDockedStyle[property] = this.dockedStyle[property];
	}
	currentDockedStyle.divID = "dockedPanel" + panel.panel.panelHandle.panelId;
	currentDockedStyle.html = panel.panel.panelTitle.innerHTML.substr(0,10);
	if(panel.panel.panelTitle.innerHTML.length > 10) currentDockedStyle.html += "...";
	currentDockedStyle.right = this.tabStyle.right + this.tabStyle.width + ((this.dockedPanels.length) * currentDockedStyle.width);
	var currentDocked = new DivPlus(currentDockedStyle);
	currentDocked.element.panel = panel;
	currentDocked.element.dockIndex = this.dockedPanels.length;
	currentDocked.element.menuBar = this;
	this.dockedPanels.push(currentDocked);
	
	currentDocked.element.returnFalse = function(){ return false; };
	
	currentDocked.element.destroy = function(){
		this.panel.style.display="block";
		this.style.display = "none";
		this.menuBar.dockedPanels.splice(this.dockIndex, 1);
		for(var i=0; i<this.menuBar.dockedPanels.length; i++){
			this.menuBar.dockedPanels[i].setRight(this.menuBar.tabStyle.right + this.menuBar.tabStyle.width + ((i) * parseInt(this.style.width)));
			this.menuBar.dockedPanels[i].element.dockIndex = i;
		}
	}
}

menuBar.prototype.newSlider = function(sliderName, sliderGranularity, initialValue, sliderFunction, sliderArray){
	var currentButtonStyle = new Array();
	for(property in this.sliderButtonStyle){
		currentButtonStyle[property] = this.sliderButtonStyle[property];
	}
	var currentChannelStyle = new Array();
	for(property in this.sliderChannelStyle){
		currentChannelStyle[property] = this.sliderChannelStyle[property];
	}
	if(defined(sliderArray.height)){
		currentButtonStyle.height = sliderArray.height;
		currentChannelStyle.height = Math.floor(sliderArray.height / 9);
	}
	if(defined(sliderArray.width)){
		currentChannelStyle.width = sliderArray.width;
		currentButtonStyle.width = Math.floor(sliderArray.width / sliderGranularity);
		currentButtonStyle.left = Math.floor((sliderArray.width / sliderGranularity) * initialValue);
	}
	currentButtonStyle.top = 0;
	currentButtonStyle.dragger.dragLimits.right = currentChannelStyle.width - currentButtonStyle.width;
	currentChannelStyle.top = Math.floor((currentButtonStyle.height / 2) - (currentChannelStyle.height / 2));
	currentChannelStyle.container = sliderName+"slider";
	currentButtonStyle.container = sliderName+"slider";
	
	var sliderContainer = new DivPlus({	position: 'relative',
										left: 50,
										height: currentButtonStyle.height,
										width: currentChannelStyle.width,
										divID: sliderName+"slider"});
	
										
	sliderContainer.sliderButton = new DivPlus(currentButtonStyle);
	sliderContainer.sliderChannel = new DivPlus(currentChannelStyle);
	sliderContainer.sliderValue = initialValue;
	
	sliderContainer.sliderButton.element.setValue = function(){
		sliderContainer.sliderValue = Math.floor(sliderContainer.sliderButton.getLeft() / (sliderContainer.getWidth()/sliderGranularity));
	}
	
	sliderContainer.sliderButton.element.returnFalse = function(){
		return false;
	}
	
	this.sliders[sliderName] = sliderContainer;
	
	sliderContainer.sliderButton.onmouseup = "this.setValue(); ";
	sliderContainer.sliderButton.onmouseup += sliderFunction;
	
	return sliderContainer;
}

menuBar.prototype.newButton = function(buttonName, buttonDescription, buttonFunction, buttonClass){
	var currentStyle = new Array();
	for(property in this.buttonStyle){
		currentStyle[property] = this.buttonStyle[property];
	}
	currentStyle.top = this.barStyle.height - (((currentStyle.height+currentStyle.buttonSpacing) * (1+((this.buttonNumber) % currentStyle.buttonsVertical))));
	this.buttonNumber++;
	currentStyle.right = ((currentStyle.width+currentStyle.buttonSpacing) * (Math.ceil(this.buttonNumber / currentStyle.buttonsVertical))) + (this.buttonHeaderStyle.width * this.buttonHeaderNumber) - currentStyle.width;
/* 		this.buttonNumber++;
	currentStyle.left = this.barStyle.width - ((currentStyle.width+currentStyle.buttonSpacing) * (Math.ceil(this.buttonNumber / currentStyle.buttonsVertical))) - (this.buttonHeaderStyle.width * this.buttonHeaderNumber);
 */	currentStyle.html = buttonName;
	currentStyle.onmouseup = buttonFunction;
	if(defined(buttonClass)){
		for(property in this.buttonClass[buttonClass]){
			currentStyle[property] = this.buttonClass[buttonClass][property];
		}
	}
	currentStyle.onmouseover = currentStyle.onmouseover + " " + this.menuName +".setHelp('"+ buttonName +"');"
	currentStyle.onmouseout = currentStyle.onmouseout + " " + this.menuName + ".clearHelp();";
    currentStyle.divID = buttonName;
	this.buttons[buttonName] = new DivPlus(currentStyle);
	this.buttons[buttonName].element.returnFalse = function(){ return false; };
	this.buttons.helpText[buttonName] = buttonDescription;
}

menuBar.prototype.newGeneric = function(genericName, genericArray){
	var currentStyle = new Array();
	for(property in this.genericStyle){
		currentStyle[property] = this.genericStyle[property];
	}
	for(property in genericArray){
		currentStyle[property] = genericArray[property];
	}
	currentStyle.divID = genericName;
	this.generics[genericName] = new DivPlus(currentStyle);
}

menuBar.prototype.newSelector = function(selectorName, selectorFunction, selectorClass, selectorButtonClass){
	var currentStyle = new Array();
	for(property in this.selectorStyle){
		currentStyle[property] = this.selectorStyle[property];
	}
	if(defined(selectorClass)){
		for(property in this.selectorClass){
			currentStyle[property] = this.selectorClass[property];
		}
	}
	var currentButtonStyle = new Array();
	for(property in this.selectorButtonStyle){
		currentButtonStyle[property] = this.selectorButtonStyle[property];
	}
	if(defined(selectorButtonClass)){
		for(property in this.selectorButtonClass){
			currentButtonStyle[property] = this.selectorButtonClass[property];
		}
	}
	currentStyle.top = (currentStyle.height * this.selectorNumber) + this.selectorStyle.top;
	currentStyle.html = "";
	currentButtonStyle.top = (currentStyle.height * this.selectorNumber) + this.selectorButtonStyle.top + this.selectorButtonStyle.selectorOffset;
	currentButtonStyle.onmouseup = selectorFunction;
	currentButtonStyle.onmousedown = "this.returnFalse();";
	currentButtonStyle.onmouseover += " " + this.menuName +".setHelp('"+ selectorName+"selector');"
	currentButtonStyle.onmouseout += " " + this.menuName + ".clearHelp();";

	currentStyle.divID = selectorName;
	this.selectors[selectorName] = new DivPlus(currentStyle);
	currentButtonStyle.divID = selectorName+"Button";
	this.selectors[selectorName + "Button"] = new DivPlus(currentButtonStyle);
	this.selectors[selectorName + "Button"].element.returnFalse = function(){ return false;};
	this.selectorNumber++;
	this.buttons.helpText[selectorName+"selector"] = "Add a filter rule";
}

menuBar.prototype.stopFish = function(){
/* 	if(defined(activeFish)){
		for(var i=0; i<activeFish.length; i++){
			activeFish[i].pathControl("off");
		}
	} */
}

menuBar.prototype.startFish = function(){
/* 	if(defined(activeFish)){
		for(var i=0; i<activeFish.length; i++){
			activeFish[i].pathControl("on", 145+i);
		}
	} */
}

menuBar.prototype.slide = function(){
	if(this.slideIndex == this.slidePositions.length){
		clearInterval(this.slideControl);
		this.slideIndex = 0;
		this.slidePositions.reverse();
		this.menuOpen = !this.menuOpen;
		if(this.menuOpen) {
			this.tab.setHTML("Close");
			this.menu.setTop(0);
			this.menuBG.setTop(0);
		} else {
			this.tab.setHTML("Options");
			this.menu.setTop(this.barStyle.top);
			this.menuBG.setTop(this.barStyle.top);
		}
		this.startFish();
	} else {
		this.menu.setTop(this.slidePositions[this.slideIndex].y);
		this.menuBG.setTop(this.slidePositions[this.slideIndex].y);
		this.slideIndex++;
	}
}

menuBar.prototype.columnBreak = function(){
	if((this.buttonNumber % this.buttonStyle.buttonsVertical) > 0){
		this.buttonNumber = this.buttonNumber + (this.buttonStyle.buttonsVertical-(this.buttonNumber % this.buttonStyle.buttonsVertical));
	}
}

menuBar.prototype.buttonHeader = function(imagePath){
	var currentStyle = new Array();
	this.buttonHeaderNumber++;
	for(property in this.buttonHeaderStyle){
		currentStyle[property] = this.buttonHeaderStyle[property];
	}	
	currentStyle.html = "<img src='"+imagePath+"' width='"+currentStyle.width+"' height='"+currentStyle.height+"' border='0'>";
	currentStyle.top = "0";
	currentStyle.right = ((this.buttonStyle.width + this.buttonStyle.buttonSpacing) * (Math.ceil(this.buttonNumber / this.buttonStyle.buttonsVertical))) + (this.buttonHeaderStyle.width * this.buttonHeaderNumber) - this.buttonHeaderStyle.width;
	currentStyle.divID = "buttonHeader"+this.buttonHeaderNumber;
	this.buttonHeaders["buttonHeader" + this.buttonHeaderNumber] = new DivPlus(currentStyle);
	this.columnBreak();
}

menuBar.prototype.setHelp = function(buttonName){
	this.generics.MenuHelpText.setHTML(this.buttons.helpText[buttonName]);
}

menuBar.prototype.clearHelp = function(){
	this.generics.MenuHelpText.setHTML("");
}
