/* 
 	. Created by Lis/2k1. rev# 8.2.2 : 14:26:19
	------------------------------------
	. (c) Multimedia Art, 2002
*/

// CVSinfo
// $Id: cPullDownMenuManager.js,v 1.4 2002/04/02 13:23:32 lis Exp $
// $Source: D:/cvsrepo/mres/Kroton/jsLib/cPullDownMenuManager.js,v $
// $Revision: 1.4 $

if (MA_BI.IsDOM)
{
	window.onresize = ReArrangePositions;
	window.onscroll = ReArrangePositions; // this property not exist in NN
}
else
{
	window.captureEvents( Event.MOUSEMOVE | Event.RESIZE );
	window.onmousemove = SaveMouseXY;
	// Fix NN bug:
	 window.onresize = function () { window.location.reload(); }
}

function LevelIs( ID ) { return(ID.split("x").length); }
function LevelUp( ID ) { return(ID.substr(0, ID.lastIndexOf("x"))); }


var onElement = "none";
var outElement = "none";
var functionTimeoutID = -1;

function Menu()
{
	this.mID = "id";
	this.mMenu = new Array();
	
	this.lm = new cLayerManager();
	// Align part
	this.mBaselineValue = 0;
	this.mOldBaselineValue = 0;
	this.mTotalWidth = 0;
	
	// Arrange level for incoming menu
	this.ArrangeLevels = ArrangeLevels;	this.ArrangeLevels( this.mMenu, 0 );
	this.ArrangePositions = ArrangePositions;
	this.ShowMenu = ShowMenu;
	this.Baseline = Baseline;
	
	this.LinkBehaviors = LinkBehaviors;
	this.CloseChilds = CloseChilds;
	this.CloseAll = Close;
	this.SelectiveClose = SelectiveClose;
	this.GetElementByID = GetElementByID;
	
	this.Init = function()
	{
		var j = 0;
		for(var i=1; this.lm.IsObjectExist( this.mID + i ); i++ )
		{
			if ( this.lm.IsObjectExist( this.mID + i + "x1" ) )
				this.mMenu[j++] = new SubMenu( this.mID + i);
			else
				this.mMenu[j++] = new MenuItem( this.mID + i);
		}
		
		this.mTotalWidth = CalculateWidth(this.mMenu);
		this.Baseline();
		this.ArrangePositions(); 
		this.LinkBehaviors(); 
		this.ShowMenu();
		
	}
	return( this.Init() );
}

function ArrangeLevels( hMenu, iLevel )
{
	for(var i=0; i<hMenu.length; i++)
	{
		hMenu[ i ].mLevel = iLevel;
		if ( typeof(hMenu[ i ].mAssets) != 'undefined' )
			this.ArrangeLevels( hMenu[ i ].mAssets, iLevel + 1 );
	}
}

function SubMenu( iID )
{
	this.mID = iID;
	this.mLevel = LevelIs( this.mID ) - 1; 
	
	this.mAssets = new Array(); // Menu memebers (or childs)
	
	this.lm = new cLayerManager( iID );
	this.mWidth = this.lm.GetSize().width;
	this.mHeight = this.lm.GetSize().height - MA_PDM_YBorder;
	
	// Functions
	this.ArrangePositions = ArrangePositions;
	this.LinkBehaviors = LinkBehaviors;
	this.CloseChilds = CloseChilds;
	this.SelectiveClose = SelectiveClose;
	this.Close = Close;
	
	this.Init = function()
	{
		var j = 0;
		for(var i=1; this.lm.IsObjectExist( this.mID + "x" + i ); i++ )
		{
			if ( this.lm.IsObjectExist( this.mID + "x" + i + "x1" ) )
				this.mAssets[j++] = new SubMenu( this.mID + "x" + i);
			else
				this.mAssets[j++] = new MenuItem( this.mID + "x" + i);
		}
		return( this );
	}
	
	return( this.Init() ); 
}

function MenuItem( iID )
{
	this.mID = iID;
	this.mLevel = LevelIs( this.mID ) - 1;
	this.lm = new cLayerManager( iID );
	this.mWidth = this.lm.GetSize().width;
	this.mHeight = this.lm.GetSize().height - MA_PDM_YBorder;
	
	this.LinkBehaviors = LinkBehaviors;
	this.CloseChilds = CloseChilds;
	this.SelectiveClose = SelectiveClose;
	this.Close = Close;
	
	return( this ); 
}

