﻿var MenuItem = Class.create ( {
	initialize: function( id, menu ) {
		this.element = $(id);
		this.element.style.cursor = "pointer";
		this.menu = menu;		
				
		var children = this.element.select( 'ul' );
		this.hasSubmenu = children.size() > 0;
		if( this.hasSubmenu ) {
			this.submenu = new Menu( children[ 0 ].identify() );
			this.submenu.setParentMenu( this.menu );
		}
	},
	
	getSubmenu: function() {
		return this.submenu;
	},
	
	isSubmenuOpen: function() {
		if( this.hasSubmenu )
		{
			var menu = this.getSubmenu();
			return menu.element.visible();
		}
		return false;
	},
	
	openSubmenu: function() {
		if( this.hasSubmenu )
		{
			var menu = this.getSubmenu();			
			menu.element.show();
		}
	},
	
	closeSubmenu: function() {
		if( this.hasSubmenu )
		{
			var menu = this.getSubmenu();			
			menu.element.hide();
		}
	},
	
	toggleSubmenu: function() {
		if( this.isSubmenuOpen() ) {
			this.closeSubmenu();
		}
		else {
			this.openSubmenu();
		}
	},	
		
	select: function( linkId ) {
		if( this.hasSubmenu ) {
			this.getSubmenu().select( linkId );
		}
	},
	
	isLinked: function() {
		return this.getLink() != "#" && this.getLink() != "";
	},
	
	getLinkTag: function() {		
		var children = this.element.select( 'a' );
		if( children.size() > 0 ) {
			return children[ 0 ];
		}
	},
	
	getLink: function() {		
		if( this.getLinkTag() ) {
			return this.getLinkTag().href;
		}
	},
	
	isExtern: function() {
		if( this.getLinkTag() ) {
			return this.getLinkTag().target == '_blank';
		}
		else {
			return false;
		}
	}

});

var Menu = Class.create( {
	initialize: function( id, startLinkId, startSubmenuLinkId ) {
		this.element = $( id );		
		this.links = new Hash();
		this.currentLink = 0;
		
		var children = this.element.childElements();
		for( var index = 0; index < children.size(); ++index ) {
			var link = children[ index ];	  		
	  		var linkId = link.identify();			
			var menuItem = new MenuItem( linkId, this );			
			link.onclick = this.select.bind( this, linkId, 0 );
			
			if( linkId != startLinkId ) {
				menuItem.closeSubmenu();
			}
			
			this.links.set( linkId, menuItem );
		}
		
		this.select( startLinkId, startSubmenuLinkId );
	},
	
	setParentMenu: function( menu ) {
		this.parent_menu = menu;
	},
	
	setChildHandleFlag: function() {
		this.child_handle = true;
	},
	
	select: function( linkId, submenuLinkId ) {

		// inform parent menu about handling
		if( this.parent_menu ) {
			this.parent_menu.setChildHandleFlag();
		}
		
		if( this.child_handle ) {
			this.child_handle = false;
			return;
		}

		var link = this.links.get( linkId );
		if( !link || link.isExtern() ) {
			return;
		}
		
		// change mark
		if( this.currentLink ) {
			this.currentLink.element.removeClassName( "current" );		
			this.currentLink.element.removeClassName( "current_sub" );
		}
		if( !submenuLinkId ) {
			link.element.addClassName( "current" );	
		}
		else {
			link.element.addClassName( "current_sub" );	
		}

		// If menu was not opened menu pop it up
		if( this.currentLink != link ) {		
			if( this.currentLink ) {
				this.currentLink.closeSubmenu();
			}

			link.openSubmenu();
		}
		else {
			link.toggleSubmenu();
		}
				
		// select submenu link
		if( submenuLinkId ) {
			link.select( submenuLinkId );
		}
		this.currentLink = link;
	},
	
	openAll: function() {
		var items = this.links.values();
		for( i = 0; i < items.length; ++i ) {
			var item = items[ i ];
			if( item && item.hasSubmenu ) {
				item.openSubmenu();
			}
		}
	},
	
	closeAll: function() {
		var items = this.links.values();
		for( i = 0; i < items.length; ++i ) {
			var item = items[ i ];
			if( item && item.hasSubmenu ) {
				item.closeSubmenu();
			}
		}
	}
});
