/////////////////////////////////
// variables
/////////////////////////////////

// -- video current time
var currentTime = 0;

// -- length of video
var duration = null;

// -- when to pause when remaining time is...
// -- in seconds
var pauseAt = 1;

// -- all the video players
var players = new Array();

// -- the timer object
var timer = null;

// -- time passed
var timerCount = 0;

// -- currently playing video instance
var ytplayer = null;

/////////////////////////////////
// prototype functionality
/////////////////////////////////

// -- make two tabs functional
var mainTabs = new Control.Tabs('tab_control', {
	linkSelector: "ul#tabs li a",
	afterChange: function() {
		fleXenv.initialized = false;
		fleXenv.initByClass(this.activeContainer);
	}
});	

// -- make the episode tabs functional
var episodes = new Control.Tabs('episode_player', {
	linkSelector: "ul#episode_tabs li a",
	afterChange: function() {
		if(ytplayer) {
			ytplayer = $('video-'+this.activeContainer.id);
			pauseAll(this.activeContainer.id);
			if(ytplayer.playVideo)
				ytplayer.playVideo();
		}
	}
});	

// -- make two tabs functional
var toolsTabs = new Control.Tabs('tools', {
	linkSelector: "ul.tools_tabs li a"
});	

/////////////////////////////////
// functions
/////////////////////////////////

//check for flash
checkFlash();
function checkFlash() {
	if (!swfobject.hasFlashPlayerVersion("9.0.46")) {
		
		//replace image
		var el = $('video-container1');
		el.setStyle({backgroundImage:'url(img/flash_prompt.jpg)'});
		el.observe('click', function(event) {
			window.location = 'http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash';
		});
	} else {
		
		//start flash
		new Event.observe($('video-container1'), 'click', function() {
			var params = { allowScriptAccess: "always", bgcolor: '#000000'  };
		  	var atts = { id: "video-ep1" };
			swfobject.embedSWF(formatVideo(ep1_video, 1), 
									"video-container1", "381", "250", "8.0.0", null, null, params, atts);
		});
	}
}

// -- check the duration against the current time
// -- so we can test the end of the video
function handleTimer() {
	timerCount++;
	if (ytplayer.getCurrentTime) {
				
		//console.log("running" + ytplayer.getCurrentTime());
		
		currentTime = ytplayer.getCurrentTime();
		duration = ytplayer.getDuration();
	
		if (duration != null) {
			//alert(duration + "/" + currentTime);
			// $('log').innerHTML = duration - currentTime;
			if (duration - currentTime < pauseAt) {
				ytplayer.pauseVideo();
			}
		}
	}
}

//pause all
function pauseAll(id) {
	for (var i=1; i<=4; i++) {
		if('ep'+i != id) {
			if($('video-ep'+i).pauseVideo) {
				$('video-ep'+i).pauseVideo();		
			}	
		}
	}
}

// -- called when youtube player is ready
// -- to rock, starts timer
function onYouTubePlayerReady(playerId) { 
	
	if (timer) timer.stop();
	//console.log(playerId);
	
	players.push(playerId);


	ytplayer = $(playerId);
	ytplayer.addEventListener("onStateChange", "onytplayerStateChange");
	ytplayer.playVideo();
	
	//disabled 9/5/08 as per client request
	//timer = new PeriodicalExecuter(handleTimer, 0.5);

}


/**
* Fired whenever the player's state changes. Possible values are 
* unstarted (-1), ended (0), playing (1), paused (2), buffering (3), video cued (5). 
*
* When the SWF is first loaded, it will broadcast an unstarted (-1) event. 
* When the video is cued and ready to play, it will broadcast a video cued event (5).
*/
function onytplayerStateChange(newState) {
	if(newState == 0 && episodes) {
		//episodes.next();
	}
	
	if(newState == 1) {
		pauseAll(episodes.activeContainer.id);
	}
	
	
}