var siteRoot = "http://www.albertlanchas.com/";

function trabajos() {
	$("ul.trabajos a.hover").removeClass("hover");
	$("ul.trabajos a").hover(
		function() {
			$(this).children("span.layer").css("visibility", "visible");
			$(this).children("span.layer").hide();
			$(this).children("span.layer").show("normal");
		},
		function() {
			$(this).children("span.layer").css("visibility", "hidden");
		}
	)
}

function comentario() {
	$("form[@name=comentario]").bind("submit", function() {
		if (!$("input[@name=nombre]").mandatory('Nombre')) return false;
		if (!$("input[@name=email]").mandatory("E-mail")) return false;
		if (!$("input[@name=email]").validateEmail(true)) return false;
		if (!$("textarea[@name=comentario]").mandatory('Comentario')) return false;
		if (!$("input[@name=captcha]").mandatory('Código anti-spam')) return false;
	});
}

$(document).ready(function() {
	var bodyId = $("body").attr("id");
	if (bodyId != '') {
		var page = bodyId.substr(5);
		// dispatch init event
		switch (page) {
			case "home":
			case "portfolio":
				trabajos();
				break;
			case "contacto":
				contacto();
				break;
			case "accesibilidad":
				accesibilidad();
				break;
			case "accesibilidad-contraste-colores":
				contrasteColores();
				comentario();
				break;
			case "colores-accesibles-aleatorios":
				coloresAccesiblesAleatorios();
				comentario();
				break;
			case "internacionalizacion-php-gettext":
				comentario();
				break;
			case "bubble":
				pr_comentario();
				break;
		}
	}
});

function contacto() {
	$("form").bind("submit",function() {
		if (!$("input[@name=nombre]").mandatory(labels['a_Nombre'])) return false;
		if (!$("input[@name=email]").mandatory("E-mail")) return false;
		if (!$("input[@name=email]").validateEmail(true)) return false;
		if (!$("textarea[@name=mensaje]").mandatory(labels['a_Mensaje'])) return false;
	});
}
function accesibilidad() {
	$("select[@name=color]").bind("change", function() {
		var val = $(this).val();
		var found = false;
		var oHead = document.getElementsByTagName('head')[0];
		var link1 = document.createElement('link');
		link1.rel = 'stylesheet';
		link1.href = 'estilos/color1.css';
		link1.type = 'text/css';
		link1.title = 'color1';
		var link2 = document.createElement('link');
		link2.rel = 'stylesheet';
		link2.href = 'estilos/color2.css';
		link2.type = 'text/css';
		link2.title = 'color2';
		$('link[@rel*=style][@href*=color]').each(function(i) {
			this.disabled = true;
			if (val == $(this).attr('title')) {
				this.disabled = false;
				found = true;
			}
		});
		if (!found) {
			if (val == '2') {
				if ($.browser.msie) {
					oHead.appendChild(link1);
				} else {
					$("head").append("<link href='estilos/color1.css' rel='stylesheet' type='text/css' />");
				}
			} else if (val == '3') {
				if ($.browser.msie) {
					oHead.appendChild(link2);
				} else {
					$("head").append("<link href='estilos/color2.css' rel='stylesheet' type='text/css' />");
				}
			}
		}
	});
	
	$("select[@name=fuente]").bind("change", function() {
		var val = $(this).val();
		var found = false;
		var oHead = document.getElementsByTagName('head')[0];
		var link1 = document.createElement('link');
		link1.rel = 'stylesheet';
		link1.href = 'estilos/fonttype1.css';
		link1.type = 'text/css';
		link1.title = 'fonttype1';
		var link2 = document.createElement('link');
		link2.rel = 'stylesheet';
		link2.href = 'estilos/fonttype2.css';
		link2.type = 'text/css';
		link2.title = 'fonttype2';
		$('link[@rel*=style][@href*=fonttype]').each(function(i) {
			this.disabled = true;
			if (val == $(this).attr('title')) {
				this.disabled = false;
				found = true;
			}
		});
		if (!found) {
			if (val == 'fonttype1') {
				if ($.browser.msie) {
					oHead.appendChild(link1);
				} else {
					$("head").append("<link href='estilos/fonttype1.css' rel='stylesheet' type='text/css' />");
				}
			} else if (val == 'fonttype2') {
				if ($.browser.msie) {
					oHead.appendChild(link2);
				} else {
					$("head").append("<link href='estilos/fonttype2.css' rel='stylesheet' type='text/css' />");
				}
			}
		}
	});
		
	$("select[@name=tamanyo]").bind("change", function() {
		var val = $(this).val();
		var found = false;
		var oHead = document.getElementsByTagName('head')[0];
		var link1 = document.createElement('link');
		link1.rel = 'stylesheet';
		link1.href = 'estilos/fontsize1.css';
		link1.type = 'text/css';
		link1.title = 'fontsize1';
		var link2 = document.createElement('link');
		link2.rel = 'stylesheet';
		link2.href = 'estilos/fontsize2.css';
		link2.type = 'text/css';
		link2.title = 'fontsize2';
		$('link[@rel*=style][@href*=fontsize]').each(function(i) {
			this.disabled = true;
			if (val == $(this).attr('title')) {
				this.disabled = false;
				found = true;
			}
		});
		if (!found) {
			if (val == 'fontsize1') {
				if ($.browser.msie) {
					oHead.appendChild(link1);
				} else {
					$("head").append("<link href='estilos/fontsize1.css' rel='stylesheet' type='text/css' />");
				}
			} else if (val == 'fontsize2') {
				if ($.browser.msie) {
					oHead.appendChild(link2);
				} else {
					$("head").append("<link href='estilos/fontsize2.css' rel='stylesheet' type='text/css' />");
				}
			}
		}
	});
		
	$("select[@name=espacio]").bind("change", function() {
		var val = $(this).val();
		var found = false;
		var oHead = document.getElementsByTagName('head')[0];
		var link1 = document.createElement('link');
		link1.rel = 'stylesheet';
		link1.href = 'estilos/letterspacing1.css';
		link1.type = 'text/css';
		link1.title = 'letterspacing1';
		$('link[@rel*=style][@href*=letterspacing]').each(function(i) {
			this.disabled = true;
			if (val == $(this).attr('title')) {
				this.disabled = false;
				found = true;
			}
		});
		if (!found) {
			if (val == 'letterspacing1') {
				if ($.browser.msie) {
					oHead.appendChild(link1);
				} else {
					$("head").append("<link href='estilos/letterspacing1.css' rel='stylesheet' type='text/css' />");
				}
			}
		}
	});
	
}