function GetElementByID( ID )
{
	ID = ID.substr(2);
	posIDs = ID.split("x");
	str = "MA_PDM.mMenu[" + posIDs[0]+"-1 ]";
	for(var i=1; i<posIDs.length; i++) str += ".mAssets["+posIDs[i]+"-1 ]"; 
	return(eval( str ));
}

function TotalHeight( iMenuE )
{
	var sum = 0;
	for(var i=0; i < iMenuE.mAssets.length; i++)
		sum += iMenuE.mAssets[ i ].mHeight;
	return( sum );
}

function TotalWidth( iMenuE )
{
	var maxW = 0;
	for(var i=0; i < iMenuE.mAssets.length; i++)
		if ( maxW < (i = iMenuE.mAssets[ i ].mWidth) ) maxW = i;
	return( maxW );
}

function CalculateWidth( iMenu )
{
	var result = 0;
	for (var i=0; i< iMenu.length; i++)	result += iMenu[i].mWidth;
	return(result);
}

function Baseline()
{
	this.mOldBaselineValue = this.mBaselineValue;
	
	if (MA_BI.GetMaxX() < this.mTotalWidth) 
	{
		this.mBaselineValue = 0;
		return;
	}
	
	switch (MA_PDM_BaselineType)
	{
		case 0:
			// No change baseline
			break;
		case 1: 
			// Align left
			this.mBaselineValue = 0;
			break;
		case 2: 
			// Align center
			this.mBaselineValue = Math.floor((MA_BI.GetMaxX() - this.mTotalWidth)/2);
			
			// Netscape holy shit bug!
			if (!MA_BI.IsDOM) this.mBaselineValue -= 10;
			// end bug correcton
			
			break;
		case 3:
			// Align right
			this.mBaselineValue = MA_BI.GetMaxX() - this.mTotalWidth;
			break;
		case 4: 
			// Custom
			//this.mBaselineValue = GetCustomBaseline(); NYI
			break;
	}
}


function GetXDeploymentStart( iParentE )
{
	//p.iParentE.lm.GetSize();
	return(1);
}
function GetYDeploymentDirection( iParentE )
{
	return(1);
}

