var YouTubeViewer = new Class({
	// youtube video viewer
	Implements: [Options,Events, Chain], 						  
	 // menu options. don't put a comma after the last option!
	options: {
		/* how to get the trigger element (e.g. a link class) */
		triggers:null,
		/* width,height */
		dimensions:[640,390]
	}, 
	
	log: function(){
		return false;
		//if(window['console'] && window['console'].firebug) console.log.apply(null, arguments);
		
	},
	// this function runs whenever a new instance of this class is made.
	initialize: function(options){ 
		this.setOptions(options);
		if(!this.options.triggers) return false;
		if( (Browser.ie && Browser.version <= 8)  || Browser.Platform.ios || Browser.Platform.android) return false;
		
		
		this.vidDimensions = {'w':this.options.dimensions[0], 'h' : this.options.dimensions[1]}
		
		document.getElements(this.options.triggers).each(
			
			/* loop thru all triggers on page */
			function(item, index, array){
				
				/* get video id from a link rel property */
				item.VideoId = this.extractYouTubeVideoId(item.get('href'));
				if(!item.VideoId) return false;
				
				/* replace href */
				//item.set('href',window.location + '#'+item.VideoId);
				
				//this.log('item.VideoId=', item.VideoId);
				
				/* add 'play button' overlay */
				//this.Trigger.grab(new Element('span',{'class':'video-thumb-overlay'}).setStyle('opacity',0.7));
				
				/* add hover fx to trigger */
				//this.Trigger.fx = new Fx.Tween(this.Trigger.getElement('span'),{'property':'opacity','duration':'short','link':'cancel'});
	
				//this.Trigger.addEvent('mouseover',this.fadeTrigger.bind(this,{'fade':'in'}))
				//this.Trigger.addEvent('mouseout',this.fadeTrigger.bind(this,{'fade':'out'}))
				
				/* add event to trigger */
				item.addEvent('click',
					function(){
						item.addClass('visited');
						this.showPanel({'VideoId':item.VideoId});
						return false;
				}.bind(this));
				
			},this
		);
	},
	
	extractYouTubeVideoId : function(url){
   	if(!url)return false;
		pos = url.indexOf('v=');
		
		if(pos == -1) {
			return false;
		}else{
			pos += String('v=').length;
		}
		
   	pos2 = url.indexOf('&',pos+1);
   	if(pos2 == -1){
   		pos2 = url.length;
   	}

   	r = url.substr(pos, pos2-pos);
   	
   	return r;
   },
	
	fadeTrigger:function(paramArray){
		//this.log(paramArray);
		if(paramArray['fade'] == 'in'){
			this.Trigger.fx.start(1);
		}else{
			this.Trigger.fx.start(0.7);
		}
		
	},
	
	showPanel:function(params){
		/* Modal Dialogue */
		if(!this.Dialogue) {
			this.Dialogue = new ModalDialogue({'bg':'dialogue-bg','container':'DialogueOverlay','emptyOnClose':false});
			this.Dialogue.container.addClass('youTubePlayer');
		}
		
		
		
		//var dialogueLining = 'lining';
		this.Dialogue.container.getElement('div.lining').grab( new Element('div' , {'id': 'YouTubeFlashPlayer'}));
		
		//if(this.Dialogue.container.getElement('div.'+dialogueLining).getProperty('html') == ''){
			/* add new content every time */
			var vidParams = '?rel=0&autoplay=1&enablejsapi=1&fs=1';
			//this.Dialogue.container.getElement('div.lining').setProperty('html', '<object width="' + this.vidDimensions.w + '" height="' + this.vidDimensions.h + '"><param name="id" value="YouTubeFlashPlayer"><param name="movie" value="http://www.youtube.com/v/'+ this.VideoId + vidParams + '"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param><embed id="YouTubeFlashPlayer" src="http://www.youtube.com/v/'+params.VideoId+vidParams + '" type="application/x-shockwave-flash" allowscriptaccess="always" width="'+this.vidDimensions.w+'" height="'+this.vidDimensions.h+'" allowfullscreen="true"></embed></object>');
			/* use swfobject */
			swfobject.embedSWF("http://www.youtube.com/v/"+ params.VideoId + vidParams  , "YouTubeFlashPlayer", this.vidDimensions.w, this.vidDimensions.h, "10.1.0", "expressInstall.swf", {}, {'wmode':'transparent','allowFullScreen':true}, {},function(e){ this.swfObjectInit(e) }.bind(this) );

		
		//}
		/* add pause on fade out  */
		this.Dialogue.addEvent('fadeBgOut',function(){$('YouTubeFlashPlayer').dispose();});
		/* add play on fade in */
		this.Dialogue.addEvent('fadeDialogueLiningIn',this.playVideo.bind(this));
		
		/* get video object */
		//this.VideoPlayer = this.Dialogue.container.getElementById('YouTubeFlashPlayer');
		
		//this.log(this.VideoPlayer);
		/* fade in */
		this.Dialogue.fadeInOut({'fade':'in'});
		
	},
	swfObjectInit:function(e){
//		Properties of this event object are:
//	    success, Boolean to indicate whether the embedding of a SWF was success or not
//	    id, String indicating the ID used in swfobject.registerObject
//	    ref, HTML object element reference (returns undefined when success=false) 
			
			this.log('e.success=',e.success);
			if(e.success){
				this.VideoPlayer = e.ref;
				this.log('this.VideoPlayer=', this.VideoPlayer);
			}else{
				this.log('e=', e);
			}
	},
	pauseVideo:function(){
		/* remove the vid altogether */
		if(this.VideoPlayer){
			document.id(this.VideoPlayer.get('id')).dispose();
		}
	},
	
	playVideo:function(){
		/* play the Flash Player, if it is paused */
		//if(this.VideoPlayer && this.VideoPlayer.getPlayerState()==2)this.VideoPlayer.playVideo();
		if(this.VideoPlayer && this.VideoPlayer.playVideo){
			this.VideoPlayer.playVideo();
		}
	}
	
});