function contrasteColores() {
	$.extend({
		brillo: function(color) {
			return ((parseInt(color.substr(0,2),16) * 299) + (parseInt(color.substr(2,2),16) * 587) + (parseInt(color.substr(4,2),16) * 114)) / 1000;
		},
		
		difColor: function(color1, color2) {
			return 	Math.abs(parseInt(color1.substr(0,2),16) - parseInt(color2.substr(0,2),16))+
							Math.abs(parseInt(color1.substr(2,2),16) - parseInt(color2.substr(2,2),16))+
							Math.abs(parseInt(color1.substr(4,2),16) - parseInt(color2.substr(4,2),16));
		}
	});
	
	$("button").bind("click", function() {
		if (!$("#colorFondo").validateColor()) return false;
		if (!$("#colorTexto").validateColor()) return false;
		
		$("p#texto").css("background-color", "#"+$("#colorFondo").val());
		$("p#texto").css("color", "#"+$("#colorTexto").val());
		
		var colorFondo = $("#colorFondo").val();
		var colorTexto = $("#colorTexto").val();
		var brilloFondo = $.brillo(colorFondo);
		var brilloTexto = $.brillo(colorTexto);
		var difBrillo = Math.abs(brilloTexto - brilloFondo);
		
		$("#difBrillo").html(Math.round(difBrillo));
		
		var difColor = $.difColor(colorFondo, colorTexto);
		$("#difColor").html(difColor);
		
		if (difBrillo >= 125) {
			if (difColor >= 500) {
				$("#semaforo").addClass("verde");
				$("#semaforo").removeClass("amarillo");
				$("#semaforo").removeClass("rojo");
			} else {
				$("#semaforo").removeClass("verde");
				$("#semaforo").addClass("amarillo");
				$("#semaforo").removeClass("rojo");
			}
		} else {
			if (difColor >= 500) {
				$("#semaforo").removeClass("verde");
				$("#semaforo").addClass("amarillo");
				$("#semaforo").removeClass("rojo");
			} else {
				$("#semaforo").removeClass("verde");
				$("#semaforo").removeClass("amarillo");
				$("#semaforo").addClass("rojo");
			}
		}
	});
	
}

