/*
Constructs a YouTubeLoader object which uses ExternalInterface to interact with javascript
within the "youTubeLoader.js" file to create an ActionScript 3 Wrapper for the YouTube
chromeless player and API.

@author Matthew Richmond <matthew@choppingblock.com>
@version 1.0
@history 2008-10-07

@Copyright 2008 Matthew Richmond <matthew@choppingblock.com>
* 
* This file is part of Sawdust, a collection of useful frameworks
* managed by the folks at The Chopping Block, Inc.
* 
* Sawdust is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* 
* Sawdust is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU Lesser General Public License for more details.
* 
* You should have received a copy of the GNU Lesser General Public License
* along with Sawdust.  If not, see <http://www.gnu.org/licenses/>.
*/

//------------------------------------
// MAIN VARIABLES
//------------------------------------

//------------------------------------
// UTILITY METHODS
//------------------------------------

function checkObj ( SWFID ) {
// 	alert("YOUTUBE checkObj", SWFID);
	if ( SWFID && createObj( SWFID ) ) {
		return createObj( SWFID );
	} else {
		//alert("YouTubeLoader: In order to call methods within a swf, you must first set the variable \"SWFID\"!");
		return false;
	}
}

function createObj ( SWFID ) {
	// alert("youTubeLoader.js : createObj");
	return document.getElementById( SWFID );
}

//------------------------------------
// SPECIAL YOUTUBE EVENT METHODS
//------------------------------------
var sendSateChanged = {};
// var listenerSetObj = {};
function onYouTubePlayerReady(SWFID) {
	var obj;
// 	alert("onYouTubePlayerReady", SWFID);
	if ( obj = checkObj( SWFID ) ) {	

		/* Flash does not handle anonymous functions, so we need an array of functions, one for each player */
		sendSateChanged[ SWFID ] = function ( newState ) { 
			var obj;
			if ( obj = checkObj( SWFID ) ) {
				obj.playerStateUpdateHandler( newState );
			}
		};
// 		if( ! listenerSetObj[ SWFID ] ){
// 			listenerSetObj[ SWFID ] = true;
		obj.addEventListener( "onStateChange", "sendSateChanged."+ SWFID ); 
// 		}
	}
	
	// PLEASE NOTE: For the purpose of this demo:
	// This calls a secondary method located in the index.html file allowing the html display to update.
	// You will most likely not need this, it's gross, remove this when you implement this code.
	//secondaryOnYouTubePlayerReady(playerId);
}


//------------------------------------
// YOUTUBE METHODS
//------------------------------------

function destroy( SWFID ) {
	var obj;
// 	alert("youTubeLoader.js : loadVideoById");
	if ( obj = checkObj( SWFID ) ) {
		obj.removeEventListener( "onStateChange", "sendSateChanged."+ SWFID ); 
// 		alert("222 youTubeLoader.js : loadVideoById:  "+ SWFID +" <> "+ obj );
// 		obj.stopVideo();
// 		obj.pauseVideo();
		obj.clearVideo();
	}
}
function loadVideoById(SWFID, id, startSeconds) {
// 	alert("YOUTUBE loadVideoById", SWFID);
	var obj;
	// alert("youTubeLoader.js : loadVideoById");
	if (obj = checkObj(SWFID)) {
		obj.loadVideoById(id,startSeconds);
	}
}

function cueNewVideo(SWFID, id, startSeconds) {
// 	alert("YOUTUBE cueNewVideo", SWFID);
	var obj;
	// alert("youTubeLoader.js : loadVideoById");
	if (obj = checkObj(SWFID)) {
		obj.cueVideoById(id, startSeconds);
	}
}

function clearVideo(SWFID) {
// 	alert("YOUTUBE clearVideo", SWFID);
	var obj;
	if (obj = checkObj(SWFID)) {
		alert("youTubeLoader.js : clearVideo");
		obj.clearVideo();
	}
}

function setSize(SWFID, w, h) {
// 	alert("YOUTUBE setSize", SWFID);
	var obj;
	// alert("youTubeLoader.js : setSize");
	if (obj = checkObj(SWFID)) {
		obj.setSize(w, h);
	}
}

function play(SWFID) {
// 	alert("YOUTUBE play", SWFID);
	var obj;
	// alert("youTubeLoader.js : play");
	if (obj = checkObj(SWFID)) {
		obj.playVideo();
	}
}

function pause(SWFID) {
// 	alert("YOUTUBE pause", SWFID);
	var obj;
	// alert("youTubeLoader.js : pause");
	if (obj = checkObj(SWFID)) {
		obj.pauseVideo();
	}
}

function stop(SWFID) {
// 	alert("YOUTUBE stop", SWFID);
	var obj;
	// alert("youTubeLoader.js : stop");
	if (obj = checkObj(SWFID)) {
		obj.stopVideo();
	}
}

function seekTo(SWFID, seconds) {
// 	alert("YOUTUBE seekTo", SWFID);
	var obj;
  	// alert("youTubeLoader.js : seekTo");
	if (obj = checkObj(SWFID)) {
		obj.seekTo(seconds, true);
	}
}

function getPlayerState(SWFID) {
// 	alert("YOUTUBE getPlayerState", SWFID);
	var obj;
	// alert("youTubeLoader.js : getPlayerState");
	if (obj = checkObj(SWFID)) {
		return obj.getPlayerState();
	}
}

function getBytesLoaded(SWFID) {
	var obj;
  	// alert("youTubeLoader.js : getBytesLoaded");
	if (obj = checkObj(SWFID)) {
		return obj.getVideoBytesLoaded();
	}
}

function getBytesTotal(SWFID) {
	var obj;
  	// alert("youTubeLoader.js : getBytesTotal");
	if (obj = checkObj(SWFID)) {
		return obj.getVideoBytesTotal();
	}
}

function getCurrentTime(SWFID) {
	var obj;
  	// alert("youTubeLoader.js : getCurrentTime");
	if (obj = checkObj(SWFID)) {
    	return obj.getCurrentTime();
	}
}

function getDuration(SWFID) {
	var obj;
  	// alert("youTubeLoader.js : getDuration");
	if (obj = checkObj(SWFID)) {
		return obj.getDuration();
	}
}

function getStartBytes(SWFID) {
	var obj;
	// alert("youTubeLoader.js : getStartBytes");
	if (obj = checkObj(SWFID)) {
		return obj.getVideoStartBytes();
	}
}

function setVolume(SWFID, newVolume) {
	var obj;
	// alert("youTubeLoader.js : setVolume");
	if (obj = checkObj(SWFID)) {
// 		alert( "obj.setVolume:  "+ obj +" <> "+ newVolume );
		obj.setVolume(newVolume);
	}
}

function getVolume(SWFID) {
	var obj;
	// alert("youTubeLoader.js : setVolume");
	if (obj = checkObj(SWFID)) {
		return obj.getVolume();
	}
}

function mute(SWFID) {
	var obj;
	// alert("youTubeLoader.js : mute");
	if (obj = checkObj(SWFID)) {
		obj.mute();
	}
}

function unMute(SWFID) {
	var obj;
	// alert("youTubeLoader.js : unMute");
	if (obj = checkObj(SWFID)) {
		obj.unMute();
	}
}

function getEmbedCode(SWFID) {
	var obj;
	// alert("youTubeLoader.js : getEmbedCode");
	if (obj = checkObj(SWFID)) {
  		return obj.getVideoEmbedCode();
	}
}

function getVideoUrl(SWFID) {
	var obj;
	// alert("youTubeLoader.js : getVideoUrl");
	if (obj = checkObj(SWFID)) {
		return obj.getVideoUrl();
	}
}
