var validates = [];
var sliders = [];

var current_lib = 0;

var lBoxs = [];


function sendForm(form, cont )
{
	var formu = $(form);
	var miAjax = new Ajax(formu.getAttribute("action"), {
		method: 'post',
		data: formu,
		update: $(cont),
		evalScripts: true
	});
	
	miAjax.request();
}

function checkForm(idForm, input)
{
	var inputs = [];
	inputs = $(idForm).getElementsByTagName('input');
	var n = 0;
	var correct = true;
	
	for(var i = 0; i < inputs.length; i++)
	{
		if( inputs[i].type == "text" || inputs[i].type == "password" )
		{
			if( inputs[i].id == input.id )
				break;
				
			var valid;
			
			switch( validates[n] )
			{
				case 1:
					valid = inputs[i].value.match(/([-a-zA-Z0-9_]+)/);
				break;
				case 2:
					valid = inputs[i].value.match(/(.+)@(.+)\.(.+)/);
				break;
				case 3:
					valid = inputs[i].value.match(/([-a-zA-Z_]+)/);
				break;	
				case 4:
					valid = inputs[i].value.match(/([0-9]+)/);
				break;	
			}
			
			if( valid == null )
			{
				inputs[i].className = "input_forms errorinput";
				if( correct == true )
					inputs[i].focus();
					
				correct = false;
			}
			else
			{
				inputs[i].className = "input_forms correctinput";
			}
			
			n++;
		}	
	}
	
	return correct;

}

function submitForm(idForm, resp, input)
{
	if( checkForm(idForm, input) === true )
	{
		sendForm(idForm, resp);
		return true;
	}
	else
	{
		return false;
	}
}

function closeBox()
{
	var cb;
	
	cb = lBoxs.length > 1 ? lBoxs.pop() : lBoxs.getLast();
	
	cb.fx.overlay.start(0);
	cb.center.style.display = cb.bottomContainer.style.display = 'none';
	cb.center.innerHTML = '';
	$(cb.title).innerHTML = cb.other;
	
	return false;
}

function checkDelete(txt)
{
	var co = confirm(txt);
	
	return co;
}

function launchLightBox(el)
{
	setTimeout('synchronizedLaunchLightBox("' + el + '")', 1000);
}

function synchronizedLaunchLightBox(el)
{
	try {
		var cb = lBoxs.getLast();
		cb.click($(el));
	}
	catch(er) {
	}
}

function stars(libro, id, curr_rating, big) {
     var image;
	 var image_over;
	 var img_id;
	 var icos_star = [];
	 if( big == 1 )
	 {
		 icos_star = ["ico_estrellaon_puntuacion.gif", "ico_estrellaoff_puntuacion.gif"];
		 image_over = "ico_estrellaover_puntuacion.gif";
	 }
	 else 
	 {
		 icos_star = ["ico_estrellas_small_on.gif", "ico_estrellas_small_off.gif"];
		 image_over = "ico_estrellaover_puntuacion_small.gif";
	}
	
	image = image_over;
	 
	 for(i=1;i<=5;i++) {
	      if(i == (id+1) || image != image_over ) {
		    image = curr_rating >= i ? icos_star[0] : icos_star[1];
		  }
		  
	      img_id = document.getElementById("star_" + libro + "_" + i);
		  img_id.src = "img/"+image;   
	 }
}

function calificar(libro, rat, calif, big)
{
	var miAjax = new Ajax("/libro/calificar", {
		method: 'post',
		data: "libro=" + libro + "&rat=" + rat + "&calificacion=" + calif + "&big=" + big,
		update: 'dvframe',
		evalScripts: true
	}).request();
	Effect.pulsate('stars_' + libro);
}

