
/*
http://stackoverflow.com/questions/7443578/youtube-iframe-api-how-do-i-control-a-iframe-player-thats-already-in-the-html/7513356#7513356
http://code.google.com/apis/youtube/js_api_reference.html
http://code.google.com/apis/youtube/iframe_api_reference.html
http://code.google.com/apis/ajax/playground/?exp=youtube#polling_the_player
http://code.google.com/p/gdata-samples/source/browse/trunk/ytplayer/iframe/index.js
http://www.bitspace.in/2011/02/youtube-api-html5-chromeless-player.html
*/

(function($) {

	 var _player;
	 var _frameId;
	 var _autoPlay;
	 var _defaultVolume;
	 var _youtubeHolder;
	 var _youtubeChromeles;
	 var _inited=false;
	 var _self;

	 $.youtubePlayer = function(data) {
		 
		_frameId = data.frameId;
		_autoPlay = data.autoPlay;
		_defaultVolume = data.defaultVolume;
		_youtubeHolder=data.youtubeHolder;
		_youtubeChromeless=data.youtubeChromeless;
		 
		var yt = new ap_YoutubePlayer(data.mediaPath);
		return yt;
	 };
	 
	 function ap_YoutubePlayer(id){
		 
		 _self = $(this);
		 
		 var zindexfix='&amp;wmode=transparent';
		 var youtubeApi;
		 var no_controls='?controls=0';
		 var no_info='&amp;showinfo=0';
		 var videoIFrameSrc;
		 if(!_youtubeChromeless){
			 youtubeApi = '?enablejsapi=1';
			 videoIFrameSrc = 'http://www.youtube.com/embed/' + id + youtubeApi + zindexfix;
		 }else{
			 youtubeApi = '&amp;enablejsapi=1';
			 videoIFrameSrc = 'http://www.youtube.com/embed/' + id + no_controls + no_info + youtubeApi + zindexfix;
		 }
		 
		 var youtubeVideoIframe = $('<iframe />', {
				frameborder: 0,
				src: videoIFrameSrc,
				width: 100 + '%',
				height: 100 + '%',
				id: _frameId
		 });
			
		 _youtubeHolder.css('display', 'block');
		 _youtubeHolder.append(youtubeVideoIframe); 
		 
		 var tag = document.createElement('script');
		 tag.src = "http://www.youtube.com/player_api";
		 var firstScriptTag = document.getElementsByTagName('script')[0];
		 firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
	 };
	
	 ap_YoutubePlayer.prototype = {
		initVideo:function(id) {
			if(_player){
				_inited=false;
				if(_autoPlay){
					_player.loadVideoById(id);
				}else{
					_player.cueVideoById(id);
				}
			}
		},
		stop:function() {
			if(_player) _player.stopVideo();
		},
		play:function() {
			if(_player) _player.playVideo();
		},
		pause:function() {
			if(_player) _player.pauseVideo();
		},
		seek:function(val) {
			if(_player) _player.seekTo(val);
		},
		isMuted:function() {
			if(_player) return _player.isMuted();
		},
		getDuration:function() {
			if(_player) return _player.getDuration();
		},
		setVolume:function(val) {
			//Sets the volume. Accepts an integer between 0 and 100.
			if(val<0) vol=0;
			else if(val > 1) val = 1;
			if(_player) _player.setVolume(val * 100);
		}
	}
			
	window.onYouTubePlayerAPIReady = function() {
		//console.log('onYouTubePlayerAPIReady');
		_player = new YT.Player(_frameId, {
			events: {
				'onReady': onPlayerReady,
				  'onPlaybackQualityChange': onPlayerPlaybackQualityChange,
				  'onStateChange': onPlayerStateChange,
				  'onError': onPlayerError
			}
		});
	}
	
	function onPlayerReady(event) {
		//console.log('onPlayerReady');
		_player.setVolume(_defaultVolume * 100);//Sets the volume. Accepts an integer between 0 and 100.
		if(_player && _autoPlay){
			_player.playVideo();
		} 
	}
	
	function onPlayerPlaybackQualityChange(event) {
		//console.log('onPlayerPlaybackQualityChange ');
	}
	
	function onPlayerStateChange(event) {
		//console.log('onPlayerStateChange ', event.data);
		
		if(event.data == 0){
			//console.log('yt ended');
			_self.trigger('ap_YoutubePlayer.END_PLAY');
		}
		if(event.data == 1){
			if(!_inited){
				_self.trigger('ap_YoutubePlayer.START_PLAY');
				_inited=true;	
			}
			//console.log('YT.PlayerState.PLAYING');
			//_videoGallery.ytStatePlaying();//for toggling pause/play icon
		}
		if(event.data == 2){
			//console.log('YT.PlayerState.PAUSED');
			//_videoGallery.ytStatePaused();
		}
		
		/*
		
		YT.PlayerState.ENDED
		YT.PlayerState.PLAYING
		YT.PlayerState.PAUSED
		YT.PlayerState.BUFFERING
		YT.PlayerState.CUED
		
		-1 (unstarted)
		0 (ended)
		1 (playing)
		2 (paused)
		3 (buffering)
		5 (video cued).
		*/
	}
	
	function onPlayerError(e) {
		//console.log(e);
		
		/*
		event.data
		 2 – The request contains an invalid parameter value. For example, this error occurs if you specify a video ID that does not have 11 characters, or if the video ID contains invalid characters, such as exclamation points or asterisks.
		100 – The video requested was not found. This error occurs when a video has been removed (for any reason) or has been marked as private.
		101 – The owner of the requested video does not allow it to be played in embedded players.
		150 – This error is the same as 101. It's just a 101 error in disguise!
		*/
		
	}

})(jQuery);



