/********** Featured Deals App **********/
(function() {
	var featuredDeals = $('#featuredDeals'),
	dealRotator = $('#dealRotator'),
	dealsInRotation = $('#dealsInRotation'),
	dealBox = $('#dealBox'),
	dealListCollection = {
		ae: {
			dealListTitle: $('#aeDealListTitle'),
			dealListLink: $('#aeDealListLink'),
			dealList: $('#aeDealList')
		},
		aerie: {
			dealListTitle: $('#aerieDealListTitle'),
			dealListLink: $('#aerieDealListLink'),
			dealList: $('#aerieDealList')
		},
		kids: {
			dealListTitle: $('#kidsDealListTitle'),
			dealListLink: $('#kidsDealListLink'),
			dealList: $('#kidsDealList')
		}
	},
	s=s_gi(s_account),
	currentBrand;
	
	if (jsContextRoot == 'web') {
		currentBrand = 'AE';
	} else {
		currentBrand = jsContextRoot;
	}	

	dealBox.unbind('mouseleave.featuredDeals').bind('mouseleave.featuredDeals',function(evt) {
		if (!featuredDeals.data('animationInProgress')) {
			featuredDeals.data().mouseLeaveTimeout = setTimeout(function(){
				featuredDeals.trigger('activateDealRotator');
			}, 2000);
		}
	}).unbind('mouseenter.featuredDeals').bind('mouseenter.featuredDeals',function(evt) {
		clearTimeout(featuredDeals.data('mouseLeaveTimeout'));
	});
	
	// NOTE: should I use toggle instead?
	featuredDeals.find('div.topLine').unbind('click.featuredDeals').bind('click.featuredDeals',function(evt) {
		if ($(this).attr('id') == 'dealRotatorTopLine') {
			featuredDeals.trigger('activateDealBox');
		} else {
			featuredDeals.trigger('activateDealRotator');
		}
	});
	
	// TODO: this is still much slower than clicking the title bar...weird...
	featuredDeals.find('a.viewDeals').unbind('click.viewDeals').bind('click.viewDeals',function(evt) {
		evt.preventDefault();
	});
				
	featuredDeals.find('div.dealListTitle').unbind('click.viewDeals').bind('click.viewDeals',function(evt) {
		evt.preventDefault();
		featuredDeals.trigger({
			type: 'toggleDealList',
			dealList: dealListCollection[$(this).data('thisBrand')].dealList,
			dealListTitle: dealListCollection[$(this).data('thisBrand')].dealListTitle
		});
	});
	
	featuredDeals.bind('toggleDealList',function(evt) {		
		if (evt.dealList.data('open')) {
			evt.dealListTitle.find('span.openCloseMarker').html('+');
			evt.dealListTitle.find('span.openCloseText').html('view deals');
			evt.dealList.animate({height: 0}, {
				duration: 600
			});
			evt.dealList.data('open',false);
		}
		else {
			evt.dealListTitle.find('span.openCloseMarker').html('&ndash;');
			evt.dealListTitle.find('span.openCloseText').html('close deals');			
			evt.dealList.animate({height: evt.dealList.data('originalHeight')}, {
				duration: 600
			});
			evt.dealList.data('open',true);
			
			s.pageName=currentBrand+':Deals:Open:'+((evt.dealListTitle.data('thisBrand') == 'kids') ? '77kids' : evt.dealListTitle.data('thisBrand'));
			s.prop11='Deals';
			s.tl(this,'o',s.pageName);
		}
	});
	
	featuredDeals.bind('activateDealBox',function(evt) {
		featuredDeals.data('animationInProgress',true);
		clearInterval(featuredDeals.data('rotatorTimeout'));
		dealRotator.css('display','none');
		dealBox.slideDown('slow',function(){
			featuredDeals.data('animationInProgress',false);
			if (!!evt.tease) {
				featuredDeals.data().mouseLeaveTimeout = setTimeout(function() {
					featuredDeals.trigger('activateDealRotator')
				}, 2000);
			} else {
				s.pageName=currentBrand+':Deals:Open';
				s.prop11='Deals';
				s.tl(this,'o',s.pageName);			
			}
		});
	});
	
	featuredDeals.bind('activateDealRotator',function(evt) {
		clearTimeout(featuredDeals.data('mouseLeaveTimeout'));
		clearInterval(featuredDeals.data('rotatorTimeout')); // clear existing intervals
		
		featuredDeals.data('animationInProgress',true);
		dealBox.slideUp('slow',function(){
			featuredDeals.data('animationInProgress',false);
			dealRotator.css('display','block');
		});		
		featuredDeals.data().rotatorTimeout = setInterval(function () {
			featuredDeals.trigger('rotateDeal')
		}, 2500);
	});
		
	featuredDeals.bind('rotateDeal',function(evt){
		var prevDealIndex = dealsInRotation.data('prevDealIndex'),
		totalDeals = dealsInRotation.data('totalDeals'),
		currentDealIndex = 0,
		rotateInProgress = false;
		// topPos, duration are all variables, hide position
		if (!rotateInProgress) {
			rotateInProgress = true;
			
			currentDealIndex = (prevDealIndex + 1) % totalDeals;		
			dealsInRotation.find('li:eq(' + prevDealIndex + ')').animate({top: -15},1000,function() {
				$(this).css('top','15px');
			});
			dealsInRotation.find('li:eq(' + currentDealIndex + ')').animate({top: 0},1000,function() {
				rotateInProgress = false;
			});			
			dealsInRotation.data().prevDealIndex = currentDealIndex;
		}
	});
	
	// TODO: this seems like a ridiculously long initialize event
	featuredDeals.bind('initialize',function(evt) {
		var dealsObj = {
			totalDeals: dealsInRotation.find('li').length,
			prevDealIndex: 0
		};
		// init values
		featuredDeals.data('animationInProgress',false);
		
		// init omniture
		s.prop1=currentBrand;
		// s.prop11 ='Deals';
				
		// Initialize DealRotator	
		dealsInRotation.data(dealsObj);
		
		// Initialize DealBox
		featuredDeals.find('.dealListTitle').each(function(index) {
			// assign brand to each component
			var thisBrand = $(this).attr('id').replace('DealListTitle', ''),
			thisDealList = dealListCollection[thisBrand].dealList;
			
			$(this).data('thisBrand',thisBrand);
				
			// set heights of each dealList
			if (!$(this).hasClass('featuredBrandDealListTitle')) {
				thisDealList.data('originalHeight',thisDealList.height());
				thisDealList.css('height',0);
				thisDealList.data('open',false);
			}
		});
		dealBox.css({
			display: 'none',
			visibility: 'visible'
		});
			
		// NOTE: courtesy of staticPromo
		var stateCookie = $.getCookie('featuredDealsState');
		switch (stateCookie) {
			case 'dealRotator':
				clearInterval(featuredDeals.data().rotatorTimeout);
				featuredDeals.data().rotatorTimeout = setInterval(function() {					
					featuredDeals.trigger('rotateDeal');
				}, 2500);
				break;
			case 'dealBox':
				featuredDeals.trigger('activateDealBox');
				break;
			default: // TODO: do the tease...
				clearInterval(featuredDeals.data().rotatorTimeout);
				$(window).unbind('load.featuredDeals').bind('load.featuredDeals', function( evt ) {
					featuredDeals.trigger({type: 'activateDealBox',tease: true});
				});
		}
		
		$.setCookie({
			name: 'featuredDealsState',
			val: 'dealRotator',
			path: '/' + jsContextRoot + '/',
			expireMins: 15
		});
	});

	if (!!featuredDeals.length) {
		featuredDeals.trigger('initialize');
	}	
})();