
// general utility to init functions
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}

// create the FAVideo instance
player = new FAVideo("vid", null, 320, 240, { autoLoad:true, autoPlay:false, clickToTogglePlay: false });
player.setSkinVisible(true);
isPlaying = false;
videoURI = null;

// hides controls if no vid is selected
function hideButtons() {
	if (videoURI == null) {
		document.getElementById("video_buttons").className = "hidden";
	}
}

// switch video when selecting from list
function switchVideo() {
	var videoList = document.getElementById("video_list");
	var videos = videoList.getElementsByTagName("a");
	for (var i=0; i < videos.length; i++) {
		videos[i].onclick = function() {
			videoURI = "_videos/" + this.className + ".flv";	// build vid uri from link class name
			player.load(videoURI);								// player is the FAVideo player defined above
			playStop(player);
			document.getElementById("playStop_btn").src = "_img/player-stop.gif"; // make sure play/stop button set to stop		
			// we need to loop on more time through the list when clicking to highlight current vid in list and unset any other
			for (var i=0; i < videos.length; i++) {
				if (videos[i].className == this.className) {
					videos[i].parentNode.className = "selected";
				} else {
					videos[i].parentNode.className = "";
				}

			}
			document.getElementById("video_buttons").className = ""; // show video buttons once vid is loaded
			isPlaying = true;
			return false;
		}
	}
}

// Utility Functions
function seek(p_player, p_seek) {
	p_player.seek(p_player.getPlayheadTime() + p_seek);
}

function playStop(p_player) {
	if (videoURI != null) {
		if(isPlaying == true) {
			p_player.stop(true);
			document.getElementById("playStop_btn").src = "_img/player-play.gif";
			isPlaying = false;
		} else {
			p_player.play();
			document.getElementById("playStop_btn").src = "_img/player-stop.gif";
			isPlaying = true;
		}
	}
}
// xtraz - au kazou
function stopVideo(p_player) {
	p_player.stop();
	isPlaying = false;
}

function pauseVideo(p_player) {
	p_player.pause(true);
	isPlaying = false;
}

function playVideo(p_player) {
	p_player.play();
}

// init
addLoadEvent(hideButtons);
addLoadEvent(switchVideo);