// Fix para links externos (rel)
function fix_external_links() {
	if (!document.getElementsByTagName) return;
	var anchors = document.getElementsByTagName("a");
	for (var i = 0; i < anchors.length; i++) {
		var anchor = anchors[i];
		if (anchor.getAttribute("rel") && anchor.getAttribute("rel") == "external") {
			anchor.target = "_blank";
		}
	}
}
window.onload = fix_external_links;
	
$(document).ready( function() {
	
	//Chequeo alto de columnas y posiciono #caja-contacto
	if ($("#leftcolumn").height() > $("#rightcolumn").height()) {
		$("#rightcolumn").height($("#leftcolumn").height());	
	}else{
		$("#leftcolumn").height($("#rightcolumn").height());
		
	}
	
    //Slider
	$("#slider").easySlider({
		auto: false,
		continuous: true,
		nextId: "slider1next",
		prevId: "slider1prev"
	});
	$("#barra-imagen").easySlider({
		auto: true,
		continuous: true,
		controlsShow: false,
		speed: 800,
		pause: 6000
	});
	$("#main-photoslider").easySlider({
		auto: true,
		continuous: true,
		controlsShow: false,
		speed: 800,
		pause: 8000
	});
	
	
	$("div.categoria h4 a").click(function (evento) {  
			evento.preventDefault();
			$(this).parent().next("ul").slideToggle("slow");
	});
	
	$("a.selected").click(function (evento) {  
			evento.preventDefault();
	});
	
	//Contacto
	$('#btn-contacto-reset').click(function (evento) { 
			evento.preventDefault();
			$($(this).attr('rel')).resetForm();
	});
	
	$('#btn-contacto-enviar').click(function (evento) { 
			evento.preventDefault();
			test = validarContacto();
			if (test) {
				$($(this).attr('rel')).submit();
			}
	});
	
	
	jQuery.fn.resetForm = function () {
		$(this).each (function() { this.reset(); });
	}
	
	$(function(){
		jQuery.fn.equalHeight = function () {
			var tallest = 0;
			this.each(function() {
				tallest = ($(this).height() > tallest)? $(this).height() : tallest;
			});
		return this.height(tallest);	
		}
		$(".columna-x3").equalHeight();
	});
});

function validarContacto(){
	if (testearLleno('#nombre')) {
		if (testearLleno('#asunto')) {
			if (testearLleno('#comentarios')) {
				if (testearLleno('#email')) {
					var filtro = /^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@+([_a-zA-Z0-9-]+\.)*[a-zA-Z0-9-]{2,200}\.[a-zA-Z]{2,6}$/;
					if(!filtro.test($('#email').val())){
						alert("El email ingresado no es valido.");
						$('#email').focus();
						return false;	
					}
					return true;
				}
			}
		}
	}
	return false;
}

function testearLleno(objeto) {
	if ($(objeto).val() == "") {
			alert("El campo "+(objeto.substring(1))+" no puede estar vacio.");
			$(objeto).focus();
			return false;
	} else { 
			return true;
	}
}

