/*

	This contains all of the javascript animation affects.

*/


var JMBtrans = function( detailsArray )
{
	this.tranOut 			= '';
	this.tranIn 			= '';
	this.openmenu			= new Array();
	this.keepopen			= 0;	//int used to store which pop out to keep open in admin mode
	this.currentParentId 	= 0;
	this.menuname 			= detailsArray['menuname'];
	this.activeitemParent 	= detailsArray['activeitemParent'] 	|| [];
	this.direction 			= detailsArray['direction'] 		|| 2;	//direction the menu should open 1 = Top, 2 = Right, 3 = Down, 4 = Left
	this.tranNumber 		= detailsArray['tranNumber'] 		|| 3;

	if( detailsArray['fadeSpeed'] != undefined ){
		this.fadeSpeed 		 	= detailsArray['fadeSpeed'];
	} else {
		this.fadeSpeed 		 	= 100;		
	}

	if( detailsArray['removeActive'] != undefined ){
		this.removeActive		= detailsArray['removeActive'];
	} else {
		this.removeActive		= 1;
	}//end if
	
	if( detailsArray['removeActive'] != undefined ){
		this.parentWidth		= detailsArray['parentWidth'];
	} else {
		this.parentWidth		= 0;
	}//end if
	
	this.ChangeTran( this.tranNumber );
	
}//end class


JMBtrans.prototype.activeMenus = function ()
{
	
	//This activates all of the menus that need to be activated!
	var menus = this.activeitemParent[ joomlaItemId ];
	
	if( menus == undefined ){ return; }

	menus = menus.split( ', ' );
	for( var i = 0; i < menus.length; i++ ){
		//Find a JMB Menu and make it active
		jQuery( 'a[menuid="' + menus[ i ] + '"]' ).addClass( 'active' );
		//Try to find a joomla menu and make that active also
		jQuery( '.item' + menus[ i ] ).addClass( 'active' );
	}//end for
	
	
}//end function

JMBtrans.prototype.ChangeTran = function( num )
{
		// 1 = Fade
		// 2 = Slide
		// 3 = Appear
		// 4 = No Transition
		
		switch( num ){
			
			case 1: this.tranOut = '.fadeOut( ' + this.fadeSpeed + ' )'; this.tranIn = '.fadeIn( ' + this.fadeSpeed + ' )';		break;
			case 2: this.tranOut = '.slideUp( ' + this.fadeSpeed + ' )'; this.tranIn = '.slideDown( ' + this.fadeSpeed + ' )'; 	break;		
			case 3: this.tranOut = '.hide()'; 							 this.tranIn = '.show()'; 								break;
			case 4:	this.tranOut = '';									 this.tranIn = '';										break;
		
		}//end switch
		
		this.tranNumber = num;

}//end function

//This is when a DIV is hovered over.
JMBtrans.prototype.overDiv = function( id, active )
{
	this.switchHideShow( id, false );
}//end function

JMBtrans.prototype.switchHideShow = function( id, value )
{
	var parentId = this.getParent( id ); 
	this.openmenu[ parentId ] = value; //stop the current sub closing

	//see if there are any parents and stop them closing too.
	var x = anchorWindow.getElementById( this.menuname + parentId );
	if( x != null )
	{
		var parentsParentId = x.parentNode.getAttribute( 'parent' );
		if( parentsParentId != 0 && parentsParentId != null )
		{
			this.openmenu[ parentsParentId ] = value;
			if( value === true ){ 
				//this.SetTimeout(  'closeSubmenu( "' + parentsParentId + '" )', 10 );
			}
			return this.switchHideShow( this.menuname + parentsParentId, value );
		}//end if
	}//end if
}//end function

JMBtrans.prototype.outDiv = function( id )
{
	this.switchHideShow( id, true );
	this.SetTimeout( 'subout( "' + id + '" )', 50 );
}//end function

JMBtrans.prototype.removeClass = function( name )
{
	childjQuery( '#' + name ).removeClass( 'active' );
}

JMBtrans.prototype.addClass = function( name )
{
	childjQuery( '#' + name ).addClass( 'active' );
}


