var promotator = {
	promoNum: 0,
	currentIdx: 1,
	markerFloatee: null,
	fadeOutTimeout: null,
	clickStop: false,
	truckPromoPresent: false,
	clickPromo: function(){
		var _this = this;
		$('#markerFloatee .marker a').live('click',function(){
			if(!$(this).parent().hasClass('truckMarker')){
				if(_this.truckPromoPresent){
					var activeNum = $('#markerFloatee .marker:last').attr('class').indexOf('activeMarker');
					if(activeNum > -1){
						genOmn.init("prop30:Close");
					}
				}
			}
			_this.clickStop = true;
			var thisObj = $(this);
			// get promo number from marker
			var num = parseFloat(thisObj.parent().attr('id').replace('markerNum',''));
			clearTimeout(_this.fadeOutTimeout);
			_this.forcePromo(num, thisObj);
			return false;
		});
	},
	hoverPromo: function(){
		var _this = this;
		var prom;
		var num;
		var thisObj;
		$('#homePageMainContentBody').bind('mouseenter', function(){
			prom = $(this).find('.promotatorPromo:visible').attr('id').toString().replace('promotatorPromoID','');
			num = parseFloat(prom);
			thisObj = $('#markerNum'+prom+' a');
			//console.log(num);
			_this.forcePromo(num, thisObj);
		});
	},
	forcePromo: function(num, thisObj, forcePlay){
		$('#homePageMainContentBody').unbind('mouseleave');
		var _this = this;
		//console.log('show called on '+num);
		
		_this.currentIdx = num;
		//console.log('your click has reset the current index to: '+_this.currentIdx);
		
		// hideall promos and show selected
		var promotatorPromos = $('#homePageMainContentBody .promotatorPromo');
		var animObjs = promotatorPromos.find('.expandingBackground, .homePromoHtml, .homePromoCta');
		promotatorPromos.stop(true, true).hide();
		animObjs.stop(true, true).hide();
		
		// show this promo
		var thisPromo = $('#homePageMainContentBody #promotatorPromoID' + _this.currentIdx);
		var thisChildren = thisPromo.find('.expandingBackground, .homePromoHtml, .homePromoCta');
		thisChildren.stop(true, true).show();
		thisPromo.stop(true, true).show();
		
		// remove active markers and set correctly
		thisObj.parent().siblings().removeClass('activeMarker');
		thisObj.parent().addClass('activeMarker');

		clearTimeout(_this.fadeOutTimeout);
		$('#homePageMainContentBody').bind('mouseleave',function(){
			$(this).unbind('mouseleave');
			_this.fadeOutTimeout = setTimeout(function(){_this.fadeOutPromo();},5000);
		});
		if(forcePlay){
			_this.fadeOutTimeout = setTimeout(function(){_this.fadeOutPromo();},5000);
		}
	},
	fadeInPromo: function(){
		var _this = this;
		if(_this.clickStop){
			//console.log('nevermind on the fadein, then');
			return false;
		} else{
			//console.log('fade in called on '+_this.currentIdx);
			// marker
			_this.markerFloatee.find('.marker').removeClass('activeMarker');
			var mark = _this.markerFloatee.find('#markerNum'+_this.currentIdx);
			mark.addClass('activeMarker');
			//
			var showSection = $('#promotatorPromoID'+_this.currentIdx); // wrapper
			var showChildren = showSection.find('.expandingBackground, .homePromoHtml, .homePromoCta'); // image and content
			showSection.show(); // show wrapper
			var dontBeGreedy = 0;
			showChildren.fadeIn(1000, function(){
				dontBeGreedy++;
				if(dontBeGreedy >= 2 && !_this.clickStop){
					_this.fadeOutTimeout = setTimeout(function(){_this.fadeOutPromo();},5000);
				}
			});
		}
	},
	fadeOutPromo: function(){
		var _this = this;
		var dontBeGreedy = 0;
		
		var hideSection = $('#promotatorPromoID'+_this.currentIdx);
		var hideChildren = hideSection.find('.expandingBackground, .homePromoHtml, .homePromoCta');
		
		_this.currentIdx++;
		if(_this.currentIdx >  _this.promoNum){
			 _this.currentIdx = 1;
		}
			
		_this.clickStop = false;
		
		hideChildren.fadeOut(1000, function(){
			dontBeGreedy++;
			if(dontBeGreedy >= 2){
				hideSection.hide();
				_this.fadeInPromo(_this.currentIdx);
			}
		});
	},
	htmlDecode: function(psEncodeString){
		var lsRegExp = /\+/g;
		return unescape(String(psEncodeString).replace(lsRegExp, " ")); 
	},
	bindTruckPromo: function(){
		var _this = this;
		$('#findBar #findTruckNames a').live('click',function(){
			var truckName = $(this).attr('href').toLowerCase();
			_this.addTruckPromo(truckName, false);
			return false;
		});
	},
	addTruckPromo: function(truckName, rotate, firstView){		
		var _this = this;
		// exterminate old promo
		if(_this.truckPromoPresent){
			$('#homePageMainContentBody #promotatorPromoID'+_this.promoNum).remove();
			$('#markerFloatee .truckMarker').remove();
			_this.promoNum--;
		}
		var newPromoNum = _this.promoNum + 1; // if 4 active promos, make this the 5th
		var newPromoHtml; // actual promotator markup
		var newMarker; // marker markup
		var newTruckPromo; // truck object
		
		// loop to find new promo
		var truckPromoObj = $.parseJSON(promotatorJson);
		var j = truckPromoObj.length;
		for(i=0;i<j;i++){
			if((truckPromoObj[i].name).toLowerCase() == truckName){
				newTruckPromo = truckPromoObj[i];
			}
		}
		if(newTruckPromo != null){
			// create markup
			newPromoHtml = '<div class="promotatorPromo" id="promotatorPromoID'+newPromoNum+'" name="'+truckName+'">'; // begin wrapper
			if(newTruckPromo.hasHtml){ // has it's own html...
				newTruckPromo.html = _this.htmlDecode(newTruckPromo.html); // decode html string
				newPromoHtml += '<div class="homePromoHtml">'+newTruckPromo.html+'</div>';
			}
			else{ // or not
				newPromoHtml += '<style type="text/css">#promotatorCta'+newPromoNum+' { left: '+newTruckPromo.left+'px; top: '+newTruckPromo.top+'px; }</style><div class="homePromoCta" id="promotatorCta'+newPromoNum+'"><a href="'+newTruckPromo.target+'">'+newTruckPromo.cta+'</a></div>';
			}
			newPromoHtml += '<div class="expandingBackground" id="promotatorImage'+newPromoNum+'"><img src="'+newTruckPromo.backgroundImage+'" alt="'+newTruckPromo.description+'" /></div>'; // image
			newPromoHtml += '</div>'; // end wrapper
			
			newMarker = '<div class="marker truckMarker" id="markerNum'+newPromoNum+'"><a href="'+newTruckPromo.target+'">&nbsp;</a></div>';
			
			// insert new promo
			_this.promoNum++;
			//$(newPromoHtml).insertBefore('#homePageMainContentBody #markerFloatee');
			$(newPromoHtml).insertAfter('#homePageMainContentBody .promotatorPromo:last');
			// insert marker
			$(newMarker).insertAfter('#markerFloatee .marker:last');
			_this.truckPromoPresent = true;
			var newPromoInt = parseFloat(newPromoNum);
			var thisObj = $('#markerNum'+newPromoNum+' a');
			if(firstView){
				_this.forcePromo(1, $('#markerNum1 a'), rotate);
			}
			else{
				_this.forcePromo(newPromoInt, thisObj, rotate);
			}
			_this.setCookie(truckName);
		}
	},
	setCookie: function(truckName){
		//console.log(truckName + ' is your cookie.');
		var truckCookieDate = new Date();
		truckCookieDate.setTime(truckCookieDate.getTime() + (1000 * 60 * 60 * 24) );
		document.cookie = "truckPresent="+truckName+"; expires="+truckCookieDate;
	},
	checkCookie: function(name){
		name = name + "=";
		var carray = document.cookie.split(';');
		for(var i=0;i < carray.length;i++) {
			var c = carray[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(name) == 0) return c.substring(name.length,c.length);
		}
		return false;
	},
	omniture: function(){
		var _this = this;
		// html-based promo link tracking
		$('.homePromoHtml .fytWrapper .fytCtas a').live('click',function(){
			// test and target
			var linkHref = $(this).attr('href');
			if((linkHref.indexOf('?')) !== -1){
				//tt_Redirect(linkHref+'&TruckLinkClick=true');
				tt_Log('TruckLinkClick');
			} else{
				//tt_Redirect(linkHref+'?TruckLinkClick=true');
				tt_Log('TruckLinkClick');
			}
			//omniture
			var truckLink = $(this).parent().parent().parent().parent().attr('name');
			var linkName = $(this).html();
			genOmn.init("events:event33,prop30:"+truckLink+"/"+linkName);
		});
		// shortcut bar clicks
		$('#findBar #findTruckNames a').live('click',function(){
			// test and target
			var linkHref = $(this).attr('href');
			//tt_Log('/shortcutbar/'+linkHref);
			//omniture
			var truckLink = $(this).html();
			var linkName = 'shortcutBar';
			genOmn.init("events:event33,prop30:"+truckLink+"/"+linkName);
		});
		// cta-based promo link tracking
		$('.homePromoCta a').live('click',function(){
			var promoTitle = ( $(this).attr('title') ) ? $(this).attr('title') : $(this).parent().siblings('.expandingBackground').children('img').attr('alt');
			if(promoTitle) genOmn.init('eVar3:Home Large Promo - '+promoTitle);
			else genOmn.init('eVar3:Home Large Promo');
		});
	},
	init: function(){
		var _this = this;
		_this.bindTruckPromo();		
		_this.promoNum = $('#homePageMainContentBody .promotatorPromo').length;
		_this.markerFloatee = $('#markerFloatee');
		_this.clickPromo();
		_this.hoverPromo();
		_this.fadeInPromo(_this.currentIdx);
		var cookieGoTime = _this.checkCookie('truckPresent');
		//console.log(cookieGoTime);
		if(cookieGoTime){
			_this.addTruckPromo(cookieGoTime, true, true);
		}
		_this.omniture();
		$(document).ready(function(){
			if(currentURLVars['ditby']){
				vgPopup.open('zimmer', '/itrucks/templates/ditbyZip/ditby.jsp', true);
			}
		});
	}
};
