// JavaScript Document
window.addEvent('resize', function(){
	var scroll = window.getSize();
			Vancho=scroll.x;
			Valto=scroll.y;
			centrarAcceso();
			mapagoogle();
});
window.addEvent('domready', function(){
	var box2 = {};
	box2 = new MultiBox('mb2', {descClassName: 'multiBoxDesc'});
	var scroll = window.getSize();
	var cargaInicial = $('cargaInicial');
			Vancho=scroll.x;
			Valto=scroll.y;
			cargaInicial.style.width=Vancho+'px';
			cargaInicial.style.height=Valto+'px';
	centrarAcceso();
	mapagoogle();
	cargaoferta();
	$$('.menusecciones').each(function(el){
		el.addEvents({
			'click':function(){
				decolora();
				eventoquitacolor();
				el.removeEvents('mouseout');
				TG(el,'#FFCC00');
			},
			'mouseout':function(){
				TG(el,'');
			}
		});
	});
	
	elementoFormulario = $("frmEmpleados");
   //Vamos a añadir un evento al formulario para cuando se envíe
   elementoFormulario.addEvent('submit', function(e) {
      //primero voy a parar el evento para que no se envíe como lo haría normalmente
      e.stop();
	  $('botones').style.visibility = "hidden";
	  $('hddLogin').value = $('txtUsuEmple').value;
	  $('hddPass').value = $('txtPassEmple').value;
      //Aquí podríamos hacer acciones como mostrar la típica imagen de cargando de Ajax
      //Luego definimos qué hacer cuando se envía el formulario 
      //Aquí "this" se refiere al formulario sobre el que defino el evento
      this.set('send', {
         //onComplete sirve para definir qué hacer cuando se recibe la respuesta
         onComplete: function(respuesta) { 
            //escribo la respuesta en la capa "resultado"
            $("comprueba").set('html', respuesta);
			//$('empleadoenvia').addEvent('click', $('frmEmp').submit());
         }
      });
      //Envío el formulario
      this.send();
   });
	
	elementoFormClientes = $("frmClientes");
   //Vamos a añadir un evento al formulario para cuando se envíe
   elementoFormClientes.addEvent('submit', function(e) {
      //primero voy a parar el evento para que no se envíe como lo haría normalmente
      e.stop();
	  $('botonesclientes').style.visibility = "hidden";
	  $('hddLoginCli').value = $('txtLoginCliente').value;
	  $('hddPassCli').value = $('txtPassCliente').value;
      //Aquí podríamos hacer acciones como mostrar la típica imagen de cargando de Ajax
      //Luego definimos qué hacer cuando se envía el formulario 
      //Aquí "this" se refiere al formulario sobre el que defino el evento
      this.set('send', {
         //onComplete sirve para definir qué hacer cuando se recibe la respuesta
         onComplete: function(respuesta) { 
            //escribo la respuesta en la capa "resultado"
            $("compruebaclientes").set('html', respuesta);
			//$('empleadoenvia').addEvent('click', $('frmEmp').submit());
         }
      });
      //Envío el formulario
      this.send();
   });
	
});

function replaceAll( text, busca, reemplaza ){  
	while (text.toString().indexOf(busca) != -1){      
		text = text.toString().replace(busca,reemplaza);  
	}  
	return text;
}

function urldecode(str) {
	str = replaceAll(str, '+', ' ');
	str = unescape(str);
	return str;
}

function aleatorio(inferior,superior){ 
    numPosibilidades = superior - inferior 
    aleat = Math.random() * numPosibilidades 
    aleat = Math.floor(aleat) 
    return parseInt(inferior) + aleat 
} 

function cambiacodigo() {
	var letraazar;
	var numletra = aleatorio(1,6);
	var numeroazar = aleatorio(1,6);
			switch (numletra) {
				case 1:
					letraazar = "A";
					break;
				case 2:
					letraazar = "B";
					break;
				case 3:
					letraazar = "C";
					break;
				case 4:
					letraazar = "D";
					break;
				case 5:
					letraazar = "E";
					break;
				case 6:
					letraazar = "F";
					break;
			}
	$("letraynumero").set('html', '<h2>'+letraazar+numeroazar+'</h2>');
	$("hddLetra").value = letraazar;
	$("hddNumero").value = numeroazar;
}

