function mycarousel_itemLoadCallback(carousel,state){
  if(carousel.has(carousel.first,carousel.last)){return;}
  $.get('/getimages',{first:carousel.first,last:carousel.last},function(data){mycarousel_itemAddCallback(carousel,carousel.first,carousel.last,data);},'json');
};
function mycarousel_itemAddCallback(carousel,first,last,data){
  carousel.size(parseInt(data.t));
  $.each(data.a,function(i,item){
    carousel.add(first+i,mycarousel_getItemHTML(item.b,item.c));
  });
};
function mycarousel_getItemHTML(url,id){
  return '<img src="/images/destaque/'+url+'" id="'+id+'" width="100" height="75" alt="" style="cursor:pointer;" />';
};
function resize(){
  var height_left=document.getElementById('left').clientHeight;
  var height_right=document.getElementById('right').clientHeight;
  var height_center=document.getElementById('center').clientHeight;
  var total=0;
  if(height_left>=height_right&&height_left>=height_center){
    var total=height_left;
  }
  if(height_right>=height_left&&height_right>=height_center){
    var total=height_right;
  }
  if(height_center>=height_left&&height_center>=height_right){
    var total=height_center;
  }
  var height_new=total;
  document.getElementById('wrapper').style.height=height_new+'px';
}
function createCookie(name,value,days,minutes,seconds){
  if(days){
    var date=new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires='; expires='+date.toGMTString();
  }else if(minutes){
		var date=new Date();
		date.setTime(date.getTime()+(minutes*60*1000));
		var expires='; expires='+date.toGMTString();
	}else if(seconds){
    var date=new Date();
		date.setTime(date.getTime()+(seconds*1000));
		var expires='; expires='+date.toGMTString();
  }else{
    var expires='';
  }
	document.cookie=name+'='+value+expires+'; path=/';
}
function readCookie(name){
	var nameEQ=name+'=';
	var ca=document.cookie.split(';');
	for(var i=0;i<ca.length;i++){
		var c=ca[i];
		while(c.charAt(0)==' ') c=c.substring(1,c.length);
		if(c.indexOf(nameEQ)==0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name){
	createCookie(name,'',-1);
}
function getCookie(name){
	var x=readCookie(name);
	return x;
}
var intervaloCrono=0;
function crono(start){
  end=+new Date();
  var diff=end-start;
  if(diff<1000){
    diff='0.'+diff;
    diff=parseFloat(diff);
  }else{
    diff=diff/1000;
  }
  var tempo=$('#crono_hidden').html();
  tempo=tempo.split(':');
  var hora=parseFloat(tempo[0]);
  var minuto=parseFloat(tempo[1]);
  var segundo=parseFloat(tempo[2])+diff;
  if(segundo>59){
    minuto=minuto+1;
    segundo=0;
  }
  if(minuto>59){
    hora=hora+1;
    minuto=0;
  }
  timeCrono=(hora<10)?'0'+hora:hora;
  timeCrono+=((minuto<10)?':0':':')+minuto;
  timeCrono+=((segundo<10)?':0':':')+segundo;
  $('#crono_hidden').html(timeCrono);
  $('#crono').html(mascara(timeCrono));
  if(intervaloCrono!=0){
    clearInterval(intervaloCrono);
  }
  start=+new Date();
  intervaloCrono=setInterval('crono('+start+')',125);
}
function mascara(valor){
  valor=valor.split(':');
  var segundo=parseInt(valor[2]);
  valor=valor[0]+':'+valor[1]+':'+((segundo<10)?'0':'')+segundo;
  return valor;
}
var intervaloCounter=0;
function getSeconds(){
  var tempo=$('#crono_hidden').html();
  tempo=tempo.split(':');
  var hora=parseFloat(tempo[0]);
  var minuto=parseFloat(tempo[1]);
  var segundo=parseFloat(tempo[2]);
  var total=(hora*3600)+(minuto*60)+segundo;
  return total;
}
function counter(start){
  $('#frangos').html(number_format(Math.round(1478.4030948756976154236428209031*getSeconds()),'','','.'));
  $('#perus').html(number_format(Math.round(21.484525621511922881785895484526*getSeconds()),'','','.'));
  $('#patos').html(number_format(Math.round(71.287988330796549974632166412988*getSeconds()),'','','.'));
  $('#suinos').html(number_format(Math.round(40.455447742262810755961440892948*getSeconds()),'','','.'));
  $('#bovinos').html(number_format(Math.round(9.2382356671740233384069000507357*getSeconds()),'','','.'));
  $('#ovinos').html(number_format(Math.round(16.005644342973110096397767630644*getSeconds()),'','','.'));
  $('#coelhos').html(number_format(Math.round(33.367357940131912734652460679858*getSeconds()),'','','.'));
  if(intervaloCounter!=0){
    clearInterval(intervaloCounter);
  }
  intervaloCounter=setInterval('counter()',125);
}
function number_format(number,decimals,dec_point,thousands_sep){
  var n=number,prec=decimals;
  n=!isFinite(+n)?0:+n;
  prec=!isFinite(+prec)?0:Math.abs(prec);
  var sep=(typeof thousands_sep=='undefined')?',':thousands_sep;
  var dec=(typeof dec_point=='undefined')?'.':dec_point;
  var s=(prec>0)?n.toFixed(prec):Math.round(n).toFixed(prec);//fix for IE parseFloat(0.55).toFixed(0)=0;
  var abs=Math.abs(n).toFixed(prec);
  var _,i;
  if(abs>=1000){
    _=abs.split(/\D/);
    i=_[0].length%3||3;
    _[0]=s.slice(0,i+(n<0))+_[0].slice(i).replace(/(\d{3})/g,sep+'$1');
    s=_.join(dec);
  }else{
    s=s.replace('.',dec);
  }
  return s;
}
$(document).ready(function(){
  $('#mycarousel').html('<ul></ul>');
  $('#mycarousel').jcarousel({
    vertical:true,
    scroll:1,
    itemLoadCallback:mycarousel_itemLoadCallback
  });
  $('.gs').click(function(){
    if($('#q').val().replace(/^\s\s*/,'').replace(/\s\s*$/,'')!=''){
      $('#gs').submit();
    }
    return false;
  });
  //Kill counter
  /*$('.killcounter').mouseover(function(){
    return overlib('<span class="overlib">Número de animais mortos no mundo pela indústria da carne, ovos e laticínios desde que você abriu essa página. Esse contador não inclui o número de peixes e outros animais aquáticos que são mortos a cada momento.</span>',CAPTION,'<span class="overlib"><b>Contador de animais mortos</b></span>',LEFT,ABOVE,WIDTH,300,BGCOLOR,'#668DB8',FGCOLOR,'#F0F1EF');
  });
  var valores=getCookie('counter');
  if(valores!=null){
    valores=valores.split('&');
    var hora=parseFloat(valores[0]);
    var minuto=parseFloat(valores[1]);
    var segundo=parseFloat(valores[2]);
    if(segundo>59){
      minuto=minuto+1;
      segundo=0;
    }
    if(minuto>59){
      hora=hora+1;
      minuto=0;
    }
    timeCrono=(hora<10)?'0'+hora:hora;
    timeCrono+=((minuto<10)?':0':':')+minuto;
    timeCrono+=((segundo<10)?':0':':')+segundo;
    $('#crono_hidden').html(timeCrono);
    start=+new Date();
    crono(start);
    counter();
  }else{
    $('#crono_hidden').html('00:00:00');
    start=+new Date();
    crono(start);
    counter();
  }*/
  $('.td').mouseover(function(){
    $(this).css({'background-color':'#cfeda3'});
  });
  $('.td').mouseout(function(){
    $(this).css({'background-color':'#eaffcf'});
  });
  $('a.image').mouseover(function(){
    $(this).css({'border-color':'#4d9800'});
  });
  $('a.image').mouseout(function(){
    $(this).css({'border-color':'#cfeda3'});
  });
  $('a.video').mouseover(function(){
    $(this).css({'border-color':'#4d9800'});
  });
  $('a.video').mouseout(function(){
    $(this).css({'border-color':'#cfeda3'});
  });
  $('a.image').fancybox({
    'cyclic':true
  });
  $('a.video').click(function(){$.fancybox({
    'hideOnOverlayClick':false,
		'padding':0,
		'autoScale':false,
		'transitionIn':'none',
		'transitionOut':'none',
		'title':this.title,
		'width':425,
		'height':344,
		'href':this.href.replace(new RegExp("watch\\?v=","i"),'v/'),
		'type':'swf'
    });
    return false;
  });
  $(window).unload(function(){
    $('a.image').removeAttr('href');$('a.image').removeAttr('class');
    $('a.video').removeAttr('href');$('a.video').removeAttr('class');
    /*tempo=$('#crono_hidden').html();
    tempo=tempo.replace(/:/g,'&');
    var cookie=tempo;
    createCookie('counter',cookie,null,null,null);*/
  });
  $('.font_size').click(function(){
    var action=$(this).attr('id');
    var fs=$('.text').css('font-size');
    fs=fs.substr(0,fs.indexOf('px'));
    if((fs=='10'&&action=='dec')||(fs=='20'&&action=='inc')){
      return false;
    }else{
      if(action=='dec'){
        fs=parseInt(fs)-1;
        $('.text').css({'font-size':''+fs+'px','line-height':''+(fs+4)+'px'});
      }else{
        fs=parseInt(fs)+1;
        $('.text').css({'font-size':''+fs+'px','line-height':''+(fs+4)+'px'});
      }
    }
	});
});