// TODO: Refactor this code substantially to separate the transparency-transition portions from the sliding-transition portion

var toAnim;
var fadeSpeed = 600;

hidePromoDD = function(){
	if(!$.ddl.active){
		$.ddl.active=true;
		/* 	******************************************
		ANIMATION is no longer used.
		This was causing issues on the homepage
		FadeIn / FadeOut is now being used
		*********************************************
		var animationParams = {
			opacity :	0
		};
		if( ! isHomepage ) {
			animationParams.height = 'hide';
		}
		$('ul.promoDDList').animate(animationParams,'normal',function(){$.ddl.active=false;});
		*********************************************/
		
		$('ul.promoDDList').fadeOut(fadeSpeed,function(){$.ddl.active=false;});
	}
}

function addMenuCallbacks() {
	
	$('a.promoDropDown').each( function() {
		
		var data = $.metadata.get(this);

		if(!$.metadata.get(this)) {
			return
		};
		
		$(this).trigger('blur');
		
		if($(this).is('#atmLocatorBtn')) {
			ATM.makeCitiesList();
		}
		
		var downArrow = $(this).find('img'); // TODO: add a more specific selector
		var menuElementID = data.el;
		$(menuElementID).addClass( data.type == 'home' ?
													data.look :
													data.type );
		
		var offset = downArrow.offset();
		offset.height = downArrow.height();
		offset.width = downArrow.width();
		
		// Set the starting parameters for the menus, tweaking it for homepage vs. backpage differences
		var menuCSS = {
			left :		'15px',
			width :		'178px',
			display :	'block',
			top : 		( data.type=='home' ? offset.top-$(menuElementID).height() : offset.top+offset.height-1 ) + 'px'
		};
		
		/* 	******************************************
		ANIMATION is no longer used.
		This was causing issues on the homepage
		FadeIn / FadeOut is now being used
		*********************************************
		var animationParams = { };
		
		if(this.data.type == 'home') {
			menuCSS.opacity = .95; // was 0
			animationParams.opacity = 0.95;
		} else {
			animationParams.opacity = 1.00;
			animationParams.height = 'show';
		}
		**********************************************/
		
		$(menuElementID).css( menuCSS );
		if( isHomepage ) {
			$(menuElementID).hide();
		} else {
			$(menuElementID).hide();
		}
		$(this).click( function() {
			hidePromoDD();
			if( isHomepage ) {
				$(menuElementID).css('opacity', .95);
			} else {
				$(menuElementID).css('opacity', 1);
			}
			$.ddl.active = true;
			toAnim = menuElementID;
			//$(menuElementID).animate( animationParams, function() { $.ddl.active = false } );
			$(menuElementID).fadeIn( fadeSpeed, function() { $.ddl.active = false } );
			return false;
		} );
		$(this).add('ul.promoDDList').not('#atmLocatorDD,#atmLocatorBtn').hover(
				function(){
					if($.ddl.timer)clearTimeout($.ddl.timer);
				},
				function(){
					$.ddl.timer = setTimeout(hidePromoDD,300);
				}
			);
		hidePromoDD();
	} );
	
	// hide any visible promo dropdowns when anything is clicked
	$(document).bind('click',hidePromoDD);
}
