Shadowbox.loadSkin('classic', 'css/skin');

$(function(){
	homeDestacados(); //cambio de fondo en la home
	hoverZoom(); //hover en el "+" de destacados y proyectos
	destacados.init(); //interaccion en pagina de destacados
	proyectos.init(); //interaccion en pagina de proyectos/investigacion
        investigacion.init();
        premios.init(); //interaccion en pagina de premios
        historico.init(); //interaccion en pagina de premios/publicaciones
	Shadowbox.init(); //lightbox
	diaporama(); // en estudio
	blockquotes();
	cv();
	$.preloadCssImages();
});

var homeDestacados = function(){
	$('body#home #menu-destacados li').hover(function(){
		$('body').addClass( $(this).attr('id').split('-')[1] );
	},function(){
		$('body').removeClass( $(this).attr('id').split('-')[1] );
	});
};

var hoverZoom = function(){
	$('.img a').hover(function(){
		$(this).parents('.img').find('a.zoom').addClass('hover');
	},function(){
		$(this).parents('.img').find('a.zoom').removeClass('hover');
	});
}

var destacados = {
	path: 'fotos/proyectos/',
	changeImage: function(){
		var $$ = $(this);
		
		$('.img li.selected').fadeOut('fast', function(){
			$('a[href=' + $$.attr('href') + ']',$('.img')).parent().fadeIn('fast', function(){
				$('.img li.selected').removeClass('selected');
				$(this).addClass('selected');
			});
		});

		return false;
	},
	zoom: function(){
		var $$ = $(this);
		if ($$.hasClass('big')){ //reducimos
			//redimensionamos
			$('img',$$).animate({ width:"395px", height: "295px"}, 250);
			//añadimos clase y cambiamos icono
			$$.removeClass('big').next().removeClass('menos');
			//eliminamos timeout
			clearTimeout(autoHide);
		}
		else{ //aumentamos
			//redimensionamos
			$('img',$$).animate({ width:"715px", height: "536px"}, 500)
			//quitamos clase y cambiamos icono
			$$.addClass('big').next().addClass('menos');
			//a los 10 segundos si el usuario no ha hecho "zoom in" lo hago automaticamente
			var autoHide = setTimeout(function(){ $('img',$$).animate({ width:'395px', height: '295px'}, 250); $$.removeClass('big').next().removeClass('menos'); }, 5000);
		}
		
		return false;
	},
	init: function(){
		if ( !$('body#destacados')[0] ) return; //solo ejecuto si estamos en destacados
		//$('.img a').click(this.zoom);
		$('.list-block a').click(this.changeImage);
	}
};


var proyectos = {
	path: 'fotos/proyectos/',
	loadProject: function(){
		var id = $(this).parent().attr('id');
		//cambiamos de selected
		$('#thumbs td a.selected').removeClass('selected');
		$(this).addClass('selected');

		//ocultamos contenido y mostramos loading
		$('#datos .content, .img a[rel], .img a.zoom').fadeOut('fast');
		$('#datos .loading, .img .loading').fadeIn('fast');

		$.getJSON($(this).attr('href'), function(json){
			

			$('#datos')
				.find('h2')
					.text(json.nombre)
				.end()
				.find('strong.donde')
					.text(json.donde)
				.end()
				.find('p.estado')
					.text(json.estado)
                		.end()
				.find('p.texto')
					.html(json.texto);



				$('#datos li').remove();
				$.each(json.datos, function(i,item){
					$('#datos ul').append('<li><strong>'+ i +'</strong><p>'+ item +'</p></li>');
				});

				//modificamos src y href
				$('.img')
					.find('a[rel]')
						.attr('href',proyectos.path+id+'-big.jpg')
                        .attr('title',json.caption)
					.end()
					.find('img')
						.attr('src',proyectos.path+id+'.jpg');

				Shadowbox.setup($('.img a[rel]')); //rellamamos al lightbox

			//quitamos loading y mostramos nuevo contenido
			$('#datos .loading').fadeOut('fast');
			$('#datos .content').fadeIn('fast');
			//quitamos el loading y mostramos la foto cuando estÃ¯Â¿Â½ completamente cargada
			$('.img img').load(function(){
				$('.img .loading').fadeOut('fast');
				$('.img a[rel], .img a.zoom').fadeIn('fast');
			});

		});

		return false;
	},
	changeCat: function(){
		$(this).parents('ul').find('li.selected').removeClass('selected');
		$(this).parent().addClass('selected');

		if ($(this).hasClass('reset')){
			$('#thumbs td').removeClass('inactive').find('a').unbind('click').click(proyectos.loadProject);
		}
		else{
			var type = $(this).text().toLowerCase();
			$('#thumbs td').addClass('inactive').find('a').unbind('click').click(function(){ return false; });
			$('#thumbs td[class*='+type+']').removeClass('inactive').find('a').unbind('click').click(proyectos.loadProject);
		}

		return false;
	},
	init: function(){
		if ( $('body#proyectos').length == 0 ) return; //solo ejecuto si estamos en proyectos
		//poner tipo de foto (clase en td) automaticamente? (equipamiento, vivienda....)
		//cargamos background celdas
		$('#thumbs td[id]').each(function(){
			$('a',$(this)).css('background-image','url('+ proyectos.path + $(this).attr('id') +'-thumb.jpg)');
		});
		//al hacer click cargo ajax y foto
		$('#thumbs td a').click(this.loadProject);
		$('.right ul a').click(this.changeCat);
	}
}


