//###################################################################
//#						Initiation Section							#
//###################################################################
//# This contains all of the javascript animation affects.			#
//# 																#
//# 																#
//# 																#
//# 																#
//###################################################################


var JMBtrans = function( detailsArray )
{
	this.tempSwitch			= false;										//When a submenu mouse moves out, it fires of the parent's mousein and out which causes the menus to close.  This varible catches the menu trying to do it.
	this.openmenu			= new Array();									//Used to store which menus are currently open
	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'] 	|| [];		//Used to set which menus are currently active
	this.direction 			= detailsArray['direction'] 		|| 2;		//direction the menu should open 1 = Top, 2 = Right, 3 = Down, 4 = Left, 5 = none
	this.tranNumber 		= detailsArray['tranNumber'] 		|| 3;
	
	//Transition Related
	this.submenuOffset		= detailsArray['submenuOffset']		|| 0;		//Really only used in Tran 5 (Scroll).
	this.parentStayHover	= detailsArray['parentStayHover']	|| false;	//Basically the parent stays at its hover state while the menu is open.
	this.overSpan			= false;										//Firefox seems to refire off subhover after it fired of the containing span onMouserOver.  Can't seem to stop it, so having to check for it.
	
	//Administrational Related - Not used in frontend
	this.tempAdminModeOffset = 0;	//Used in administration mode to place the submenus next to each other.  Allowing them to see them when Sub Level is clicked.
	this.subMenuSelected	 = false;
	
	//Fire off on the onClick or on the onHover?
	if( detailsArray['onAClick'] != undefined ){
		this.onAClick 		 	= detailsArray['onAClick'];
	} else {
		this.onAClick 		 	= 0;		
	}

	//When the mouse hovers over it, fire an transition event off.
	if( detailsArray['onHover'] != undefined ){
		this.onHover 		 	= detailsArray['onHover'];
	} else {
		this.onHover 		 	= false;		
	}

	//We need to test for IE6 as it does not support opactiy!!!
	if( jQuery.browser.msie){
		if( parseInt(jQuery.browser.version) < 8 ){
			this.onHover = false;
		}//end this
	}//end if
	

	if( detailsArray['fadeSpeed'] != undefined ){
		this.fadeSpeed 		 	= detailsArray['fadeSpeed'];
	} else {
		this.fadeSpeed 		 	= 100;		
	}
	
	if( detailsArray['closeSpeed'] != undefined ){
		this.closeSpeed 		= detailsArray['closeSpeed'];
	} else {
		this.closeSpeed 		= 50;		
	}	

	if( detailsArray['removeActive'] != undefined ){
		this.removeActive		= detailsArray['removeActive'];
	} else {
		this.removeActive		= 1;
	}//end if
	
	if( detailsArray['parentWidth'] != undefined ){
		this.parentWidth		= detailsArray['parentWidth'];
	} else {
		this.parentWidth		= 0;
	}//end if
	
	//Eneded variable loading, now processing function
	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
		var meenuFullname = jQuery( 'a[menuid="' + menus[ i ] + '"]' ).addClass( 'active' ).attr('id');
		//Try to find a joomla menu and make that active also
		jQuery( '.item' + menus[ i ] ).addClass( 'active' );
		//Now lets open it's parent if it needs doing
		jQuery( '#' + meenuFullname ).trigger('onclick');		
		
	}//end for
	
	
}//end function

JMBtrans.prototype.ChangeTran = function( num )
{
	/* Depreciated Funciton */
	this.tranNumber = num;

}//end function


JMBtrans.prototype.updateStyles = function()
{
	/* This function updates the styles of all of the menu systems as so the transition works correctly */
	if( this.direction == 5 ){ return; }
	switch( this.tranNumber ){
		
		case 5: 
			var submenuOffset = this.submenuOffset;
			childjQuery( '.' + this.menuname + '-submenu' ).each( function(){
														 this.style.overflow = 'hidden';
														 this.style.display = "block";
													 })			
			childjQuery( '.' + this.menuname + '-subinner' ).each( function(){
													 	var height = this.offsetHeight - submenuOffset;
														this.style.position = 'relative';
														this.style.top = "-" + height + "px"
														this.setAttribute( 'ht', height );	//line duplicated in jmb.checker.js class
													 });
			childjQuery( '.' + this.menuname + '-submenu' ).css( 'display', 'none' );
			break;
			
		default:	
			childjQuery( '.submenu[menu='+this.menuname+']' ).each( function(){
														 this.style.overflow = 'auto'; //Changed to auto for IE6
													 });
			
			childjQuery( '.submenu[menu='+this.menuname+'] > .subinner' ).each( function(){
													 	this.style.position = 'static';
														this.style.top	= '0px';
													 });
			break;
	}
	
}

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