function ArrangePositions()
{
	var i, eCss, p, iE, cHeight, xStart, yStart;
	if ( typeof(this.mMenu) != "undefined" )
	{

		for( i=0; i < this.mMenu.length; i++)
		{
			this.lm.SetObject( this.mMenu[i].mID );
			p = this.lm.GetPosition();
			var newXPos = p.left - this.mOldBaselineValue + this.mBaselineValue;
			this.lm.SetPosition(newXPos, p.top );
			if ( typeof(this.mMenu[i].mAssets) != "undefined" )
				this.mMenu[i].ArrangePositions();
		}
	}
	else if ( typeof(this.mAssets) != "undefined" )
	{

		this.lm.SetObject( this.mID )
		p = this.lm.GetRect();
		var clm = new cLayerManager();
				
		var subWidth = TotalWidth(this);
		var subHeight = TotalHeight(this);
		var scrollX = MA_BI.GetScrolledLeft();
		var scrollY = MA_BI.GetScrolledUp();
		var MaxX = MA_BI.GetMaxX() + scrollX - 10;
		var MaxY = MA_BI.GetMaxY() + scrollY - 10;
		var xZero = scrollX;
		var yZero = scrollY;
		var xOffset = 2;
		var yOffset = 2;

		cHeight = 0;
		if ( this.mLevel )
		{
			if ( (p.left + p.width + subWidth) < MaxX ){
				xStart = p.left + p.width - xOffset ;
			}else if( (p.left - subWidth) > xZero ){
				xStart = p.left - subWidth + xOffset;
			}else{
				xStart = p.left + p.width - xOffset;
			}
			
			if ((p.top + subHeight) < MaxY){
				yStart = p.top + yOffset;
			}else if( (p.top - subHeight) > yZero ){
				yStart = p.top + p.height - yOffset - subHeight;
			}else{
				yStart = p.top + yOffset
			}
		}
		else
		{
			switch( MA_PDM_DeployDirection )
			{
				case 0:
					if((p.left + subWidth) < MaxX ) {
						xStart = p.left;
					} else if( p.left + p.width - subWidth > yZero ) {
						xStart = p.left + p.width - subWidth;
					} else {
						xStart = p.left;
					}
					if ((p.top + p.height + subHeight) < MaxY) {
						yStart = p.top + p.height;
					} else if(p.top - subHeight > yZero) {
						yStart = p.top - subHeight;
					} else{
						yStart = p.top + p.height;
					}
					break;
				case 1:
					if((p.left + subWidth) < MaxX ) {
						xStart = p.left;
					} else if( p.left + p.width - subWidth > yZero) {
						xStart = p.left + p.width - subWidth;
					} else {
						xStart = p.left;
					}
					
					if (p.top - subHeight > yZero) {
						yStart = p.top - subHeight;
					} else if(p.top + p.height + subHeight < MaxY) {
						yStart = p.top + p.height;
					} else{
						yStart = p.top;
					}
					break;
				case 2:
					if(p.left + p.width + subWidth < MaxX) {
						xStart = p.left + p.width;
					} else if(p.left - subWidth > yZero) {
						xStart = p.left - subWidth;
					} else {
						xStart = p.left + p.width;
					}
					
					if (p.top + subHeight < MaxY) {
						yStart = p.top;
					} else if( p.top + p.height - subHeight > yZero ) {
						yStart = p.top + p.height - subHeight;
					} else{
						yStart = p.top;
					}
					break;
				case 3:
					if(p.left - subWidth > yZero) {
						xStart = p.left - subWidth;
					} else if(p.left + p.width + subWidth < MaxX) {
						xStart = p.left + p.width;
					} else {
						xStart = p.left - subWidth;
					}
					
					if (p.top + subHeight < MaxY) {
						yStart = p.top;
					} else if( p.top + p.height - subHeight > yZero ) {
						yStart = p.top + p.height;
					} else{
						yStart = p.top;
					}
					break;
			}
		}	
		for( i=0; i < this.mAssets.length; i++)
		{
			iE = this.mAssets[ i ];
			clm.SetObject( iE.mID );
			
			clm.SetPosition( xStart, yStart + cHeight);
			cHeight += iE.mHeight;
			
			if ( typeof(this.mAssets[ i ].mAssets) != 'undefined')
				this.mAssets[ i ].ArrangePositions();
		}
	}
	return;
}

function ReArrangePositions()
{
	if ( typeof(MA_PDM) != "undefined" )
	{
		MA_PDM.Baseline();
		MA_PDM.ArrangePositions();
	}
	else
	{
		setTimeout( "ReArrangePositions", 500 );
	}
}

function LinkBehaviors()
{
	var i;
	if ( typeof(this.mMenu) != 'undefined' )
	{
		for( i=0; i < this.mMenu.length; i++)
			this.mMenu[i].LinkBehaviors();
	}
	else if ( typeof(this.mAssets) != 'undefined')
	{
		LinkHandlers( this.mID);
		for( i=0; i < this.mAssets.length; i++)
		{
			LinkHandlers( this.mAssets[ i ].mID );
			if ( typeof(this.mAssets[ i ].mAssets) != 'undefined')
				this.mAssets[ i ].LinkBehaviors();
		}
	}
	else
	{
		LinkHandlers( this.mID );
	}
}

function LinkHandlers( ID )
{
	if( MA_BI.IsDOM )
	{
		var Obj = document.getElementById( ID );
		Obj.onmouseover = hOver;
		Obj.onmouseout = hOut;
	}
	else
	{
		eval("document." + ID + ".onmouseover = hOver;");
		eval("document." + ID + ".onmouseout = hOut;");
	}
}

function ShowPath( ID, iObj )
{
	if ( typeof(iObj) == "undefined" )
		iObj = MA_PDM;
		
	var lm = new cLayerManager();
	if ( iObj.mID.indexOf('x') >= 0 )
	{
		var iID = iObj.mID.substr( 0, iObj.mID.lastIndexOf('x') );
		lm.SetObject( iObj.mID );
		if ( ID.indexOf( iID ) == 0 ) lm.Show(); else lm.Hide();
	}
	
	var iChilds;
	
	if (typeof(iObj.mMenu) != "undefined")
		iChilds = iObj.mMenu;
	else if (typeof(iObj.mAssets) != "undefined")
		iChilds = iObj.mAssets;
	else
		return;
		
	for (var i=0; i<iChilds.length; i++)
		ShowPath( ID, iChilds[i] );
}