var investigacion = {
	path: 'fotos/investigacion/',
	loadProject: function(){
		var id = $(this).parent().attr('id');
		//cambiamos de selected
		$('#thumbs td a.selected').removeClass('selected');
		$(this).addClass('selected');

		//ocultamos contenido y mostramos loading
		$('#datos .content, .img a[rel], .img a.zoom').fadeOut('fast');
		$('#datos .loading, .img .loading').fadeIn('fast');

		$.getJSON($(this).attr('href'), function(json){


			$('#datos')
				.find('h2')
					.text(json.nombre)
				.end()
				.find('strong.periodo')
					.text(json.periodo)
				.end()
				.find('p.descripcion')
					.html(json.descripcion);



				//modificamos src y href
				$('.img')
					.find('a[rel]')
						.attr('href',investigacion.path+id+'-bigs.jpg')

					.end()
					.find('img')
						.attr('src',investigacion.path+id+'.jpg');

				Shadowbox.setup($('.img a[rel]')); //rellamamos al lightbox



			//quitamos loading y mostramos nuevo contenido
			$('#datos .loading').fadeOut('fast');
			$('#datos .content').fadeIn('fast');
			//quitamos el loading y mostramos la foto cuando estï¿½ completamente cargada
			$('.img img').load(function(){
				$('.img .loading').fadeOut('fast');
				$('.img a[rel], .img a.zoom').fadeIn('fast');
			});

		});

		return false;
	},
	init: function(){
		if ( $('body#investigacion').length == 0 ) return; //solo ejecuto si estamos en proyectos
		//poner tipo de foto (clase en td) automaticamente? (equipamiento, vivienda....)
		//cargamos background celdas
		$('#thumbs td[id]').each(function(){
			$('a',$(this)).css('background-image','url('+ investigacion.path + $(this).attr('id') +'-thumb.jpg)');
		});
		//al hacer click cargo ajax y foto
		$('#thumbs td a').click(this.loadProject);
	}
}


var premios = {
	path: 'fotos/curriculum/',
	loadProject: function(){
		var id = $(this).attr('rel');
		//cambiamos de selected
		$('.right td a.selected').removeClass('selected');
		$(this).addClass('selected');

		//ocultamos contenido y mostramos loading
		$('#bottom .left ul, .img a[rel], .img a.zoom').fadeOut('fast');
		$('#bottom .left .loading, .img .loading').fadeIn('fast');
		$.getJSON($(this).attr('href'), function(json){
			//borramos los li's y añadimos los nuevos
			$('#bottom .left li').remove();
			$.each(json, function(i,item){
				$('#bottom .left ul').append('<li>'+ item +'</li>');
			});

			//modificamos src y href
			$('.img')
				.find('a[rel]')
					.attr('href',premios.path+id+'-big.jpg')
				.end()
				.find('img')
					.attr('src',premios.path+id+'.jpg');

			Shadowbox.setup($('.img a[rel]')); //rellamamos al lightbox

			//quitamos loading y mostramos nuevo contenido
			$('#bottom .left .loading').fadeOut('fast');
			$('#bottom .left ul').fadeIn('fast');
			//quitamos el loading y mostramos la foto cuando esté completamente cargada
			$('.img img').load(function(){
				$('.img .loading').fadeOut('fast');
				$('.img a[rel], .img a.zoom').fadeIn('fast');
			});

		});

		return false;
	},
	init: function(){
		if ( !$('body#curriculum')[0] ) return; //solo ejecuto si estamos en proyectos/investigacion

		//al hacer click cargo ajax y foto
		$('.right td a').click(this.loadProject);
	}
}

