window.addEvent('domready', function() {
	$$(".heroSubMenu li a").removeClass("active"); //IE6
	$$(".heroSubMenu li a").addEvent("click", function(e) {
		$$(".heroSubMenu li a").removeClass("active");
		this.addClass("active");
	});
});


/*
 * HeroModuleRotation: Cycle through hero items
**/
function HeroModuleRotation() {
	
	var showHideItems = $$("div.heroSubMenu ul li");
	if(showHideItems.length > 0) {
	var showHidePopUp = $$(".heroMenuPopUp");

	//var showHideItemFirst = showHideItems[0];
	//var showHideItemLast = showHideItems[showHideItems.length-1];
	var showHideItemFirst =  $$("div.heroSubMenu ul li:first-child");
	var showHideItemLast =  $$("div.heroSubMenu ul li:last-child");
	showHideItemFirst.addClass("heroTopItem");
	showHideItemLast.addClass("heroBottomItem");
	
	showHidePopUp.setStyle('display','none');
		
	//Loop through each list item, and attach click event, which displays the content at the correct index.
	showHideItems.each(function(item, index){
		item.addEvent('click',function(e){
			e.stop();
			showHidePopUp.setStyle('display','none');
			showHidePopUp[index].setStyle('display','block');
			stop();
		});
	});
	}

	/*
	 * Hero rotation 
	**/
	var showDuration = 8000; // duration for the pause in hero module transitions
	
	var container = $('hero');
	var templates = container.getElements('div.templateSwitch');
	var links = $('heroMenuLinks');
	var currentIndex = 0;
	var interval;
	var toc = [];
	var tocActive = 'heroActive';
	var flashVideo = $('flashObject');
	
	//Hide pagination if 1 or less
	if(templates.length<=1) $$("#heroMenu, #heroMenuTransparency").dispose();
	else $$("#heroMenu, #heroMenuTransparency").setStyle("visibility", "visible");

	//Public Methods
	var start = this.start = function () { 
		interval = show.periodical(showDuration);
	}
	var stop = this.stop = function () {
		$clear(interval);
	}
	
	$$('#heroMenuLinks #placeholder').destroy();
	
	var show = function(to) {
		
		templates[currentIndex].fade('out');
		toc[currentIndex].removeClass(tocActive);
		templates[currentIndex = ($defined(to) ? to : (currentIndex < templates.length - 1 ? currentIndex+1 : 0))].fade('in');
		toc[currentIndex].addClass(tocActive);
		
		//Stop all video
		var videoObjects = $$(".heroFeature").getElements("object[id*='Video']");
		if(videoObjects!=null) {
			for(var i=0; i<videoObjects.length; i++) {
				try {
					$("Video" + i).VideoPlayerStop();
				} catch(e) { }
			}
		}
	};
	
	templates.setStyle('display','block');
	
	templates.each(function(img,i){
		toc.push(new Element('li',{
			html: '<span>'+ i +'</span>',
			'class':(i == 0 ? '' + tocActive : ''),
			events: {
				click: function(event) {
					event.preventDefault();
					//showHidePopUp.setStyle('display','none');
					show(i);
					stop();
				}
			}
		}).inject(links));
		if(i > 0) { img.set('opacity',0); }
	});
	 
	//Events
	$$('.templateSwitch').addEvent('click',function(e){
		stop();
	});	
	$$('.heroPopupClose').addEvent('click', function(){
		showHidePopUp.setStyle('display','none');
		return false;
	});

}


