var onYouTubePlayerReady;
var onYouTubeStateChange;
var onYouTubeError;

var MooTube = new Class({
	
	Implements:[Options,Events],
	
	options:{
		defaultVideo:false,
		autoStart:false,
		width:425,
		height:356,
		catchLinks:true
	},
	
	initialize:function(target,options){
		this.playerReady = false;
		this.setOptions(options);
		this.width = this.options.width;
		this.height = this.options.height;
		
		this.duration = false;
		
		this.player = new Swiff('http://www.youtube.com/apiplayer?enablejsapi=1&playerapiid=mootube',{
			id: "myytplayer",
			width:this.width,
			height:this.height,
			container:$(target),
			params:{
				allowScriptAccess: "always",
				bgcolor: "#cccccc"
			}
		});
		
		onYouTubePlayerReady = this.onPlayerReady.bind(this);
		onYouTubeStateChange = this.onStateChange.bind(this);
		onYouTubeError = this.onError.bind(this);
	},
	
	onPlayerReady:function(playerId){
		this.playerReady = true;
		this.player.remote('addEventListener','onStateChange', 'onYouTubeStateChange');
		this.player.remote('addEventListener','onError', 'onYouTubeError');
		if(this.options.defaultVideo){
			if(this.options.autoStart){
				this.play(this.options.defaultVideo);
			}else{
				this.cue(this.options.defaultVideo);
			}
		}
		if(this.options.catchLinks){
			window.addEvent('click',function(e){
				var target = $(e.target);
				if(target.match('img')){
					target = target.getParent('a');
					if(!target){return;}
				}
				if(target.match('a')){
					var href = target.get('href');
					if(href.contains('youtube.com/watch?v=')){
						e.stop();
						this.play(href);
					}
				}
			}.bind(this));
		}
		this.fireEvent('onReady');
	},
	
	onStateChange:function(state){
		this.state = state
		switch(state){
			case -1:
				this.status='playerReady';
				break;
			case 0:
				this.duration=false;
				this.status='stoped';
				this.fireEvent('onEnded');
				break;
			case 1:
				if(!this.duration){
					this.getDuration();
					this.fireEvent('onStart',this.duration);
				}
				this.status='playing';
				this.fireEvent('onPlay');
				break;
			case 2:
				this.status='paused';
				this.fireEvent('onPause');
				break;
			case 3:
				this.status='buffering';
				this.fireEvent('onBuffering');
				break;
			case 5 :
				this.status='videoReady';
				this.fireEvent('onVideoReady');
				break;
		};
		console.log(this.state);
	},
	
	onError:function(error){
		switch(error){
			case 100:
				this.fireEvent('onNotFound');
				break;
			case 101:
				this.fireEvent('onNotAllowed');
				break;
		};
	},
	
	//play current video if no vid passed, or the new vid.
	play:function(vid,offset){
		if(this.playerReady){
			if(vid){
				if(vid.indexOf('=')>-1){
					vid=vid.split('=')[1];
				}
				this.player.remote('loadVideoById',vid,offset);
			}else if([2,3,5].contains(this.state)){
				this.player.remote('playVideo');
			}
		}
	},
	
	//cue passed video id
	cue:function(vid,offset){
		if(this.playerReady){
			if(vid.indexOf('=')>-1){
				vid=vid.split('=')[1];
			}
			this.player.remote('cueVideoById',vid,offset);
		}
	},
	
	//pause
	pause:function(){
		if(this.playerReady && (this.state == 1 || this.state == 3)){
			this.player.remote('pauseVideo');
		}
	},
	
	//play/pause
	playPause:function(){
		if(this.playerReady){
			if(this.state == 1 || this.state == 3){
				this.pause();
			}else{
				this.play();
			}
		}
	},
	
	//stop video. Video cannot be resume, you can only load a new one from here
	stop:function(){
		if(this.playerReady){
			this.player.remote('stopVideo');
		}
	},
	
	//clear the player
	clear:function(){
		if(this.playerReady){
			if(this.state != 0){
				this.stop();
			}
			this.player.remote('clearVideo');
		}
	},
	
	//mute player's sound
	mute:function(){
		if(this.playerReady){
			this.player.remote('mute');
		}
	},
	
	//unmute player's sound
	unMute:function(){
		if(this.playerReady){
			this.player.remote('unMute');
		}
	},
	
	//toggle player's sound
	toggleMute:function(){
		if(this.playerReady){
			if(this.player.remote('isMuted')){
				this.unMute();
			}else{
				this.mute();
			}
		}
	},
	
	//set volume, mute if set to 0, unmute else
	setVolume:function(vol){
		if(this.playerReady){
			vol = vol.limit(0,100);
			if(!vol){
				this.mute();
			}else{
				this.player.remote('setVolume',vol);
				if(this.player.remote('isMuted')){
					this.unMute();
				}
			}
		}
	},
	
	//returns player's volume
	getVolume:function(){
		if(this.playerReady){
			return this.player.remote('getVolume');
		}
		return false;
	},
	
	getTime:function(){
		if(this.playerReady){
			return this.player.remote('getCurrentTime');
		}
		return false;
	},
   
	getDuration:function(){
		if(this.playerReady){
			if(!this.duration){
				this.duration=this.player.remote('getDuration');
			}
			return this.duration;
		}
		return false;
	},
	
	seek:function(time){
		if(this.playerReady && [1,2,3].contains(this.state)){
			this.player.remote('seekTo',time,true);
		}
	},	
	
	//set new size
	setSize:function(width,height){
		if(this.playerReady){
			if($type(width)=='number'){
				this.width = width;
			}
			if($type(height)=='number'){
				this.height = height;
			}
			this.player.remote('setSize',this.width,this.height);
		}
	}
});