Element.implement({
	addGreedyEvent : function(type, fnc){
		var target = this;
		this.addEvent(type,function(e){
			var a = new Event(e);
			a.target=target;
			fnc(a);
		})
	}
})
var Menu = new Class({
	chains : [],
	duration : 300,
	elType : 'ul',
	childType : 'li',
	initialize: function(grey2nd){
		Cufon.replace('.menu2 a', {
			textShadow: '1px 1px rgba(0, 0, 0, 0.2)',
			hover: true
		});
		if (!Browser.Engine.trident) {
			$$( '#menu ul' ).each(function( item, index ){
				item.setStyle('display','block');
			});
		}
		$$( '#menu li' ).each(function( item, index ){			
			var link = item.getChildren( 'a' );
			
 			var ul = item.getChildren( 'ul' );
 			var temp  = new Element( 'img', {
					src: URL_ROOT+'images/menu_bg2.gif',
					opacity: 0.8
					});
 			if ((this.getDepth(item) >= 1) ) {
 				temp  = new Element( 'img', {
					src: URL_ROOT+'images/menu_bg2_2nd.gif',
					opacity: 0.8
				});
 			}
			temp.addGreedyEvent( 'mouseenter', this.showMouseOverEffect.bind( this ) );
			temp.addGreedyEvent( 'mouseleave', this.hideMouseOverEffect.bind( this ) );
			temp.link = link;
			temp.addGreedyEvent( 'click', this.clickMouse.bind( this ) );
			link.setStyle('color','white');
 			//alert(item.getPosition().y);
 			
			link[0].addGreedyEvent( 'mouseenter', this.showMouseOverEffect.bind( this ) );
			link[0].addGreedyEvent( 'mouseleave', this.hideMouseOverEffect.bind( this ) );
 			item.adopt( temp ).adopt( link[0] );
 			if ( ul.length )
	 			item.adopt( ul[0] );
		}.bind( this ));
		$$( '#menu ul' ).each(function( item, index ){
			item.setStyle('display','none');
		});
		this.addEventsTo( $( 'menu' ) );
		Cufon.replace('#menu a', {
			hover: 'true',
			textShadow: '1px 1px rgba(0, 0, 0, 0.2)'
		});
		
	},		
	addEventsTo : function( e ) {
		if( e.getChildren( this.childType ).length ) {
			$each( e.getChildren( this.childType ), function( item ) {
				if( item.getChildren( this.elType ).length ) {
					item.timeOut = null;
					item.addGreedyEvent( 'mouseenter', this.showMenuForEvent.bind( this ) );
					item.addGreedyEvent( 'mouseleave', this.hideMenuForEvent.bind( this ) );
					var aUL = item.getChildren( this.elType )[0];
					item.UL = aUL;
					item.fx = {
						show : new Fx.Morph( aUL, { duration : this.duration } ),
						hide : new Fx.Morph( aUL, { duration : this.duration } )
					}
					item.ULSize = {
						width : aUL.getSize().x,
						height : aUL.getSize().y
					}
					this.addEventsTo( aUL );
				}
			}.bind( this ));
		}
	},
	showMenuForEvent : function( e ) {
		e = e.target;
		$clear( e.timeOut );
		e.UL.setStyles({
			display : 'block'
		});
	},
	hideMenuForEvent : function( e ) {
		e = e.target;
		$clear( e.timeOut );
		e.timeOut = function() {
			e.UL.setStyle( 'display', 'none' );	
		}.delay( Browser.Engine.trident ? 0 : 0 );
		
	},
	clickMouse : function( e ) {
		e = e.target;
		document.location = e.link;
		
	},
	showMouseOverEffect : function ( e ) {
		e = e.target;
		e.getParent().getFirst('img').setStyles({
			opacity : 0.9
		})
	},
	hideMouseOverEffect : function ( e ) {
		e = e.target;
		e.getParent().getFirst('img').setStyles({
			opacity : 0.7
		})	
	},		
	getDepth: function(target) {
		var depth = 0;
		var test = false;
		while( test != true ) {
			if (target.getParent('ul').id == 'menu') {
				test = true;
			} else {
				target = target.getParent('ul');
				depth++;
			}
		}
		return depth;	
	}
});
//window.addEvent('domready',function(){
//	new Menu();			
//});
