var selected;

$().ready(function(){

	//MENU PPAL
	$("ul#menu ul").hide();

    var SELECTOR = "a[@href*=" + SECC_OPEN + "]";
    $(SELECTOR).parent().parent().addClass("sel").children("ul").show();

	$("a[@href*=sub]").click(function(){

		$("ul#menu ul").hide();
		$("ul#menu > li").removeClass("sel");

		if( selected != $(this).attr("href") ){
			$(this).parent().parent().addClass("sel").children("ul").show();
			selected = $(this).attr("href");
		}else{
			selected = null;
		}

		return false;

	});



	//PLANO DE VIVIENDAS TIPO

	$("#plano").mouseover(function(){
		$(this).attr("src", map_over);
	});
	$("#plano").mouseout(function(){
		$(this).attr("src", map_norm);
	});


	//VISOR DE IMAGENES DE PROMOCIONES
	if(document.getElementById("minis")){
		var objDiv = document.getElementById("minis");
		var step = 0;
		var s;

		var myScroll = function(args){
			objDiv.scrollTop = objDiv.scrollTop + step;
		}

		$("div.img_desc").hide();
		$("div.mem-cont").hide();
		$("div#f-0.img_desc").show();
		$("div#f-0-cont.mem-cont").show();


		$("#fup").mouseover(function(){
			$(this).addClass("over");
			step = -5;
			s = window.setInterval(myScroll, 50);
		});
		$("#fdown").mouseover(function(){
			$(this).addClass("over");
			step = 5;
			s = window.setInterval(myScroll, 50);
		});
		$("#fdown").mouseout(function(){
			$(this).removeClass("over");
			window.clearInterval(s);
		});
		$("#fup").mouseout(function(){
			$(this).removeClass("over");
			window.clearInterval(s);
		});

		$("#minis a").click(function(){
			$("div.img_desc").hide();
    		$("div.mem-cont").hide();

			var divId = $(this).attr("rel");
			$("div#"+divId+".img_desc").fadeIn("fast");
			$("div#"+divId+"-cont.mem-cont").show();

			return false;
		});

	}



	//VISOR DE CALIDADES
	if(document.getElementById("minis-calidades")){
		var objDiv = document.getElementById("minis-calidades");
		var step = 0;
		var s;

		var myScroll = function(args){
			objDiv.scrollTop = objDiv.scrollTop + step;
		}

		//$("div.mem-cont").hide();

		//$("div#f_0-cont.mem-cont").show();

		$("#fup").mouseover(function(){
			$(this).addClass("over");
			step = -5;
			s = window.setInterval(myScroll, 50);
		});
		$("#fdown").mouseover(function(){
			$(this).addClass("over");
			step = 5;
			s = window.setInterval(myScroll, 50);
		});
		$("#fdown").mouseout(function(){
			$(this).removeClass("over");
			window.clearInterval(s);
		});
		$("#fup").mouseout(function(){
			$(this).removeClass("over");
			window.clearInterval(s);
		});

		$("#minis-calidades a").click(function(){

			$(this).parent().find("a").removeClass("active");
			$(this).addClass("active");

			$("#minis-calidades a").each(function(){
				var img_norm = $(this).attr("img_norm");
				$(this).find("img").attr("src", img_norm);
			});

			var img_over = $(this).attr("img_over");
			$(this).find("img").attr("src", img_over);


			var ancla = "#" + $(this).attr("rel") + "-cont";
			self.document.location.href = ancla;

			return false;
		});
		$("#minis-calidades a").mouseover(function(){
			if($(this).attr("class")!="active"){
				var img_over = $(this).attr("img_over");
				$(this).find("img").attr("src", img_over);
			}
		});
		$("#minis-calidades a").mouseout(function(){
			if($(this).attr("class")!="active"){
				var img_norm = $(this).attr("img_norm");
				$(this).find("img").attr("src", img_norm);
			}
		});

	}




	//VISOR DE GALERIA
	if(document.getElementById("minis-galeria")){
		var objDiv = document.getElementById("minis-galeria");
		var step = 0;
		var s;

		var myScroll = function(args){
			objDiv.scrollTop = objDiv.scrollTop + step;
		}

		$("div.img_desc").hide();

		$("div#f-0.img_desc").show();

		$("#fup").mouseover(function(){
			$(this).addClass("over");
			step = -5;
			s = window.setInterval(myScroll, 50);
		});
		$("#fdown").mouseover(function(){
			$(this).addClass("over");
			step = 5;
			s = window.setInterval(myScroll, 50);
		});
		$("#fdown").mouseout(function(){
			$(this).removeClass("over");
			window.clearInterval(s);
		});
		$("#fup").mouseout(function(){
			$(this).removeClass("over");
			window.clearInterval(s);
		});

		$("#minis-galeria a").click(function(){

			$("div.img_desc").hide();

			var divId = $(this).attr("rel");
			$("div#"+divId+".img_desc").fadeIn("fast");

			return false;
		});

	}


	//VISOR DE VISITA VIRTUAL
	if(document.getElementById("minis-visita")){
		var objDiv = document.getElementById("minis-visita");
		var step = 0;
		var s;

		var myScroll = function(args){
			objDiv.scrollLeft = objDiv.scrollLeft + step;
		}



		$("#fder").mouseover(function(){
			$(this).addClass("over");
			step = +7;
			s = window.setInterval(myScroll, 50);
		});
		$("#fizq").mouseover(function(){
			$(this).addClass("over");
			step = -7;
			s = window.setInterval(myScroll, 50);
		});
		$("#fder").mouseout(function(){
			$(this).removeClass("over");
			window.clearInterval(s);
		});
		$("#fizq").mouseout(function(){
			$(this).removeClass("over");
			window.clearInterval(s);
		});


	}



	$("table.calidades tr").mouseover(function(){
		$(this).children("td").addClass("over");
	});
	$("table.calidades tr").mouseout(function(){
		$(this).children("td").removeClass("over");
	});


	//funcionalidad imprimir
	$("a[@href*=#print]").click(function(){
		window.print();
	});




	$("#mas_informacion").submit( function() {

		$(".row", this).removeClass("error");
		var valid = true;

		function markError(_id){
			$(_id,this).parent().parent().addClass("error");
			valid = false;
		}

		function isValidEmail(emailAddress){
			var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
			if (filter.test(emailAddress)) return true;
			else return false;
		}

		if($("#nombre",this).val().length < 2){
			markError("#nombre");
		}
		if($("#apellidos",this).val().length < 2){
			markError("#apellidos");
		}
		if( !isValidEmail( $("#email",this).val() ) ){
			markError("#email");
		}
		if($("#telefono",this).val().length < 2){
			markError("#telefono");
		}



		if(!valid) $("form #msg_error").html("Por favor, revisa el formulario y completa los datos marcados en rojo.");

		return (valid);
	} );




});

