/*
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 () {	
	if (SWFID) {
		createObj();
		return true;
	} else{
		alert("YouTubeLoader: In order to call methods within a swf, you must first set the variable \"SWFID\"!");
		return false;
	}
}

function createObj () {	
	obj = document.getElementById(SWFID);
}

//------------------------------------
// SPECIAL YOUTUBE EVENT METHODS
//------------------------------------

function onYouTubePlayerReady(playerId) {	
	if (checkObj()) {		
		obj.addEventListener("onStateChange", "onytplayerStateChange");
	}	
}

function onytplayerStateChange(newState) { 
	obj.playerStateUpdateHandler(newState);	
	secondaryOnytplayerStateChange(newState)
}

//------------------------------------
// YOUTUBE METHODS
//------------------------------------

function loadVideoById(id, startSeconds) {	
	if (checkObj()) {
		obj.loadVideoById(id,startSeconds);
	}
}

function cueNewVideo(id, startSeconds) {	
	if (checkObj()) {
		obj.cueVideoById(id, startSeconds);
	}
}

function clearVideo() {
	// alert("youTubeLoader.js : clearVideo");
	if (checkObj()) {
		obj.clearVideo();
	}
}

function setSize(w, h) {	
	
	if (checkObj()) {
		//alert (w+" / set SIZE / "+h);
		obj.setSize(w, h);
	}
}

function play() {
	// alert("youTubeLoader.js : play");
	if (checkObj()) {
		obj.playVideo();
	}
}

function pause() {	
 //alert("youTubeLoader.js : pause");
	if (checkObj()) {
		obj.pauseVideo();
	}
}

function stop() {	
 //alert("youTubeLoader.js : stop");
	if (checkObj()) {
		obj.stopVideo();
	}
}

function seekTo(seconds) {
	if (checkObj()) {
		obj.seekTo(seconds, true);
	}
}

function getPlayerState() {
	if (checkObj()) {
		return obj.getPlayerState();
	}
}

function getBytesLoaded() {
  	if (checkObj()) {
		return obj.getVideoBytesLoaded();
	}
}

function getBytesTotal() {
  	if (checkObj()) {
		return obj.getVideoBytesTotal();
	}
}

function getCurrentTime() {
  	if (checkObj()) {
    	return obj.getCurrentTime();
	}
}

function getDuration() {
  	if (checkObj()) {
		return obj.getDuration();
	}
}

function getStartBytes() {
	if (checkObj()) {
		return obj.getVideoStartBytes();
	}
}

function setVolume(newVolume) {
	if (checkObj()) {
		obj.setVolume(newVolume);
	}
}

function getVolume() {
	if (checkObj()) {
		return obj.getVolume();
	}
}

function mute() {
	if (checkObj()) {
		obj.mute();
	}
}

function unMute() {
	if (checkObj()) {
		obj.unMute();
	}
}

function getEmbedCode() {
	if (checkObj()) {
  		return obj.getVideoEmbedCode();
	}
}

function getVideoUrl() {
	if (checkObj()) {
		return obj.getVideoUrl();
	}
}