/*
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
//------------------------------------

//var SWFID; // Must be set to id of swf or nothing will work.
//var obj = new Object;

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

function checkObj (playerId) {
	// alert("youTubeLoader.js : checkObj");
	if (playerId) {
		return createObj(playerId);		
	} else{
	    debugger;
		//alert("YouTubeLoader: plyerId doesn't set");
		return undefined;
	}
}

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

//------------------------------------
// SPECIAL YOUTUBE EVENT METHODS
//------------------------------------

function onYouTubePlayerReady(playerId) {{
	var obj = checkObj(playerId);
	if (obj) {{	
		obj.addEventListener('onStateChange', 'onytplayerStateChange' + playerId);
	}}	
}}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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