/* DropDown color constants - can't use const - must use var because ie 6 chokes on const declarations */
var dropdownNormalBknd = '#000';
var dropdownNormalColor = '#FFF';
var dropdownActiveBknd = '#00F';
var dropdownActiveColor = '#FFF';

var levelTwoDropDownULController = new Class (
	{
		initialize: function ( dropDownULTag ) {
			this.ulTag = dropDownULTag;
			this.menuDropFx = new Fx.Styles ( dropDownULTag, { duration:750, wait:false, transition: Fx.Transitions.Expo.easeOut, fps:30.0 } );
			this.showDropDown = this.showDropDown.bind(this);
			this.hideDropDown = this.hideDropDown.bind(this);
			dropDownULTag.setStyle ( 'display', 'none' );
			dropDownULTag.setStyle ( 'left', '0px' );
			var liControllers = new Array();
			dropDownULTag.getChildren().filterByTag('li').each(
				function ( li, index ) {
					if ( li.getProperty('id') == 'current' ) {
						/* set the background on the current item */
						li.getChildren().filterByTag('a').each(
							function ( aTag, index ) {
								aTag.setStyle('color',dropdownActiveColor);
								aTag.setStyle('background-color',dropdownActiveBknd);
							}
						);
					} else {
						if ( li.hasClass('active') ) {
							li.getElements( 'a' ).each (
								function ( aTag, index ) {
									if ( 
										( aTag.getParent().getProperty('id') == 'current' )
										|| ( aTag.getParent().hasClass('active') ) 
									) {
										aTag.setStyle('color',dropdownActiveColor);
										aTag.setStyle('background-color',dropdownActiveBknd);
									}
								}
							);
						}
					}
				}
			);

		},
		showDropDown: function () {
			this.menuDropFx.stop();
			this.ulTag.setOpacity ( '1' );
			this.ulTag.setStyle ( 'left', '0px' );
			this.ulTag.setStyle ( 'display', 'block' );
		},
		hideDropDown: function () {
			this.menuDropFx.start( { 'opacity': '0.0' } );
		}
	}
);

var levelOneDropDownLiController = new Class (
	{
		initialize: function ( menuLItag ) {
			var fx = new Array();
			var children = menuLItag.getChildren();
			var aTag = children.filterByTag( 'a' );
			var ulTag = children.filterByTag( 'ul' );
			var ulControllers = new Array();
			var isActive = false;
			var isCurrent = false;
			aTag.each (
				function ( aTag, index ) {
					fx[index] = new Fx.Styles ( aTag, { duration:750, wait:false, transition: Fx.Transitions.Expo.easeOut, fps:30.0 } );
					isActive = aTag.getParent().hasClass('active');
					isCurrent = aTag.getParent().getProperty('id') == 'current';
					aTag.setStyle('color',dropdownNormalColor);
					aTag.setStyle('background-color',dropdownNormalBknd);
					if ( isCurrent || isActive ) {
						fx[index].start( { 'background-color': dropdownActiveBknd, 'color': dropdownActiveColor } );
					}
				}
			);
			ulTag.each (
				function ( dropDownULTag, index ) {
					ulControllers[index] = new levelTwoDropDownULController ( dropDownULTag );
				}
			);
			menuLItag.addEvent( 
				'mouseover', 
				function( e ) { 
					new Event ( e ).stop();
					aTag.each (
						function ( aTag, index ) {
							fx[index].stop();
							aTag.setStyle('background-color', dropdownActiveBknd);
							aTag.setStyle('color', dropdownActiveColor);
						}
					);
					ulControllers.each( function ( ul, index ) { ul.showDropDown(); } );
				}
			);					
			menuLItag.addEvent( 
				'mouseout', 
				function( e ) { 
					new Event ( e ).stop(); 
					aTag.each (
						function ( aTag, index ) {
							fx[index].stop();
							if ( isCurrent || isActive ) {
								fx[index].start( { 'background-color': dropdownActiveBknd, 'color': dropdownActiveColor } );
							} else {
								fx[index].start( { 'background-color': dropdownNormalBknd, 'color': dropdownNormalColor } );
							}
						}
					);
					ulControllers.each( function ( ul, index ) { ul.hideDropDown(); } );
				}
			);
		}
	}
);

var dropDownController = new Class (
	{
		initialize: function ( menuULtag ) {
			var liControllers = new Array();
			menuULtag.getChildren().filterByTag('li').each(
				function ( li, index ) {
					liControllers[index] = new levelOneDropDownLiController ( li );
				}
			);
		}
	}
);

function sfHoverOut() {
	clearTimeout(this.timer);
	this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
}

var applyHMenu = new Class(
	{
		initialize: function() {
			var objs = $$('.mod_hmenu ul.menu');
			objs.each(
				function ( obj ) { 
					if ( window.ie6 ) {
						var hoverListItems = obj.getElements('li');
						hoverListItems.each(
							function ( listItemElement ) {
								listItemElement.onmouseover = function() {
									clearTimeout ( this.timer );
									if ( this.className.indexOf(" sfhover" ) == -1 ) {
										this.className+=" sfhover";
									}
								}
								listItemElement.onmouseout = function() {
									this.timer = setTimeout( sfHoverOut.bind ( this ), 100 );
								}
							},
							this
						);
					}
					this.hoverEffect = new dropDownController(obj);
				},
				this
			);
		}
	}
);
window.addEvent( 'domready', function() { document.applyeffects = new applyHMenu(); } );
