$(window).load(function(){
//FUNÇÃO QUE PEGA PLAYLIST
function pegaplaylist(playlistid,elemento,categoria){

  var alturafinal;
  var playListURL = 'http://gdata.youtube.com/feeds/api/playlists/'+playlistid+'?v=2&alt=json&callback=?';
  var videoURL= 'http://www.youtube.com/watch?v=';
  $.getJSON(playListURL, function(data) {
    var list_data="";
    $.each(data.feed.entry, function(i, item) {
		var feedTitle = item.title.$t;
        var feedDesc = item.media$group.media$description.$t;
		var desc = feedDesc.split('[english]');
		if(secao=="home-en"){
			feedDesc = desc[1];
		}else{
			feedDesc = desc[0];
		}
        var feedURL = item.link[1].href;
        var fragments = feedURL.split("/");
        var videoID = fragments[fragments.length - 2];
        var url = videoURL + videoID + "&autoplay=1";
        var thumb = "http://img.youtube.com/vi/"+ videoID +"/0.jpg";
		var thumb1 = "http://img.youtube.com/vi/"+ videoID +"/1.jpg";
		var thumb2 = "http://img.youtube.com/vi/"+ videoID +"/2.jpg";
		var thumb3 = "http://img.youtube.com/vi/"+ videoID +"/3.jpg";
        image = '/upload/images/logos_videos/'+videoID+'.jpg';
		
        list_data += '<li><a href="#inline" rel="colorbox" titulo="'+ feedTitle +'" title="'+ feedTitle +'" id="'+videoID+'" alt="'+feedDesc+'" rev="'+categoria+'">';
		//ATIVA IMAGENS QUE ESTIVEREM NA PASTA
	    arrayimg = imagens.split(".jpg,");
	    $.each(arrayimg, function() {
		    if(this==videoID){
		      list_data += '<img class="logo_cliente '+videoID+'" alt="'+ feedTitle+'" src="'+ image +'" width="150px">';
	    	}
	    });
		list_data += '<img alt="'+ feedTitle+'" src="'+ thumb +'" width="150px"><img alt="'+ feedTitle+'" src="'+ thumb1 +'" width="150px"><img alt="'+ feedTitle+'" src="'+ thumb2 +'" width="150px"><img alt="'+ feedTitle+'" src="'+ thumb3 +'" width="150px"></a></li>';
		$("."+elemento).html(list_data);
    });

	 $("a[rel='colorbox']").click(function() {
		 $("#titcategoria").delay(300).html($(this).attr("rev"));
		 $("#descvideo").delay(300).html($(this).attr("alt"));
		 $("#videoyoutube").delay(300).attr("src","http://www.youtube.com/embed/"+$(this).attr("id")+"?hd=1");
	 });
	 
	 $("a[rel='colorbox']").easyTooltip(); 
	 $("a[rel='colorbox']").colorbox({width:"628", height:"500", inline:true, arrowKey:false});
	//CLICA NO MAIS
	alturafinal=((Math.ceil($("."+elemento).find("a").size()/4))*99)-15;
	if(alturafinal<100){
		$("."+elemento).next("a").css("visibility","hidden");
	}
	$("."+elemento).next("a.mais").click(function() {
      if($(this).prev("ul").height()==84){
    	  $(".open").stop(true,true).animate({
            height: "84px"
          }, 500 );
    	  $("."+elemento).stop(true,true).animate({
            height: alturafinal+"px"
          }, 500 );
    	  $(".open").next("a").removeClass("menos");
    	  $(".open").next("a").html(veja_mais);
    	  $(this).addClass("menos").html(fechar);
     	  $(".open").removeClass("open");
    	  $("."+elemento).addClass("open");
	  }else{
	$(this).prev("ul").stop(true,true).animate({
    height: "84px"
    }, 1500 );
	$(this).html(veja_mais);
	$(".open").removeClass("open");
	$(this).removeClass("menos");
	return false;  
	  }
	  return false;
	});
  });
}

$(".lista a").live("mouseenter", function() {      
	$(this).animate({"margin-left": "-150px"}, 1000 ).delay(500);
	$(this).animate({"margin-left": "-300px"}, 1000 ).delay(1000);
	$(this).animate({"margin-left": "-450px"}, 1000 ).delay(1500);
	$(this).animate({"margin-left": "0px"}, 1000 ).delay(2000);
	$(this).animate({"margin-left": "-150px"}, 1000 ).delay(2500);
	$(this).animate({"margin-left": "-300px"}, 1000 ).delay(3000);
	$(this).animate({"margin-left": "-450px"}, 1000 ).delay(3500);
}).live("mouseleave", function() {
    $(this).stop(true,true).animate({"margin-left": 0},1000);
});  
pegaplaylist("B13030E9A51F82C1","cont","Programas de TV");
pegaplaylist("CE7FE6CC202AC711","cont1","Comunicação Corporativa");
pegaplaylist("E5869D4A84CA891B","cont2","Documentários");
pegaplaylist("D147449924368332","cont3","Teleducação");
pegaplaylist("A763F1FAB328A6B5","cont4","Comerciais");
//TWITTER
var timer = setInterval( showDiv, 10000);
numero=0;
function showDiv(){
  numero++;
  if((numero)<$('#twitter p').size()){
  }else{
	numero=0;
  }
  $("#twitter div").stop(true,true).animate({"margin-left":(-300*numero)+'px'}, 500);
}
$("#dir").click(function () {
	clearInterval(timer);
	if((numero+1)==$('#twitter p').size()){
		$("#twitter div").stop(true,true).animate({"margin-left":'0px'}, 500);
		numero=0;
	}else{
		numero++;
		$("#twitter div").stop(true,true).animate({"margin-left":(-300*numero)+'px'}, 500);
	}
	return false;
});
$("#esq").click(function () {
	clearInterval(timer);
	if(numero==0){
		$("#twitter div").animate({"margin-left":(-300*($('#twitter p').size()-1))+'px'}, 500);
		numero=($('#twitter p').size()-1);
	}else{
		numero=numero-1;
		$("#twitter div").stop(true,true).animate({"margin-left":(-300*numero)+'px'}, 500);
	}
	return false; 
});
$(".portfolio").colorbox({iframe:true, width:425, height:344, inline:false, arrowKey:false, href:"http://www.youtube.com/embed/29qPizIfx50?rel=0&wmode=transparent&autoplay=1"});


});
