//***********************************************************************************
//* Class:		Submenu for mo2i.nl													*
//* Goal:		Represents one Submenu object which is controlled via the global	*
//*				Submenuus object or if ne											*
//***********************************************************************************
//* Created:	Frank (Chevalier Media), 2004										*
//***********************************************************************************

function Submenu(intMainId, strId, arrSubs, intActiveSub)
{
	this.intMainId		= intMainId;
	this.strId			= strId;
	this.strHTML		= "";
	this.arrSubs		= arrSubs;
	this.intActiveSub	= intActiveSub;
	this.blnCreated		= false;

	//scrollvars
	this.intSpeedAdd	= 20;//msec added per subitem
	this.intSpeed		= 250+this.countSubs()*this.intSpeedAdd;//msec offset, menu collapse duration

	this.intFPS			= 100;
	this.intHeight		= 0;
	this.intStartY		= 0;
	this.intStarttime	= 0;
	this.intDiffY		= 0;

	this.intPausehiding	= 200;//how long to wait before really hiding the menu.
	this.objInterval;
	this.blnShowing		= false;
	this.blnActive		= false;
	this.blnHiding		= false;

	SUBMENUUS.addSubmenu(this);

	return this;
}

//**************************************************************************************************************//
//*	User editable, for different designs and actions
//**************************************************************************************************************//

Submenu.prototype.create = function(strParentDiv, strStyle)
{
	//header
	this.strHTML	= "<div id=\"submenu_"+ this.getId() +"\" name=\"submenu_"+ this.getId() +"\"  class=\"SUBMENUholder\" ";
	this.strHTML	+="style=\"position:absolute;visibility:hidden;";

	//styles
	this.addStyles(strStyle);
	this.strHTML	+="\">";

	//white line
	/*
	this.strHTML	+="<div id=\"submenu_"+ this.getId() +"_white\" style=\"position:absolute;z-index:3;\">";
	this.strHTML	+="<table width=\"100%\" cellpadding=\"0\" cellspacing=\"0\" border=\"0\">";
	this.strHTML	+="<tr><td width=\"100%\" class=\"SUBMENUwhite\"><img src=\"img\/shim.gif\" width=\"1\" height=\"1\" vspace=\"0\">";
	this.strHTML	+="<\/td><\/tr><\/table><\/div>";
	*/

	//subs
	this.strHTML	+="<div id=\"submenu_"+ this.getId() +"_menu\" class=\"SUBMENUschuif\" style=\"position:relative;top:0px;left:0px;z-index:2;\" onmouseover=\"SUBMENUUS.showSubmenu('"+ this.getId() +"')\" onmouseout=\"SUBMENUUS.hideSubmenu('"+ this.getId() +"')\">";


	var c=0;
	while(c<this.countSubs())
	{
		var strLinkClass = "SUBMENUlink";
		if(this.intActiveSub == this.arrSubs[c][0]){strLinkClass = strLinkClass + "Active";}


		this.strHTML +="<label class=\"SUBMENUlabel\"><a href=\"index.php?fId="+ this.arrSubs[c][0] +"\" target=\"_self\" class=\""+ strLinkClass +"\" onfocus=\"this.blur();\" onclick=\"SUBMENUUS.getSubmenu('"+ this.getId() +"').activate('"+ this.getMainId() +"000');\">"+ this.arrSubs[c][1] +"<\/a><\/label>";
		c++;
	}

	//footer
	this.strHTML	+="<\/div><\/div>";

	//fill HTML
	document.getElementById(strParentDiv).innerHTML	= document.getElementById(strParentDiv).innerHTML + this.strHTML;

	//Set start values
	this.startUp();

	//done
	this.blnCreated	= true;
}

Submenu.prototype.activate = function(strValue){
/*
	if(parent.mainmenu.activateClick)
	{
		parent.mainmenu.activateClick(parseInt(strValue));
	}
*/
}

//**************************************************************************************************************//
//*	End - User editable
//**************************************************************************************************************//


Submenu.prototype.addStyles = function(strStyle)
{
	var arrStyles	 = strStyle.split(",");
	var s=0;
	while(s < arrStyles.length)
	{
		var arrStyle = arrStyles[s].split("=");
		if(arrStyle.length == 2)
		{
			switch(arrStyle[0])
			{
				case "width":
					this.strHTML += "min-width:"+ arrStyle[1] +"px;";
					break;
				case "left":
					this.strHTML += "left:"+ arrStyle[1] +"px;";
					break;
				case "top":
					this.strHTML += "top:"+ arrStyle[1] +"px;";
					break;
			}
		}
		s++;
	}
}

Submenu.prototype.getId = function(){return this.strId;}
Submenu.prototype.getMainId = function(){return this.intMainId;}
Submenu.prototype.countSubs = function(){return this.arrSubs.length;}
Submenu.prototype.getObjSubmenu = function(){return document.getElementById("submenu_"+ this.getId());}
Submenu.prototype.isCreated = function(){return this.blnCreated;}
Submenu.prototype.toggleSubmenu = function(strToggle){document.getElementById("submenu_"+ this.getId()).style.visibility=strToggle;}
Submenu.prototype.getObjMenu = function(){return document.getElementById("submenu_"+ this.getId() +"_menu");}

Submenu.prototype.setPausehiding = function(intValue){this.intPausehiding = intValue;}
Submenu.prototype.getPausehiding = function(){return this.intPausehiding;}