function Dialog(html, fc, time)
{
	var ventanaalert = $('ventanaalert');
	var top, left;
	
	try
	{
		top = Browser.pageScrollY() + (Browser.clientHeight() / 2) - (parseInt(ventanaalert.style.height)/2);
		left = (Window.getWidth() / 2) - (parseInt(ventanaalert.style.width) / 2);
	}
	catch(e)
	{
		top = Browser.pageScrollY() + (Browser.clientHeight() / 2) - (parseInt(ventanaalert.style.height)/2);
		left = (myPageWidth() / 2) - (parseInt(ventanaalert.style.width) / 2);
	}
	
	var is_optional = false;
	var yes_no = false;
	
	if( html.match(/(borrar|imprimir factura)/i) ) {
		yes_no = true;
		is_optional = true;
	}
	else if( html.match(/(factura|personalizar|borrar)/i) ) {
		is_optional = true;
	}
	
	ventanaalert.setStyles({'top': top + 'px', 'left': left + 'px', display: ''})
	
	var btn_parent = $("ventanaalert_btn").parentNode;
	btn_parent.removeChild($("ventanaalert_btn"));
	
	var ventana_btn = new Element("input", {type: 'button', 'class': "btn_small_small", "value": (yes_no ? "Si" : "Aceptar"), 'id': "ventanaalert_btn" });
	btn_parent.appendChild(ventana_btn);
	
	$("ventanaalert_btn").removeEvents();
	$("ventanaalert_btn").removeEvent("click");

	var btn = $('ventanaalert_btn');

	//btn.onclick = function() { };

	
	btn.addEvent('click', function() {
		Effect.fade('ventanaalert');
		//$('ventanaalert').style.display = "none";
	});
	
	if( fc )
	{
		btn.addEvent('click', fc);
	}
	
	$("ventanaalert_txt").setHTML(html);
	
	if( is_optional ) {
		$('p_ventanaalert_btn2').style.display = "";
		$('ventanaalert_btn2').value = yes_no ? "No" : "Cancelar";
	}
	else {
		$('p_ventanaalert_btn2').style.display = "none";	
	}
	
	if( time )
	{
		setTimeout(fc, time);
	}
	
	Effect.appear('ventanaalert');
}

function WaitBubok(time)
{
	var wait_bubok = $('wait_bubok');
	var top, left;
	
	try
	{
		top = Browser.pageScrollY() + (Browser.clientHeight() / 2) - (parseInt(wait_bubok.style.height)/2);
		left = (Window.getWidth() / 2) - (parseInt(wait_bubok.style.width) / 2);
	}
	catch(e)
	{
		top = Browser.pageScrollY() + (Browser.clientHeight() / 2) - (parseInt(wait_bubok.style.height)/2);
		left = (myPageWidth() / 2) - (parseInt(wait_bubok.style.width) / 2);
	}
	
	if( time )
	{
		setTimeout("$('wait_bubok').style.display = 'none';", time);
	}
	
	wait_bubok.setStyles({'top': (top-20) + 'px', 'left': left + 'px', display: ''})
}

function EsperaBubok()
{
	var wait_bubok = $('espera_bubok');
	var top, left;
	
	try
	{
		top = Browser.pageScrollY() + (Browser.clientHeight() / 2) - (parseInt(wait_bubok.style.height)/2);
		left = (Window.getWidth() / 2) - (parseInt(wait_bubok.style.width) / 2);
	}
	catch(e)
	{
		top = Browser.pageScrollY() + (Browser.clientHeight() / 2) - (parseInt(wait_bubok.style.height)/2);
		left = (myPageWidth() / 2) - (parseInt(wait_bubok.style.width) / 2);
	}
	
	wait_bubok.setStyles({'top': (top-20) + 'px', 'left': left + 'px', display: ''})
}



var myScrollTop = function()
{
	if(window.pageYOffset)
	{
		return window.pageYOffset;
	 }
	 else {
		return  Math.max(document.body.scrollTop,document.documentElement.scrollTop);
	 }
}

var myPageWidth = function() 
{
	return document.body.clientWidth;
}

var myPageHeight = function() 
{
	if( window.innerHeight )
		return window.innerHeight;
	else
		return document.body.clientHeight;
}

function showHide(dv)
{
	dv = $(dv);
	if( dv.style.display == "none" ) {
		dv.style.display = "";
	} else {
		dv.style.display = "none";
	}
}