//Runs through a series of checks and positionings to prepare the menu for checking out.
JMBtrans.prototype.subhover = function( name, active )
{

	//Turn of active from class
	if( active == 1 && this.removeActive == 1 ){ this.removeClass( name ); }

	//Are the submenus to appear?
	if( this.tranNumber == 4 ){ return };

	var tempDir		= this.direction;	//used as so the funciton can overide the default this.direction
	var parent 		= anchorWindow.getElementById( name );
	this.currentParentId = parent.getAttribute("menuid");

		var childname = name.replace( this.menuname, "submenu-" );
		var child = anchorWindow.getElementById( childname );
		
		if( child == null ){ return; } //check to see if this item does actually have a submenu
		
		//Is this a submenu of a submenu and this.direction is either Top | Bottom?  Force right.
		if( parent.getAttribute( "parent" ) != "0" ){
			tempDir = 2;
		}//end if

		//Has the childs position already been set?  If so dont reset it.
			//Which way should the menu open?
			//1 = Top, 2 = Right, 3 = Down, 4 = Left
			var moveSubMenu = true;
			
			switch( tempDir ){
				case 1:	//Top issue
					var x = getAnchorPosition( name ).x;
					var y = getAnchorPosition( name ).y - child.offsetHeight;
					break;
				case 2:	//Right
					var x = getAnchorPosition( name ).x + parent.offsetWidth;
					var y = getAnchorPosition( name ).y;
					break;
				case 3:	//Down
					var x = getAnchorPosition( name ).x;
					var y = getAnchorPosition( name ).y + parent.offsetHeight;
					break;
				case 4:	//Left issue
					var x = getAnchorPosition( name ).x - child.offsetWidth;
					var y = getAnchorPosition( name ).y;
					break;
				case 5:
					if( jQuery.browser.msie )
					{
						child.style.POSITION = 'inherit';
					} else {
						child.style.position = 'inherit';					
					}
					child.style.top = '';
					child.style.left = '';					
					moveSubMenu = false;
					break;
			}//end switch
		
			//Change position of menu
			if( moveSubMenu ){
				child.style.position = 'fixed';					
				child.style.top = y + 'px';
				child.style.left = x + 'px';
				child.style.zIndex = '1002';
			}//end if

		//Lets Now set the child's width
		if( this.parentWidth == 1 ){
			child.style.width = childjQuery( '#' + name ).width();
		}//end oif
		
		//open menu
		this.openSubmenu( '.' + childname );
	
}//end function

JMBtrans.prototype.getParent = function( name )
{
	if( name == null ) return '';
	name = name.replace( /(\.|#)/, '' );
	var x = anchorWindow.getElementById( name );
	if( x == null ){ return; }
	return x.getAttribute("parent");
}//end function

/*
	subout
	Description: Runs a series of tests to see if it is ok to close the submenu
 */
JMBtrans.prototype.subout = function( name, active )
{
	
	//Turn on active from class
	if( active == 1 && this.removeActive == 1 ){ this.addClass(name); }

	var parentId = this.getParent( name );

	//Did the use move from the submenu back to the parent?  If so do not close the submenu
	if( this.currentParentId == parentId ){ return; }

	//Firstly: check to see if the menuitem has a submenu
	var childname = name.replace( this.menuname, "submenu-" );
	if( anchorWindow.getElementById( childname ) == null ){ return; } //check to see if this item does actually have a submenu and isnt a submenu itself
	
	//Secondly: test to see if the menu item's parent is selected
	if( selectedItem != "" )
	{ 

		if( selectedItem == name ){ return false; }	//menu item selected dont close child
		
		//Checks to see if the parent id is in the selectedItem
		if( selectedItem.indexOf( parentId ) > 0 ){ return false; }
		
		//Has the user hover overed the parent item of the submenu?  If so dont hide it.
		if( this.getParent( selectedItem ) == this.currentParentId  ){ return false; }
		
		//Check to see if the selected item has the same parent as the item the mouse is exiting -- mainly to stop the div tags closing the submenu
		if( this.getParent( selectedItem ) == parentId  && name.indexOf( 'submenu-' ) == 0 ){ return false; }		
		
	}//end if
	
	//Thirdy: It appears we can close the submenu
	this.SetTimeout( 'closeSubmenu( ".' + childname + '" )', 1 ); //hide sub menus
		
}//end function

JMBtrans.prototype.SetTimeout = function( func, milsec )
{
	var toEval = this.menuname + 'JMBtran.' + func; //Had to create this outside of the setTimeout as so the browser would understand this.
	setTimeout( function(){ eval( toEval ); }, milsec );//hide sub menus
	
}//end function

JMBtrans.prototype.openSubmenu = function( submenuName )
{

	if( submenuName == '' || submenuName == null ){	return false; } 

	this.openmenu[ this.getParent(submenuName) ] = true; 
	eval( "childjQuery( '" + submenuName + "' )" + this.tranIn );
	
}//end function

JMBtrans.prototype.closeSubmenu = function( submenuName )
{
	//Can we close the menu first of all?
	if( submenuName == '' || submenuName == null ){
		eval( "childjQuery( '.submenu' )" + this.tranOut );
	} else if( !isNaN( submenuName ) ){
		if( this.openmenu[ submenuName ] === false ){ return; }
		eval( "childjQuery( '#submenu-" + submenuName + "' )" + this.tranOut );
		this.openmenu[ submenuName ] = false;	
	} else {
		if( this.openmenu[ this.getParent(submenuName) ] === false ){ return; }
		eval( "childjQuery( '" + submenuName + "' )" + this.tranOut );
		this.openmenu[ this.getParent(submenuName) ] = false;
	}//end if
	
}//end function

JMBtrans.prototype.closeAllButOne = function( name )
{

	if( name == null ){ name = ''; }
	name = trim(name);
	
	name = name.replace( this.menuname, 'submenu-' );
	var rg = new RegExp( "[0-9]+", "" );

	for( var menu in this.openmenu ){
		//Is the item a function or int?
		if( rg.test( menu ) == true ){
			//if int, is the display open?  If it is, close it.
			var subitem = 'submenu-' + menu;
			if( name != subitem )
			{	
				this.closeSubmenu( '.' + subitem );
			}
		}//end if
	}//end if
	
}//end function

