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

  var members = $('#team ul.text li').length;
  var index = Math.floor(Math.random()*members);

  image_element(index)
    .css("opacity", "1");
  if ($.browser.msie) {
    text_element(index)
      .css("display", "inline");
  } else {
    text_element(index)
      .css("opacity", "1");
  }

  function text_element(index) {
    return $($('#team ul.text li')[index]);
  }

  function image_element(index) {
    return $('#team li.m'+index+' a img')
  }

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

  var testimonials = $('#testimonials li');
  var testimonials_index = Math.floor(Math.random()*testimonials.length);

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

  function transition_testimonials(current_index, index) {
    if ($.browser.msie) {
      $(testimonials[current_index])
        .css("display", "none");
      $(testimonials[index])
        .css("display", "inline");
    } else {
      $(testimonials[current_index])
        .stop().animate({opacity:0}, "slow");
      $(testimonials[index])
        .stop().animate({opacity:1}, "slow");
    }
  }

  var timer = $.timer(10000, function(t) {
    var current_index = index;
    index = (index + 1) % members;
    transition(current_index, index);
    transition_testimonials(testimonials_index, (testimonials_index + 1) % testimonials.length);
    testimonials_index = (testimonials_index + 1) % testimonials.length;
  });

  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')
    .click(function(ev) {
      ev.preventDefault();
      timer.stop();
      var new_index =  $(this).parents('li').get(0).className.replace('m','');
      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;
    }
  });
});

