var smoothPopup = new Class ({
	initialize:function(tipo,url,texto,ancho,alto){
		this.bodY =$(document.body);
		//this.bodY.setStyles({overflow:'hidden'});		
		this.addPop(tipo,url,texto,ancho,alto);	
	},
	getAncho:function(objeto){
		var scrollSizeW = objeto.getScrollSize();
		var ancho = scrollSizeW.x;
		return ancho;
	},	
	getAlto:function(objeto){
		var scrollSizeH = objeto.getScrollSize();
		var alto = scrollSizeH.y;
		return alto;
	},
	addPop:function(tipo,url,texto,ancho,alto){		
		this.backGround = new Element('div',{id:'smoothpopup_backGround'});
		
		var mgTop =$(document.body).getStyle('margin-top').toInt();
		var mgLeft =$(document.body).getStyle('margin-left').toInt();		
		
		this.backGround.setStyles({
			width:this.getAncho(this.bodY)+mgLeft+'px',
			height:this.getAlto(this.bodY)+mgTop+'px',
			position:'absolute',
			zIndex:'9997',
			marginLeft:-mgLeft+'px',
			marginTop:-mgTop+'px'			
		});	this.backGround.inject(this.bodY,'top');
		
		window.addEvent('resize', function(){
			var navegador = navigator.appName;if (navegador == "Microsoft Internet Explorer"){
			var anchoW = document.body.clientWidth;var altoW=document.body.clientHeight}else{var anchoW=window.innerWidth;var altoW=window.innerHeight};
			var mgTop =$(document.body).getStyle('margin-top').toInt(); var mgLeft =$(document.body).getStyle('margin-left').toInt();	
			$('smoothpopup_backGround').setStyles({
				width:anchoW+mgLeft+'px',
				height:altoW+mgTop+'px',
				marginLeft:-mgLeft+'px',
				marginTop:-mgTop+'px'			
			});											
		});	
		
		var opacityFX = new Fx.Morph($(this.backGround),{
		duration:500,
		onComplete:function(){					
			var preloader = new Element('div',{id:'smoothpopup_Preloader'});
			preloader.setStyles({
				display:'none',
				position:'absolute',				 
				zIndex:'9998',
				width: '100px',
				height: '54px',
				left:'50%',
				marginLeft:'-60px',
				marginTop:'-54px',
				textAlign:'center'				
			});	preloader.inject($(document.body),'top');
			
			var preloadImg = new Element('img');
			preloadImg.src = "img/loadinfo.net.gif";
			preloadImg.setStyles({marginTop:'7px'});
			preloadImg.inject(preloader);
				
				var preloadFX = new Fx.Morph(preloader,{
					duration:200,
					onStart:function(){
						$('smoothpopup_Preloader').setStyle('display','block')
					},
					onComplete:function(){												
						
						var anchoC = ancho;
						var altoC = alto;
						
						var popBox = new Element('div',{id:'smoothpopup_popBox'});						
						
						popBox.setStyles({
							display:'none',
							position:'absolute',				 
							zIndex:'9999',
							left:'50%',
							marginTop:"-9999px",
							marginBottom:'100px'
						});	
						
						popBox.inject($(document.body));
						
						var contenidoBg = new Element('div',{id:'smoothpopup_Contenido_Bg'});
						contenidoBg.inject(popBox);
							
						var checkLoad=function(){
							if(tipo=="imagen"){																
								var navegador = navigator.appName;if (navegador == "Microsoft Internet Explorer"){									
									if(contIE.width > 30 && contIE.height > 30){
										armarPopup(tipo,anchoC,altoC);
										clearInterval(intervalID);
									}
								}else{
									if(contenido.complete != false){
										armarPopup(tipo,anchoC,altoC);
										clearInterval(intervalID);
									};
								};
							}else{
								armarPopup(tipo,anchoC,altoC);
								clearInterval(intervalID);	
							};
						};						
						
						switch(tipo){
							case "imagen":
								var contIE=new Image();
								contIE.src=url;
								var contenido = new Element('img',{id:'smoothpopup_Contenido'});
								contenido.setStyle('display','none');
								contenido.src = url;
								contenido.inject(contenidoBg);												
								delay = 1000;								
							break;
							case "iframe":
								var navegador = navigator.appName;if (navegador == "Microsoft Internet Explorer"){
								var anchoW = document.body.clientWidth;}else{var anchoW=window.innerWidth};
								if (anchoC == "" || altoC == ""){anchoC=anchoW;altoC=500;};
								var contenido = new Element('iframe', {id:'smoothpopup_Contenido'});							
								contenido.setStyle('display','none');
								contenido.src = url;
								contenido.inject(contenidoBg);							
								contenido.frameborder = 0;							
								delay = 3000;
							break;
						};						
						
						if (tipo=="youtube" || tipo=="vimeo" || tipo=="myspace" || tipo=="yahoo" || tipo=="google" || tipo=="dailymotion" || tipo=="soundcloud" || tipo=="swf"){
							var contenido = new Element('div',{id:'smoothpopup_Contenido'});
							var contenidoSWF = new Element('div',{id:'smoothpopup_Video_swf'});
							contenido.setStyle('display','none');
							contenidoSWF.inject(contenido);
							contenido.inject(contenidoBg);
							
							var p = {menu: "false",wmode: "opaque",allowFullScreen:true,allowscriptaccess:'always'};
							
							switch(tipo){
								
								case "swf":
									var finalULR = url;
									if (anchoC == "" || altoC == ""){anchoC=600;altoC=400;};							
								break;
								case "youtube":
									var finalULR="http://www.youtube.com/v/"+url+"&hl=es_ES&fs=1&rel=0&color1=0x3a3a3a&color2=0x999999";
									if (anchoC == "" || altoC == ""){anchoC=640;altoC=385;};
								break;
								case "vimeo":
									var finalULR="http://vimeo.com/moogaloop.swf?clip_id="+url+"&amp;server=vimeo.com&amp;show_title=0&amp;show_byline=0&amp;show_portrait=0&amp;color=ffffff&amp;fullscreen=1";
									if (anchoC == "" || altoC == ""){anchoC=563;altoC=450;};								
								break;
								case "myspace":
									var finalULR="http://mediaservices.myspace.com/services/media/embed.aspx/m="+url+",t=1,mt=video";
									if (anchoC == "" || altoC == ""){anchoC=640;altoC=385;};
								break;
								case "yahoo":
									var urls = url.split("/");
									var p = {menu: "false",wmode: "opaque",allowFullScreen:true,allowscriptaccess:'always',flashVars:"id="+urls[1]+"&vid="+urls[0]+"&lang=es-es&intl=es&thumbUrl=&embed=1"};
									var finalULR="http://d.yimg.com/static.video.yahoo.com/yep/YV_YEP.swf?ver=2.2.46";
									if (anchoC == "" || altoC == ""){anchoC=640;altoC=385;};
								break;
								case "google":
									var finalULR="http://video.google.com/googleplayer.swf?docid="+url+"&hl=en&fs=true";
									if (anchoC == "" || altoC == ""){anchoC=640;altoC=385;};
								break;
								case "dailymotion":
									var finalULR="http://www.dailymotion.com/swf/video/"+url+"?theme=none&foreground=%23F7FFFD&highlight=%23787878&background=%23171D1B&additionalInfos=1&hideInfos=1&start=&autoPlay=0&colors=background%3A171D1B%3Bforeground%3AF7FFFD%3Bspecial%3A787878%3B";
									if (anchoC == "" || altoC == ""){anchoC=640;altoC=385;};
								break;
								case "soundcloud":
									var finalULR="http://player.soundcloud.com/player.swf?url="+url+"&show_comments=false&auto_play=false&color=333333";
									if (anchoC == "" || altoC == ""){anchoC=300;altoC=81;};
								break;
							};
							
							swfobject.embedSWF(finalULR, "smoothpopup_Video_swf" , anchoC , altoC , "10.0.0", null, null, p, null);			
							delay = 3000;
							
						};
						
						var intervalID = window.setInterval(checkLoad, delay);
						
						function armarPopup(tipo,ancho,alto){							
							
							var navegador = navigator.appName;if (navegador == "Microsoft Internet Explorer"){
							var altoW = document.body.clientHeight;}else{var altoW=window.innerHeight};
								
							if (tipo=="imagen"){				
								var navegador = navigator.appName;if (navegador == "Microsoft Internet Explorer"){
									anchoC = contIE.width;
									altoC = contIE.height;
								}else{
									anchoC = contenido.width;
									altoC = contenido.height;								
								}
							}else{								
								anchoC = ancho.toInt();
								altoC = alto.toInt();								
							};
							
							var toolBar = new Element('div',{id:'smoothpopup_Toolbar'});
							toolBar.setStyles({
								position:'relative',
								width: anchoC+'px',
								height: '25px',
								marginLeft:'10px'
							}); toolBar.inject(popBox,'top');
							
							var divCerrar = new Element('div',{id:'smoothpopup_Cerrar'});
							divCerrar.setStyles({width:anchoC+"px",height:"15px",paddingTop:'5px',paddingBottom:'7px',textAlign:'right'});		
							divCerrar.inject(toolBar);
							
							var btnCerrar = new Element('a',{id:'smoothpopup_btn_Cerrar'});				
							btnCerrar.inject(divCerrar);
							btnCerrar.href="javascript:void(0)";
							btnCerrar.set('html',"cerrar <span class='cross'>[x]</span>");
							
							btnCerrar.addEvent('click', function(){
								var alto = $('smoothpopup_popBox').style.height;
								var inicio = $('smoothpopup_popBox').style.top;
								
								var myMorph = new Fx.Morph($('smoothpopup_backGround'),{duration:500});
								myMorph.start({ 'opacity': [.8,0] });
								var myMorph = new Fx.Morph($('smoothpopup_popBox'), {
									duration:1000,
									transition:Fx.Transitions.Back.easeIn,
									onComplete: function(){
										
										if($('smoothpopup_Contenido')){
											swfobject.removeSWF('smoothpopup_Video_swf');
											$('smoothpopup_Contenido').destroy();
										};
										
										if($('smoothpopup_backGround')){$('smoothpopup_backGround').destroy();};
										if($('smoothpopup_Preloader')){$('smoothpopup_Preloader').destroy();};
										if($('smoothpopup_popBox')){$('smoothpopup_popBox').destroy();};
										$(document.body).setStyles({overflow:''});
									}				
									});
								myMorph.start({ 'top': [inicio.toInt(),-alto.toInt()] });		
							});								

							var altoCF = altoC;						
							
							contenido.setStyles({
									position: 'relative',									
									width: anchoC+"px",
									height: altoC+"px"
							});
							
							contenidoBg.setStyles({
									position: 'relative',
									marginLeft:'9px',
									marginBottom:'9px',
									width: anchoC+"px",
									height: altoC+"px"
							});						
							
							if (altoCF > altoW){								
								$(document.body).setStyle('overflow','visible');
								$('smoothpopup_backGround').setStyle('height',altoCF);
							};
							
							if(texto){
								var popText = new Element('div',{id:'smoothpopup_popText'});
								popText.setStyles({
									position:'relative',		 
									width: (anchoC-8)+'px',
									height: '25px',
									padding:'4px',										
									marginLeft:'9px',
									marginBottom:'9px',
									overflow:'hidden'
								});popText.inject(popBox);	
								
								popText.set('html',texto);	
								altoCF=altoCF+80;
							}else{
								altoCF=altoCF+35;	
							};					
							
							var navegador = navigator.appName;if (navegador == "Microsoft Internet Explorer"){
							var altoW = document.body.clientHeight;}else{var altoW=window.innerHeight};
							var altoTop = altoCF + altoW + $(document.body).getStyle('margin-top').toInt();
							
							popBox.setStyles({								
								width: (anchoC+20)+'px',
								height: altoCF+'px',								
								marginLeft:-((anchoC+20)/2)+'px',
								marginTop:-altoTop-10+'px',								
								display:'block'
							});	
							
							if (altoW>altoCF){var Top=((altoW+ altoW + $(document.body).getStyle('margin-top').toInt())/2)+((altoCF+ altoW + $(document.body).getStyle('margin-top').toInt())/2);}else{var Top=(altoTop);};
							
							var preload_2_FX = new Fx.Morph($('smoothpopup_Preloader'),{
								duration:200,
								onComplete:function(){
									
									var boxFX = new Fx.Morph(popBox,{
									duration:1000,
									onComplete:function(){																												
										var navegador = navigator.appName;if (navegador == "Microsoft Internet Explorer"){
										var altoW = document.body.clientHeight;var anchoW = document.body.clientWidth;}else{var altoW=window.innerHeight;var anchoW=window.innerWidth};
										var alto = $('smoothpopup_popBox').getStyle('height').toInt();
										var ancho = $('smoothpopup_popBox').getStyle('width').toInt();
										if (alto>altoW){											
											$('smoothpopup_backGround').setStyles({
												height:alto+'px',
												width:ancho+'px'
											})				
										}
										$('smoothpopup_Contenido').setStyle('display','block');																				
									}						
									}); boxFX.start({ 'top': [-altoTop+10,Top] });								
								}						
							}); preload_2_FX.start({ 'top': [54,-54] });
						};
					}
				}); preloadFX.start({ 'top': [-54,54] });
			}
		}); opacityFX.start({ 'opacity': [0,.6] });		
	}
});
