
$(document).ready(function() {
	
	$('a').click(function() {
		if($(this).attr('rel') == 'external') {
			$(this).attr('target', '_blank');
		}
	})
	
	var mapW = $('.noticias li:first').width();
	
	//$('#mapa').css('width', (mapW+171));
	//$('#contacto_home').css('margin-right', (mapW+171));
	
	if ($('#vimeo').length) {
		var video = $('#vimeo');
		var vidW = video.closest('.contenido').width();
		video.css('width', (vidW+75));
		video.css('height', Math.floor(((vidW+75)*268)/476));
	}
	
	$('#banners').slider({width:'100',med: '%',navParent: '#banNav',holes: 1});
	//$('.noticias').slider({width:'35',med: '%',holes: 3});
	$('.noticias2').slider({width:'50',med: '%',holes: 2});
	$('.blog').slider({width:'171',med: 'px', holes: 1});
	
	$('.proyectos').slider({width:'100',med: '%',navParent: '#proyNav',holes: 1});
	
	$(".noticia").click(function(){
		location.href=$('a:first',this).attr('href');
	});
	
	// CHECKBOX MAP
	$('input[type="checkbox"]').parent().mouseenter(function() {
		$(this).find('.cbMask').addClass('hover');
	});
	
	$('input[type="checkbox"]').parent().mouseleave(function() {
		$(this).find('.cbMask').removeClass('hover');
	});
	
	$('input[type="checkbox"]').change(function() {
		$(this).is(':checked')?$(this).siblings('.cbMask').addClass('active'):$(this).siblings('.cbMask').removeClass('active');
	});
	
	$('#checkAll').click(function () {
		$('.filtro :checkbox').attr('checked', this.checked);
		$(this).is(':checked')?$('.filtros .cbMask').addClass('active'):$('.filtro .cbMask').removeClass('active');
	});
	
	$('.filtro input:checkbox').click(function () {
		if ($('.filtro input:checkbox').length == $('.filtro input:checkbox:checked').length) {
			$('#checkAll').attr('checked', 'checked');
			$('#checkAll').siblings('.cbMask').addClass('active');
		}else{
			$('#checkAll').attr('checked', '');
			$('#checkAll').siblings('.cbMask').removeClass('active');
		}
	});
	
	
	//startTwitter();
	//$('.people').nextTweet();
	$('.people').startTwitter();
	$('.tweets').startTwitter();
	$('.peopleChat').startTwitter();
	
	
	// FORMULARIOS
	$('input:not(:checkbox)').focus(function() {
		$(this).siblings('span').fadeTo(200,0.2);
		$(this).closest('.inputBg, .buscador').addClass('active');
	});
	$('input:not(:checkbox)').blur(function() {
		if ($(this).val() == '') {
			$(this).siblings('span').fadeTo(200,1);
			$(this).closest('.inputBg, .buscador').removeClass('active');
		}
	});
	
	$('textarea').focus(function() {
		$(this).siblings('span').fadeTo(200,0.2);
		$(this).closest('.inputBg').addClass('active');
	});
	$('textarea').blur(function() {
		if ($(this).val() == '') {
			$(this).siblings('span').fadeTo(200,1);
			$(this).closest('.inputBg').removeClass('active');
		}
	});
	
	$('#hS-button').click(function(e) {
		e.stopPropagation();
		if ($(this).hasClass('close')) {
			$(this).removeClass('close').animate({ right: 10 }, 300).addClass('open').siblings('.buscaInput').stop().animate({ right: 0 }, 300, function(){
				$('input:first',this).focus();
			}).closest('.buscador').animate({ width: 172 }, 300);
			$('.menu.idiomas').fadeTo(300, 0);
			return false;
		//}else if ($(this).hasClass('open') && $('input:first',this).val() == '') {
		}else if ($(this).hasClass('open') && $('#idBuscar').val() == '') {
			//alert($('input:first',this).val());
			$(this).animate({ right: 0 }, 300).removeClass('open').addClass('close').siblings('.buscaInput').stop().animate({ right: -30 }, 300).closest('.buscador').animate({ width: 25 }, 300);
			$('.menu.idiomas').fadeTo(300, 1);
			return false;
		}
	});
	
	$('.buscaInput').click(function(e) {
		e.stopPropagation();
	});
	
	$('body').click(function() {
		$('#hS-button').animate({ right: 0 }, 300).removeClass('open').addClass('close').siblings('.buscaInput').stop().animate({ right: -30 }, 300).closest('.buscador').animate({ width: 25 }, 300);
		$('.menu.idiomas').fadeTo(300, 1);
	});
	
	
	$(".menu.idiomas").mouseenter(function () {
		$('.oculto',this).css("display", "block");
	});
	
	$(".menu.idiomas").mouseleave(function () {
		$('.oculto',this).css("display", "none");
	});
	
	
	$('.newsletter-hand a').click(function() {
		var winHeight = $(window).height();
		var winWidth = $(window).width();
		
		$('#mask').height(winHeight);
		$('#mask').width(winWidth);
		
		$('#modSuscripcion').css('left', ((winWidth/2) - ($('#modSuscripcion').width()/2)));
		$('#modSuscripcion').css('top', ((winHeight/2) - ($('#modSuscripcion').height()/2)));
		
		$('#mask').fadeTo(300, 0.4);
		$('#modSuscripcion').fadeTo(300, 1);
		
		/*
		if ($(this).hasClass('closed')) {
			$('#newsletter').stop().animate({ top: 0 }, 300)
			$(this).removeClass('closed').addClass('opened');
		}else{
			$('#newsletter').stop().animate({ top: -50 }, 300)
			$(this).removeClass('opened').addClass('closed');
		}
		*/
		return false;
	});
	
	$('#mask').click(function() {
		$(this).fadeTo(300, 0, function(){
			$(this).hide();
		});
		$('#modSuscripcion').fadeTo(300, 0, function(){
			$(this).hide();
		});
	});
});