//###################################################################
//#							Div Related								#
//###################################################################

JMBtrans.prototype.outDiv = function( id )
{
	
	//This delay has been added as IE6 fired off events in a diferent order to IE7+ && FF et al.  Therefore
	//to ensure that everything works like a dream this has been created.
	this.SetTimeout( 'timedelayedoutDiv( "' + id + '" )', 10 );
	
}//end function

JMBtrans.prototype.timedelayedoutSub = function( id )
{
//	this.SetTimeout( 'subout( "' + id + '" )', 0 );
	this.subout( id );
}

JMBtrans.prototype.timedelayedoutDiv = function( id )
{
	if( this.tempSwitch ){ return; }//We check this value as so when a user goes from one menu to another, the parent div class fires off the mouseout therefore closing the menu.  This chekc ensures that doesnt happen.

	this.switchHideShow( id, true );
	this.SetTimeout( 'subout( "' + id + '" )', this.closeSpeed );
}//end function

//This is when a DIV is hovered over.
JMBtrans.prototype.overDiv = function( id, active )
{
	
	this.switchHideShow( id, false );
}//end function

//###################################################################
//#							Sub Related								#
//###################################################################

//Determines whether to open or close a menu
JMBtrans.prototype.onClickItem = function( name )
{
	var x = anchorWindow.getElementById( name.replace( this.menuname, "submenu-" ) );
	if( x.style.display != "block" ){
		this.subhover( name );
		this.addClass( name );
	} else {
		this.subout( name );
		this.removeClass( name );
	}//end if
	
}//end function

//Runs through a series of checks and positionings to prepare the menu for checking out.
JMBtrans.prototype.subhover = function( name )
{

	if( this.onHover ){ this.onHoverTransition(name); }
	
	this.tempSwitch = true;		//We set this to true as so when a user goes from one menu to another, the parent div class fires off the mouseout therefore closing the menu.  This prevents that.
	
	//Test to see if we are still in admin mode and if the menus are still open
	if( this.subMenuSelected && this.direction != 6 && this.direction != 7 ){ return; }

	//Has the user stopped the submenus appearing by selecting tranistation number 4?
	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
		
		//Lets see if the onHover is active.  If so we need to change name, and find out the parent tag
		switch( this.onHover ){
			case 1:
				name = 'background-' + name;
				parent 	= anchorWindow.getElementById( name );
				break;
		}//end switch

		//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, 5 = None, 
			//	6 = Admin Mode Verticle ( display across the top all sub menu items ), 
			//	7 = Admin Mode Horizontal ( display down the side all sub menu items )
			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:	//None
					if( jQuery.browser.msie )
					{
						child.style.POSITION = 'inherit';
					} else {
						child.style.position = 'inherit';					
					}
					child.style.top = '';
					child.style.left = '';					
					moveSubMenu = false;
					break;
				case 6: //Admin mode For menus that are verticle
					var container = anchorWindow.getElementById( 'jmb-' + this.menuname );
					if( this.tempAdminModeOffset == 0 ){
						var x = getAnchorPosition( name ).x + container.offsetWidth + 10;
					} else {
						var x = parent.offsetWidth + this.tempAdminModeOffset + 10
					}
					this.tempAdminModeOffset = x;
					var y = 0;
					break;
				case 7: //Admin mode For menus that are horizontal
					var container = anchorWindow.getElementById( 'jmb-' + this.menuname );
					if( this.tempAdminModeOffset == 0 ){
						var y = getAnchorPosition( name ).y + container.offsetHeight + 10;
					} else {
						var y = parent.offsetHeight + this.tempAdminModeOffset + 10
					}
					this.tempAdminModeOffset = y;
					var x = 0;
					break;				
			}//end switch
		
			//Change position of menu
			if( moveSubMenu ){
//				child.style.position = 'absolute';					
				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 ){
			var tempWidth = anchorWindow.getElementById( name ).offsetWidth + 'px';
			child.style.width = tempWidth;
			//MS - have - 32 to cope with boarders.
			childjQuery( '#' + childname ).find( 'a[parent='+this.getID(childname)+']' ).each( function(){ this.style.width = (anchorWindow.getElementById( name ).offsetWidth - 32 ) + 'px'; });
		}//end oif
		
		//open menu
		this.openSubmenu( '.' + childname );
	
}//end function

/*
	subout
	Description: Runs a series of tests to see if it is ok to close the submenu
 */