Submenu.prototype.startUp = function()
{
	//calculate startY and Height
	var objMenu			= this.getObjMenu();
	this.intHeight		= objMenu.offsetHeight;
	this.intDiffY		= this.intHeight;
	this.intStartY		= parseInt(objMenu.style.top) - this.intDiffY;
	objMenu.style.top	= this.intStartY + "px";
	//this.showMenu();
}

//show
Submenu.prototype.showMenu = function()
{
	if(this.blnActive && !this.blnShowing)
	{
		//determine start vars
		if(typeof(this.objInterval) != "undefined" && this.objInterval != -1){
			self.clearInterval(this.objInterval);
			this.objInterval = -1;
		}

		this.intStarttime	= new Date();
		this.intStarttime	= this.intStarttime.getTime() - parseInt(Math.abs((parseInt(this.getObjMenu().style.top)-this.intStartY)/this.intDiffY) * this.intSpeed);

		this.blnShowing 	= true;
		this.blnHiding 		= false;

		this.toggleSubmenu("visible");
		this.objInterval	= self.setInterval('SUBMENUUS.getSubmenu("'+ this.getId() +'").moveMenu()', parseInt(1000/this.intFPS));
	}
}

//hide
Submenu.prototype.hideMenu = function()
{
	if(!this.blnActive && !this.blnHiding)
	{
		//determine start vars
		if(typeof(this.objInterval) != "undefined" && this.objInterval != -1){
			self.clearInterval(this.objInterval);
			this.objInterval = -1;
		}

		this.intStarttime	= new Date();
		this.intStarttime	= this.intStarttime.getTime() - parseInt(Math.abs((parseInt(this.getObjMenu().style.top)-(this.intStartY+this.intDiffY))/this.intDiffY) * this.intSpeed);

		this.blnHiding 		= true;
		this.blnShowing 	= false;

		this.objInterval	= self.setInterval('SUBMENUUS.getSubmenu("'+ this.getId() +'").moveMenu()', parseInt(1000/this.intFPS));
	}
}

//move
Submenu.prototype.moveMenu = function()
{
	var dateNow		= new Date();
	var intDiffMS	= dateNow.getTime() - this.intStarttime;
	var intNewY		= parseInt(this.getObjMenu().style.top);

	if(intDiffMS >= this.intSpeed)
	{
		if(this.blnShowing){intNewY=this.intStartY+this.intDiffY;}
		else if(this.blnHiding){intNewY=(this.intStartY);this.toggleSubmenu("hidden");}
		self.clearInterval(this.objInterval);
	}else{
		if(this.blnHiding){intDiffMS=this.intSpeed-intDiffMS;}
		intNewY = this.easeOutQuad(intDiffMS, this.intStartY, this.intDiffY, this.intSpeed);
	}
	this.getObjMenu().style.top = intNewY +"px";
}



	//***********************************************************************************
	//* Easing Functions by Robert Penner:												*
	//* Version:	Easing Equations v1.5												*
	//*				May 1, 2003															*
	//***********************************************************************************

		Submenu.prototype.easeOutQuad = function (t, b, c, d) {
			return -c *(t/=d)*(t-2) + b;
		}

	//***********************************************************************************
	//* Created:	(c) 2003 Robert Penner, all rights reserved.						*
	//*				This work is subject to the terms in								*
	//*				http://www.robertpenner.com/easing_terms_of_use.html.				*
	//***********************************************************************************



//***********************************************************************************
//* Class:		SubmenuContainer for mo2i.nl										*
//* Goal:		Represents a container for all Submenu instances in a webpage.		*
//*				Submenu instances are controlled via this container					*
//***********************************************************************************
//* Created:	Frank (Chevalier Media), 2004										*
//***********************************************************************************

function SubmenuContainer()
{
	this.arrSubmenuus	= new Array();
	return this;
}

SubmenuContainer.prototype.addSubmenu = function(objSubmenu)
{
	if(!this.arrSubmenuus[objSubmenu.getId()])
	{
		this.arrSubmenuus[objSubmenu.getId()]	= objSubmenu;
	}else{
		alert("Submenu with ID '"+ objSubmenu.getId() +"' already exists!");
	}
}

SubmenuContainer.prototype.getSubmenu = function(strSubmenuId)
{
	if(this.arrSubmenuus[strSubmenuId])
	{
		return this.arrSubmenuus[strSubmenuId];
	}else{
		alert("Submenu with ID '"+ objSubmenu.getId() +"' doesn't exist!");
		return null;
	}
}

SubmenuContainer.prototype.showSubmenu = function(strSubmenuId)
{
	var objSubmenu;
	objSubmenu	= this.getSubmenu(strSubmenuId);
	if((objSubmenu) && (objSubmenu.isCreated()))
	{
		objSubmenu.blnActive	= true;
		objSubmenu.showMenu();
	}
}

SubmenuContainer.prototype.hideSubmenu = function(strSubmenuId)
{
	var objSubmenu;
	objSubmenu	= this.getSubmenu(strSubmenuId);
	if((objSubmenu) && (objSubmenu.isCreated()))
	{
		objSubmenu.blnActive	= false;
		setTimeout("SUBMENUUS.getSubmenu('"+ strSubmenuId +"').hideMenu()",SUBMENUUS.getSubmenu(strSubmenuId).getPausehiding());//to prevent immediate hiding, like the Windows Start menu
	}
}

//***********************************************************************************
//* Goal:		Create the global container instance								*
//***********************************************************************************
var SUBMENUUS	= new SubmenuContainer();
