/* 
	. Created by Lis/2k1. rev# 8.2.2 : 14:26:19
	------------------------------------
	. (c) Multimedia Art, 2002
*/

// CVSinfo
// $Id: cPullDownMenuManager.js,v 1.19 2003/08/13 13:08:07 lis Exp $
// $Revision: 1.19 $
// $Name:  $

if (MA_BI.IsIE || MA_BI.IsDOM) {
	window.onresize = ReArrangePositions;
	window.onscroll = ReArrangePositions; // this property not exist in NN
}

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;

// Deploy direction
var PD_DEPLOY_DOWN 	= 0,
	PD_DEPLOY_UP 	= 1,
	PD_DEPLOY_RIGHT	= 2,
	PD_DEPLOY_LEFT	= 3,
	
	PD_ALIGN_NO = 0,
	PD_ALIGN_LEFT = 1,
	PD_ALIGN_CENTER = 2,
	PD_ALIGN_RIGHT = 3,
	PD_ALIGN_USER = 4;

function Menu() {
	this.mID = "id";
	this.mMenu = new Array();
	
	this.lm = new cLayerManager();

	// hilite styles 
	this.mStyledPathEnable = false;
	
	// 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.CloseAll = Close;
	this.GetElementByID = GetElementByID;

	this.Init = function()
	{
		for(var i=1; this.lm.IsObjectExist( this.mID + i ); i++ )
		{
			if ( this.lm.IsObjectExist( this.mID + i + "x1" ) )
				this.mMenu.push( new SubMenu( this.mID + i ) );
			else
				this.mMenu.push( new MenuItem( this.mID + i ) );
		}

		// search for hilited styles only for IE
		this.mStyledPathEnable = false;
		var re = /\.(MenuItem|MainItem)-hilite/i
		if ( MA_BI.IsDOM ) {
			var sSheets = document.styleSheets;
			for( var i=0; i < sSheets.length; i++ ) {
				if ( !/.*\/PDMenuStyle(DOM|IE)\.css/.test(sSheets[i].href) )
					continue;
				
				// Is access in IE denied??
				if ( MA_BI.IsIE && typeof(sSheets[i].rules) == "unknown") {
					break;
				}
				
				styleSheetRules = ( MA_BI.IsIE ) ? sSheets[i].rules : sSheets[i].cssRules;
				for( var j=0; j < styleSheetRules.length; j++ ) {
					if ( re.test(styleSheetRules[j].selectorText) ) {
						this.mStyledPathEnable = true;
						break;
					}
				}
			}
		}
		
		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.Close = Close;
	
	this.Init = function()
	{
		for(var i=1; this.lm.IsObjectExist( this.mID + "x" + i ); i++ )
		{
			if ( this.lm.IsObjectExist( this.mID + "x" + i + "x1" ) )
				this.mAssets.push( new SubMenu( this.mID + "x" + i) );
			else
				this.mAssets.push( 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 values will be additional corrected in Init function 
	this.mWidth = this.lm.GetSize().width;
	this.mHeight = this.lm.GetSize().height - MA_PDM_YBorder;
	
	this.LinkBehaviors = LinkBehaviors;
	this.Close = Close;

	this.Init = function () {
		// Correct div sizes for DOM browsers
		if (MA_BI.IsDOM) {
			// Search for TABLE element and gets his size.
			var els = this.lm.Obj.getElementsByTagName( "TABLE" );
			// get first (must be only one TABLE element in DIV tag) and gets his width
			if ( els.length >= 1 ) { 
				var ch = els.item(0).offsetHeight - MA_PDM_YBorder + 1;
				var cw = els.item(0).offsetWidth;
				if (ch > this.mHeight) {
					this.mHeight = ch;
				}
				if (cw > this.mHeight ) {
					this.mWidth = cw;
				}
				// Correct div width
				this.lm.SetSize( this.mWidth, this.mHeight );
			}
		}
	}
	this.Init();
	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 );
}

// calculates maxiuim width for vertivally dropped menus
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 )
{
	// just get last elements pos and width and substruct fist x
	var first = iMenu[0].lm;
	var last = iMenu[iMenu.length-1].lm;
	
	var result = last.GetPosition().left + last.GetSize().width - first.GetPosition().left; 
	return(result);
}

function Baseline()
{
	this.mOldBaselineValue = this.mBaselineValue;
	switch (MA_PDM_BaselineType)
	{
		case PD_ALIGN_NO:
			break;
			
		case PD_ALIGN_LEFT: 
			this.mBaselineValue = 0;
			break;
			
		case PD_ALIGN_CENTER: 
			if ( MA_BI.GetMaxX() < this.mTotalWidth ) {
				this.mBaselineValue = 0;
			} else {
				this.mBaselineValue = Math.floor((MA_BI.GetMaxX() - this.mTotalWidth)/2);
			}
			// Netscape holy shit bug!
			// Scroller space 
			if (MA_BI.IsNN4) this.mBaselineValue -= 10;
			break;
			
		case PD_ALIGN_RIGHT:
			// Align right
			this.mBaselineValue = MA_BI.GetMaxX() - this.mTotalWidth;
			if ( typeof(MA_PDM_WidthTreshold) != 'undefined'
				&& MA_PDM_WidthTreshold > MA_BI.GetMaxX() ) {
				this.mBaselineValue = MA_PDM_WidthTreshold - this.mTotalWidth;
			}
			break;
			
		case PD_ALIGN_USER: 
			// Custom
			// User must define GetCustomBaseline() function  
			this.mBaselineValue = GetCustomBaseline(); // 
			break;
	}
}


function GetXDeploymentStart( iParentE )
{
	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 PD_DEPLOY_DOWN: 
					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 PD_DEPLOY_UP: 
					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 PD_DEPLOY_RIGHT:
					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 PD_DEPLOY_LEFT:
					if(p.left - subWidth > xZero) {
						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 - subHeight;
					} 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.IsIE || MA_BI.IsDOM ) {
		var Obj = MA_BI.IsDOM ? document.getElementById( ID ) : document.all[ ID ];
		Obj.onmouseover = hOver;
		Obj.onmouseout = hOut;
		
		if (MA_BI.IsDOM) 
			linkClicklessensDOM( Obj );
	}
	else
	{
		eval("document." + ID + ".onmouseover = hOver;");
		eval("document." + ID + ".onmouseout = hOut;");
	}
}

function linkClicklessensDOM( iObj ) {
	var atags = iObj.getElementsByTagName("A");
	
	for(var i=0; i<atags.length; i++){
		if ( /(.*[\\\/]|^)#$/.test(atags[i].href) ) {
			atags[i].onclick = function () { return( false ); };
			atags[i].style.cursor = "default";
		}
	}
}

var lastIdsPath = ""; 
function ChangeStyle( ID ) {
	if ( lastIdsPath == ID ) 
		return;
	// change unused items to normal style
	var lidseq = lastIdsPath.split("x");
	var nidseq = ID.split("x");
	
	if ( lastIdsPath != "" ) {
		// Check that wich elements used again and no need to change it style
		for( var j=0; j<lidseq.length && j<nidseq.length ; j++ ){
			if (lidseq[j] != nidseq[j])
				break;
		}
		
		// Turn only not used now elements
		for( var i=j; i<lidseq.length; i++ ) {
			var obj = document.getElementById(lastIdsPath.split("x").splice(0,i+1).join("x"));
			obj.className = (i==0) ? "MainItem" : "MenuItem";
		}
	}
	
	for( var i=j; i<nidseq.length; i++ ){

		var obj = document.getElementById(ID.split("x").splice(0, i+1).join("x"));
		obj.className = (i==0) ? "MainItem-hilite" : "MenuItem-hilite";
	}
	lastIdsPath = ID;
}

function ShowPath( ID, iObj )
{
	if ( MA_PDM.mStyledPathEnable ) {
		ChangeStyle( ID );
	}
	
	if ( typeof(iObj) == "undefined" )
		iObj = MA_PDM;
		
	if ( iObj.mID.indexOf('x') >= 0 ) {
		var iID = iObj.mID.substr( 0, iObj.mID.lastIndexOf('x') );
		iObj.lm.SetObject( iObj.mID );
		if ( (new RegExp( "^"+iID+"(|x.+)$" )).test( ID ) ) {
			iObj.lm.Show(); 
		} else {
			iObj.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 RestoreClassName( iLayerObj ) {
	iLayerObj.Obj.className = iLayerObj.Obj.className.replace(/-hilite$/, "");
} 

function Close()
{
	var i;
	if ( typeof(this.mMenu) != "undefined" )
	{
		for( i=0; i<this.mMenu.length; i++) {
			RestoreClassName( this.mMenu[ i ].lm );
			
			if ( typeof(this.mMenu[i].mAssets) != 'undefined')
				this.mMenu[ i ].Close();
		}
	}
	else if( typeof(this.mAssets) != "undefined" )
	{
		if ( this.mLevel ) {
			RestoreClassName( this.lm );
			this.lm.Hide();
		}
		
		for( i=0; i < this.mAssets.length; i++)
			this.mAssets[ i ].Close();
	}
	else
	{
		RestoreClassName( this.lm );
		this.lm.Hide();
	}
}

function ShowMenu()
{
	for (var i=0; i<this.mMenu.length; i++ )
		this.lm.SetObject( this.mMenu[i].mID ).Show();
}

/*
	subs = [[	id,	width,	height,	z_index,	innerData,	haveChilds ], 
				0  	1  		2  		3       	4        	5
*/
function createHTMLMenuChilds( cd, urlSuffix ) {
	var html = "";
	var dataCell = "";
	var imgDims;
	for( var i=0; i< cd.length; i++ ) {
		html += '<DIV id="' + cd[i][0] + '" class="MenuItem" style="width:'+cd[i][1]+'px; height:'+cd[i][2]+'px; z-index:'+cd[i][3]+';">'
				+ '<TABLE WIDTH="'+cd[i][1]+'" HEIGHT="'+cd[i][2]+'"><TR>';
				
		dataCell = '<TD width="100%">'+cd[i][4]+'</TD>';
		var imgDims = MA_PDM_ImageDimentions[ cd[i][5] ? 0 : 1 ];
		spcCell =  '<TD align="right"><img src="' + urlSuffix + 'inc/img/' + ( cd[i][5] ? 'smi' : 'spc' ) 
					+ '.gif" width="' + imgDims[0] + '" height="' + imgDims[1] + '"></TD>';
		if ( MA_PDM_DeployDirection == PD_DEPLOY_LEFT ) {
			html += spcCell + dataCell;
		} else {
			html += dataCell + spcCell;
		}
		html += '</TR></TABLE></DIV>';
	}
	return ( html );
}

function MA_PDM_RA() {
	if ( typeof(MA_PDM) != "undefined" ) {
		MA_PDM.ArrangePositions();
	} else {
		setTimeout( "MA_PDM_RA()", 2000 );
	}
}

