$(document).ready(function(){
	$('DIV.imgrande a').lightBox();
});



function change_photo(newphoto,newlink){
	
  	document.getElementById('img_main').src="recursos/uploads/"+newphoto;
	document.getElementById('link_main').href="recursos/uploads/"+newlink;
	
}

function validar(){
        
	var form = document.getElementById("contactoform_1");	
	
	if(form.nombre.value == "" || form.email.value == "" || form.telefono.value == "" || form.direccion.value == "" || form.comentario.value ==""){
		alert("Los siguientes campos son obligatorios: Nombre, Email, Teléfono, Dirección y Comentarios.");
		return false;
	}

	if(form.nombre.value.match(/^[0-9!"·\$%\&]*$/i)){
		alert("El nombre y el apellido no deben contener símbolos o números.");
		return false;
	}
        
	if(!form.email.value.match(/^[0-9a-zA-Z~$&_-]{1,50}([.][0-9a-zA-Z~$&_-]+)*@[0-9a-zA-Z~$&_-]{3,64}([.][0-9a-zA-Z~$&_-]{2,3}){1,3}$/i)){
		alert("El email no es válido.");
		return false;
	}
        
	if(!form.telefono.value.match(/^[0-9 -]{9,}$/i)){
		alert("El teléfono no es válido.");
		return false;
	}
	
	if(form.private.checked == false){
		alert("Es necesario que acepte la nota legal.");
		return false;
	}
        
	return true;

}

function validate(cual){
	
	var form = document.getElementById(cual);
	
	if (cual == 'formfamily'){
		if(form.name.value == ""){
			alert("Debes introducir un nombre para la familia.");
			return false;
		}
	}
	
	if (cual == 'formproduct'){
		if(form.name.value == ""){
			alert("Debes introducir un nombre para el producto.");
			return false;
		}
		if(form.text.value == ""){
			alert("Debes introducir un texto descriptivo del producto.");
			return false;
		}
	}
	
	if (cual == 'formimg'){
		if(form.img.value == "" || form.img.value.length < 9){
			alert("Debes seleccionar una imagen.");
			return false;
		}
	}
	
	return true;
	
}

function confirmar(formObj,registro) { 

	if(confirm("¿Esta seguro que desea borrar "+registro+"?")) { 
		return true;
	}else{
		return false;	
	}

}

/** Shorthands **/
var Event = YAHOO.util.Event;
var Connect = YAHOO.util.Connect;
var Dom = YAHOO.util.Dom;
var CustomEvent = YAHOO.util.CustomEvent;
var Selector = YAHOO.util.Selector;


var Scroll = function(el, percent, direction, size){
	this.init(el, percent, direction, size);
}
Scroll.HORIZONTAL = 0;
Scroll.VERTICAL = 1;

Scroll.prototype = {
	dd: null,
	el: null,
	initXY:0,

	init: function(el, percent, direction, size){

		this.el = el;

		var slider = document.createElement('div');
		slider.className = 'slider';
		slider.id = "slider";
		el.appendChild(slider);

		var flecha1= document.createElement('div');
		flecha1.className = 'flecha-arriba';
		slider.appendChild(flecha1);

		var flecha2= document.createElement('div');
		flecha2.className = 'flecha-abajo';
		slider.appendChild(flecha2);


		if(direction == Scroll.HORIZONTAL){
			slider.style.width = size*percent + "px";
		}
		else{
			slider.style.height = size*percent + "px";
		}

		this.dd = new YAHOO.util.DD(slider);
		this.initConstraints(direction);

	},

	initConstraints: function(dir){

		//Get the top, right, bottom and left positions
		var region = Dom.getRegion(this.el);

		//Get the element we are working on
		var el = this.dd.getEl();

		//Get the xy position of it
		var xy = Dom.getXY(el);

		//Get the width and height
		var width = parseInt(Dom.getStyle(el, 'width'), 10);
		var height = parseInt(Dom.getStyle(el, 'height'), 10);

		//Set left to x minus left
		var left = xy[0] - region.left;

		//Set right to right minus x minus width
		var right = region.right - xy[0] - width;

		//Set top to y minus top
		var top = xy[1] - region.top;

		//Set bottom to bottom minus y minus height
		var bottom = region.bottom - xy[1] - height;

		if(dir == Scroll.HORIZONTAL){
			//Set the constraints based on the above calculations
			this.dd.setXConstraint(left, right);
			this.dd.setYConstraint(0, 0);
		}
		else{
			//Set the constraints based on the above calculations
			this.dd.setXConstraint(0, 0);
			this.dd.setYConstraint(top, bottom);
		}

		this.initXY = xy;

	}


}

Event.addListener(window, 'load', function(){
	new CajaScroll('scroll-box');
});

var CajaScroll = function(id){
	this.init(id);
}

CajaScroll.HEIGHT = 294;

CajaScroll.prototype = {

	scroll: null,
	box:null,
	percent: 0,
	height:0,

	init: function(id){
		var caja = document.getElementById('texto-scroll');

		if(!caja){
			return;
		}

		var scrollEl = Selector.query('.desplazamiento-vertical')[0];

		this.height = caja.offsetHeight;

		if(this.height > CajaScroll.HEIGHT ){
			this.percent = CajaScroll.HEIGHT/this.height;
			
		}
		else{
			scrollEl.style.display = "none";
		}

		this.scroll = new Scroll(scrollEl, this.percent, Scroll.VERTICAL, CajaScroll.HEIGHT);
		this.scroll.dd.on('dragEvent',this.onDrag, this, true);

		this.box = caja;

	},

	onDrag: function(e, ul){

		var xy = Dom.getXY(this.scroll.dd.getEl());
		
		var top = xy[1] - this.scroll.initXY[1];
		
		var totalHeight = this.scroll.el.offsetHeight;

		var maxTop = totalHeight*(1-this.percent);
		var percent = top/maxTop;

		var hiddenPart =  this.height - CajaScroll.HEIGHT;

		var offset = -hiddenPart*percent;

		this.box.style.top = offset + "px";

	}

}

Event.addListener(window, 'load', animateSlideshow);

var currentIndex = 0;
var currentZ = 1;

function animateSlideshow(){

	var ul = document.getElementById('slideshow');
	if(!ul){
		return;
	}

	var li = Selector.query('#slideshow LI');

	var currentLi = li[currentIndex];

	var xy = Dom.getXY(currentLi);

	var motion = new YAHOO.util.Motion(currentLi,
			{points: {from: [xy[0]+10,xy[1] ],
					  to:	xy}}, 0.6, YAHOO.util.Easing.backIn  );

	motion.animate();

	var anim = new YAHOO.util.Anim(currentLi, {opacity: {from: 0, to: 1 }}, 0.7);
	anim.animate();

	setTimeout(function(){
		currentLi.style.visibility = 'visible';
		currentLi.style.zIndex = currentZ}, 10);
		
	currentZ++;
	currentIndex++;

	if(currentIndex == li.length){
		currentIndex = 0;
	}

	setTimeout(animateSlideshow, 3000);

}

Event.addListener(window, 'load', loadMenu);

var thread = 0;
var menuVisible = false;

function loadMenu(){

	var productos = document.getElementById('productos');
	
	if(!productos)return;

	Event.addListener(productos, 'mouseover', function(e){
		clearTimeout(thread);
		var menu = document.getElementById('menu-productos');
		var xy = Dom.getXY(productos);
		
		menu.style.display = "block";
		menuVisible = true;
		Dom.setXY(menu, [xy[0] + 18, xy[1] + 32]);
	});

	Event.addListener(productos, 'mousemove', function(e){
		if(menuVisible){
			return;
		}
		clearTimeout(thread);

		var menu = document.getElementById('menu-productos');
		var xy = Dom.getXY(productos);

		menu.style.display = "block";
		menuVisible = true;
		Dom.setXY(menu, [xy[0] + 18, xy[1] + 32]);
	});

	Event.addListener(productos, 'mouseout', function(){
		thread = setTimeout(function(){
			var menu = document.getElementById('menu-productos');
			menu.style.display = "none";
			menuVisible = false;
		}, 100);
	});

	Event.addListener('menu-productos', 'mouseover', function(){
		clearTimeout(thread);
	});

	Event.addListener('menu-productos', 'mouseout', function(){
		thread = setTimeout(function(){
			var menu = document.getElementById('menu-productos');
			menu.style.display = "none";
			menuVisible = false;
		}, 50);
	});

}
