(function($) {


        $.videosMaisRecentes = function(element, start, offset, category, idVideo, playerVideoElement) {
		
		if ( category == "" ) {
			if ( playerVideoElement ) {
				$.maisRecentes( element, start, offset, idVideo, "/videos.videosMaisRecentes.html", "div#dataVideos", ".video", playerVideoElement);
			} else {
				$.maisRecentes( element, start, offset, idVideo, "/videos.videosMaisRecentes.html", "div#dataVideos", ".video", "");
			}
		} else {
			if ( playerVideoElement ) {
				$.maisRecentes( element, start, offset, idVideo, "/categorias/" + category + "/videos/index.videosMaisRecentes.html",
 "div#dataVideos", ".video", playerVideoElement);
			} else {
				$.maisRecentes( element, start, offset, idVideo, "/categorias/" + category + "/videos/index.videosMaisRecentes.html",
 "div#dataVideos", ".video", "");
			}
		}

		// Converte de milisegundos e popula a duração do video na div
		$(element).find("div.video").each( 
			function(n) {

				////alert( "duracao: " + $(this).find("div.infoVideo span.duracaoVideo").html() );
				////alert( "duracao convertido: " + $.convertMsDuracao( $(this).find("div.infoVideo span.duracaoVideo").html()) );
			
				$(this).find("h1#duracao").html($.convertMsDuracao( $(this).find("div.infoVideo span.duracaoVideo").html()));
			}
		);
	};

        $.contosMaisRecentes = function(element, start, offset, category, idconto) {
		
		if ( category == "" ) {
			$.maisRecentes( element, start, offset, idVideo, "/contos.contosMaisRecentes.html", "");
		} else {
			$.maisRecentes( element, start, offset, idVideo, "/categorias/" + category + "/contos/index.contosMaisRecentes.html", "");
		}
	};

        $.fotosMaisRecentes = function(element, start, offset, category, idGaleria) {
		
		if ( category == "" ) {
			$.maisRecentes( element, start, offset, idGaleria, "/fotos.fotosMaisRecentes.html", "div#dataFotos", ".boxDestaque", "");
		} else {
			$.maisRecentes( element, start, offset, idGaleria, "/categorias/" + category + "/fotos/index.fotosMaisRecentes.html", "div#dataFotos",".boxDestaque", "");
		}
	};


	$.maisRecentes = function(element, start, offset, idCorrente, fragmentUrl, originElement, elementClass, playerVideoElement) {
		        //alert("start: " + start + "\n" + "offset: " + offset + "\n" + "idVideo: " + idCorrente + "\n" + "fragmentUrl: " + fragmentUrl );	
		

			$.ajaxSetup({
  				async: false
			});


			$.get(fragmentUrl, 
				function(data) {

					//alert( data );
					//alert( $(originElement).html() );
					$(originElement).append(data);
					//alert( $(originElement).html() );

					if( playerVideoElement ) {
						divVideoCorrente = $(originElement).find("div:nth-child(1)").clone()
						//alert("video player: " + $(divVideoCorrente).html() );
						$.fillVideoPlayer( playerVideoElement, divVideoCorrente );
					}

					if( idCorrente != "" ) {
						$(originElement).find("div#" + idCorrente).remove()
					} else if ( elementClass == ".video" && fragmentUrl.indexOf("categorias") != -1 ) {
						$(originElement).find("div:nth-child(1)").remove()
					}					

					element.append($(originElement).find(elementClass).slice(start,offset));	
					element.css("visibility", "visible");
					//alert( $(originElement).find(elementClass).html() );

					//alert( $(originElement).html() );
					//alert( element.html() );

					$(originElement).html("");

				} 
			);
	};

        $.convertMsDuracao = function( duracao_parm ) 
	{
	        // duracao
                var duracao_ms = parseInt(duracao_parm);
	        	var duracao = "";
                var hora = parseInt(duracao_ms / 3600000);
                
                if( hora != 0 ) 
            	{
					if( hora < 10 )
					{
                        duracao = duracao + "0" + hora + ":";
					}
					else
					{
                        duracao = duracao + hora + ":";
					}
					
					duracao_ms = duracao_ms % 3600000;
					
                } 
				else 
				{
                    duracao = duracao + "00:";
                }

                var minuto = parseInt(duracao_ms / 60000);
                if( minuto != 0 ) 
                {
					if( minuto < 10 ) 
					{
                        duracao = duracao + "0" + minuto + ":";
					} 
					else
					{
                        duracao = duracao + minuto + ":";
					}
			
					duracao_ms = duracao_ms % 60000;

                } 
				else 
				{
		            duracao = duracao + "00:";
		        }
		
                var segundos = Math.round( duracao_ms / 1000) ;
                if( segundos != 0 ) 
				{
					if( segundos < 10 )
					{
                        duracao = duracao + "0" + segundos;
					}
					else 
					{
                        duracao = duracao + segundos;
					}
		
		        } 
                else 
                {
                    duracao = duracao + "00";
		        }
		
				return duracao;
	};

	$.fillVideoPlayer = function( playerElement, data ) {
		var titulo = ""
		var descricao = ""
		var duracao = ""
		var link = ""
		var categoria = ""

		infoVideo = $(data).find("div.infoVideo");
		titulo = $(infoVideo).find("span.tituloVideo").text();
		descricao = $(infoVideo).find("span.descricaoVideo").text();
		link = $(infoVideo).find("span.linkVideo").text();
		categoria = $(infoVideo).find("span.categoriaVideo").text();
		duracao = $(infoVideo).find("span.duracaoVideo").text();
		dataCriacaoVideo = $(infoVideo).find("span.dataCriacaoVideo").text();
		idVideo = $(infoVideo).find("span.idVideo").text();
		rating = $(infoVideo).find("span.ratingVideo").text();
		

		/*
		//alert(    "titulo: " + titulo 
			+ "\n descricao: " 
		        + descricao 
			+ "\n link: "
			+ link + "\n categoria: " 
			+ categoria 
			+ "\n duracao: " + duracao );
		*/

		$("div#playerVideoCategoria div#descricaoVideoCategoria ul li.titulo").html("<h2>"+titulo);
		$("div#playerVideoCategoria div#descricaoVideoCategoria ul li.descricao").html(descricao);
		$("div#playerVideoCategoria div#descricaoVideoCategoria ul li#linkInput input").attr("value", link);
		$("div#playerVideoCategoria div#descricaoVideoCategoria ul li.data").html(dataCriacaoVideo);
		$("div#playerVideoCategoria div#descricaoVideoCategoria ul li.rating img").attr("src", "/images/videos/rating" + rating + ".gif");
		
		var src = "http://playervideo.globo.com/webmedia/player/embed/GMCAbrePlayer?banda=L&midiaId=" + idVideo;
		$(playerElement).find("iframe").attr("src", src);
		
		/*
		//encontra o object flashplayer a ser preenchido
		$(playerElement).find("object#videoPlayerGlobo").find("param").each( 
			function( n ) {
				if ( $(this).attr("value") == "midiaId=" ) {
					$(this).attr("value", "midiaId=" + idVideo );				
				}
			}
		);
		*/

		//$(playerElement).find("object#videoPlayerGlobo").find("embed").attr("flashvars", "midiaId=" + idVideo );

		//$(playerElement).find("object#videoPlayerGlobo").css("display", "block");
		
		//$("div#playerVideoCategoria div#descricaoVideoCategoria ul li#embedInput input").attr("value", '<object id="videoPlayerGlobo" width="480" height="392">' + $("object#videoPlayerGlobo").html() + '</object>');
	};

})(jQuery);
