$(function(){
	/* Formulario de contacto */
	var api = $('.contact-toggle').overlay({ 
		target: '#contact-form-wrapper',
		speed: 300,
		closeOnClick: false,
		onLoad: function() {
			contacto();
		},
		api: true
	});
	function contacto(){
		jQuery('#contact_form').unbind().submit(function(event) {
			event.preventDefault();
			var url = jQuery(this).attr('action');
			var name = jQuery('#cf_name').val();
			var email = jQuery('#cf_email').val();
			var asunto = jQuery('#cf_asunto').val();
			var mensaje = jQuery('#cf_mensaje').val();

			jQuery.post(url, {'cf_name': name, 'cf_email': email, 'cf_asunto': asunto, 'cf_mensaje': mensaje}, function(response) {
				jQuery('#contact-form-wrapper').html(response);
				contacto();
			});
		});
		
		$('.input').fieldtag();
		
		$('.cerrar').bind('click', function(){
			api = $('.contact-toggle').overlay();
			api.close();
		});
	};
	
	/* Cycle Destacado Home */
	$('#destac_slideshow .gallery-item').cycle({
		speed: 200,
		timeout: 5000
	});
	
	/* Visor de proyectos */
	$('#thumbs li')
	.hover(
		function(){
			$(this).find('img').siblings('.gallery-caption').slideDown(100);
		},
		function(){
			$(this).find('img').siblings('.gallery-caption').slideUp(100);
		})
	.click(
		function() {
			if (!$.browser.msie) {
				$(this).block({ 
					message: '<span class="loader">Cargando</span>',
					timeout: 2000,
					showOverlay: false,
					css : {
						border : 'none',
						backgroundColor : 'none'
						}
				});
			};
		var url = $(this).find('img').attr('src').replace('-146x90', '');
		var wrap = $('#project-viewer');
		var img = new Image();
		img.src = url;
		img.onload = function() {
			wrap.find('img').attr({'src': url, 'height': img.height});

			if ($('#toggle-full').hasClass('active')) {
				$('#project-viewer').animate({'height':img.height});
			}
		};
	})
	.filter(':first').click().addClass('active');
	
	$("#thumbs").scrollable({
		items:'.gallery-item'
	});
	
	/* Visor de proyectos */
	$('.size-medium').wrap('<div class="img_container"></div>');
	
	$('#toggle-full')
	.hover(
		function(){
			$(this).animate({
				'opacity' : 1
			});
		},
		function(){
			$(this).animate({
				'opacity' : 0.5
			});
		}
	)
	.css('opacity',0.5)
	.click(function() {
		$(this).toggleClass('active').text('Tamaño reducido');
		
		var currentImageH = $('#project-viewer img').height();
		
		if ($(this).hasClass('active')) {
			
			$('#project-viewer').animate({'height':currentImageH});
			$('#project-viewer img').animate({'top':0});
		} else {
			$(this).text('Tamaño completo');
			$('#project-viewer').animate({'height':'300px'});
			$('#project-viewer img').animate({'top':'-30%'});
		}
		
	});
	
	$('.cancelar').click(function() {
		return false;
	});
	
	/* Escondemos en la página de proyecto las categorías vacías */
	$('.list_link li:empty').each(function(){
		$(this).parents('.list_link_sorter_wrap').hide()
	});
	
	/* Escondemos el navegador de miniaturas si solo hay una */
	if($('.gallery-icon').length < 2) {
		$('#thumbs').hide();
	};
	
	/* ir al proyecto al clicar en la imagen */
	var projectImg = $('.project_thumbs img');
	projectImg.click(
		function()
		{
   			location.href = $(this).parent().find('a').attr('href');
	});
	
	// Fancybox
	$("a[href$='jpg'],a[href$='png']").fancybox({
		'titleShow'		: false,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic'
	});
	
	// Cambiador de address
	$('.switch').click(function(){
		$(this).addClass('hidden');
		$(this).siblings('.switch').removeClass('hidden');
		$(this).siblings('address').toggleClass('hidden');
	});
});