/*
 * Funciones ALKLA
 *
 * Copyright (c) 2011 Alkla Estudio de Diseño
 * http://www.alkla.es
 *
 */
 
$(document).ready(function(){
	
	
	// Listeners
	$("#contacto-form").bind('submit', validar_contacto);
	$("#interesa-form").bind('submit', validar_interesa);
	$("#buscar-form").bind('submit', validar_busqueda);
	$("#button-back").bind('click', button_back);
	
	$("#header a").bind('click', reset_filtro);
	$("#breadcrumbs a").bind('click', reset_filtro);
	$(".entry-info .info a").bind('click', reset_filtro);
	$(".not-found a").bind('click', reset_filtro);
	$("#post-info a").bind('click', reset_filtro);
	

	// Slide
	$('#index-slide').cycle({ 
		fx: 'fade', 
		speed: 1000, 
		timeout: 3000,
		pager:  '#index-slide_nav'
	});
	
	
	// Photos
	$('#post-galeria.jcarousel ul').jcarousel({
    	wrap: 'circular',
		scroll: 1,
		visible: 4
    });
	$('#index-escaparate ul').jcarousel({
    	wrap: 'circular',
		scroll: 1,
		visible: 6
    });
	$('#empresa-escaparate ul').jcarousel({
    	wrap: 'circular',
		scroll: 1,
		visible: 6
    });
	
	
	// Buscador Slider Range
	$("#slider-rango_precios").slider({
		range: true,
		min: 0,
		max: parseInt($("#buscar-precio_maximo").val()),
		step: parseInt($("#buscar-precio_paso").val()),
		values: [ parseInt($("#buscar-precio_min").val()), parseInt($("#buscar-precio_max").val()) ],
		slide: function( event, ui ) {
			$("#buscar-precio_min").val( ui.values[0]);
			$("#buscar-precio_max").val( ui.values[1]);
			$("#buscar-precio").val( setPrice(ui.values[0]) + ' - ' + setPrice(ui.values[1]));
		}
	});
	
	
	// Casos de éxito
	$('#casos_exito_box').cycle({ 
		fx: 'fade', 
		speed: 1000, 
		timeout: 6000
	});
	
	// Logos
	$('#sidebar-logos .widget-content').cycle({ 
		fx: 'fade', 
		speed: 1000, 
		timeout: 3000,
		pager:  '#sidebar-logo_nav'
	});
	
});





/**********************************************/
/*
/*   VARIOS
/*
/**********************************************/

function button_back() {
	history.go(-1);
	return false;
}

function setPrice(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + '.' + '$2');
	}
	return x1 + x2;
}

function reset_filtro()
{
	eraseCookie('gwb-precio_min');
	eraseCookie('gwb-precio_max');
	return true;
}

