/**
 * @author BackSpc
 */

var estilomenu='';
function detecta()
{
if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)){ //test for Firefox/x.x or Firefox x.x (ignoring remaining digits);
	 var ffversion=new Number(RegExp.$1) // capture x.x portion and store as a number
	 if (ffversion>=3 || ffversion>=2 || ffversion>=1){
		 estilomenu='menu_elemento';
	 }
	} else {
		estilomenu='menu_elemento_ie';
	}
}

var mostrada='';
var amostrar='';
function mostrar(id)
{
	amostrar=id;
	if(mostrada==amostrar)
	{
		return;
	} else {
		Effect.toggle(mostrada, 'appear', {duration:  1.5, afterFinish:mostrarSig });
	}
}
function mostrarSig(){
	Effect.toggle(amostrar, 'appear', {duration: 1.5});
	mostrada=amostrar;
}
x = document.viewport.getWidth();
y = document.viewport.getHeight();
mitad=(x/2)-(331/2);
var cabezaDrag;
function init()
{
	new Effect.Move('cabeza',{x:mitad, duration:4, transition: Effect.Transitions.spring, afterFinish:iniciaDraggable });
	$('esquina').observe("mouseover",abreEsquina);
	$('esquina').observe("mouseout",cierraEsquina);
	var imagenesArray = [];
	var ruta = 'wp-content/themes/backTheme/images/hoja_f';
	var ruta2 = '.gif'
	for(j=1;j<=16;j++) {
		imagenesArray[j] = document.createElement('img');
		imagenesArray[j].src = ruta+j+ruta2;
	}
	var proceso = 0, ultimoTime = [], valorActual;
	function abreEsquina(event,valor) {
		if(proceso==0 )
			proceso = 1;
		else if(proceso==2) {
			borrarTiempos();
			ultimoTime=[];
			proceso=1;
			abreEsquina(0,valorActual);
			return;
		}
		if(valor==undefined)
		{
			$('esquina').src = imagenesArray[1].src;
			valorActual=1;
			a= setTimeout(function(){
					abreEsquina(0, 2)
				},50);
			ultimoTime.push(a);
		} else {
			if(valor<16) {
				$('esquina').src = imagenesArray[valor].src;
				valorActual=valor;
				a = setTimeout(function(){
					abreEsquina(0, valor + 1)
				},50);
				ultimoTime.push(a);
			} else {
				proceso=0;
			}
		}
	};
	
	function cierraEsquina(event,valor) {
		if (proceso == 0) {
			proceso = 2;
		} else if (proceso == 1) {
			borrarTiempos();
			ultimoTime = [];
			proceso = 2;
			cierraEsquina(0, valorActual);
			return;
		}
		if (valor == undefined) {
			$('esquina').src = imagenesArray[15].src;
			valorActual=15;
			a = setTimeout(function(){
				cierraEsquina(0, 15)
			}, 50);
			ultimoTime.push(a);
		} else {
			if (valor > 0) {
				$('esquina').src = imagenesArray[valor].src;
				valorActual=valor;
				a = setTimeout(function(){
					cierraEsquina(0, valor - 1)
				}, 50);
				ultimoTime.push(a);
			} else {
				proceso=0;
			}
		}
	};
	function borrarTiempos() {
		ultimoTime.each(function(p){
			clearTimeout(p)
		});
	};
	$('btn-principal').onmouseover = function() {
		this.src = "wp-content/themes/backTheme/images/btn_principal_over.jpg";
	};
	$('btn-principal').onmouseout = function(){
		this.src = "wp-content/themes/backTheme/images/btn_principal.jpg";
	};
	$('btn-nosotros').onmouseover = function() {
		this.src = "wp-content/themes/backTheme/images/btn_nosotros_over.jpg";
	};
	$('btn-nosotros').onmouseout = function(){
		this.src = "wp-content/themes/backTheme/images/btn_nosotros.jpg";
	};
	$('btn-servicios').onmouseover = function() {
		this.src = "wp-content/themes/backTheme/images/btn_servicios_over.jpg";
	};
	$('btn-servicios').onmouseout = function(){
		this.src = "wp-content/themes/backTheme/images/btn_servicios.jpg";
	};
	$('btn-clientes').onmouseover = function() {
		this.src = "wp-content/themes/backTheme/images/btn_clientes_over.jpg";
	};
	$('btn-clientes').onmouseout = function(){
		this.src = "wp-content/themes/backTheme/images/btn_clientes.jpg";
	};
	$('btn-informacion').onmouseover = function() {
		this.src = "wp-content/themes/backTheme/images/btn_informacion_over.jpg";
	};
	$('btn-informacion').onmouseout = function(){
		this.src = "wp-content/themes/backTheme/images/btn_informacion.jpg";
	};
};

function activa() {
	$('principal').className=estilomenu;
	Effect.toggle('principal', 'slide', {duration: 0.3, afterFinish:nosotros });
}

function nosotros() {
	$('nosotros').className=estilomenu;
	Effect.toggle('nosotros', 'slide', {duration: 0.3, afterFinish:servicios });
}

function servicios() {
	$('servicios').className=estilomenu;
	Effect.toggle('servicios', 'slide', {duration: 0.3, afterFinish:clientes });
}

function clientes() {
	$('clientes').className=estilomenu;
	Effect.toggle('clientes', 'slide', {duration: 0.3, afterFinish:informacion });
}

function informacion() {
	$('informacion').className=estilomenu;
	Effect.toggle('informacion', 'slide', {duration: 0.3, afterFinish:principal});
}

function principal() {
	//Effect.toggle('contenido', 'appear', {duration: 1.5});
	Effect.toggle('div_principal', 'appear', {duration: 1.5});
	mostrada='div_principal';
}

function esperaCarga() {
  var elemento = $('cabeza');
  detecta();
  if(elemento.complete) {
    init();
	activa();
  } else {
    setTimeout(esperaCarga, 100);
  }
};

function iniciaDraggable() {
	cabezaDrag = new Draggable('cabeza', {
		constraint: 'horizontal',
		onEnd: function(){
			var a=$('cabeza').style.left;
			var b=a.substring(0,a.length-2);
			var nuevaPosicion=mitad-parseInt(b);
			detieneDraggable();
			if(nuevaPosicion>30 || nuevaPosicion < -30 )
				new Effect.Move('cabeza',{x:nuevaPosicion, duration:4, transition: Effect.Transitions.spring, afterFinish:iniciaDraggable });
			else
				new Effect.Move('cabeza',{x:nuevaPosicion, duration:1, afterFinish:iniciaDraggable });
		}
	});
};
function detieneDraggable() {
	cabezaDrag.destroy();
};

function resize() {
	x = document.viewport.getWidth();
	y = document.viewport.getHeight();
	mitad=(x/2)-(331/2);
	var a=$('cabeza').style.left;
	var b=a.substring(0,a.length-2);
	var nuevaPosicion=mitad-parseInt(b);
	new Effect.Move('cabeza',{x:nuevaPosicion, duration:4, transition: Effect.Transitions.spring, afterFinish:iniciaDraggable });
}

document.observe("dom:loaded", esperaCarga);
Event.observe(window, "resize", resize);

