/* Cufon */

$(document).ready(function() { 
  function updateMenu() {
    var id = $(this).attr('id');
    setTimeout(function() {
      //Cufon.replace('a#mm_what_link', {}, false);
      Cufon.replace('a#' + id + '_link', {}, false);
    }, 10);
  }
  $('#main_menu li#mm_who, #main_menu li#mm_what, #main_menu li#mm_news').hover(updateMenu, updateMenu);
});

Cufon.replace('#main_menu li a', { hover: true });
Cufon.replace('#main_menu li a#mm_who_link', { hover: false });
Cufon.replace('#main_menu li a#mm_what_link', { hover: false });
Cufon.replace('#main_menu li a#mm_news_link', { hover: false });
Cufon.replace('h3.title');
Cufon.replace('#destacado h2');
Cufon.replace('#destacado p.read_more a');
Cufon.replace('.secciones h2');
Cufon.replace('.home div.two_column.left h2');
Cufon.replace('.home div.colabora h2');

/* Noticias */

function pieDeFotoNoticias() {
  if ($('.graphic .graphic_image img').length > 0) {
    var $img = $('.graphic .graphic_image img');
    var imgWidth = $img.width();
    $img.closest('.graphic').css('width', imgWidth);
  }
}

/* Detalle proyecto */

$(document).ready(function(){
	$('.project_list p').click(function() {
	  if ($(this).hasClass('opened')) {
	    $(this).removeClass('opened');
	    $(this).next().css('display', 'none');			    
	  }
	  else {
	    $(this).addClass('opened');
	    $(this).next().css('display', 'block');
	  }
	});
});

/* Forms */

$(document).ready(function() {
  function overlayLabel(selectors) {
    $(selectors).each(function() {
      var selector = this;
      if ($(selector).find('input').val() != '')
        $(selector).find('strong').animate({'opacity': '0'}, 0);

      $(selector).find('input').focus(function() {
        if ($(this).val() == '')
          $(selector).find('strong').animate({'opacity': '0'});
      });

      $(selector).find('input').blur(function() {
        if ($(this).val() == '')
          $(selector).find('strong').animate({'opacity': '1'});
      });    
    });
  }
  
  overlayLabel('.overlay_label');
  $("#partner_signup select").sexyCombo();
  overlayLabel('#advanced_search label[for=advanced_search_string]');
  $("#advanced_search select#advanced_search_area").sexyCombo();
});

/* Areas */

$(document).ready(function(){
	if ($('div.areas_list h3').length > 0) {	
	  $('div.areas_list ul').hide();
	  $('div.areas_list h3').css('cursor', 'pointer');
	  $('div.areas_list h3').click(function() {
		  if ($(this).hasClass('opened')) {
		    $(this).removeClass('opened');
		    $(this).next().css('display', 'none');			    
		  }
		  else {
		    $(this).addClass('opened');
		    $(this).next().slideDown();
		  }
		});
	}
});

/* Tabs */

$(document).ready(function(){
  if ($('.tab_menu').length > 0) {
	  $('.tab_menu li').each(function() {
	    if (!$(this).hasClass('active')) {
	      $($(this).find('a').attr('href')).css('display', 'none');
	    }
	    $(this).find('a').click(function() {
	      $($('.tab_menu li.active a').attr('href')).css('display', 'none');
	      $($(this).attr('href')).css('display', 'block');
	      
	      $('.tab_menu li.active').removeClass('active');
	      $(this).parents('li').eq(0).addClass('active');
	      return false;
	    });
	  });
	}
});

/* Fotogaleria */

$(document).ready(function(){
  if ($('.photo_detail').length > 0) {
    
    var ulWidth = 0;
    var ulPosition = 0;
    var ulLeft = 0;
    
    $('.photogallery_list > ul li a').each(function() {
      var img = new Image();
      var href = $(this).attr('href');
      var link = $(this);
      
      
      $(img).attr('src', href).addClass('image_detail');
      $(img).load(function() { 
        $(this).hide();
        link.parents('li').eq(0).append(img);
      });
      
      $(this).click(function() {
        $('.photogallery_list ul li.active').removeClass('active');
        $(this).parents('li').eq(0).addClass('active');
        
        $('.video_photos_area p.photogallery_detail img').remove();
        $(this).parents('li').eq(0).find('img.image_detail').clone().appendTo('.video_photos_area p.photogallery_detail');
        $('.video_photos_area p.photogallery_detail img').show();
        $('.video_photos_area h3').replaceWith('<h3>' + $(this).attr('title') + '</h3>');
        
        return false;
      });
      
      /* Ul width */
      ulWidth += $(this).closest('li').outerWidth();
      
    });
    
    $('.photogallery_list > ul').width(ulWidth + 'px');
    
    $('.photogallery_list div.controls li.prev a').click(function() {
      
      var $ul = $('.photogallery_list ul');
      var liWidth = $('.photogallery_list ul li').outerWidth();
      
      if (ulPosition > 0) {
        ulPosition = ulPosition - 1;
      }
      
      $ul.animate({
        'left': liWidth * ulPosition * -1
      });
      
      /*
      var prev = $('.photogallery_list ul li.active').prev();
      
      if (prev.length > 0) {
        $('.photogallery_list ul li.active').removeClass('active');
        prev.addClass('active');

        $('.video_photos_area p.photogallery_detail img').remove();
        prev.find('img.image_detail').clone().appendTo('.video_photos_area p.photogallery_detail');
        $('.video_photos_area p.photogallery_detail img').show();
        $('.video_photos_area h3').replaceWith('<h3>' + prev.find('a').attr('title') + '</h3>');
      }
      */
      return false;
      
    });
    
    $('.photogallery_list div.controls li.next a').click(function() {
      
      var $ul = $('.photogallery_list > ul');
      var liWidth = $('.photogallery_list > ul li').outerWidth();
      
      if ((ulPosition + 8) < $('.photogallery_list > ul li').length ) {
        ulPosition = ulPosition + 1;
      }
      
      $ul.animate({
        'left': liWidth * ulPosition * -1
      });
      
      /*
      var next = $('.photogallery_list ul li.active').next();
      
      if (next.length > 0) {
        $('.photogallery_list ul li.active').removeClass('active');
        next.addClass('active');

        $('.video_photos_area p.photogallery_detail img').remove();
        next.find('img.image_detail').clone().appendTo('.video_photos_area p.photogallery_detail');
        $('.video_photos_area p.photogallery_detail img').show();
        $('.video_photos_area h3').replaceWith('<h3>' + next.find('a').attr('title') + '</h3>');
      }
      */
      
      return false;
      
    });
    
	}
});

/* Evento on load */

window.onload = function() {
  // Pie de foto en Noticias
  pieDeFotoNoticias();
  
}