JMBtrans.prototype.subout = function( name )
{
	
	if( this.onHover ){ this.outHoverTransition(name); }
	
	this.tempSwitch = false;  //We set this to false as so when a user goes from one menu to another the submenu closes, the parent div class fires off the mouseout therefore closing the menu.  This prevents that.
	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 Sub Items State has been selected
	if( this.subMenuSelected ){ return false; }

	//Secondly: 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
	
	//Thirdly: 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
	
	//Fourthly: It appears we can close the submenu
	this.SetTimeout( 'closeSubmenu( ".' + childname + '" )', 1 ); //hide sub menus
		
}//end function


//###################################################################
//#							Functions								#
//###################################################################

JMBtrans.prototype.triggerClick = function( name )
{
	childjQuery( '#' + name ).trigger('onclick');
//	childjQuery( '#' + this.menuname + this.getParent( name ) ).trigger('onclick');
//	name = 'submenu' + name.replace( /[A-Za-z]*/, '' );
//	console.log( '#submenu' + this.getParent( name ) );
//	childjQuery( '#submenu' + this.getParent( name ) ).trigger('onclick');
}//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

JMBtrans.prototype.getID = function( name )
{
	var reg = new RegExp( this.menuname, 'gi' );
	return name.replace( reg, '' ).replace( /submenu\-/, '' );	
}//end function

JMBtrans.prototype.getMenuNameInstance = function( name )
{
	return eval( this.menuname + 'JMBtran' );
}

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.removeClass = function( name )
{
	childjQuery( '#' + name ).removeClass( 'active' );
}

JMBtrans.prototype.addClass = function( name )
{
	childjQuery( '#' + name ).addClass( 'active' );
}


JMBtrans.prototype.prepareTransition = function()
{
	var toEval = this.menuname + 'JMBtran.';
	childjQuery( 'div#jmb-' + this.menuname + ' > a' ).each( function(){
																eval( toEval + 'onHoverTransitionSetup( "' + this.id + '" )' );
															})
	
	
}

//Configures the onHoverTransition if needed.
JMBtrans.prototype.onHoverTransitionSetup = function( submenuName )
{
	if( submenuName == '' || submenuName == null ){	return false; } 
	
	switch( this.onHover ){
			//This is a basic fade up and fade down for background images.
			default:
				//We need to test one or two things first.
				var sel = childjQuery( '#' + submenuName );
				if( sel.parent().get(0).tagName == 'DIV' ){
					var aclass = sel.attr('class');
					var height = sel.height();
					var width = sel.width();  //This is not bringing in the width?!
					var padding = 'padding-left: ' + sel.css('padding-left') + '; padding-right: ' + sel.css('padding-right') + '; padding-top: ' + sel.css('padding-top') + '; padding-bottom: ' + sel.css('padding-bottom') + '; ';
					sel.wrap( '<span class="'+aclass+'" id="background-' + submenuName + '" style="height:'+height+'px; ' 
							 	+ 'width:'+width+'px; position: relative; background-image: '+sel.css('background-image')+'; display:block; '
								+ padding
								+ 'border-left-width: ' + sel.css('border-left-width') + '; border-left-color: ' + sel.css('border-left-color') + '; border-left-style: ' + sel.css('border-left-style') + '; '
								+ 'border-right-width: ' + sel.css('border-right-width') + '; border-right-color: ' + sel.css('border-right-color') + '; border-right-style: ' + sel.css('border-right-style') + '; '
								+ '"></span>' );
					sel.addClass('hov')
					var top = 0 - parseInt( sel.css('border-top-width') );
					var left = 0 - parseInt( sel.css('border-left-width') );
					childjQuery( '#background-' + submenuName ).prepend('<span class="'+aclass+'" id="foreground-' + submenuName 
																		+ '" style="z-index: 1000; opacity: 0; position: absolute; left: '+left+'px; top: '+top+'px; display: block; ' 
																		+ 'height:'+height+'px; width:'+width+'px; '
																		+ padding
																		+ ' margin-left: ' + sel.css('border-left-width') + '; '
																		+ 'background-image:'+sel.css('background-image') +'; background-repeat: ' + sel.css('background-repeat') + '; '
																		+'"></span>');
					sel.removeClass( 'hov' );
					
					
					//We need to remove some potentailly interfering properties
					sel.css('padding-left', '0px');
					sel.css('padding-right', '0px');
					sel.css('border-left', '0px none');
					sel.css('border-right', '0px none');
					
					//Need to redefine the a link
					sel.css( {zIndex: 1001, width:width, height:height, display:'block', position:'absolute', top: sel.css('padding-top')+'px'} )
					sel.removeClass( aclass );
				}//end if
				break;
			case false: 
				break;
	}//end switch

}

