
//Para que funcionen los actives
var DAEMON_INTERVAL = null;
var INTERVAL_TIME = 5000;
var mini=1;
var max=15;


// Slider central
function slideDaemon() {
	var i = 0;
	var next= 0;
	$(".paginador a").each(function(){
		if ($(this).hasClass("active")) {
			next = i + 1;
			$(this).removeClass("active");
		}
		i++;
		if (next > ($('.paginador a').size()-1)) next = 0;
	});
	$(".paginador a").eq(next).trigger("click");
}

$(document).ready(function() {
	//Shadowbox.init();
	$("a.aviso").fancybox({
		transitionIn	: 'elastic',
		transitionOut	: 'elastic',
		titlePosition 	: 'over',
		type: "ajax",
		width: 650,
		height: 400,
		titleShow: false,
		autoScale: false,
		autoDimensions: false
	});
});
function secProductos() {
			$('div.center h2.ancho a').bind('mousedown', function() {
					$('a.active').removeClass("active");
					var t=$(this);
					$(t).addClass("active");
					var location = this.href.toString().match(/(\#.+)/)[1];
					if ($('div.app-explain-open').length == 0)
						$(location).addClass("app-explain-open").slideDown('fast');
					else
						$('div.app-explain-open').removeClass("app-explain-open").slideUp('fast', function (){
							$(location).addClass("app-explain-open").slideDown('slow');
						});
				return false;
			});
			$('div.center a:first').trigger("mousedown");

			/*$('.app-explain p.ancho a.zoom').bind('click',function() {
				Shadowbox.open({
					player: 'img',
					content: $(this).attr('href')
				})
				return false;
			})
			
			$('.app-explain p.ancho a.link').bind('click',function() {
				Shadowbox.open({
					player: 'img',
					content: $(this).attr('href')
				})
				return false;
			})*/
			
			$(".app-explain p.ancho a.zoom").fancybox({
				'transitionIn'		: 'elastic',
				'transitionOut'		: 'elastic',
				'titlePosition' 	: 'over'
			});

			return false;
}

function secWarmup() {
	$("div.people")
		.bind('mouseover', function() {
			$("div.people").css("z-index", 1);
			$(this).css("z-index", 200);
		})
		.find("a")
			.bind('mouseover', function() {
				$(this).next().slideDown('fast');
			})
			.bind('mouseout', function() {
				$(this).next().slideUp('fast');
			});

		/*$('div.people').bind('click', function() {
			Shadowbox.open({
					player: 'iframe',
					content: 'includes/contact_form.php?destinatario='+$(this).children('a').attr('title'),
					height: 320,
					width: 310,
					handleOversize: "none"
				 });	
		});*/
		
		$("div.people a").fancybox({
			transitionIn	: 'elastic',
			transitionOut	: 'elastic',
			titlePosition 	: 'over',
			height: 320,
			width: 310,
			titleShow: false,
			type: "iframe"
		});

		return false;
}

function secCasos() {
	//Para que se activen los casos
			var a = $('div.center h2 a')
				a.bind('click', function() {
					var t = $(this);
					if(t.hasClass("active")) return false;
					a.filter(".active").removeClass("active");	
					$(t).addClass("active");
					var location = this.href.toString().match(/(\#.+)/)[1];
					if ($('div.cases-img-open').length == 0){
						$(location).addClass("cases-img-open").slideDown('slow', function(){
							$(this).find("div.paginador a:first").trigger("click")
						});
					}
					else
						$('div.cases-img-open').removeClass("cases-img-open").slideUp('slow', function (){
							$(location).addClass("cases-img-open").slideDown('slow', function(){
								$(this).find("div.paginador a:first").trigger("click")
							});
						});
					return false;
			});
			//Para que funcionen los actives
			$('div.paginador a').bind('click', function() {
				$('p.pie-foto').hide();
				$('div.paginador a.active').removeClass("active");
				var t = $(this);
				$(t).addClass("active");
				var location = this.href.toString().match(/(\#.+)/)[1];
				if ($('.img-open').length == 0){
					$(location).addClass("img-open").slideDown('slow');
					$('div.cases-img-open p.pie-foto small').html($($(t).attr('href')).attr('alt'));
					$('p.pie-foto').fadeIn('slow');
				}
				else {
					$('.img-open').removeClass("img-open").slideUp('slow');
					$(location).addClass("img-open").slideDown('slow');
		
					$('div.cases-img-open p.pie-foto small').html($($(t).attr('href')).attr('alt'));
					$('p.pie-foto').fadeIn('slow');
				}
			});

			a.eq(0).trigger("click");
			return false;
}

function secHome() {
			DAEMON_INTERVAL = setInterval("slideDaemon()", INTERVAL_TIME);
			
			
			$('div.paginador a').bind('click', function() {
				$('div.paginador a.active').removeClass("active");
				var t = $(this);
				$(t).addClass("active");
				if ($('.img-open').length == 0){
					$($(t).attr('href')).addClass("img-open").slideDown('slow');
					$('div.pie-foto').fadeIn('slow');
					$('.tituloPost').hide();
					$('.tituloPost').html('"'+$(t).attr('title')+'"');
					$('.tituloPost').fadeIn('slow');
					$('.tituloPost').parent('a').attr('href',$($(t).attr('href')).parent('a').attr('href'));
					$('.tituloPost').parent('a').attr('title',$($(t).attr('href')).parent('a').attr('title'));
				} else {
					$('.img-open').removeClass("img-open").slideUp('slow');
					$($(t).attr('href')).addClass("img-open").slideDown('slow');
					$('div.pie-foto').fadeIn('slow');
					$('.tituloPost').hide();
					$('.tituloPost').html('"'+$(t).attr('title')+'"');
					$('.tituloPost').fadeIn('slow');
					$('.tituloPost').parent('a').attr('href',$($(t).attr('href')).parent('a').attr('href'));
					$('.tituloPost').parent('a').attr('title',$($(t).attr('href')).parent('a').attr('title'));
				}
				return false;
			});
			$('div.paginador a:first').trigger("click");
			return false;
}

function secContacto() {
	//Para que funcionen los actives
	/*$('#mapa').bind('click', function() {
		Shadowbox.open({
			player: 'iframe',
			content: $(this).attr('href'),
			handleOversize: "none"
		 });
		 return false;
		}
	)*/
	$("#mapa").fancybox({
		transitionIn	: 'elastic',
		transitionOut	: 'elastic',
		titlePosition 	: 'over',
		type: "iframe",
		width: 650,
		height: 400,
		titleShow: false,
		autoScale: false,
		autoDimensions: false
	});
	
	$("#formContacto").submit(function() {
		
			jQuery.ajax({
			  url: 'controllers/contactmail.php',
			  type: 'POST',
			  data: (
					{
					nombre : $('#nombre').val(),
					email : $('#email').val(),
					telefono: $('#telefono').val(),
					comentarios: $('#comentarios').val()
					}
			  ),
			  error: function() {
				 alert('Se ha producido un error al enviar el mensaje.');
			  },
			  success: function(data) {
				 if (data=='0') alert('Datos insuficientes en el formulario')
				 else if (data=='success') alert('Formulario enviado correctamente.')
				 else alert('Se produjo un error al registrar el formulario de contacto.');
			  }
			});
		return false;
	});
	//Para apagar los campos al pinchar dentro
	$("#formContacto input, #formContacto textarea").bind('focus', function() {
		if ($(this).val()=="Nombre") $(this).val('');
		if ($(this).val()=="E-mail") $(this).val('');
		if ($(this).val()=="Tel") $(this).val('');
		if ($(this).val()=="Comentarios") $(this).val('');
	})
	//
	$("#formContacto input, #formContacto textarea").bind('blur', function() {
		if ($(this).val()=="" && $(this).attr('id')=='nombre') $(this).val('Nombre');
		if ($(this).val()=="" && $(this).attr('id')=='email') $(this).val('E-mail');
		if ($(this).val()=="" && $(this).attr('id')=='telefono') $(this).val('Tel');
		if ($(this).val()=="" && $(this).attr('id')=='comentarios') $(this).val('Comentarios');
	})
}

