(function($) {
	var	linkHref = '';
	
	$.fn.macYouTube = function(options) {
					
			var vOptions = $.extend({}, $.fn.macYouTube.defaults, options);
			var first = true;
			$("#YoutubeThumbs").html('');
			$("#Youtubelarge").html('');
			$(".Youtubeinfo").html('<li class="title"></li><li class="date"></li><li class="views"></li><li class="stars"></li><li class="desc"></li>');
			
			// oculta a div
			$(this).hide();

			// Titulo
			if (vOptions.titulo) {
				$(this).append('<h2>'+ vOptions.texto +'</h2>');
			}
		
			// Listagem dos videos
			$(this).append('<div id="youtube-videos"></div>');

			//Oculta a div dos videos
			$("div#youtube-videos").hide();

			//Recupera o texto digitado no loader
			var pl = $(vOptions.loaderText);
			$(this).append(pl);

			var linkDirecto = vOptions.linkDirecto;
			if(linkDirecto==null) linkDirecto="";

			//Mostra o loading enquanto iremos puxar os videos
			$(this).show();			

			//Adiciona o footer(rodape) com a assinatura do video
			/*$(this).append('<div id="footer">Veja nosso canal: <a href="http://www.youtube.com/' + vOptions.pUsuario + '" target="_blank">http://www.youtube.com.br/' + vOptions.pUsuario + '</a></div>');*/
          	
          	
          	;
			var montaLinha = '';
			var montaEstrela = '';
			$.getJSON("http://gdata.youtube.com/feeds/base/users/" + vOptions.pUsuario + "/uploads?alt=json-in-script&callback=?", function(data) {
				if(data)
				{
					$(pl).remove();

					$.each(data.feed.entry, function(i,item){
						
						//Recuperando o ID do video
						linkHref = linkHref + item.link[0].href;
						vPosInicioHref = linkHref.indexOf("v=", 1);
						vPosFinalHref = linkHref.indexOf("&", 1);
						pIdYoutube = linkHref.substring(vPosInicioHref+2,vPosFinalHref);
						
						//Procuramos aonde esta as views e puxamos o valor
						var vProcuraContent = jQuery(item.content.$t).find("table > tbody > tr:first > td:last");	
						vViews = $(vProcuraContent).find("div:eq(1)").text();
						
						var description = jQuery(item.content.$t).find("table > tbody > tr:eq(0) > td:eq(1) > div:eq(1)").text();
						//Avaliações
						vAvaliacoes = $(vProcuraContent).find("div:eq(3)").text();
						
						var author = item['content']['$t'];
						//Recuperando o tempo do video
						var vProcuraTime = jQuery(item.content.$t).find("table > tbody > tr:last > td:first");	
						vTime = $(vProcuraTime).find("span:eq(1)").text();
						

						//Inicializando a data para colocar no formato brasileiro(dd/mm/yyyy)
						//var vData = new Date(item.published.$t);

						//Coloca no formato brasileiro
						//montaData = vData.getDate();
						
						var myDate = item.published.$t.split('-');
						var myDay = myDate[2].substring(0,2);
						montaData = myDay + "/" + myDate[1] + "/" + myDate[0];
						
						if (first == true) {
							var vId = pIdYoutube;
							var vTitle = item.title.$t;
							var vDate = montaData;
							var vStars = "<span class='estrelas'>";
							$(vProcuraContent).find("div:eq(2) > img").each(function(){
								vStars = vStars + '<img src="' + $(this).attr("src") + '" alt="" title="" />';
							});
							vStars = vStars + "</span><br/>";
							var vViewed =  vViews.replace('Views:','') + '<span> '+traducir(tradReproduc)+'</span>';
							var vDesc = description;
							
							first = false;
						}
						
						//Monta a div onde ficara todos os videos
						if ((vOptions.id == 0) && (vId == pIdYoutube)) {
							montaLinha = montaLinha + "<div class='youtube-videos active'>";
						} else if (vOptions.id == pIdYoutube) {
							montaLinha = montaLinha + "<div class='youtube-videos active'>";
						} else {
							montaLinha = montaLinha + "<div class='youtube-videos'>";
						}
						//montaLinha = montaLinha + "<span class='imagem'><a href='" + item.link[0].href + "' target='_blank'><img class='rounded5' src='http://i.ytimg.com/vi/" + pIdYoutube + "/default.jpg' /></a></span>";
						montaLinha = montaLinha + "<span class='imagem'><a rel='"+pIdYoutube+"' href='"+linkDirecto+"#video'><img class='rounded5' src='http://i.ytimg.com/vi/" + pIdYoutube + "/default.jpg' /></a></span>";
						montaLinha = montaLinha + "<span class='titulo'>" + item.title.$t + "</span>";
						
						
						var estrellas = "<span class='estrelas'>";
						//Procurando as estrelas e adicionando na nossa div
						$(vProcuraContent).find("div:eq(2) > img").each(function(){
							estrellas = estrellas + '<img src="' + $(this).attr("src") + '" alt="" title="" />';
						
						});
						estrellas = estrellas + "</span></div>";
						//montaLinha = montaLinha + "<span class='idyoutube'>ID do video: " + pIdYoutube + "</span><br/>";
						//montaLinha = montaLinha + "<span class='link'>Url: " + item.link[0].href + "</span><br/>";
						//montaLinha = montaLinha + "<span class='datapuclicacao'>" + montaData + "</span> - ";
						
						var extraInfo = "<span class='tempo'>" + vTime + "</span>";
						extraInfo =  extraInfo + "<span class='views'><strong>" + vViews.replace('Views:','') +"</strong> " +traducir(tradReprod)+"</span>";
						
						//montaLinha = montaLinha + "</div>";	

					
						/*if ((e == 0) && (setid == set.id)) {
							var setsli = $("<li/>").attr("class","active").append(setslink).append("<h4>"+set.title._content+
								"</h4><p class='photos'><strong>" + set.photos + "</strong> fotos</p>");
						}*/
						$("#RS2_YouTube div[id*=youtube-videos]").append(montaLinha + estrellas);
						$(".divColDer_02 div[id*=youtube-videos]").append(montaLinha + estrellas);
						$(".divListadoM15 div[id*=youtube-videos]").append(montaLinha + extraInfo + estrellas);
						
						
						if ((vOptions.id == 0) && (vId == pIdYoutube)) {
							var selectedVideo ='<object id="Youtube"><param name="movie" value="http://www.youtube.com/v/'+ pIdYoutube + '?version=3"><param name="allowFullScreen" value="true"><param name="allowScriptAccess" value="always"><embed src="http://www.youtube.com/v/'+ pIdYoutube + '?version=3" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="682" height="415"></object>';
						} else if (vOptions.id == pIdYoutube) {
							var selectedVideo ='<object id="Youtube"><param name="movie" value="http://www.youtube.com/v/'+ pIdYoutube + '?version=3"><param name="allowFullScreen" value="true"><param name="allowScriptAccess" value="always"><embed src="http://www.youtube.com/v/'+ pIdYoutube + '?version=3" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="682" height="415"></object>';
							vId = vOptions.id;
							var vId = pIdYoutube;
							var vTitle = item.title.$t;
							var vDate = montaData;
							var vStars = "<span class='estrelas'>";
							$(vProcuraContent).find("div:eq(2) > img").each(function(){
								vStars = vStars + '<img src="' + $(this).attr("src") + '" alt="" title="" />';
							});
							vStars = vStars + "</span><br/>";
							var vViewed =  vViews.replace('Views:','') + '<span> '+traducir(tradReproduc)+'</span>';
							var vDesc = description;
						} /*else {	        	
							var setsli = $("<li/>").append(setslink).append("<h4>"+set.title._content+
								"</h4><p class='photos'><strong>" + set.photos + "</strong> fotos</p>");
						}*/
						
						
						$("#Youtubelarge").html(selectedVideo);
						$(".divListadoM15 h2").html(vTitle);
						$(".divListadoM15 .Youtubeinfo .title").html(vTitle);
						$(".divListadoM15 .Youtubeinfo .date").html(vDate);
						$(".divListadoM15 .Youtubeinfo .stars").html(vStars);
						$(".divListadoM15 .Youtubeinfo .views").html(vViewed);
						$(".divListadoM15 .Youtubeinfo .desc").html(vDesc);
						montaLinha = '';	
						linkHref = '';					

						//Limitando para quantos itens deseja colocar			    
						if(vOptions.itens){
							if ( i == vOptions.numItens -1 ) return false;
						}
					  });
					
					//Quando passar o mouse em cima da DIV ele muda de cor.
					$("div#youtube-videos div").hover(function () {
						  $(this).addClass("hoverYoutube");
						},
						function () {
						  $(this).removeClass("hoverYoutube");
						}
					  );	
				}
				$(".youtube-videos a").click(function(){
					$("#YoutubeThumbs").macYouTube({
						pUsuario: "UIC", 
						titulo: false, 
						loaderText: "<div class='loading'><img src='/includes/images/loadingtrans.gif'></div>",
						slideIn: true,
						itens: true,
						numItens: 20,
						id: $(this).attr("rel")
					});
				});
			});

			// Mostrar os videos
			if (vOptions.slideIn) {
				$("div#youtube-videos").slideDown(1000);
			}		

			 // plugin defaults
			$.fn.macYouTube.defaults = {
				pUsuario: null,
				titulo: true,
				texto: "Meus Videos",
				loaderText: "",
				slideIn: true,
				itens: false,
				numItens: 0
			};
			
			
			
			

	}

})(jQuery);

