

YouTubePlayer = function( containerId, videoId )
{
	if( !YouTubePlayer.STATE )
	{
		YouTubePlayer.STATE = {
				
				UNSTARTED : -1, 

				ENDED : 0, 

				PLAYING : 1, 

				PAUSED : 2,

				BUFFERING : 3,

				VIDEO_CUED : 5
			};
	}
	
	if( !YouTubePlayer.onReady )
	{
		YouTubePlayer.onReady = function( playerId )
		{
			if( YouTubePlayer.availablePlayers[playerId] )
			{
				YouTubePlayer.availablePlayers[playerId].init();
			}
		}
	}
	
	if( !YouTubePlayer.availablePlayers )
	{
		YouTubePlayer.availablePlayers = {};
	}
	
	this.containerId = containerId;
	this.player = null;
	this.playButton = null;
	this.videoId = videoId;
	this.ready = false;
	this.seeker = null;
	this.volume = null;
	this.started = false;
	this.timeView = null;
	this.inited = false;
	var This = this;
	
	this.changePlayButtonState = function( state )
		{
			if( this.ready )
			{
				if( state == YouTubePlayer.STATE.PLAYING )
				{	
					this.playButton.removeClass("play");
				}
				else
				{
					this.playButton.addClass("play");
				}
			}
		};
		
	this.changeSeekerState =  function()
		{
			this.seeker.slider( "moveTo", this.getCurrentTime(), 0 , true);
		};
		
	this.changeCurrentTimeView = function( )
		{
			var currentTime = this.__formatTime( this.getCurrentTime( true ) );
			var totalTime = this.__formatTime( this.getTotalTime( true ) );
			this.timeView.html( currentTime +" / "+ totalTime);
		};
		
	this.changeVolume = function( value )
		{
			this.volume.slider( "moveTo", value , 0 , true);
		};

	this.updateView = function()
		{
			This.changeSeekerState();
			This.changeCurrentTimeView();
		};
	
	this.toggleMute = function()
		{
			if( this.ready )
			{
				if( this.player.isMuted() )
				{
					this.player.unMute();
					this.changeVolume( this.getVolume() );
				}
				else
				{
					this.player.mute();
					this.changeVolume( 0 );
				}
								
			}
		};
	
	this.playButtonClicked = function()
		{
			if( this.ready )
			{
				var state = this.player.getPlayerState();
				if( state != YouTubePlayer.STATE.PLAYING )
				{
					
					this.player.playVideo();
					this.player.seekTo( this.player.getCurrentTime() , true );  
				}
				else
				{
					this.player.pauseVideo();
				}
			}
		};
		
	this.getVolume = function()
	{
		return this.player.getVolume();
	};

	this.getCurrentTime = function( minutes )
	{
		if( this.ready )
		{
			var currentTime = this.player.getCurrentTime();
			if( !minutes )
			{
				return currentTime;
			}
			else 
			{
				return currentTime / 60;
			}
		}
		else
		{
			return 0;
		}
	};
	
	this.__formatTime = function( floatValue )
	{
		var time = new String( floatValue );
		var minutes = "0";
		var seconds = "00";
		if( time.indexOf(".") != -1 )
		{
			var timeA = time.split(".");

			minutes = timeA[0];
			seconds = new String( Math.ceil( (new Number( "0." + timeA[1] )).valueOf() * 60 ) );
			if( seconds.length < 2)
			{
				seconds = "0" + seconds;
			}
			else if( seconds.length >= 2)
			{
				seconds = seconds.slice(0,2);
			}
			else
			{
				seconds = "00";
			}

			 
		}
		else
		{
			minutes = new String(floatValue);
			seconds = "00";
		}

		return minutes +":"+ seconds;
	};

	this.getTotalTime = function( minutes )
	{
		if( this.ready )
		{
			var totalTime = this.player.getDuration();
			if( !minutes )
			{
				return totalTime;
			}
			else 
			{
				return totalTime / 60;
			}
		}
		else
		{
			return 0;
		}
	};

	this.__setUpdateMethods = function( init )
	{
		if( init )
		{
			this.seeker.everyTime( 1000, "seeker" + this.containerId , This.updateView );
		}
		else
		{
			this.seeker.stopTime( "seeker" + this.containerId, This.updateView );	
		}
		
	};

	
	this.__slideChanged = function( event, ui ) 
	{
		
		if( This.ready )
		{
			
			This.player.seekTo( ui.value, true );
		}
	};
	
	this.__volumeChanged = function( event, ui )
	{
		if( This.ready )
		{					
			This.player.setVolume( ui.value );
			if( This.player.isMuted() )
			{
				This.player.unMute();						
			}
		}
	};

	this.__onBuffering = function()
	{
		this.__setUpdateMethods( false );
		this.changePlayButtonState( YouTubePlayer.STATE.BUFFERING );
	};
	
	this.__onPlaying = function()
	{
		this.__setUpdateMethods( true );
		if( !this.started )
		{
			this.seeker.slider({ 
				min: 0, 
				max :  This.player.getDuration(),
				startValue : 0,
				stop : This.__slideChanged 

			});

			this.volume.slider({ 
				min: 0, 
				max : 100,
				startValue : This.player.getVolume(),
				stop : This.__volumeChanged 
			});
			
			this.changePlayButtonState( YouTubePlayer.STATE.PLAYING );
			this.player.pauseVideo();
			this.started = true;
		}
		else
		{
			this.changePlayButtonState( YouTubePlayer.STATE.PLAYING );
		}

	};

	this.__onEnd = function()
	{
		this.__setUpdateMethods( false );
		this.changePlayButtonState( YouTubePlayer.STATE.PLAYING );
	};
	
	this.__onPause = function() 
	{
		this.__setUpdateMethods( false );
		this.changePlayButtonState( YouTubePlayer.STATE.ENDED );
	};

	this.__stateChanged = function( newState )
	{
		switch ( newState ) 
		{
			case YouTubePlayer.STATE.PLAYING:
				this.__onPlaying();
				break;
			case YouTubePlayer.STATE.PAUSED:
				this.__onPause();
				break;
			case YouTubePlayer.STATE.BUFFERING:
				this.__onBuffering();
				break;
			case YouTubePlayer.STATE.ENDED:
				this.__onEnd();
				break;
			default:
				break;
		}				
	};
	
	this.loadVideoById = function( videoId )
	{
		this.videoId = videoId;
		this.player.loadVideoById( this.videoId, 0 );
		$(".on_youtube", "#" + this.containerId).attr("href","http://www.youtube.com/user/WIDZEW1910TV#p/u/0/" + videoId);
	};

	

	this.__loadPlayer = function( videoId )
	{
		var t = new Object();
		this.player = $('#ytplayer' + this.containerId ).get(0);
		var onStateChangeMethod = "youTubePlayer" + this.containerId + ".__stateChanged";		
		this.player.addEventListener( "onStateChange", onStateChangeMethod );		
		this.loadVideoById( videoId );			
		this.ready = true;
		
	};
	
	this.init = function()
	{
		if( !this.inited )
		{
			this.__loadPlayer( videoId );
		}
	};
	
	this.seeker = $(".seeker","#"+this.containerId);
	this.volume = $(".volume","#"+this.containerId);
	this.playButton = $(".playButton","#"+this.containerId);	
	this.timeView = $(".current_time","#"+this.containerId);	

};

onYouTubePlayerReady  = function( playerId )
{
	if( YouTubePlayer.availablePlayers[playerId] )
	{
		YouTubePlayer.availablePlayers[playerId].init();
	}
}

Multimedia = function( containerId )
{
	this.containerId = containerId;
	this.player = $( "#player" + this.containerId );
	
	this.changeImage = function( img )
		{
			this.player.empty();
			this.player.html("<img width='256' height='190' src='"+$(img).attr("src")+"' />");
			$(".gallery_input", "#" +  this.containerId ).val($(img).attr("gallery"));
		}
	
};


		