//para premios y publicaciones
var historico = {
	loadProject: function(){
		path = '/fotos/' + $('body').attr('id') + '/';
		var id = $(this).attr('rel');
		//cambiamos de selected
		$('.right td a.selected').removeClass('selected');
		$(this).addClass('selected');

		//ocultamos contenido y mostramos loading
		$('#bottom .left ul, .img a[rel], .img a.zoom').fadeOut('fast');
		$('#bottom .left .loading, .img .loading').fadeIn('fast');

		$.getJSON($(this).attr('href'), function(json){
			//borramos los li's y añadimos los nuevos
			$('#bottom .left li').remove();
			$.each(json, function(i,item){
				if (item.indexOf('titulo//') > -1)  $('#bottom .left ul').append('<li class="title">'+ item.replace('titulo//','') +'</li>');
				else  $('#bottom .left ul').append('<li>'+ item +'</li>');
			});

			if (id){
				//modificamos src y href
				$('.img')
					.find('a[rel]')
						.attr('href',path+id+'-big.jpg')
					.end()
					.find('img')
						.attr('src',path+id+'.jpg');

				Shadowbox.setup($('.img a[rel]')); //rellamamos al lightbox
			}

			//quitamos loading y mostramos nuevo contenido
			$('#bottom .left .loading').fadeOut('fast');
			$('#bottom .left ul').fadeIn('fast');
			//quitamos el loading y mostramos la foto cuando esté completamente cargada
			$('.img img').load(function(){
				$('.img .loading').fadeOut('fast');
				$('.img a[rel], .img a.zoom').fadeIn('fast');
			});

		});

		return false;
	},

	init: function(){
		if ( !$('body#curriculum')[0] && !$('body#publicaciones')[0] ) return; //solo ejecuto si estamos en curriculum o publicaciones
		//var path = 'fotos/' + $('body').attr('id') + '/';
		//al hacer click cargo ajax y foto
		$('.right td a').click(this.loadProject);
	}
}

//transición de imagenes
var diaporama = function(){
	$('.diaporama').innerfade({ 
		speed: 'slow', 
		timeout: 4000, 
		type: 'sequence', 
		containerheight: '260px' 
	}); 
}

//coloco las comillas en las blockquotes
var blockquotes = function(){
	$('blockquote > *').each(function(){
		$(this).html('"' + $(this).html() + '"');
	});
}

//cv presentacion
var cv = function(){
	$('#estudio.presentacion #bottom .right ul a').click(function(){
		var target = $('.' + $(this).attr('rel'));
		$(this).parent().addClass('active').siblings().removeClass('active');
		target.siblings(':visible').fadeOut(function(){
			target.fadeIn();
		});
	})
}

/*-------------------------------------------*/

function validarEmail(valor) {
	return (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/).test(valor);
}

function linkBack(){
	$('a.link-back').click(function(){
		history.back();
		return false;
	});
};

function externalLinks(){
	$('a[href^="http://"]').addClass('external');
};


//plugin para corregir el overflow en ie
/*(function ($) {
  $.fn.fixOverflow = function () {
    if ($.browser.msie) {
      return this.each(function () {
        if (this.scrollWidth > this.offsetWidth) {
          $(this).css({ 'padding-bottom' : '20px', 'overflow-y' : 'hidden' });
        }
      });            
    } else {
      return this;
    }
  };
})(jQuery);*/

// usage
//$('pre').fixOverflow();