var ddMenuMainProgress = new Class({
	initialize: function() {
		this.state = false;
		this.item = false;
		this.progress = false;
		this.effect = false;		
	},

	active: function(link, progress) {
		return (this.item==link && this.state && (!progress || this.progress==progress));
	},
	reset: function() {
		this.state = false;
		this.item = false;
		this.progress = false;
		this.effect = false;
	}
});

var ddMenuMain = new Class({
	
	initialize: function(menuId) {

		
		this.menuContainer = $(menuId);
		this.menuId = menuId;
		this.overLink = false;
		this.overSublink = false;
		this.progress = new ddMenuMainProgress();
		this.links = $$('#' + this.menuId + ' a.firstLevelItem');
		this.containers = $$('#' + this.menuId + ' li.menuMainItem');
		this.subLinks = $$('#' + this.menuId + ' li.menuMainItem ul li a');
		
		for (c=0; c<this.containers.length; c++) {
			var menus = this.containers[c].getElements('ul');
			if (menus.length) {
				menus[0].hide();
			}
		}
		
		this.activeElement = false;
		this.activeLink = false;
		this.activeLinkIndex = false;
		
		this.links.each(function(item) {
			
			item.addEvent('mouseover', function() {
				this.overLink = true;

				if (this.activeElement) {
					if (this.activeElement!=item) {
						//this.deactivate(false);
						this.closeMenu(this.activeElement);
						
						this.activate(item);
					}
				}
				else {
					this.activate(item);	
				}	
				item.addClass('firstLevelItemActive');										
			}.bind(this));
			
			item.addEvent('mouseout', function() {
				this.overLink = false;
				if (this.activeLink && !this.overLink && !this.overSublink) {
					item.removeClass('firstLevelItemActive');
					this.activeLink = false;		
				}
			}.bind(this));
			
		}.bind(this));
		
		this.subLinks.each(function(item) {
			
			item.addEvent('mouseover', function() {
				this.overLink = true;
				this.overSublink = true;
				parentLink = this.links[this.activeLinkIndex];
				parentLink.addClass('firstLevelItemActive');
				this.activeLink = parentLink;

			}.bind(this));
			
			item.addEvent('mouseout', function() {
				this.overLink = false;
				this.overSublink = false;
				parentLink = this.links[this.activeLinkIndex];
				parentLink.removeClass('firstLevelItemActive');
				this.activeLink = false;

			}.bind(this));
			
		}.bind(this));		
		
		
		this.deactivate.periodical(1000, this, true);
	},

	
	activate: function(link){
			
			for (c=0; c<this.links.length; c++) {
				if (this.links[c]==link) {
					linkIndex = c;
				}
			}

			var menus = this.containers[linkIndex].getElements('ul');

			
			this.activeLink = link;
			this.activeLinkIndex = linkIndex;
			if (menus.length>0) {
				
				this.activeElement = menus[0];
				
				if (this.progress.active(this.activeElement, 'deactivate')) {
					this.progress.effect.cancel();
					this.progress.reset();
				}
				if (!this.progress.active(this.activeElement)) {
					
					this.progress.state = true;
					this.progress.item = this.activeElement;
					this.progress.progress = 'activate';	
					
					var mySlide = new Fx.Slide(this.activeElement, {
										transition: Fx.Transitions.Sine.easeOut,
										duration: 400,
										onComplete: function(){
			        						this.progress.reset();
										}.bind(this)});
					this.activeElement.show();
					mySlide.slideIn('vertical');
				
				}
	
			}
			else {
				this.activeElement = false;
			}			
		
		

	},
	
	deactivate: function(checkLink){

		if (this.activeElement && (!checkLink || !this.overLink)) {
			
			if (!this.progress.active(this.activeElement)) {
			
				
				this.progress.state = true;
				this.progress.item = this.activeElement;
				this.progress.progress = 'deactivate';				
				this.progress.effect = new Fx.Slide(this.activeElement, {
											transition: Fx.Transitions.Sine.easeIn,
											duration: 200,					
											onComplete: function(){
			        							this.activeElement.hide();
			        							this.activeElement = false;
			        							this.progress.reset();
	    									}.bind(this)});
				this.progress.effect.slideOut('vertical');

				/*
				new Fx.Tween(this.activeElement, 
					{
						duration: 500,
						property: 'opacity'
					}).start(0);
				*/					
			}


								
		}		
	},
	
	closeMenu: function(menu) {



		this.progress.state = true;
		this.progress.item = menu;
		this.progress.progress = 'deactivate';
		this.progress.effect = new Fx.Slide(menu, {
									transition: Fx.Transitions.Sine.easeIn,
									duration: 200,			
									onComplete: function(){
		    							menu.hide();
		    							this.progress.reset();
									}.bind(this)});
		this.progress.effect.slideOut('vertical');			
			
	}
	
	
});

window.addEvent('domready', initMenuMain);
function initMenuMain() {
	topMenuMain = new ddMenuMain('menudrop');
	
}