//This function simply changes the effect of the menu on hover.
JMBtrans.prototype.onHoverTransition = function( submenuName )
{
	if( submenuName == '' || submenuName == null ){	return false; } 
	
	switch( this.onHover ){
			case false: 
				break;
			default:
				//We need to test one or two things first.
				var sel = childjQuery( '#' + submenuName );
				sel.parent().get(0).addClass( 'hov' );
				childjQuery( '#foreground-' + submenuName ).animate( {opacity:"1"}, { queue:false, duration:600 } );
				break;
				
	}//end switch

}

//This function simply changes the effect of the menu when out hover.
JMBtrans.prototype.outHoverTransition = function( submenuName )
{
	if( submenuName == '' || submenuName == null || !this.onHover ){	return false; } 

	switch( this.onHover ){
			default:
				var sel = childjQuery( '#' + submenuName );
				sel.parent().get(0).removeClass( 'hov' );
				childjQuery( '#foreground-' + submenuName ).animate( {opacity:"0"}, { queue:false, duration:600 } );

				break;
				
	}//end switch


}


JMBtrans.prototype.inSpan	= function( id )
{
	this.overSpan = true;
	this.switchHideShow( id, false );
}

JMBtrans.prototype.outSpan	= function( id )
{
	this.overSpan = true; 
}

JMBtrans.prototype.openSubmenu = function( submenuName )
{
	if( submenuName == '' || submenuName == null ){	return false; } 

	this.openmenu[ this.getParent(submenuName) ] = true; 
	
	var parentName = this.menuname + this.getParent(submenuName);
	
	//Turn off active from class
	if( this.removeActive == 1 ){ this.removeClass( parentName ); }
	
	// 1 = Fade
	// 2 = Slide
	// 3 = Appear
	// 4 = No Transition
	// 5 = Scroll Down

	if( this.parentStayHover ){
		childjQuery( '#' + parentName ).addClass( 'hov' );
	}//end if

	switch( this.tranNumber ){
		
		case 1: 
			childjQuery( submenuName ).fadeIn( this.fadeSpeed );
			break;
		case 2: 
			childjQuery( submenuName ).slideDown( this.fadeSpeed );
			break;
		case 3: 
			childjQuery( submenuName ).show();
			break;
		case 4:	
			//Do nothing
			break;
		case 5: 
			submenuName = submenuName.replace( /\./, '#' ) + '-inner';
			var parentName = submenuName.replace( /-inner/, '' );
			childjQuery( parentName ).css( "display", "block" );
			childjQuery( submenuName ).stop().animate( { top:"0px" }, { queue:false, duration:this.fadeSpeed } ); 
			break;
	}//end switch
	
	
}//end function

JMBtrans.prototype.closeSubmenu = function( submenuName )
{
	//Has the mouse gone over a span tag and firefox refires off this function?  If so ignore ff.
	if( this.overSpan ){ this.overSpan = false; return false; }

	//Can we close the menu first of all?
	if( submenuName == '' || submenuName == null ){
		submenuName = '.submenu';
	} else if( !isNaN( submenuName ) ){
		if( this.openmenu[ submenuName ] === false ){ return; }
		submenuName = '#submenu-' + submenuName;
		this.openmenu[ submenuName ] = false;	
	} else {
		if( this.openmenu[ this.getParent(submenuName) ] === false ){ return; }
		this.openmenu[ this.getParent(submenuName) ] = false;
	}//end if
	// 1 = Fade
	// 2 = Slide
	// 3 = Appear
	// 4 = No Transition
	// 5 = Scroll Down

	switch( this.tranNumber ){
		
		case 1: 
			childjQuery( submenuName ).fadeOut( this.fadeSpeed );
			break;
		case 2: 
			childjQuery( submenuName ).slideUp( this.fadeSpeed );
			break;		
		case 3: 
			childjQuery( submenuName ).hide();
			break;
		case 4:	
			//Do Nothing
			break;
		case 5: 
			submenuName = submenuName.replace( /\./, '#' ) + '-inner';
			var topPX = '-' + childjQuery( submenuName ).attr('ht') + 'px';
			var parentName = submenuName.replace( /-inner/, '' );
			var jmb = this.getMenuNameInstance();
			childjQuery( submenuName ).stop().animate( { top:topPX }, this.fadeSpeed, function(){ 
																						childjQuery( parentName ).css("display", "none");
																						if( jmb.parentStayHover ){
																							childjQuery( '#' + jmb.menuname + jmb.getParent(submenuName) ).removeClass( 'hov' );
																						}//end if
																					 });
			break;
	}//end switch

	//Turn on active from class
	if( this.removeActive == 1 ){ this.addClass( this.menuname + this.getParent(submenuName) ); }

}//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



JMBload.continueProcessing();