$(window).bind("resize", function() {
	
	var mapW = $('.noticias li:first').width();
	
	//$('#mapa').css('width', (mapW+171));
	//$('#contacto_home').css('margin-right', (mapW+171));
	
	
	var winHeight = $(window).height();
	var winWidth = $(window).width();
	
	$('#mask').height(winHeight);
	$('#mask').width(winWidth);
	
	$('#modSuscripcion').css('left', ((winWidth/2) - ($('#modSuscripcion').width()/2)));
	$('#modSuscripcion').css('top', ((winHeight/2) - ($('#modSuscripcion').height()/2)));
	
	if ($('#vimeo').length) {
		var video = $('#vimeo');
		var vidW = video.closest('.contenido').width();
		video.css('width', (vidW+75));
		video.css('height', Math.floor(((vidW+75)*268)/476));
	}
	
});




// TWITTER SLIDER	
var c=0;
var t;
var timer_is_on=0;

function nextTweet()	{
	var element = $('.people li:first');
	element.clone().appendTo('.people');

	Cufon.refresh();
	element.fadeTo(200,0, function(){
		$(this).animate({'height': 0, 'margin-bottom': 0}, function(){
			$(this).remove();
		});
	});
	//c=c+1;
	t=setTimeout("nextTweet()",5000);
}

function startTwitter()	{
	if (!timer_is_on) {
		timer_is_on=1;
		nextTweet();
	}
}

(function($){

	$.fn.nextTweet = function(options) {
		
		if ($('li',this).length > 3) {
			var object = $(this);
			var element = $('li:first',this);
			if (object.hasClass('play')) {
				element.clone().appendTo(this);
				Cufon.refresh();
				element.fadeTo(200,0, function(){
					$(this).animate({'height': 0, 'margin-bottom': 0}, function(){
						$(this).remove();
					});
				});
			}
			
			setTimeout(function(){object.nextTweet(options);},options.time);
		}
	}
	
	$.fn.startTwitter = function(options) {
		var opts = $.extend({}, $.fn.startTwitter.defaults, options);
		
		$(this).addClass('play').nextTweet(opts);
		
		$(this).mouseenter(function(){ $(this).removeClass('play') });
		$(this).mouseleave(function(){ $(this).addClass('play') });
	}
	
	$.fn.startTwitter.defaults = { time: 10000 };
	
})(jQuery);
	
	

