/********** Featured Deals App **********/
var featuredDealsApp = function() {
	var featuredDeals = $('#featuredDeals'),
	dealRotator = $('#dealRotator'),
	dealsInRotation = $('#dealsInRotation'),
	dealBox = $('#dealBox'),
	DEALLIST_TITLE = "dealListTitle",
	DEALLIST_LINK = "dealListLink",
	DEALLIST = "dealList",
	dealListCollection = {
		ae: {
			"dealListTitle": $('#aeDealListTitle'),
			"dealListLink": $('#aeDealListLink'),
			"dealList": $('#aeDealList')
		},
		aerie: {
			"dealListTitle": $('#aerieDealListTitle'),
			"dealListLink": $('#aerieDealListLink'),
			"dealList": $('#aerieDealList')
		},
		kids: {
			"dealListTitle": $('#kidsDealListTitle'),
			"dealListLink": $('#kidsDealListLink'),
			"dealList": $('#kidsDealList')
		}
	},
	// dealListCollection["ae"]["dealListTitle"] = $("#" + "ae" + "DealListTitle");
	s=s_gi(s_account),
	currentBrand,
	EVT_NAMESPACE = ".featuredDeals",
	ACT_DEALROTATOR = "activateDealRotator",
	ACT_DEALBOX = "activateDealBox",	
	EVT_MOUSELEAVE = "mouseleave",
	EVT_MOUSEENTER = "mouseenter",
	EVT_LOAD = "load",
	EVT_CLICK = "click",
	THIS_BRAND = "thisBrand";
	
	if (jsContextRoot == 'web') {
		currentBrand = 'AE';
	} else {
		currentBrand = jsContextRoot;
	}

	dealBox.unbind(EVT_MOUSELEAVE+EVT_NAMESPACE).bind(EVT_MOUSELEAVE+EVT_NAMESPACE,function(evt) {
		if (!featuredDeals.data('animationInProgress')) {
			featuredDeals.data().mouseLeaveTimeout = setTimeout(function(){
				featuredDeals.trigger(ACT_DEALROTATOR);
			}, 2000);
		}
	}).unbind(EVT_MOUSEENTER+EVT_NAMESPACE).bind(EVT_MOUSEENTER+EVT_NAMESPACE,function(evt) {
		clearTimeout(featuredDeals.data('mouseLeaveTimeout'));
	});
	
	// NOTE: should I use toggle instead?
	featuredDeals.find('div.topLine').unbind(EVT_CLICK+EVT_NAMESPACE).bind(EVT_CLICK+EVT_NAMESPACE,function(evt) {
		if ($(this).attr('id') == 'dealRotatorTopLine') {
			featuredDeals.trigger(ACT_DEALBOX);
		} else {
			featuredDeals.trigger(ACT_DEALROTATOR);
		}
	});
	
	// TODO: this is still much slower than clicking the title bar...weird...
	featuredDeals.find('a.viewDeals').unbind(EVT_CLICK+EVT_NAMESPACE).bind(EVT_CLICK+EVT_NAMESPACE,function(evt) {
		evt.preventDefault();
	});
	
	featuredDeals.find('div.dealListTitle').unbind(EVT_CLICK+EVT_NAMESPACE).bind(EVT_CLICK+EVT_NAMESPACE,function(evt) {
		evt.preventDefault();
		featuredDeals.trigger({
			type: 'toggleDealList',
			dealList: dealListCollection[$(this).data(THIS_BRAND)].dealList,
			dealListTitle: dealListCollection[$(this).data(THIS_BRAND)].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(THIS_BRAND) == 'kids') ? '77kids' : evt.dealListTitle.data(THIS_BRAND));
			s.prop11='Deals';
			s.tl(this,'o',s.pageName);
		}
	});
	
	featuredDeals.bind(ACT_DEALBOX,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(ACT_DEALROTATOR)
				}, 2000);
			} else {
				s.pageName=currentBrand+':Deals:Open';
				s.prop11='Deals';
				s.tl(this,'o',s.pageName);
			}
		});
	});
	
	featuredDeals.bind(ACT_DEALROTATOR,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;
		
		// 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(THIS_BRAND,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 cookieName = "featuredDealsState",
		stateCookie = $.getCookie(cookieName);
		switch (stateCookie) {
			case 'dealRotator':
				clearInterval(featuredDeals.data().rotatorTimeout);
				featuredDeals.data().rotatorTimeout = setInterval(function() {
					featuredDeals.trigger('rotateDeal');
				}, 2500);
				break;
			case 'dealBox':
				featuredDeals.trigger(ACT_DEALBOX);
				break;
			/*default: // TODO: do the tease...
				clearInterval(featuredDeals.data().rotatorTimeout);
				$(window).unbind(EVT_LOAD+EVT_NAMESPACE).bind(EVT_LOAD+EVT_NAMESPACE, function( evt ) {
					featuredDeals.trigger({type: ACT_DEALBOX,tease: true});
				});*/
		}
		
		$.setCookie({
			name: cookieName,
			val: 'dealRotator',
			path: '/' + jsContextRoot + '/',
			expireMins: 15
		});
	});

	if (!!featuredDeals.length) {
		featuredDeals.trigger('initialize');
	}
};
