/**
 * Navigation class
 * Object for opening and closing menu
 * 
 * @version 	0.1
 * @date  		2009-05-02
 * @author 		Sybren Dotinga - SD
 * @copyright 	JDI internet professionals
 */
function Navigation( nodeID, menuID ) {
	
	this.menuChilds 					= null;
	this.DefaultHeight 					= 0; //Default value is 384 because the height div.Lijnen. Min-height is set in CSS!
	this.Height 						= 0; 
	
	this.menuHolder						= menuID;
	this.menuNodeID 					= nodeID;
	this.menuNode 						= document.getElementById( nodeID );
	
	this.timeOutID 						= null;
	this.timeOutTime					= 50;
	
	this.OpenTime 						= 400;
	this.CloseTime 						= 300;
	
	this.allowedToOpen					= 0;
	this.linkVisited					= 0;
	this.setActions();
	
	this.state 							= this.CLOSED;
	
	this.menuChilds 					= this.getMenuChilds();
	this.Height 						= this.getHeight();
	
	this.ArrayPos 						= NavigationArray.push( this );
	
	this.showTimeoutDuration			= 500; // in miliseconds
	this.showTimeout					= null; // will contain a timeout object. do not touch
	
	this.directOpened = 0;
}

Navigation.prototype.setActions = function(){
	var Menu = this;
	
	$( 'div.ContentWrapper' ).bind( 'mouseenter', function(){
		Menu.allowedToOpen = 1;
	}).bind('mouseleave', function(){
		Menu.allowedToOpen = 1;
	});

	$( 'div.Header' ).bind( 'mouseenter', function(){
		Menu.allowedToOpen = 1;
	}).bind('mouseleave', function(){
		Menu.allowedToOpen = 1;
	});
		
	$( 'a.BlockMenu' ).bind( 'mouseleave', function(){
		Menu.allowedToOpen = 1;
	});
		
	$( 'a.AllowMenu' ).bind( 'mouseenter', function(){
		Menu.allowedToOpen = 1;
		clearTimeout(Menu.showTimeout);
		Menu.showTimeout = setTimeout(function() {
			Menu.changeState( 0 );			
		}, Menu.showTimeoutDuration);
	});
	
	$( '#' + this.menuHolder ).bind( 'mouseleave', function(){
		clearTimeout(Menu.showTimeout);
		Menu.changeState( 2 );
	}).bind('mouseenter', function(){

		if( Menu.allowedToOpen == 1 ) {
			clearTimeout(Menu.showTimeout);
			Menu.showTimeout = setTimeout(function() {
				Menu.changeState( 0 );			
			}, Menu.showTimeoutDuration);			
		}
	});
	
	$( 'img.Anchor' ).bind( 'click', function(){
		//if( this.directOpened == 1 ) {
		//	clearTimeout(Menu.showTimeout);
		//	Menu.changeState( 2 );
		//	this.directOpened = 0;
		//} else {
			Menu.changeState( 0 );			
			this.directOpened = 1;
		//}
	});
	
}

Navigation.prototype.changeState = function( newState ) {
		
	switch( this.state ) {
	
		case this.ANIMATING :
			this.closeMenu();
		break;
	
		default : 		
			switch( newState ) {
				case this.OPEN :
					/*
					 * Open menu
					 */
					this.state = this.ANIMATING;
					this.openMenu();
				break;
				
				case this.ANIMATING :
					/*
					 * Do nothing
					 */
				break;
				
				case this.CLOSED :					
					/*
					 * Close menu
					 */
					this.state = this.ANIMATING;
					this.closeMenu();
				break;
			}
		break;
	}
}

Navigation.prototype.closeMenu = function() {
	
	var Object = this;
	
	$( this.menuNode ).animate(
		{
			height: 20
		}, 
		{ 
			queue: false, 
			duration: this.CloseTime, 
			complete: function () {		
				Object.menuNode.className = 'Closed';
				Object.state = 0;
			}
		}
	);
}

Navigation.prototype.openMenu = function() {
	
	var Object = this;
	
	$( this.menuNode ).animate(
		{
			height: this.getHeight()
		}, 
		{ 
			queue: false, 
			duration: this.OpenTime, 
			complete: function () {		
				Object.menuNode.className = 'Open';
				Object.state = 2;
			}
		}
	);
}

Navigation.prototype.getMenuChilds = function() {
	if ( this.menuChilds == null ) {
		
		var Nodes = document.getElementById( this.menuNodeID + 'Kolommen' ).childNodes;
		var Childs = new Array();
		
		if ( Nodes ) {			
			for( var i = 0; i < Nodes.length; i++ ) {
				var Child = Nodes[ i ];
				if( Child.className ) {
					if ( ( Child.tagName == 'DIV' ) && ( Child.className.search( 'Kolom' ) != -1 ) ) {				
						Childs.push( Child );
					}
				} else {
					//Deze valt af, zal een text-node zijn van Firefox of de div die de float cleared
				}
			}
		} else {
			throw 'No columns';
		}
		
		this.menuChilds = Childs;
	}
	return this.menuChilds;
}

Navigation.prototype.getHeight = function() {
	if ( this.Height == 0 ) {
		
		var Childs = this.getMenuChilds();
		var Height = this.DefaultHeight;
		
		for( var i = 0; i < Childs.length; i++ ) {
			var Kolom = Childs[ i ];			
			Height = ( Kolom.clientHeight > Height ) ? Kolom.clientHeight : Height;
		}
		this.Height = Height;
	}
	return this.Height;
}
	
/*
 * Event types
 */
Navigation.prototype.GOTMOUSE  	= 0;
Navigation.prototype.LOSTMOUSE 	= 1;

/*
 * States
 */
Navigation.prototype.OPEN      	= 0;
Navigation.prototype.ANIMATING 	= 1;
Navigation.prototype.CLOSED    	= 2;

var NavigationArray = new Array();
