/*
 * VeSi' cool menu 
 * Damiano Seno @ faberadv. june09
 */
var iMenu = Class.create();
iMenu.prototype = {
	root: null,
	domMenus: [],
	currentPath: [],
	defaultPath:[],
	menuPrefix: 'menu',
	levelPrefix: 'level',
	depth: 0, // max depth
	options: {
	},
	initialize: function( obj ) {
		if ( typeof( Prototype ) == 'undefined' ) {
			window.alert( "Fatal: iMenu needs Prototype in order to be used" );
			return; // fatal error, exit immediately
		}
		if ( typeof( Element.hover ) == 'undefined' ) {
			window.alert( "Fatal: iMenu needs protohover.js in order to be used" );
			return; // fatal error, exit immediately				
		}
		var options = this.options;
		Object.extend( this, obj || {} );
		this.options = Object.extend( options, obj.options || {} );
		this.depth = this.currentPath.length;
		var this_ptr = this;
		this.currentPath.each(
			function( item ) {
				this_ptr.defaultPath.push( item );
			}.bind(this)
		);
		// process menus once the page as loaded
		Event.observe( 
				window, 
				'load', 
				this._processMenus.bindAsEventListener( this ), 
				false
		);
	},
	_processMenus: function() {
		if ( !this.root ) { return; }
		if ( Object.isString( this.root ) ) {
			this.root = $$( 'div.' + this.root )[ 0 ];
			if ( !this.root ) return;
		}
		this.domMenus = [];
		var this_ptr = this;
		var index = 0;
		var links = this.root.getElementsByTagName( 'a' ); // get all links of menu
		for ( var i = 0; i < links.length; i++ ) {
			var link = links[ i ];
			Event.observe( 
					link, 
					'mouseover', 
					this_ptr._handleHover.bindAsEventListener( this_ptr, link ), 
					false
			);
			link.onmouseover = function() { };
		}
			
		this.root.hover(
			function() {}, // hover handling already set
			function() {
				this_ptr.restore(); // restore 
			} 
		);			
	},
	_handleHover: function ( e, link ) {
		
		this._selectLink( link );
		
		var params = this._parseParams( link.getAttribute( 'params' ) );
		var childrenDiv = $( this.menuPrefix + params.id );
		if ( !childrenDiv ) {
			// ho cliccato su una foglia, non fare niente
			return;
		}
		var index = -1;
		if ( (index = this._isinPath( params.id )) >= 0 ) {
//			console.log( "caso 1 index:" + index );
			if ( !childrenDiv.visible() ) childrenDiv.show(); 
		} else if ( (index = this._isinPath( params.parent )) >= 0 ) {
//			console.log( "caso 2 index:" + index ); 
			this._show( index + 1, params.id )
		} else {
			// caso root?
//			console.log( "caso 3 (root?) index:" + index );
			this._show( 0, params.id );
		}
	},
	_show: function( start, id ) {
		var end = Math.max( this.depth, this.currentPath.length );
//		console.log( "show: " + this.currentPath + " start: " + start + " end: " + end + " id " + id);
		for ( var i = start; i < end; i++ ) {
//			console.log( this.menuPrefix + this.currentPath[ i ] );
			var div = $( this.menuPrefix + this.currentPath[ i ] );
			if ( div ) div.hide();
		}
		this.currentPath.splice( start, end - start ); // remove elements and add new elem
		this.currentPath.push( id );
//		console.log();
		$( this.menuPrefix + id ).show();
	},
	_parseParams: function( params ) {
		if ( params == null || typeof( params ) == 'undefined' ) return;		
		var objparams = {};
		var arrayparams = params.split( "," );
		for ( var i = 0; i < arrayparams.length; i++ ) {
			var pvalarray = arrayparams[ i ].split( "=" );
			if ( pvalarray[ 0 ] != "" && pvalarray[ 1 ] != "" ) {
				objparams[ pvalarray[ 0 ] ] = pvalarray[ 1 ]; 
			}
		}
		return objparams;
	},
	_isinPath: function( id ) {
//		console.log( "search " + id + " in " + this.currentPath );
		if ( this.currentPath == null ) return -1;
		for ( var i = 0; i < this.currentPath.length; i++ ) {
			if ( this.currentPath[ i ] == id ) return i;
		}
		return -1;
	},
	restore: function( e ) {
//		console.log( "restore called!" );
		this.currentPath.each(
			function( item ) {
				var div = $( this.menuPrefix + item );
				if ( div ) div.hide();
			}.bind(this)
		);
		this.currentPath = [];
		this.defaultPath.each( 
			function( item ) {
				var div = $( this.menuPrefix + item );
				if ( div ) div.show(); 
				this.currentPath.push( item );
			}.bind(this) 
		);
		
		this._showActiveLinks();
	},
	_getParentLinks: function( link ) {
		var domBody = document.getElementsByTagName( 'body' )[ 0 ];
		var pnode = link;
		while ( (pnode = pnode.parentNode) != domBody ) {
			if ( pnode.tagName == 'DIV' ) { break; }
		}
		return pnode.getElementsByTagName( 'li' );
	},
	_selectLink: function ( srcLink ) {
		srcLink = $(srcLink).up(); // parentNode
		srcLink.addClassName( 'selected' );
		var links = this._getParentLinks( srcLink );
		for ( var i = 0; i < links.length; i++ ) {
			var link = $( links[ i ] );
			if ( link != srcLink && link.hasClassName( 'selected' ) ) link.removeClassName( 'selected' );
		}
	},
	_showActiveLinks: function() {
		var links = this.root.getElementsByTagName( 'li' );
		for ( var i = 0; i < links.length; i++ ) {
			var link = links[ i ];
			link = $(link);
			if ( link.hasClassName( 'active' ) ) link.addClassName( 'selected' );
			else if ( link.hasClassName( 'selected' ) ) link.removeClassName( 'selected' );
		} 
	}
};