function mapagoogle() {
	$('map').style.height = Valto - 155 + 'px';
	$('map').style.width = Vancho - 100 + 'px';
}

function vuelveaintentar() {
	$("comprueba").set('html', '');
	$('botones').style.visibility = "visible";
}
function vuelveaintentarclientes() {
	$("compruebaclientes").set('html', '');
	$('botonesclientes').style.visibility = "visible";
	cambiacodigo();
}
window.addEvent('load', function(){
	(function inicia(){ 
			var cargaInicial = $('cargaInicial');
				cargaInicial.style.width='0px';
				cargaInicial.style.height='0px';
				cargaInicial.style.visibility='hidden';
				inicio();
				muestraoferta.delay(500);
	 }).delay(3000);
});

function muestrate() {
	var azar = aleatorio(1,$$('.fotoindex').length);
	var i = 1;
	$$('.fotoindex').each(function(el){
		if(azar == i) {
			el.set('morph', {duration: 500}).morph({'visibility': 'visible', 'filter':'alpha(opacity=100)', 'opacity': '1'});
		}			   
		i++;
	});
};

function muestraoferta(){
	$('fotoblanca').set('morph', {duration: 500}).morph({'visibility': 'hidden', 'filter':'alpha(opacity=0)', 'opacity': '0'});
	muestrate();
	muestrablanco.delay(10000);
};

function muestrablanco(){
	if ($('fotoblanca')) {
		$$('.fotoindex').each(function(el){
			el.set('morph', {duration: 500}).morph({'visibility': 'hidden', 'filter':'alpha(opacity=0)', 'opacity': '0'});
		});
		$('fotoblanca').set('morph', {duration: 500}).morph({'visibility': 'visible', 'filter':'alpha(opacity=100)', 'opacity': '1'});
		muestraoferta();	
	}
};

function cargaoferta(){
	var req = new Request.HTML({
		url:'fotooferta.php', 
		method:'GET',
		onSuccess: function(html) {
			//Clear the text currently inside the results div.
			$('oferta').set('html', '');
			//Inject the new DOM elements into the results div.
			$('oferta').adopt(html);
		},
		//Our request will most likely succeed, but just in case, we'll add an
		//onFailure method which will let the user know what happened.
		onFailure: function() {
			$('oferta').set('text', 'Carga fallida.');
		}
	});
	req.send();
}

function TG(a, changeTo){
	a.style.backgroundColor = changeTo;
};

function decolora() {
	$$('.menusecciones').each(function(el){
		TG(el,'');					   
	});
};

function verMenu(num){
	if ($('submenu' + num)) {
			$$('.submenu').each(function(el){
				el.set('tween', {
					duration: 700,
					transition: Fx.Transitions.Quad.easeOut
				}).tween('left', '-200px');
			});
			$('submenu' + num).style.visibility='visible';
			$('coche').style.overflow= 'hidden';
			$('coche').set('tween', {
				duration: 400,
				transition: Fx.Transitions.Quad.easeOut,
				onComplete: function(){
					$('oferta').set('tween', {
						duration: 300,
						transition: Fx.Transitions.Quad.easeOut
					}).tween('left', '210px');
					$('submenu' + num).set('tween', {
						duration: 400,
						transition: Fx.Transitions.Quad.easeOut
					}).tween('left', '15px');
					} 
			}).tween('width', '0px');
	}
};