function hOver( ev )
{
	var ID;
	if( MA_BI.IsIE)
	{	
		
		ID = FindParentEvent(window.event.srcElement);
		if ( ID == "") return;
		window.event.cancelBubble = true;
	}
	else
	{
		if ( MA_BI.IsDOM )
		{
			ID = FindParentEvent(ev.target);
			if ( ID == "") return;
		}
		else
			ID = ev.target.id;
	}	
	onElement = ID;
	if ( functionTimeoutID  != -1 ) 
	{
		clearTimeout( functionTimeoutID );
		functionTimeoutID = -1;
	}
	ShowPath( onElement );
}

function FindParentEvent( obj )
{
	if( MA_BI.IsIE ) 
	{
		if (obj.tagName == "HTML") return("");
		if ( (obj.id.indexOf("id") == 0) && (obj.tagName == "DIV") ) return( obj.id );
		return( FindParentEvent( obj.parentElement ) );
	}
	else
	{
		if ( typeof(obj) == "undefined" 
			|| typeof(obj.tagName) == "undefined" 
			|| obj.tagName == "HTML") return("");
		if ( (obj.id.indexOf("id") == 0) && (obj.tagName == "DIV") ) return( obj.id );
		return( FindParentEvent( obj.offsetParent ) );
	}
}

function InBounds( iElementId )
{
	var p = new cLayerManager( iElementId ).GetRect();
	var result = (( p.left < mouseX ) && ((p.left + p.width) > mouseX )
		&& ( p.top < mouseY ) && ((p.top + p.height) > mouseY ))
	return( result );
}

function CheckedClose()
{
	if (( onElement == outElement) && !InBounds( onElement )) 
		MA_PDM.CloseAll();
	functionTimeoutID = -1;
}

function hOut( ev )
{
	var ID;
	if( MA_BI.IsIE)
	{
		if ((ID = FindParentEvent(window.event.srcElement)) == "") return;
		window.event.cancelBubble = true;
	}
	else
	{
		if ( MA_BI.IsDOM )
		{
			ID = FindParentEvent(ev.target);
			if (ID == "") return;
		}
		else
			ID = ev.target.id;
	}
	outElement = ID;
	if (onElement == outElement)
		functionTimeoutID = setTimeout( "CheckedClose()", 1000);
}


function CloseChilds()
{
	if ( typeof(this.mAssets) == 'undefined') return;
	for(var i=0; i < this.mAssets.length; i++)
			this.mAssets[ i ].Close();
}


function Close()
{
	var i;
	if ( typeof(this.mMenu) != "undefined" )
	{
		for( i=0; i<this.mMenu.length; i++)
			if ( typeof(this.mMenu[i].mAssets) != 'undefined') 
				this.mMenu[ i ].Close();
	}
	else if( typeof(this.mAssets) != "undefined" )
	{
		if ( this.mLevel )
			this.lm.Hide();
		
		for( i=0; i < this.mAssets.length; i++)
			this.mAssets[ i ].Close();
	}
	else
	{
		this.lm.Hide();
	}
}

function SelectiveClose( ID )
{
	var i;
	if ( typeof(this.mMenu) != 'undefined' )
	{
		for( i=0; i<this.mMenu.length; i++)
			if ( typeof(this.mMenu[i].mAssets) != 'undefined' ) 
				this.mMenu[ i ].SelectiveClose( ID );
	}
	else if ( typeof(this.mAssets) != 'undefined' )
	{
		if ( this.mLevel ) off(this.mID);
		for( i=0; i < this.mAssets.length; i++)
			this.mAssets[ i ].SelectiveClose( ID );
	}
	else
	{
		this.lm.Hide();
	}
}


function ShowMenu()
{
	for (var i=0; i<this.mMenu.length; i++ )
		this.lm.SetObject( this.mMenu[i].mID ).Show();
}
