function serverTime(){
  var time = null;
  $.ajax({url: 'counter.php',
      async: false, dataType: 'text',
      success: function(text) {
          time = new Date(text);
      }, error: function(http, message, exc) {
          time = new Date();
  }});
  return time;
}

function retornarTempo(p){
  var horas = (p[3] * 24) + p[4];
  var minutos = (horas * 60) + p[5];
  var segundos = (minutos * 60) + p[6];
  var indice = 2.4595;
  var nascidos = Math.round((segundos * indice) + 6974036375);
  nascidos = $().number_format(nascidos, {
    numberOfDecimals:0,
    decimalSeparator:',',
    thousandSeparator:'.',
    symbol:''
  });
  $('#counterOutput').html(nascidos);
}

$(function(){
 
  $('#counterFoo').hide();
  $('#counterFoo').countdown({
    since: new Date(2011, 7-1, 1),
    serverSync: serverTime,
    onTick: retornarTempo
  });
  $('#counterFoo').empty();
 
  /************
   * stories slider
   ************/
  var $photosSliderContainer = $("#photosContainer");
  var $histories = $photosSliderContainer.find('.history');
 
 
  var $sliderBtLeft = $("#sliderBtLeft");
  var $sliderBtRight = $("#sliderBtRight");
 
  var isSliderAnimating = false;
 
  var sliderCurrentIndex = 1;
  function sliderSlidePictures(forward){
    if(isSliderAnimating){
      return false;
    }
    sliderCurrentIndex = forward ? sliderCurrentIndex + 1 : sliderCurrentIndex - 1;
    if(sliderCurrentIndex === ($histories.length - 1)){
      $photosSliderContainer.css("left", "148px");
      sliderCurrentIndex = 1;
    }
    if(sliderCurrentIndex === 0){
      $photosSliderContainer.css("left", ((($histories.length - 1)*-671)+148)+"px");
      sliderCurrentIndex = $histories.length - 2;
    }
    isSliderAnimating = true;
    $photosSliderContainer.stop().animate({"left":((sliderCurrentIndex*-671)+148)+"px"}, 300, function(){
      isSliderAnimating = false;
    });
    sliderHighlightPicture();
  }
  function sliderHighlightPicture(){
    $histories.fadeTo(0, 0.5);
    $photosSliderContainer.find('.history:eq('+sliderCurrentIndex+')').fadeTo(500, 1);
  }
 
  $sliderBtRight.click(function(){
    sliderSlidePictures(true);
  });
  $sliderBtLeft.click(function(){
    sliderSlidePictures(false);
  });
 
  function sliderPicturesSetup(){
    var $firstItem = $photosSliderContainer.find('.history:first').clone();
    var $lastItem = $photosSliderContainer.find('.history:last').clone();
    $firstItem.appendTo($photosSliderContainer);
    $lastItem.prependTo($photosSliderContainer);
   
    $photosSliderContainer = $("#photosContainer");
    $histories = $photosSliderContainer.find('.history');
  }
  sliderPicturesSetup();
  sliderHighlightPicture();
   
  /************
   * organizations slider
   ************/
  var $photosOrgContainer = $("#orgContainer");
  var $orgItems = $photosOrgContainer.find('.item');

  var $orgBtLeft = $("#orgsBtLeft");
  var $orgBtRight = $("#orgsBtRight");

  var isOrgAnimating = false;

  var orgCurrentIndex = 0;
  function orgSlidePictures(forward){
    if(isOrgAnimating){
      return false;
    }
    orgCurrentIndex = forward ? orgCurrentIndex + 1 : orgCurrentIndex - 1;
    if(orgCurrentIndex > ($orgItems.length - 4)){
      orgCurrentIndex = ($orgItems.length - 4);
      return false;
    }
    if(orgCurrentIndex < 0){
      orgCurrentIndex = 0;
      return false;
    }
    isOrgAnimating = true;
    $photosOrgContainer.stop().animate({"left":((orgCurrentIndex*-212)+15)+"px"}, 300, function(){
      isOrgAnimating = false;
    });

  }

  $orgBtRight.click(function(){
    orgSlidePictures(true);
  });
  $orgBtLeft.click(function(){
    orgSlidePictures(false);
  });

  var maxItemHeight = 0;
  var $featuredItemsList = $("#featuredItems");
  var $featuredItems = $featuredItemsList.find('.item');
  $featuredItems.each(function(){
    var $this = $(this);
    if($this.height() > maxItemHeight){
      maxItemHeight = $this.height();
    }
  });
  $featuredItems.css('height', maxItemHeight+'px');

});