(function($){

	$.fn.slider = function(options) {
		var opts = $.extend({}, $.fn.slider.defaults, options);
		var sWidth = opts.width+opts.med;
		
		var container = $(this);
		
		if (opts.navParent != ''){
			var navParent = $(opts.navParent);
		}else{
			var navParent = $(this).parent();
		}
		
		
		//var limit = (2*opts.holes) - $('li',container).length;
		var limit = opts.holes - 1;
		
		if (limit < 0) limit=0;
		
		navParent.find('.fright').stop().click(function(e) {
			e.stopImmediatePropagation();
			
			var fLeft = $(this).parent().children('.fleft');
			var fRight = $(this);
			
			var left = parseInt(Math.ceil($('li:last',container).css('left').replace(opts.med, '')));
			var lastLeft = left-parseInt(opts.width);
			//alert(lastLeft+' - '+(limit*opts.width))
			if ( lastLeft >= (limit*opts.width) ) {
				var dot = $(this).parent().children('.dot');
				var origen = parseInt(dot.index($(this).parent().children('.activo')));
				
				fLeft.addClass('factiva');
				
				$('li:not(:animated)',container).animate({left: '-='+sWidth}, 200, function() {
					//fRight.removeClass('factiva');
					dot.removeClass('activo');
					dot.eq(origen+1).addClass('activo');
					if (dot.last().hasClass('activo')) fRight.removeClass('factiva');
				});
			}
		});
		
		navParent.find('.fleft').stop().click(function(e) {
			e.stopImmediatePropagation();
			
			var fLeft = $(this);
			var fRight = $(this).parent().children('.fright');
			
			var left = parseInt(Math.ceil($('li:first',container).css('left').replace(opts.med, '')));
			var lastLeft = left+parseInt(opts.width);
			
			if ( lastLeft <= 0 ) {
				var dot = $(this).parent().children('.dot');
				var origen = parseInt(dot.index($(this).parent().children('.activo')));
				
				fRight.addClass('factiva');
				
				$('li:not(:animated)',container).animate({left: '+='+sWidth}, 200, function() {	
					//fLeft.removeClass('factiva');
					dot.removeClass('activo');
					dot.eq(origen-1).addClass('activo');
					if (dot.first().hasClass('activo')) fLeft.removeClass('factiva');
				});
			}
		});
		
		navParent.find('.dot').stop().click(function(e) {
			e.stopImmediatePropagation();
			
			var activeDot = $(this);
			
			if ( activeDot.is(':not(.activo)') ) {
				var dot = activeDot.parent().children('.dot');
				
				var origen = parseInt(dot.index(activeDot.parent().children('.activo')));
				var destino = parseInt(dot.index(activeDot));
				var dWidth = (opts.width*(destino-origen))+opts.med;
				
				$('li:not(:animated)',container).animate({left: '-='+dWidth}, 200, function() {
					dot.removeClass('activo');
					activeDot.addClass('activo');
				
					var fLeft = dot.parent().children('.fleft');
					var fRight = dot.parent().children('.fright');
					
					fLeft.addClass('factiva');
					fRight.addClass('factiva');
					
					if (dot.first().hasClass('activo')) fLeft.removeClass('factiva');
					if (dot.last().hasClass('activo')) fRight.removeClass('factiva');
				});
			}
		});
	
	
	
	}
	
	$.fn.slider.defaults = {
		width: '35%',
		med: '%',
		navParent: '',
		holes: 1
	};
		
})(jQuery);



