var e24Videos = new Class ({

	initialize: function(clientid, sequenceid, videoid, options) {
        //this.e24_SERVER = 'http://www.equipo24.com/dev/e24presenter/';
        this.clientid = clientid;
        this.videoid = videoid;
        this.sequenceid = sequenceid;
        this.e24_SERVER = options.server;
        this.demourl = options.demourl;
        this.swf = options.swf?options.swf:'e24presenter2.swf';
        this.onclose = options.onclose;
        this.onbeforeplay = options.onbeforeplay;
		this.onfixpos = options.onfixpos;
		this.video = options.video;
		
		this.swf = 'igenapronline2.swf';
        this.e24_SERVER = 'http://www.igena.com/cont/e24presenter/';
		//this.swf = 'e24presenter2.swf';
        //this.e24_SERVER = 'http://www.presentadoronline.com/';
		
        var scope = this;
		this.eventPosition = this.position.bind(this);		
		this.eventKeyDown = this.keyboardListener.bindWithEvent(this);
		var referer = '';
		try {
		  var referer = window.location;
		}
        catch(e) {
		}		
    },

	calcPosition: function() {
	   var video = this.video;	
	   var buttonWidth = 76; 
	   var buttonHeight = 40; 
	   
	   var margins = 0;
	   if (video.margin) {
			margins = video.margin;
	   }
	   if (this.demourl) {
			//margins = 22;
	   }
	   
	   if (video.position == 'layered' && this.containerLayer) {
	        var sz = this.containerLayer.getSize();
			containerWidth = sz.x;
			containerHeight = sz.y;
			this.divposition = 'absolute';
	   }
	   else {
	        this.containerLayer = document.body;
			containerWidth = window.getWidth();
			containerHeight = window.getHeight();
			if (video.position == 'fixed') {
				this.divposition = Browser.Engine.trident ? 'absolute' : 'fixed';
			}
			else {
				this.divposition = video.position;
			}
	   }
	   var posx = video.posx*1;
	   var posy = video.posy*1;
	   var width = video.width*1;
	   var height = video.height*1;
	   var buttonx = posx + (width - buttonWidth) / 2;
	   var buttony = posy + (height - buttonHeight) / 2;
	   
	   switch (video.alignment) {
	     case 'top-left': 
		        posy = 0 + margins;
		        posx = 0 + margins;
				buttonx = posx;
				buttony = posy;
		        break;
	     case 'top-center': 
		        posy = 0 + margins;
		        posx = (containerWidth - width) / 2;
				buttonx = posx + (width - buttonWidth) / 2;
				buttony = posy;
		        break;
	     case 'top-right': 
		        posy = 0 + margins;
		        posx = containerWidth - width - margins;
				buttonx = posx + width - buttonWidth - margins;
				buttony = posy;
		        break;
	     case 'center-left': 
		        posy = (containerHeight - height) / 2;
		        posx = 0 + margins;
				buttonx = posx;
				buttony = posy + (height - buttonHeight) / 2;
		        break;
	     case 'center-center': 
		        posy = (containerHeight - height) / 2;
		        posx = (containerWidth - width) / 2;
				buttonx = posx + (width - buttonWidth) / 2;
				buttony = posy + (height - buttonHeight) / 2;
		        break;
	     case 'center-right': 
		        posy = (containerHeight - height) / 2;
		        posx = containerWidth - width - margins;
				buttonx = posx + width - buttonWidth;
				buttony = posy + (height - buttonHeight) / 2;
		        break;
	     case 'bottom-left': 
		        posy = containerHeight - height - margins;
		        posx = 0 + margins;
				buttonx = posx + 0;
				buttony = posy + height - buttonHeight;
		        break;
	     case 'bottom-center': 
		        posy = containerHeight - height - margins;
		        posx = (containerWidth - width) / 2;
				buttonx = posx + (width - buttonWidth) / 2;
				buttony = posy + height - buttonHeight;
		        break;
	     case 'bottom-right': 
		        posy = containerHeight - height - margins;
		        posx = containerWidth - width - margins;
				buttonx = posx + width - buttonWidth;
				buttony = posy + height - buttonHeight;
		        break;
	   }
	   this.posy = posy + (video.position == 'fixed' && Browser.Engine.trident?window.getScrollTop():0);
	   this.posx = posx + (video.position == 'fixed' && Browser.Engine.trident?window.getScrollLeft():0);
	   this.buttonx = buttonx + (video.position == 'fixed' && Browser.Engine.trident?window.getScrollLeft():0);
	   this.buttony = buttony + (video.position == 'fixed' && Browser.Engine.trident?window.getScrollTop():0);
	},
	
	render: function(options) {
		var video = this.video;
		if (options) {
	        if (options.onclose) this.onclose = options.onclose;
	        if (options.onbeforeplay) this.onbeforeplay = options.onbeforeplay;
			if (options.onfixpos) this.onfixpos = options.onfixpos;
			if (options.video) this.video = options.video;
		}	

	   if (this.sequenceid != -1) {
			var objectid = this.sequenceid;
	   }
	   else {
			var objectid = this.videoid;
	   }	
		
	   
	   if (window.parent && !window.parent.e24Presentor) {
			if (video.debug) { return false;}
	   }
	   this.containerLayer = document.id(video.layer);
	   this.calcPosition();
	   
	   if (this.demourl) {
			if (this.bodycontainer = document.id('e24iframe')) {
				if (window.ie) { delta = 0; } else { delta = 5;}
				this.bodycontainer.setStyles({'width': window.getWidth() - delta , 'height': window.getHeight() - delta});
			 
				if (!window.ie) {
					window.addEvent('resize', function(e){
					this.bodycontainer.setStyles({'width': window.getWidth() - delta , 'height': window.getHeight() - delta});
				}.bindWithEvent(this));			 
			}
			else {
			  this.divposition = 'absolute';		   
			}  
		   }	
		   
	   }
	   
	   if (document.id('e24-container' + objectid)) document.id('e24-container' + objectid).dispose();
	   this.videocontainer = new Element('div', {'id': 'e24-container' + objectid, 'styles': {'position': this.divposition,
	                                                                                  'left':this.posx + 'px','top':this.posy + 'px',
																					  'width': video.width + 'px', 'height': video.height + 'px', 
																					  'z-index': 20, 
																					  'display': 'block'}}).inject(this.containerLayer);
	   if (video.options) {
			this.videocontainer.setStyles('position:' + this.divposition + ';left:' + this.posx + 'px;top:' + this.posy + 'px;width:' +  video.width + 'px;height:' +  video.height +
										  ';z-index:20;display:block;' + video.options);
	   }																			  

	   if (document.id('e24-playbtn' + objectid)) document.id('e24-playbtn' + objectid).dispose();
       if (video.showbutton) {																					     
		   this.playbtn = new Element('img', {'id': 'e24-playbtn' + objectid, 'src': this.e24_SERVER + 'media/images/playbtn.gif', 
																						'styles': {'position': this.divposition,
		                                                                                  'left':this.buttonx + 'px','top':this.buttony + 'px',
																						  'width': 'auto', 'height': 'auto', 
																						  'z-index': 20, 																					  
																						  'cursor': 'pointer', 
																						  'display': 'none'}}).inject(this.containerLayer);
		   this.playbtn.addEvent('click', this.showPlayer.bindWithEvent(this));
       }		   


	   this.so = new SWFObject(this.e24_SERVER + this.swf, 'e24Presenter', video.width, video.height, 'ffffff');
	   this.so.addParam('quality', 'high');
	   //so.addParam('allowScriptAccess', 'sameDomain');
	   this.so.addParam('allowScriptAccess', 'always');
	   this.so.addParam('allowFullScreen', 'false');
	   this.so.addParam('salign', 'lt');
	   this.so.addParam('wmode', 'transparent');
	   
	   if (this.sequenceid != -1) {
	     this.so.addVariable('sequenceid', this.sequenceid);
	   }
	   
	   this.so.addVariable('videoid', this.videoid);
	   this.so.addVariable('contextid', objectid); //para saber el id que tengo que llamar para los eventos tipo videos32.defaultclose...
	   //this.so.addVariable('sequenceid', this.sequenceid); el swf espera que sea undefined si no hay sequencias
	   this.so.addVariable('clientid', this.clientid);
	   this.so.addVariable('langid', video.langid);
	  // this.so.addVariable('referer', window.location);
	   
	   this.so.write(this.videocontainer);
	   
	   if (video.position == 'fixed') {
	   		window.addEvent('resize', this.eventPosition);
			if (Browser.Engine.trident) {
				window.addEvent('scroll', this.eventPosition);
				
				
			}	
	   }	
	   document['addEvent']('keydown', this.eventKeyDown);	   
	},

	keyboardListener: function(event){
		switch (event.keyCode){
			case 27: case 88: case 67: this.defaultClose(); break;
		}
	},
	
	position: function(){
		this.calcPosition();
		if (this.videocontainer) {
			this.videocontainer.setStyles({left: this.posx, top: this.posy});
			if (this.playbtn) {
				this.playbtn.setStyles({left: this.buttonx, top: this.buttony});
			}	
			
			if (this.onfixpos) {
				this.onfixpos();
			}
		}	
	},
	
	showPlayer: function() {
		if (this.onbeforeplay) {
		  this.onbeforeplay();
		}
	
	    if (this.playbtn) {
		  this.playbtn.setStyle('display', 'none');
		}  
	    this.so.write(this.videocontainer);		
	    this.videocontainer.setStyle('display', 'block');
	},

	goTo: function(url) {
	    location.replace(url);
	},
	
	defaultClose: function() {
		document['addEvent']('keydown', this.eventKeyDown);	   
		if (this.videocontainer) {
			this.videocontainer.setStyle('display', 'none');
		    if (this.playbtn) {
			  this.playbtn.setStyle('display', 'block');
			}  
			
			if (this.onclose) {
			  this.onclose();
			}
			this.videocontainer.set('html', '');
		}	
	}
});	
