$(function() {
  $('#team li a img')
    .css("opacity", "0.1");
  if ($.browser.msie) {
    $('.text li')
      .css("display", "none");
  } else {
    $('.text li')
      .css("opacity", "0");
  }

  var images_selectors = ['#team li.m0 a img', '#team li.m1 a img', '#team li.m2 a img', '#team li.m3 a img'];
  var texts_selectors = ['#santiago','#jose','#sebastian','#martin'];
  var index = Math.floor(Math.random()*4);

  $(images_selectors[index])
    .css("opacity", "1");
  if ($.browser.msie) {
    $(texts_selectors[index])
      .css("display", "inline");
  } else {
    $(texts_selectors[index])
      .css("opacity", "1");
  }

  function transition(current, next) {
    if ($.browser.msie) {
      $(texts_selectors[current]).css("display", "none");
    } else {
      $(texts_selectors[current]).stop().animate({opacity:0}, "slow");
    }
    $(images_selectors[current]).stop().animate({opacity:0.1}, "slow");
    if ($.browser.msie) {
      $(texts_selectors[next]).css("display", "inline");
    } else {
      $(texts_selectors[next]).stop().animate({opacity:1}, "slow");
    }
    $(images_selectors[next]).stop().animate({opacity:1}, "slow");
  }

  var timer = $.timer(10000, function(t) {
     var current_index = index;
     index = (index + 1) % 4;
     transition(current_index, index);
  });

  if(!Array.indexOf){
    Array.prototype.indexOf = function(obj){
      for(var i=0; i<this.length; i++){
        if(this[i]==obj){
          return i;
        }
      }
     return -1;
    }
  }  

  $('#team li a img')
    .click(function(ev) {
      ev.preventDefault();
      timer.stop();
      var new_index = images_selectors.indexOf('#team li.' + $(this).parents('li').get(0).className + ' a img');
      transition(index, new_index);
      index = new_index;
      timer.reset(10000);
      return false;
    });

    $('#contact').click(function() {
	$.scrollTo('#footer', 1500);
	$('#name').focus();
    });
    
    $('a[rel=external]').attr('target','_blank');

    $('ul.thumbs a').fancybox({
	'speedIn': 250,
	'speedOut': 250,
	'overlayShow': false,
	'transitionIn':'elastic',
	'transitionOut':'elastic',
	'hideOnContentClick':true
    });

  var last_project_active = 1;
  $.timer(10000, function(t) {
    if ($('aside#latest .body').size() > last_project_active) {
        last_project_active++;
        $('aside#latest').scrollTo('+=290px', 1000);
    } else {
        $('aside#latest').scrollTo('0px', 1000);
        last_project_active = 1;
    }
  });
});