function cargaContacta(){
	$('cuerpo').scroll="no";
	var contacto=$('contacta')
			contacto.style.visibility='visible';
			contacto.style.overflow= 'hidden';
			contacto.style.height= (Valto-55)+'px';
			contacto.set('tween', {
				duration: 700,
				transition: Fx.Transitions.Quad.easeOut,
				onComplete: function(){
					var num = 1;
					$$('.mapaIF').each(function(el){
						el.load('mapa.php?a=' + num);
						num++;
					});
					contacto.style.overflow= 'auto';
					} 
			}).tween('width', (Vancho+20)+'px');
	if ($('cmbOfertas')) $('cmbOfertas').style.visibility = 'hidden';
};
function cierraContacta(){
	var contacto=$('contacta')
			$$('.mapaIF').each(function(el){
				el.set('html', '&nbsp;');
			});
			/*$('mapaIF').set('html', '&nbsp;');
			$('mapaIF2').set('html', '&nbsp;');
			$('mapaIF3').set('html', '&nbsp;');
			$('mapaIF4').set('html', '&nbsp;');*/
			contacto.style.overflow= 'hidden';
			contacto.set('tween', {
				duration: 1000,
				transition: Fx.Transitions.Quad.easeOut,
				onComplete: function(){
					contacto.style.height= '55px';
					contacto.style.visibility='hidden';
					$('cuerpo').scroll="yes";
					} 
			}).tween('width', '0px');
	if ($('cmbOfertas')) muestracomboofertas.delay(1000);
};
function cargaActuacion(){
	$('cuerpo').scroll="no";
	var actuacion=$('actuacion')
			actuacion.style.visibility='visible';
			actuacion.style.overflow= 'hidden';
			actuacion.style.height= (Valto-55)+'px';
			actuacion.set('tween', {
				duration: 700,
				transition: Fx.Transitions.Quad.easeOut,
				onComplete: function(){
					actuacion.style.overflow= 'auto';
					} 
			}).tween('width', (Vancho+20)+'px');
	if ($('cmbOfertas')) $('cmbOfertas').style.visibility = 'hidden';
};
function cierraActuacion(){
	var actuacion=$('actuacion')
			/*$('mapaIF').set('html', '&nbsp;');
			$('mapaIF2').set('html', '&nbsp;');
			$('mapaIF3').set('html', '&nbsp;');
			$('mapaIF4').set('html', '&nbsp;');*/
			actuacion.style.overflow= 'hidden';
			actuacion.set('tween', {
				duration: 1000,
				transition: Fx.Transitions.Quad.easeOut,
				onComplete: function(){
					actuacion.style.height= '55px';
					actuacion.style.visibility='hidden';
					$('cuerpo').scroll="yes";
					} 
			}).tween('width', '0px');
	if ($('cmbOfertas')) muestracomboofertas.delay(1000);
};
function abreClientes(){
	$('cuerpo').scroll="no";
	var fondoBlanco=$('blancoFondo')
	var banda=$('accesoCarga')
	var formularioAcceso=$('accesoF')
	var centradoV=(Valto/2)-(250/2)
			fondoBlanco.style.width= (Vancho+20)+'px';
			fondoBlanco.style.height= Valto+'px';
			banda.style.height= Valto+'px';
			banda.set('tween', {
				duration: 1000,
				transition: Fx.Transitions.Quad.easeOut
			}).tween('width', (Vancho+200)+'px');
			formularioAcceso.set('tween', {
				duration: 1000,
				transition: Fx.Transitions.Elastic.easeOut
			}).tween('top', centradoV);
	cambiacodigo();
	if ($('cmbOfertas')) $('cmbOfertas').style.visibility = 'hidden';
};
function cierraClientes(){
	var fondoBlanco2=$('blancoFondo')
	var banda2=$('accesoCarga')
	var formularioAcceso2=$('accesoF')
			formularioAcceso2.set('tween', {
				duration: 400,
				transition: Fx.Transitions.Quad.easeOut
			}).tween('top', '-300px');
							
			banda2.set('tween', {
				duration: 800,
				transition: Fx.Transitions.Quad.easeOut,
				onComplete: function(){
					fondoBlanco2.style.width= '0px';
					fondoBlanco2.style.height= '0px';
					banda2.style.height= '0px';
					$('cuerpo').scroll="yes";
					}
			}).tween('width', '0px');
	$('txtLoginCliente').value = "";
	$('txtPassCliente').value = "";
	$('txtCardPass').value = "";
	if ($('cmbOfertas')) muestracomboofertas.delay(800);
};
function abreEmpleados(){
	$('cuerpo').scroll="no";
	var fondoBlanco=$('blancoFondo')
	var banda=$('accesoCarga')
	var formularioAcceso=$('accesoEmpleados')
	var centradoV=(Valto/2)-(250/2)
			fondoBlanco.style.width= (Vancho+20)+'px';
			fondoBlanco.style.height= Valto+'px';
			banda.style.height= Valto+'px';
			banda.set('tween', {
				duration: 1000,
				transition: Fx.Transitions.Quad.easeOut
			}).tween('width', (Vancho+200)+'px');
			formularioAcceso.set('tween', {
				duration: 1000,
				transition: Fx.Transitions.Elastic.easeOut
			}).tween('top', centradoV);
	if ($('cmbOfertas')) $('cmbOfertas').style.visibility = 'hidden';
};
function cierraEmpleados(){
	var fondoBlanco2=$('blancoFondo')
	var banda2=$('accesoCarga')
	var formularioAcceso2=$('accesoEmpleados')
			formularioAcceso2.set('tween', {
				duration: 400,
				transition: Fx.Transitions.Quad.easeOut
			}).tween('top', '-300px');
							
			banda2.set('tween', {
				duration: 800,
				transition: Fx.Transitions.Quad.easeOut,
				onComplete: function(){
					fondoBlanco2.style.width= '0px';
					fondoBlanco2.style.height= '0px';
					banda2.style.height= '0px';
					$('cuerpo').scroll="yes";
					}
			}).tween('width', '0px');
	if ($('cmbOfertas')) muestracomboofertas.delay(800);
};
function muestracomboofertas() {
	$('cmbOfertas').style.visibility = 'visible';
}
function funcioncierre() {
	$('txtUsuEmple').value = "";
	$('txtPassEmple').value = "";
	vuelveaintentar();
}
function funcioncierreclientes() {
	$('txtLoginCliente').value = "";
	$('txtPassCliente').value = "";
	$('txtCardPass').value = "";
	vuelveaintentarclientes();
}
function centrarAcceso(){
	var formularioAcceso3=$('accesoF')
	var formularioAcceso4=$('accesoEmpleados')
	var formularioAccesoAncho3=400/2
	var pagina3=Vancho/2
	var posicionancho3=pagina3-formularioAccesoAncho3
		formularioAcceso3.style.left= posicionancho3+'px';
		formularioAcceso4.style.left= posicionancho3+'px';
};
function eventoquitacolor() {
	$$('.menusecciones').each(function(al){
		al.addEvent('mouseout', function(){
			TG(al,'');
		});
	});
};

function quitaiconomenu() {
	$$('.iconomenu').each(function(el){
		el.set('tween', {
			duration: 500
			//transition: Fx.Transitions.Cubic.easeIn // This could have been also 'bounce:out'
		}).tween('left','-200px');
	});
};

function shake_xy(n) {
	if (self.moveBy) {
	for (i = 10; i> 0; i--) {
	for (j = n; j> 0; j--) {
	self.moveBy(0,i);
	self.moveBy(i,0);
	self.moveBy(0,-i);
	self.moveBy(-i,0);
	}
	}
	}
};
function shake_x(n) {
	if (self.moveBy) {
	for (i = 10; i> 0; i--) {
	for (j = n; j> 0; j--) {
	self.moveBy(i,0);
	self.moveBy(-i,0);
	}
	}
	}
};
function shake_y(n) {
	if (self.moveBy) {
	for (i = 10; i> 0; i--) {
	for (j = n; j> 0; j--) {
	self.moveBy(0,i);
	self.moveBy(0,-i);
	}
	}
	}
};
//-->
