// JavaScript Document
// Archivo JScript

var errFunc = function(t) {
	//myAjaxProgressLoading.hideLoading();
    alert('Error ' + t.status + ' -- ' + t.statusText + ' -- ' + t.responseText );
	//$('visor').innerHTML=t.responseText;
}

function generarAleatorio(largo){
	
	var caracter = new Array;
	var resultado="";
	caracter=["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "w", "x", "y", "z","_","-"];
	for(i=0;i<largo;i++){
		resultado=resultado + caracter[ Math.floor(Math.random() * caracter.length) ];
	}
	return resultado;
	}


var myTools = {
		
	start: function(){
		
		// creo la capa mascara y la inserto  
		myTools.box = new Element('div').addClass('box').injectInside(document.body).addEvent('click', myTools.hideBox);
		// creo la capa de contenido y la inserto  
		myTools.contenido = new Element('div').injectInside(document.body);
		myTools.contenido.setProperty('id','visor');
		
		// defino las caracteristicas de la clase box
		
		myTools.box.setStyles({
		    'position': 'absolute',
			'top': '0',
			'left': '0',
			'z-index': '400',
			'width': '100%',
			'height': '100%',
			'background-color': '#CCC',
			'filter':'alpha(opacity=60)',
			'-moz-opacity': '0.6',
			'opacity': '0',
			'cursor':'pointer'
		});
		
				 
		// estilos para contenido
		myTools.contenido.setStyles({
			'z-index': '500',
			'position':'absolute',
			'width':'640px'
		});

				// creo los efectos
		myTools.boxEffect = new Fx.Tween(myTools.box);
		myTools.contenidoEffect = new Fx.Tween(myTools.contenido);
		
	},
	
	paginaTop:function(){
		window.scrollTo(0, 0);	
	},
	
	
	ocultarCombos:function(){
		    $$('select').each(function(el){
							    el.setStyle('visibility','hidden');
						    }
		    );
	},
	
	mostrarCombos:function(){
		    $$('select').each(function(el){
							    el.setStyle('visibility','visible');
						    }
		    );
	},
		
	
	showBox: function(title){
		
		// oculto las combos para el puto explorer
		if(window.ActiveXObject) {
    		myTools.ocultarCombos();
		}
        // llamo a la funcin que hace que la mascara este siempre en toda la ventana
		myTools.followBox();
		window.addEvent('scroll', myTools.followBox);
		
				
		myTools.contenido.setStyles({
			'top': Window.getScrollTop()+ 100 +'px',
			'z-index': '500'	
		});
		
		myTools.box.setStyles({
			'height': Window.getHeight() + 'px',
			'width': '100%',
			'visibility': 'visible'			
		});
		
		
		// We change the background-color of the element
		myTools.boxEffect.start('opacity', '0.8');
		myTools.contenidoEffect.start('opacity', '1'); 
		/*myTools.boxEffect.custom(myTools.boxEffect.now,0.8).chain(function(){
			myTools.contenidoEffect.custom(myTools.contenidoEffect.now, 1);
		});*/
		
		
	},
	
	
	hideBox: function(){
	    
		
		window.removeEvent('scroll', myTools.followBox);
		
		$('visor').innerHTML='';
		
		
		myTools.contenidoEffect.start('opacity', '0'); 
		myTools.boxEffect.start('opacity', '0');
		myTools.box.setStyles({
					'height': '0',
					'width': '0',
					'visibility': 'hidden'
				});
		

		if(window.ActiveXObject) {
		    myTools.mostrarCombos();
		}
	},
	
	followBox: function(){
		myTools.contenido.setStyle('top', Window.getScrollTop()+ 100 +'px');
		myTools.box.setStyle('top', Window.getScrollTop()+ 'px');
	},
	
	
	centrarGaleria: function(nombreGaleria,pxTop,pxLeft){
		if(pxTop==undefined || pxTop==null){
			pxTop=Math.round( (parseInt(Window.getHeight(),10) - parseInt($(nombreGaleria).getStyle('height'),10)) / 2 );
			}
		if(pxLeft==undefined || pxLeft==null){
			pxLeft=Math.round(parseInt(Window.getWidth(),10)/2 - parseInt($(nombreGaleria).getStyle('width'),10)/2);
			}
		if($(nombreGaleria)){
		    $(nombreGaleria).setStyles({
			    'top':  pxTop + 'px',
			    'left': pxLeft + 'px'
		    });
		}
		return false;
	},
	
	verGaleria: function(){
		myTools.showBox();
		myTools.centrarGaleria('myGallery');
		myTools.startGallery();
		},
	
	enviarAmigo: function(){
		myTools.showBox();
		myTools.centrarGaleria('visor');
		},
		
	cargaEnviar: function(urlenvio,idioma){
		myTools.centrarGaleria('visor',50);
		new Request.HTML(
		{
		url:'/email/enviar_zaitel.asp?idioma=' + idioma ,
		method: 'post',
		data:{'aleat':generarAleatorio(30)},
		update:$('visor'),
		onRequest: myTools.fondoanadir,
		//onSuccess:myTools.centrarGaleria('visor'),
		onFailure: errFunc
		}).post();
		return false;
		
		/*
		new Ajax('/email/enviar.asp?aleat=' + generarAleatorio(30),
		{method: 'post',
		postBody:'urlenvio=' + urlenvio,
		update:$('visor'),
		onComplete:myTools.enviarAmigo,
		//onRequest: myAjaxProgressLoading.showLoading,
		//onSuccess: myAjaxProgressLoading.hideLoading,
		//evalScripts: true,
		onFailure: errFunc
		}).request();
		return false;
		*/
	},
	
	
	enviarMensaje: function(){
		$('visor').load('/email/enviando_zaitel.asp');
		new Request.HTML(
		{
		url:'/email/enviar_zaitel.aspx',
		method: 'post',
		data:{'aleat':generarAleatorio(30)},
		update:$('visor'),
		onRequest: myTools.fondoanadir,
		onSuccess:myTools.centrarGaleria('visor'),
		onFailure: errFunc
		}).post($('formenviar'));
		return false;
		/*
		new Ajax('/email/enviar.aspx?aleat=' + generarAleatorio(30),
		{method: 'post',
		postBody:$('enviocorreo'),
		update:$('visor')
		//,
		//onComplete:myTools.enviarAmigo,
		//onRequest: myAjaxProgressLoading.showLoading,
		//onSuccess: myAjaxProgressLoading.hideLoading,
		//evalScripts: true,
		//onFailure: errFunc
		}).request();
		return false;
		*/
	},
	
	fondoanadir:function(){
		
		myTools.showBox();
		
		},
	
	
		
	descargas: function(idseccion, iditem,idioma){
		
		new Request.HTML(
		{
		url:'/zaitel/list_descargas.asp?idioma=' + idioma + '&idseccion=' + idseccion + '&id=' + iditem + '&aleat=' + generarAleatorio(30),
		method: 'post',
		data:{'do':'1'},
		update:$('visor'),
		onRequest: myTools.fondoanadir,
		onSuccess:function(){
					myTools.centrarGaleria('visor',50,null);
					myTools.paginaTop();
					},
		onFailure: errFunc
		}).post();
		return false;
	},
	
	imagenes: function(idseccion, iditem,idioma){
		
		new Request.HTML(
		{
		url:'/zaitel/list_imagenes_slide.asp?idioma=' + idioma + '&idseccion=' + idseccion + '&id=' + iditem + '&aleat=' + generarAleatorio(30),
		method: 'post',
		data:{'do':'1'},
		update:$('visor'),
		onRequest: myTools.fondoanadir,
		onSuccess: function(){
					myTools.centrarGaleria('visor',50,null);
					myTools.paginaTop();
					},
		onComplete:function(){
				
				var data  = null;
				var myShow = new Slideshow.KenBurns('show', data, { captions: true, controller: true, width: 640,thumbnails:false,hu :'',delay:5000 });
				var mybox = new Lightbox({ 
				  'onClose': function(){ this.pause(false); }.bind(myShow), 
				  'onOpen': function(){ this.pause(true); }.bind(myShow) 
				});
				//var myShow = new Slideshow('show', data, { captions: true, controller: true, height: 300, width: 400,thumbnails:false,hu :'',loader:false,delay:5000 });
		},
		onFailure: errFunc,
		evalScripts: true
		}).post();
		return false;
	},
	
	multibox:function(){ 
		
		if($$('.mb').length > 0)//only triggered if 'mb' class found on page
		{
			var initMultiBox = new multiBox('mb', {
				descClassName: 'multiBoxDesc',//the class name of the description divs
				path: 'Flash/',//path to mp3 and flv players
				useOverlay: true,//use a semi-transparent background. default: false;
				maxWidth: 600,//max width (set to false to disable)
				maxHeight: 400,//max height (set to false to disable)
				addDownload: false,//do you want the files to be downloadable?
				pathToDownloadScript: './multiBox/Scripts/ForceDownload.asp',//if above is true, specify download script
				addRollover: true,//add rollover fade to each multibox link
				addOverlayIcon: true,//adds overlay icons to images within multibox links
				addChain: true,//cycle through all images fading them out then in
				recalcTop: true//subtract the height of controls panel from top position
			});
		};
	},
	
	videos: function(idseccion, iditem,idioma){
		
		new Request.HTML(
		{
		url:'/zaitel/list_videos.asp?idioma=' + idioma + '&idseccion=' + idseccion + '&id=' + iditem + '&aleat=' + generarAleatorio(30),
		method: 'post',
		data:{'do':'1'},
		update:$('visor'),
		onRequest: myTools.fondoanadir,
		onSuccess:function(){
					myTools.centrarGaleria('visor',50,null);
					myTools.paginaTop();
					},
		onFailure: errFunc
		}).post();
		return false;
	},
	
	enviarContacto: function(){
		$('visor').load('/email/enviando_zaitel.asp');
		new Request.HTML(
		{
		url:'/email/contactar_zaitel.aspx' ,
		method: 'post',
		data:{'aleat':generarAleatorio(30)},
		update:$('visor'),
		onRequest: myTools.fondoanadir,
		onSuccess:myTools.centrarGaleria('visor'),
		onFailure: errFunc
		}).post($('formcontacto'));
		return false;
	},
	
	cargaPestana: function(urlCargar,capaDestino,indicePestana){
		
		new Request.HTML(
		{url:urlCargar,
		method: 'post',
		data:{'do':'1'},
		update:$(capaDestino),
		onComplete:function(){
			// todas sin seleccionar
			$$('.pest').removeClass('pestseleccionada'); 
			$('pest' + indicePestana).addClass('pestseleccionada');
			//makeScrollbar( $('posicionadorcontenido'), $('scrollbar1'), $('handle1') );
			//makeScrollbar( $('contenidocentral'), $('scrollbar1'), $('handle1') );
			
			},
		/*,onRequest: myAjaxProgressLoading.showLoading,
		onSuccess: myAjaxProgressLoading.hideLoading,*/
		onFailure: errFunc
		}).post();
		return false;
	},
	
	/* buscador */

	buscarGeneral: function (){
		if($('termino').get('value').trim().length > 0 )
			$('frmbuscar').submit();
	},
	
	/* menu de navegacion tipo flicker */

	 		
	 startMenuNavegacion:function(){
		  // quito la clase activa a todas las cabeceras
		  $$('span.head_menu').each(function(el){
							    el.removeClass('active');
						    }
		  );
		  // añadir evento click a todas las imágenes
		  $$('img.arrow').each(function(el){
							   var submenu=el.getParent().getParent().getChildren('div.sub_menu');
							   							   
							   // evento onclick
							   el.addEvent('click',function(e){
									//alert(submenu)
									e=new Event(e);
									
									// oculto todos los submenus
									$$('div.sub_menu').each(function(elSub){
											elSub.setStyle('display','none');
											}
										);
									// poner todas flechas en su estado original
									$$('img.arrow').each(function(elFlecha){
											elFlecha.src='images/arrow.png';
											}
										);
									// quito el activo de las cabeceras que no son en la que pincho
									// en que cabecera hemos pinchado
							   		var idheadsubmenu=el.getParent().getProperty('id');
									$$('span.head_menu').each(function(elCabecera){
											if(elCabecera.getProperty('id')!=idheadsubmenu){
												elCabecera.removeClass('active');	
												}
											}
										);
									
									
									if(submenu.getStyle('display')=="block"){
										submenu.getParent().removeClass('active');
										submenu.setStyle('display','none');
										el.src='images/arrow_hover.png';
										}
									else{
										
										submenu.getParent().addClass('active');
										submenu.setStyle('display','block');
										el.src='images/arrow_select.png';
										}
									
									
									
									e.stop();
								});
							    
								// evento over imagen
								 el.addEvent('mouseover',function(e){
									//alert(submenu)
									e=new Event(e);
									el.src='images/arrow_hover.png';
									// activo el span
									el.getParent().addClass('active');
									e.stop();
								});
								 
								// evento out imagen
								 el.addEvent('mouseout',function(e){
									//alert(submenu)
									e=new Event(e);
									if(submenu.getStyle('display')!="block"){
										el.src='images/arrow.png';
										// activo el span
										el.getParent().removeClass('active');
										}
									else{
										el.src='images/arrow_select.png';
										}
									e.stop();
								});
						    }
		   ); // each img.arrow
		   
		   // para las cabeceras span.head_menu
		   $$('span.head_menu').each(function(el){
							// evento over cabacera
							 el.addEvent('mouseover',function(e){
								//alert(submenu)
								e=new Event(e);
								el.addClass('over');
								e.stop();
							});	
							// evento out cabacera
							 el.addEvent('mouseout',function(e){
								//alert(submenu)
								e=new Event(e);
								el.removeClass('over');
								e.stop();
							});	
						}
			); // each span head_menu
		   // para los div.submenu
		   $$('div.sub_menu').each(function(el){
							//evento over submenu
							 el.addEvent('mouseover',function(e){
								//alert(submenu)
								e=new Event(e);
								el.setStyle('display','block');
								e.stop();
							});	
							
						}
			); // each span head_menu
		 	
			document.addEvent('click',function(e){
					e=new Event(e);
					if($(e.target).getParents('#menu_principal') != null){
						if(e.target.getParents('#menu_principal').length==0){
							$$('span.head_menu').each(function(el){
									el.removeClass('active');
								}
							);
							$$('div.sub_menu').each(function(el){
									el.setStyle('display','none');
								}
							);
							$$('img.arrow').each(function(el){
									el.src='images/arrow.png';
								}
							);
							
							}
						}
					}
			);
		 }
		
};

	function makeScrollbar(content,scrollbar,handle,horizontal,ignoreMouse){
		
		var steps = (horizontal?(content.getScrollSize().x - content.getSize().x):(content.getScrollSize().y - content.getSize().y))
		
		if(steps>0){ // hay que hacer scroll
			
			$(scrollbar).setStyle('visibility','visible');
			var slider = new Slider(scrollbar, handle, {	
				steps: steps,
				mode: (horizontal?'horizontal':'vertical'),
				onChange: function(step){
					// Scrolls the content element in x or y direction.
					var x = (horizontal?step:0);
					var y = (horizontal?0:step);
					content.scrollTo(x,y);
				}
			}).set(0);
			if( !(ignoreMouse) ){
				// Scroll the content element when the mousewheel is used within the 
				// content or the scrollbar element.
				$$(content, scrollbar).addEvent('mousewheel', function(e){	
					e = new Event(e).stop();
					var step = slider.step - e.wheel * 30;	
					slider.set(step);					
				});
			}
			// Stops the handle dragging process when the mouse leaves the document body.
			$(document.body).addEvent('mouseleave',function(){slider.drag.stop()});
		}
		else{
			$(scrollbar).setStyle('visibility','hidden');
			}
	}