function createCookie(name,value,days)
{
	if (days) {
		var date = new Date();
		 date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name)
{
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name)
{
	createCookie(name,"",-1);
}



/**********************************************/
/*
/*   BUSQUEDA
/*
/**********************************************/

function validar_busqueda()
{
	var operacion = $("#buscar-operacion").val();
	var productos = $("#buscar-productos").val();
	var tipos = $("#buscar-tipos").val();
	var precio_min = $("#buscar-precio_min").val();
	var precio_max = $("#buscar-precio_max").val();
	var modelo = $("#buscar-modelo").val();
	var lang = $("#buscar-lang").val();
	var path = $("#buscar-path").val();
	
	if (modelo ==  $("#buscar-modelo_txt").val()) {
	
		var aux = productos.split("-");
		var producto = '';
		for (i=0; i<=aux.length-1; i++) {
			var obj = "#buscar-producto-" + aux[i];
			if ($(obj).attr('checked')) {
				producto = producto + '-' + aux[i];
			}
		}
		producto = producto.substring(1);
		
		var aux = tipos.split("-");
		var tipo = '';
		for (i=0; i<=aux.length-1; i++) {
			var obj = "#buscar-tipo-" + aux[i];
			if ($(obj).attr('checked')) {
				tipo = tipo + '-' + aux[i];
			}
		}
		tipo = tipo.substring(1);
		
		var param = "operacion=" + operacion + "&producto=" + producto + "&tipo=" + tipo + "&precio_min=" + precio_min + "&precio_max=" + precio_max + "&lang=" + lang;
		
		createCookie('gwb-precio_min', precio_min);
		createCookie('gwb-precio_max', precio_max);
		
	}else{
		var param = "operacion=" + operacion + "&producto=&tipo=&precio_min=&precio_max=&lang=" + lang + "&modelo=" + modelo;
	}
	
	$.ajax({
		async:true,
		type: "post",
		dataType: "xml",
		contentType: "application/x-www-form-urlencoded",
		url: path + "/library/busqueda.php",
		data:param,
		success:validar_busqueda_complete,
		timeout:10000,
		error:null
	});
	return false;
	
}
function validar_busqueda_complete(t)
{	
	var url = $("url", t).text();
	$("#buscar-form").attr('action', url);
	document.getElementById("buscar-form").submit();
}




/**********************************************/
/*
/*   CONTACTO
/*
/**********************************************/

function validar_contacto()
{
	var email = $("#contacto-email").val();
	var nombre = $("#contacto-nombre").val();
	var telefono = $("#contacto-telefono").val();
	var mensaje = $("#contacto-mensaje").val();
	var path = $("#contacto-path").val();
	var error = false;
	var error_msg = "";
	
	$("#contacto-msg").removeClass();
	
	if (nombre == $("#contacto-check_nombre").val()){ nombre = "";	}
	if (email == $("#contacto-check_email").val()){ email = "";	}
	if (telefono == $("#contacto-check_telefono").val()){ telefono = "";	}
	if (mensaje == $("#contacto-check_mensaje").val()){ mensaje = "";	}

	if (nombre == ""){
		error = true;
		error_msg = error_msg + $("#contacto-error_nombre").val() + '<br/>';
	}
	if (email == ""){
		error = true;
		error_msg = error_msg + $("#contacto-error_email").val() + '<br/>';
	}
	
	if (!error){	
		var param = "email=" + email + "&nombre=" + nombre + "&telefono=" + telefono + "&mensaje=" + mensaje;
		$("#contacto-msg").html("...");
		$.ajax({
			async:true,
			type: "post",
			dataType: "xml",
			contentType: "application/x-www-form-urlencoded",
			url: path + "/library/contacto.php",
			data:param,
			success:validar_contacto_complete,
			timeout:10000,
			error:null
		});
	}else{
		$("#contacto-msg").addClass("error");
		$("#contacto-msg").html(error_msg);
	}
	return false;
}
function validar_contacto_complete(t)
{	
	var error_msg = $("#contacto-ok").val();
	$("#contacto-msg").html(error_msg);
	document.getElementById("contacto-form").reset();
}



/**********************************************/
/*
/*   INTERESA
/*
/**********************************************/

function validar_interesa()
{
	var email = $("#interesa-email").val();
	var nombre = $("#interesa-nombre").val();
	var telefono = $("#interesa-telefono").val();
	var mensaje = $("#interesa-mensaje").val();
	var path = $("#interesa-path").val();
	var barco = $("#interesa-barco").val();
	var error = false;
	var error_msg = "";
	
	$("#interesa-msg").removeClass();
	
	if (nombre == $("#interesa-check_nombre").val()){ nombre = "";	}
	if (email == $("#interesa-check_email").val()){ email = "";	}
	if (telefono == $("#interesa-check_telefono").val()){ telefono = "";	}
	if (mensaje == $("#interesa-check_mensaje").val()){ mensaje = "";	}

	if (nombre == ""){
		error = true;
		error_msg = error_msg + $("#interesa-error_nombre").val() + '<br/>';
	}
	if (email == ""){
		error = true;
		error_msg = error_msg + $("#interesa-error_email").val() + '<br/>';
	}
	
	if (!error){	
		var param = "email=" + email + "&nombre=" + nombre + "&telefono=" + telefono + "&mensaje=" + mensaje + "&barco=" + barco;
		$("#interesa-msg").html("...");
		$.ajax({
			async:true,
			type: "post",
			dataType: "xml",
			contentType: "application/x-www-form-urlencoded",
			url: path + "/library/interesa.php",
			data:param,
			success:validar_interesa_complete,
			timeout:10000,
			error:null
		});
	}else{
		$("#interesa-msg").addClass("error");
		$("#interesa-msg").html(error_msg);
	}
	return false;
}
function validar_interesa_complete(t)
{	
	var error_msg = $("#interesa-ok").val();
	$("#interesa-msg").html(error_msg);
	document.getElementById("interesa-form").reset();
}

