
/**
*	Hero Object
*   
*	@pre: heroID - article ID, unique identifier for DOM structure elements
*		  rotSpeed - rotation speed in milliseconds
*		  numItems - number of hero items (number of articles in rotation)
*		  nextPrevFlag - flag passed to tell if we need pager links or not
*
*	@properties: heroID - article ID, set by heroID(pre)
*				 rotateSpeed - rotation speed in milliseconds, set by rotSpeed(pre)
*				 disableRotate - flag, 0 continue rotating, 1 stop rotating permanently
*				 currHeroContentItem - current hero content item number
*				 numHeroContentItems - number of hero items/articles, set by numItems(pre)
*   			 nextPrevFlag - flag used to determine whether we have next/prev links
*				 
*   @functions: init() - perform initialization
*				rotateHeroContent() - perform rotation
*
*	@instantiation: var varName = new heroObject(heroID, rotSpeed, numItems);
*					varName.init();	
*		    
**/
function heroObject(heroID, rotSpeed, numItems, heroType) {
	var self = this;
	
	// object properties
	self.heroID = heroID;
	self.rotateSpeed = rotSpeed;
	self.rotatePause = 0;
	self.disableRotate = 0;
	self.currHeroContentItem = 1;
	self.numHeroContentItems = numItems;
	self.heroType = heroType;
	self.disablePrevNext = 0;

	//object methods 

	/** Initialization function **/
	self.init = function() {
		if ( self.numHeroContentItems > 1 ) { //only start the rotation if we have more than 1
			setTimeout(self.startRotate, heroModuleTimerOffset);
			heroModuleTimerOffset += 1200;
		}
		// turn on first content block
		$('#hero_content_' + self.heroID + '_1').css('display','block');

		// Initialize Nav to have the first class 'on'
		$('#hero' + self.heroID + ' .heroNavItem:first').addClass('navOn');

		// set event listener for hover state pause
		$("#hero" + self.heroID).hover(
			function(){
				self.rotatePause = 1;
			},
			function(){
				self.rotatePause = 0;
			}
		);

		// Set events for left/right links if the flag is set
		if ( self.heroType == "expertModule" ) {
			
			$('#left' + self.heroID).click(function(){
				if (self.disablePrevNext == 0) {
					self.rotateLeft();
					self.disableRotate = 1;
				}
			});
			$('#right' + self.heroID).click(function(){
				if (self.disablePrevNext == 0) {
					self.rotateRight();
					self.disableRotate = 1;
				}
			});
		} else if (self.heroType == 'noNav') {
				//don't mess with links if there's no nav
			
		} else { // this isn't a sidebar, it's a hero
			// Handle nav clicks.  
			$('#hero' + self.heroID + ' .heroNavItem a').click(
				function() {
					//Disable auto-rotating on any click
					self.disableRotate = 1;  
	
					//Change Nav to show the right item (abstract this later)
					$('#hero' + self.heroID + ' .heroNavItem').removeClass('navOn');

					// Check if the parent is heroNavItem, if so add 'navOn', if not add it to the parent of the parent
					//      note: this is brittle, DOM dependant.  refactor later to be more robust
					var temp = $(this).parent().attr('class');
					if ( typeof(temp) != 'undefined' && temp.match('heroNavItem') ) {
						$(this).parent().addClass('navOn');
					} else {
						$(this).parent().parent().addClass('navOn');
					}
					
	
					// call content change function passing the href of the nav link (which is the same as the id of the new content)
					temp = $(this).attr('href');
					$('#hero' + self.heroID + ' .hero_content_item:visible').fadeOut('fast', function(){
						$(temp).fadeIn('fast');
					});
					return false;
				}
			);
		}

	}

	/** Rotation start function **/
	self.startRotate = function() {
		self.intervalID = setInterval(self.rotateHeroContent, self.rotateSpeed);
	}

	/** Rotate Hero Content **/
	self.rotateHeroContent = function() { 
		if ( self.disableRotate == 1 ) {  // rotating isn't disabled, if it is no timeout will be set and rotating stops permanently
			clearInterval(self.intervalID);
		} else {
			if ( self.rotatePause != 1 && pauseAllHeros != 1) {  // rotating is paused, don't rotate 
				self.rotateRight();	
			} //if (self.rotatePause...
		}// else
	} //self.rotateHeroContent = function ...

	/** Rotate content left/back **/
	self.rotateLeft = function() {
		self.disablePrevNext = 1;
		var navItem = '#hero' + self.heroID + ' .heroNavItem'
		if ( self.currHeroContentItem > 1 ) {
			$(navItem).removeClass('navOn');
			$(navItem).eq(self.currHeroContentItem-2).addClass('navOn');
			$('#hero_content_' + self.heroID + "_" + self.currHeroContentItem).fadeOut('fast', function(){
				self.currHeroContentItem--;
				$("#hero_content_" + self.heroID + "_" + self.currHeroContentItem).fadeIn('fast', function(){
					self.disablePrevNext = 0;
				});
			});
		} else {
			$(navItem).removeClass('navOn');
			$(navItem).eq(self.numHeroContentItems - 1).addClass('navOn');
			$('#hero_content_' + self.heroID + "_" + self.currHeroContentItem).fadeOut('fast', function(){
				$("#hero_content_" + self.heroID + "_" + self.numHeroContentItems).fadeIn('fast', function(){
					self.disablePrevNext = 0;
				});
			});
			self.currHeroContentItem = self.numHeroContentItems;
		}
	}

	/** Rotate content Right/forward **/
	self.rotateRight = function() {
		self.disablePrevNext = 1;
		var navItem = '#hero' + self.heroID + ' .heroNavItem'
		if ( self.currHeroContentItem < self.numHeroContentItems) {
			$(navItem).removeClass('navOn');
			$(navItem).eq(self.currHeroContentItem).addClass('navOn');
			$('#hero_content_' + self.heroID + "_" + self.currHeroContentItem).fadeOut('fast', function(){
				self.currHeroContentItem++;
				$("#hero_content_" + self.heroID + "_" + self.currHeroContentItem).fadeIn('fast', function(){
					self.disablePrevNext = 0;
				});
			});
		} else {
			$(navItem).removeClass('navOn');
			$(navItem).eq(0).addClass('navOn');
			$('#hero_content_' + self.heroID + "_" + self.currHeroContentItem).fadeOut('fast', function(){
				$("#hero_content_" + self.heroID + "_1").fadeIn('fast', function(){
					self.disablePrevNext = 0;
				});
			});
			self.currHeroContentItem = 1;
		}
	}
	
} // function heroObject(...


/**
*  This is a hack/fix for IE6's png support/css issue
*/
$(function(){
	if ( jQuery.browser.msie && jQuery.browser.version < 7.0) {
		$('.video_hero_watch_now').css('position','absolute');
	}
});






