// Constants  ==================================================================
//==============================================================================

// Behaviors ===================================================================
//==============================================================================
FancyMenu = Behavior.create({
	initialize: function() {
	},
	
	onmouseover: function(e) {
		source = Event.element(e);
		//alert('asdf');
		//$('menu').setStyle({ display: 'none' });
		var parent = source.ancestors()[1];
		
		if(parent.hasClassName('lh_submenu')) {
			this.__show(parent);
		}
		//console.log(source.ancestors()[1]);
	},
	
	onmouseout: function(e) {
		source = Event.element(e);
		var parent = source.ancestors()[1];
		
		if(parent.hasClassName('lh_submenu')) {
			this.__hide(parent);
		}
	},
	
	__show: function(e) {
		var menu = Element.getElementsBySelector(e, '.lh_container');
		menu = menu[0];
		Element.setStyle(menu, { display: 'block', position: 'absolute', zIndex: 9999 });
	},
	
	__hide: function(e) {
		var menu = Element.getElementsBySelector(e, '.lh_container');
		menu = menu[0];
		Element.setStyle(menu, { display: 'none' });
	},

// internal methods ------------------------------------------------------------		
	__hideSubmenus: function() {
		this.submenus.each(function(item){
			if(item.visible) item.__hide();
		});
	}
});

FancyMenu.Submenu = Behavior.create({
	initialize: function(fancyMenu) {
		console.log('asdfjkl');
		this.fancyMenu = fancyMenu;
		this.container = $$('div.lh_container');		
		this.container = this.container[0];
		this.visible = false;
		// get a quasi-unique id for effect scopes
		var aDate = new Date(); 
		this.id = aDate.getTime() + Math.random();
		
		// set its styles
		this.container.setStyle({
			position: 'absolute',
			zIndex: 2000
			//width: this.fancyMenu.settings.get('submenuWidth') + 'px'
		});
	},
	
// events ----------------------------------------------------------------------
	onmouseover: function(e) {
		console.log('moused');
		Event.stop(e);
		source = Event.element(e);
		if(!this.__within(this.container, e.pointerX(), e.pointerY())) {
			this.__show();
		}		
	},
	onmouseout: function(e) {
		Event.stop(e);
		if(!this.__within(this.container, e.pointerX(), e.pointerY())) {
			this.__hide();
		}
	},
	
// internal methods ------------------------------------------------------------		
	__show : function() {
		
		if(this.container && !this.visible) {
			this.fancyMenu.__hideSubmenus();
			var id = this.id;			
			var scope = this;
			new Effect.BlindDown(this.container, {
				duration: '.2',
				queue: {scope: 'menu' + id, position: 'end', limit: 1},
				afterFinish: function() {scope.visible = true}				
			});		
		} else {
			return false;
		}
	},
	__hide : function() {
		if(this.container && this.visible) {		
			var id = this.id;
			var scope = this;				
			new Effect.BlindUp(this.container, {
				duration: '.1',
				queue: {scope: 'menu' + id, position: 'end', limit: 1},
				afterFinish: function() {scope.visible = false}				
			});				
		} else {
			return false;
		}
	},
	__within : function(element, x, y) {
	    this.xcomp = x;
	    this.ycomp = y;
	    this.offset = Element.cumulativeOffset(element);

	    return (y >= this.offset[1] &&
	            y <  this.offset[1] + element.offsetHeight &&
	            x >= this.offset[0] &&
	            x <  this.offset[0] + element.offsetWidth);
	}
});

// Functions ===================================================================
//==============================================================================

// Events ======================================================================
//==============================================================================
/*if (!Prototype.Browser.IE) {*/
	Event.addBehavior({
		'.lh_hMenu' : FancyMenu()
	});
/*}*/

// OnReady =====================================================================
//==============================================================================
Event.onReady(function() {
	
});
