/*
	movieLayer.js 
	Thomas BARDY (10/10/2008)
	affiche les vidéos dans un layer quand c'est possible 

	nécessite la librairies prototype 

*/
	var VideoLayer = new Class.create();
	
	VideoLayer.prototype ={
		vars:  $H({
				//tous les variables nécessaires  
				regFindExps:  $H({'Youtube': /youtube/gi , 'DailyMotion':/dailymotion/gi, 'GoogleVideo':/video.google/gi, 'default':/.*/gi}),
				templates: {Youtube: new Template('<object width="#{width}" height="#{height}">'+
							'<param name="movie" value="#{url}"></param><param name="allowFullScreen" value="true"></param>'+
							'<embed src="#{url}" type="application/x-shockwave-flash" allowfullscreen="true" width="#{width}" height="#{height}"></embed>'+
							'</object>'),
							DailyMotion: new Template('<object width="#{width}" height="#{height}">'+
							'<param name="movie" value="#{url}"></param><param name="allowFullScreen" value="true"></param><param name="allowScriptAccess" value="always"></param>'+
							'<embed src="#{url}" type="application/x-shockwave-flash" width="#{width}" height="#{height}" allowFullScreen="true" allowScriptAccess="always"></embed></object>'),
							GoogleVideo: new Template('<embed id="VideoPlayback" src="#{url}" style="width:#{width};height:#{height}" allowFullScreen="true" allowScriptAccess="always" type="application/x-shockwave-flash"> </embed>')
							},
				id: 		"videoLayerDiv",
				idHeader:	"videoLayerHeader",
				idInner:    "videoLayerDivInner",
				title:      "Video",
				tab: 		null,
				aElement: "a.videoA",
				width:	   425,
				height:    344,
				transformURL : {Youtube : function(sUrlOld) {
									sUrlOld = new String(sUrlOld);
									return "http://www.youtube.com/v/" + sUrlOld.toQueryParams().v + "&hl=fr&fs=1";
								},
								DailyMotion : function(sUrlOld) {
									sUrlOld = new String(sUrlOld);
									var vUrlOld = sUrlOld.split("/");
									return "http://www.dailymotion.com/swf/" + vUrlOld[vUrlOld.length - 1] + "&related=0";
								},
								GoogleVideo : function(sUrlOld) {
									sUrlOld = new String(sUrlOld);										
									return "http://video.google.com/googleplayer.swf?docid=" + sUrlOld.toQueryParams().docid + "&hl=fr&fs=true";
								}
							   }								
								
				}),
				
		initialize: function(parameters){
			//initialisation
			var oDiv;
			var oDivHeader;
			var oDivBody;
			
			var myThis = this;			
			
			this.vars.update(parameters);
			
			var oDivWindowLayer       = new Element('div', {'id':this.vars.get('id'), 'class': 'windowLayer'}).setStyle(
							{'position':'absolute', 'top':'0', 'left':'0', 'z-index':'100',  'display':'none', 'width':this.vars.get('width') + 13}
							);
			var oDivWindowLayerLeft			= new Element('div', {'class': 'windowLayerLeft'});
			var oDivWindowLayerTitle		= new Element('div', {'class' : 'windowLayerTitle'});
			var oDivWindowLayerCloseBottom	= new Element('input', {'class' : 'windowLayerCloseButton '});
			var oDivWindowLayerTitleText	= new Element('p',	 {'class' : 'windowLayerTitleText'});
			var oDivWindowLayerBody			= new Element('div', {'class' : 'windowLayerBody'});
			var oDivWindowLayerInner		= new Element('div', {'id':this.vars.get('idInner'), 'class' : 'windowLayerInner'}).setStyle({'position':'relative', 'width':this.vars.get('width'), 'height':this.vars.get('height')});
			
			this.vars.set('tab',new Hash());
			
			$(document.body).insert(oDivWindowLayer);
			
			oDivWindowLayer.insert(oDivWindowLayerLeft);
			
			oDivWindowLayerLeft.insert(oDivWindowLayerTitle);
			oDivWindowLayerLeft.insert(oDivWindowLayerBody);			
			
			oDivWindowLayerBody.insert(oDivWindowLayerInner);	
			oDivWindowLayerTitle.insert(oDivWindowLayerCloseBottom);
			oDivWindowLayerTitle.insert(oDivWindowLayerTitleText);
			oDivWindowLayerTitleText.update(this.vars.get('title'));
			
			new Draggable(oDivWindowLayer,{delay:250});
			$$(this.vars.get('aElement')).each(function(e){myThis.configure(e);}); 
			
			Event.observe(oDivWindowLayerCloseBottom, 'click', this.close.bindAsEventListener(this));
		},
		
		close: function(){
			//close
			var oDiv = $(this.vars.get('id'));
			var oDivInner = $(this.vars.get('idInner'));
			
			oDiv.fade();
			oDivInner.update("");
		},
		
		active: function(e){
			//Lorsque qu'on click
			var oA = e.element();
			var sUrl;
			var sInnerHTML;
			var sTypeVideo;
			var oDiv;
			var oDivInner;
			var aPos;
	
			//on recherche la balise A
			if(oA.tagName!="A") oA = oA.ancestors().find(function(e){return e.tagName=="A";});
			sUrl = this.vars.get('tab').get(oA.identify());
			if (sUrl) {
				//on recherche 
				sTypeVideo = this.vars.get('regFindExps').find(function(pair){return sUrl.match(pair.value);}).key;
			
				if (sTypeVideo == "default"){
					window.open(sUrl);
				}
				else {
					
					sUrl = this.vars.get('transformURL')[sTypeVideo](sUrl);
					sInnerHTML = this.vars.get('templates')[sTypeVideo].evaluate({'url':sUrl, 'width':this.vars.get('width'), 'height':this.vars.get('height')});
					
					oDiv = $(this.vars.get('id'));
					oDivInner = $(this.vars.get('idInner'));
										
					oDivInner.update(sInnerHTML);
					aPos = $(e.element()).cumulativeOffset();				
					oDiv.setStyle({'left':aPos[0]+'px', 'top':aPos[1]+'px'});
					oDiv.appear();
				}
			}		
		},
		
		configure: function(e){
			//on configure un élément
			var tab;
			tab = this.vars.get('tab').set(e.identify(),e.href);
			e.href="javascript:void(0)"
			Event.observe(e, 'click', this.active.bindAsEventListener(this));	
		},
		
		version: '1.0.0'
	};