function coloresAccesiblesAleatorios() {
	$.extend({
		color1: null,
		
		colorAccesible: function() {
			if ($.color1.length == 0) {
				$('#color1 .colorBoxColor').toggleClass('loading');
				$('#color1 .icon').addClass('loading').css('display', 'inline');
			} else {
				$('#color1 p').html('#'+$.color1);
				$('#color1 .colorBoxColor').css('background-color', '#'+$.color1);
			}
			$('#color2 .colorBoxColor').toggleClass('loading');
			$('#color2 .icon').addClass('loading').css('display', 'inline');
			var data = "color1="+$.color1;
			$.ajax({
				type: "POST",
				url: siteRoot + "ajax-coloraccesible",
				data: data,
				success: function(msg) {
					//alert (msg);
					var json = eval('(' + msg + ')');
					var result = json.result;
					
					var color1 = result[0];
					var color2 = result[1];
					
					$('.icon').hide();
					
					if (color1 != null) {
						if ($.color1.length == 0) {
							$('#color1 p').html('#'+color1);
							$('#color1 .colorBoxColor').css('background-color', '#'+color1);
							$('#color1 .colorBoxColor').toggleClass('loading');
						}
						$('#colorBase').val(color1);
						$('#color2 p').html('#'+color2);
						$('#color2 .colorBoxColor').css('background-color', '#'+color2);
						$('#color2 .colorBoxColor').toggleClass('loading');
						
						$("#texto").css('background-color', '#'+color1);
						$("#texto").css('color', '#'+color2);
					} else {
						$('#color2 p').html('#');
						$('#color2 .icon').removeClass('loading').addClass('error').css('display', 'inline');
					}
					
				}
			});
		}
		
	});
	
	$("button#go").bind("click", function() {
		if (!$("#colorBase").validateColor()) return false;
		
		$.color1 = $("#colorBase").val();
		$.colorAccesible();
	});
	
	$("button#random").bind("click", function() {
		$.color1 = '';
		$.colorAccesible();
	});
}

// prototype
function pr_comentario() {
	var form = $('comentario');
	Event.observe(form, 'submit', function(event) {
		var nombre = form['nombre'];
		var email = form['email'];
		var comentario = form['comentario'];
		
		if ($F(nombre) == '') {
			alert ('Nombre es obligatorio');
			nombre.focus();
			Event.stop(event);
			return false;
		}
		if ($F(email) == '') {
			alert ('email es obligatorio');
			email.focus();
			Event.stop(event);
			return false;
		}
		// test si email correcte
		var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (!$F(email).match(emailRegEx)) {
			alert ('Dirección de email no válida');
			email.focus();
			Event.stop(event);
			return false;
		}
		if ($F(comentario) == '') {
			alert ('Comentario es obligatorio');
			comentario.focus();
			Event.stop(event);
			return false;
		}
		
	});
}

/*------------------------------------------------------------------------------*/
$.fn.mandatory = function(name) {
	if ($(this).is("input[@type='checkbox']") | $(this).is("input[@type='radio']")) {
		if (!$(this).is(":checked")) {
			$(this).formError(name + ' ' + labels['a_es obligatorio']);
			return false;
		}
	} else {
		if ($(this).val() == "") {
			$(this).formError(name + ' ' + labels['a_es obligatorio']);
			return false;
		}
	}
	
	return true;
}

$.fn.formError = function(msg) {
	alert(msg);
	$(this).focus();
}

$.fn.validateEmail = function(mandatory) {
	if (mandatory == null) mandatory = false;
	
	var email = $(this).val();
	if (!mandatory && email == "") {
		return true;
	}
	
	var emailRegEx = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (!email.match(emailRegEx)) {
		$(this).formError(labels['a_Direcci&oacute;n de email no v&aacute;lida']);
		return false;
	}
	return true;
}

$.fn.validateColor = function() {
	var codigo = $(this).val();
	
	var ret = true;
	if (codigo.length == 6) {
		var strValido = "0123456789AaBbCcDdEeFf";
		
		for (i=0;i<codigo.length;i++) {
			strChar = codigo.charAt(i);
			if (strValido.indexOf(strChar) == -1) {
				ret = false;
				break;
			}
		}
	} else {
		ret = false;
	}
	
	if (!ret) {
		$(this).formError("Color no válido");
		return false;
	}
	
	return true;
}
