// TODO: Refactor this code substantially to separate the transparency-transition portions from the sliding-transition portion

var toAnim;

hidePromoDD = function(){
	if(!$.ddl.active){
		$.ddl.active=true;
		var animationParams = {
			opacity :	0
		};
		if( ! isHomepage ) {
			animationParams.height = 'hide';
		}
		$('ul.promoDDList').animate(animationParams,'normal',function(){$.ddl.active=false;});
	}
}

function addMenuCallbacks() {
	
	$('a.promoDropDown').each( function() {
		if(!this.data) {
			return
		};
		
		$(this).trigger('blur');
		
		if($(this).is('#atmLocatorBtn')) {
			ATM.makeCitiesList();
		}
		
		var downArrow = $(this).find('img'); // TODO: add a more specific selector
		var menuElementID = this.data.el;
		$(menuElementID).addClass( this.data.type == 'home' ?
													this.data.look :
													this.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 :		offset.left+'px',
			width :		offset.width+'px',
			display :	'block',
			top : 		( this.data.type=='home' ? offset.top-$(menuElementID).height() : offset.top+offset.height-1 ) + 'px'
		};
		var animationParams = { };
		
		if(this.data.type == 'home') {
			menuCSS.opacity = 0;
			animationParams.opacity = 0.95;
		} else {
			animationParams.opacity = 1.00;
			animationParams.height = 'show';
		}
		
		$(menuElementID).css( menuCSS );
		if( isHomepage ) {
			// do nothing
		} else {
			$(menuElementID).hide();
		}
		$(this).click( function() {
			hidePromoDD();
			if( isHomepage ) {
				// do nothing
			} else {
				$(menuElementID).css('opacity', 1);
			}
			$.ddl.active = true;
			toAnim = menuElementID;
			$(menuElementID).animate( animationParams, 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);
}