var v1Rotation = {
	curPageIndex: 0,
	numPages: -1,
	curMediaElem: {},
	curVideoId: -1,
	curTabElem: {},
	curRelatedLinkElem: {},
	autoDelay: 0, //milliseconds between auto-rotation (0 = no auto-rotation)
	autoTimer: -1, //reference to interval id
	fadeDuration: 500, //milliseconds to fade up new content - not currently used due to IE issues with jquery fade
	
	init: function(_autoDelay) {
		v1Rotation.mediaElems = $("div.v1-rotation div.mediaWrapper > div[class*='-container']");
		for (var i=0; i<v1Rotation.mediaElems.length; i++) {
			v1Rotation.mediaElems[i] = $(v1Rotation.mediaElems[i]);
		}
		v1Rotation.tabElems = $("div.v1-rotation div.tabWrapper a");
		for (var i=0; i<v1Rotation.tabElems.length; i++) {
			v1Rotation.tabElems[i] = $(v1Rotation.tabElems[i]);
		}
		v1Rotation.relatedLinkElems = $("div.v1-rotation div.relatedLinksWrapper div");
		for (var i=0; i<v1Rotation.relatedLinkElems.length; i++) {
			v1Rotation.relatedLinkElems[i] = $(v1Rotation.relatedLinkElems[i]);
		}
		
		if (typeof(_autoDelay) != "undefined") {
			if (parseInt(_autoDelay) > 0) {
				v1Rotation.numPages = v1Rotation.mediaElems.length;
				v1Rotation.autoDelay = parseInt(_autoDelay);
				v1Rotation.startTimer();
			}
		}
	},
		
	startTimer: function(){
		clearInterval(v1Rotation.autoTimer);
		v1Rotation.autoTimer = setInterval(v1Rotation.goNext, v1Rotation.autoDelay);		
	},
		
	stopTimer: function(){
		clearInterval(v1Rotation.autoTimer);
	},
		
	goNext: function(){
		var newIndex = v1Rotation.curPageIndex + 1;
		if (newIndex > v1Rotation.numPages - 1) {
			newIndex = 0;
		}
		v1Rotation.setPage(newIndex, true);
	},
		
	setPage: function(index, doContinue) {
		if (v1Rotation.curPageIndex != index) {
			v1Rotation.hideElems(v1Rotation.curPageIndex);
			v1Rotation.curPageIndex = index;
			v1Rotation.showElems(v1Rotation.curPageIndex);
			
			var continueAuto = (typeof(doContinue) != "undefined") ? doContinue : false;
			if (continueAuto == false) {
				v1Rotation.stopTimer();
			}
		}
	},
		
	hideElems: function(index) {
		var hiddenVideoHolder = v1Rotation.mediaElems[index].find("a.video-image-holder:hidden");
		if (hiddenVideoHolder.length != 0) {
			//have to use a try/catch since IE's typeof returns "object" instead of "undefined"
			try {
				//need to halt video so we don't get phantom audio in IE
				var player = window["v1RotationPlayer-" + v1Rotation.curVideoId];
				var playerModule = player.getModule(APIModules.VIDEO_PLAYER);
				playerModule.cueVideo(v1Rotation.curVideoId); //incredibly, calling stop() thru javascript API doesn't work even though the function exists ???
			} catch (e) {
				//nothing to do
			}
			v1Rotation.mediaElems[index].find("div[id*='v1RotationVideoContainer']").hide();
			hiddenVideoHolder.show();
		}
		v1Rotation.mediaElems[index].hide();
		v1Rotation.tabElems[index].addClass("active");
		v1Rotation.tabElems[index].removeClass("on");
		v1Rotation.relatedLinkElems[index].hide();
	},
		
	showElems: function(index) {
		v1Rotation.mediaElems[index].show();
		v1Rotation.tabElems[index].addClass("on");
		v1Rotation.tabElems[index].removeClass("active");
		v1Rotation.relatedLinkElems[index].show();
	},
		
	showVideo: function(anchor, videoId){
		var _anchor = $(anchor);
		var videoContainer = _anchor.parent().find("div#v1RotationVideoContainer-" + videoId);
		_anchor.hide();
		videoContainer.show();
		v1Rotation.curVideoId = videoId;
		
		//have to use a try/catch since IE's typeof returns "object" instead of "undefined"
		try {
			var player = window["v1RotationPlayer-" + v1Rotation.curVideoId];
			var playerModule = player.getModule(APIModules.VIDEO_PLAYER);
			playerModule.play();
		} catch (e) {
			//nothing to do
		}
	}
};

