/*************************************************************
 * Lib core
 * funções core TV Intertribal
 * data: 15/01/2010
 * @author roberto-trevisan
 * HOTMEDIA 
 * ***********************************************************
 */
 
var TVapi = {
	Version: '1.0.0',
	preCampanha: false,
	paramCampanha: "&campanha=true",
	getUltimosVideosurl: "xml/getUltVid/",
	getXMLurl : "xml/getXML/?id=",
	getVideoDetalheurl: "xml/getVideoDetalhe/?id=",
	getPlayerLiburl: "http://www.hotmedia.com.br/eventos/flash2/video/lib/?",
	setupTV: function(){
		$("#areaAbas li").mousedown(function(i){TVapi.getPlaylist(this.id,16,0);})
		this.getUltimosVideos();
		this.getPlaylist("c505",16,0)
	},
	changeFundoAba: function(canalId,img){
		$("#areaAbas li").css({'backgroundImage' : 'url(img/aba_off.gif)'});
		$("#areaAbas li").css({'color' : '#B8AB07'});
		$('#c'+canalId).css({'backgroundImage' : 'url('+img+')'});
		$('#c'+canalId).css({'color' : '#FF8C43'});
	},
	changeLateral: function(ide){
		$("#areaLateral").css("visibility", "hidden");
		$("#areaLateralAjuda").css("visibility", "hidden");
		$("#areaLateralApresentacao").css("visibility", "hidden");
		$("#areaLateralVideoDetalhe").css("visibility", "hidden");
		$("#"+ide).css("visibility", "visible");
	},
	loadScript: function (scriptUrl){
		$.ajax({ type: "GET", 
			url: scriptUrl, 
			data: "" , 
			dataType: "script", 
			success: function(data){ 
				 TVapi.playFLV("areaPlayer");
 				 window.scroll(0,0);
			} 
		}); 
	},
	setEventLateral: function(){
		$("#areaLateral div .video_img").mouseover(function(i){TVapi.showHplay(this.id,true)})
		$("#areaLateral div .video_img").mousedown(function(i){TVapi.prePlayFLV(this.id);})
		$("#areaLateral div .video_img").mouseout(function(i){TVapi.showHplay(this.id,false)})
	},
	setEventInferior: function(){
		$("#areaVideoList div .video_img").mouseover(function(i){TVapi.showHplay(this.id,true)})
		$("#areaVideoList div .video_img").mousedown(function(i){TVapi.prePlayFLV(this.id);})
		$("#areaVideoList div .video_img").mouseout(function(i){TVapi.showHplay(this.id,false)})
	},
	showHplay: function(ide,m){
		if(m)
			$("#"+ide+" img").css("visibility", "visible");
		else
			$("#"+ide+" img").css("visibility", "hidden");
	},
	showUltimosVideos: function(){
		this.changeLateral("areaLateral");
	},
	showApresentacao: function(){
		this.changeLateral("areaLateralApresentacao");
	},
	showAjuda: function(){
		this.changeLateral("areaLateralAjuda");
	},
	getLinksURL: function (canal,idVideo){
		 var saida = new StringBuffer();
		 saida.add("<div id=\"watch-url-div\">");
		 saida.add("<form id=\"urlForm\" name=\"urlForm\" action=\"\"><label for=\"watch-url-field\" class=\"watch_title\">URL</label>");
		 saida.add("<input type=\"text\" readonly=\"\" onclick=\"javascript:document.urlForm.video_link.focus();document.urlForm.video_link.select();\" value=\"http://www.tvintertribal.com.br/"+canal+"-"+idVideo+"\" class=\"video-embed\" id=\"watch-url-field\" name=\"video_link\" title=\"use: ctrl-C para copiar\" />");
		 saida.add("</form></div>");
		 return saida.toString();
	},
	getLinksEmbeded: function (urlstream){
		var saida = new StringBuffer();
		saida.add("<div id=\"watch-embed-div\">");
		saida.add("<form id=\"embedForm\" name=\"embedForm\" action=\"\"><label for=\"embed_code\" class=\"watch_title\">Incorporar</label>");
		saida.add("<input type=\"text\" readonly=\"\" onclick=\"javascript:document.embedForm.embed_code.focus();document.embedForm.embed_code.select();\" value='<object width=\"640\" height=\"360\"><param value=\"http://www.hotmedia.com.br/eventos/flash2/video/player18.swf\" name=\"movie\" /><param value=\"uri="+urlstream+"&img=http://www.hotmedia.com.br/eventos/flash2/video/img/coringa_zion_640x360_2.jpg&type=1&tmb=http://zion.hotmedia.com.br:8080/zion/base/sys/img/video/39/3805.jpg&xmlConfig=http://www.hotmedia.com.br/eventos/flash2/playerConfig/getVideoRelac/\" name=\"FlashVars\" /><param name=\"allowFullScreen\" value=\"true\"/><embed width=\"640\" height=\"360\" allowFullScreen=\"true\" flashvars=\"uri="+urlstream+"&img=http://www.hotmedia.com.br/eventos/flash2/video/img/coringa_zion_640x360_2.jpg&type=1&tmb=http://zion.hotmedia.com.br:8080/zion/base/sys/img/video/39/3805.jpg&xmlConfig=http://www.hotmedia.com.br/eventos/flash2/playerConfig/\" type=\"application/x-shockwave-flash\" quality=\"high\" src=\"http://www.hotmedia.com.br/eventos/flash2/video/player18.swf\"/></object>' name=\"embed_code\" id=\"embed_code\" class=\"video-embed\" title=\"use: ctrl-C para copiar\" />");
		saida.add("</form></div>");
		return saida.toString();
	},
	prePlayFLV: function(str){
		var parametro = "";
		var pos = str.indexOf("-")
		if (pos == -1) {
			var canalId = str.substr(1);;
			var videoId = "";
			parametro = "id="+canalId;
		} else {
			var canalId = str.substr(1,pos-1);
			var videoId = str.substr(pos+1);
			parametro = "id="+canalId+"&cod="+videoId;
			if(this.preCampanha) parametro = parametro + this.paramCampanha;
			this.getVideoDetalhe(canalId, videoId);
		}
		this.loadScript(this.getPlayerLiburl+parametro);
	},
	playFLV: function (divName){
		flashembed(divName, 
			{
			   src: 'player18.swf',
			   width: '640',  
			   height: '360',
			   bgcolor: '#000000'
			},
			{config: fpConf}
		);
	},
	getUltimosVideos: function(){
		$.ajax({
        	type: "GET",
            url: this.getUltimosVideosurl,
            dataType: "xml",
            beforeSend: function(){$("#areaLoading").show()},
            complete: function(){$("#areaLoading").hide()},
            success: function(data){
				TVapi.geraUltimosVideos(data);
			}
		});
	},
	geraUltimosVideos: function(data){
		var htmlPlaylist = new StringBuffer();
			htmlPlaylist.add("<div class=\"titcanal titulo\">Últimos Vídeos</div>");
			htmlPlaylist.add("<ul>");
			$(data).find('item').each(function(){
				//var title = reduzTexto($(this).find('title').text(), 50);
				var title = $(this).find('title').text();
				//var description = reduzTexto($(this).find('description').text(), 58);
				var channelId = $(this).find('channel_cod').text();
				var videoId = $(this).find('cod').text();
				var imgUrl = $(this).find('link').text();
				var data = dataFormatoBr($(this).find('pubDate').text());
				var time = $(this).find('time').text();
				time = time.substring(time.indexOf(':')+1);
				var htmlItem = new StringBuffer();
				htmlItem.add("<li>\n");
				htmlItem.add(" <div>\n");
				htmlItem.add("  <div id=\"u"+channelId+"-"+videoId+"\" class=\"video_img\" style=\"background-image: url("+imgUrl+");\"><img class=\"imgthumb\" src=\"img/h_play2.png\" width=\"54\" height=\"54\" alt=\"\" /></div>\n");
				htmlItem.add("  <div class=\"video_desc\">\n");
				htmlItem.add("   <div class=\"video_tit\">"+title+"</div>\n");
				htmlItem.add("   <div class=\"video_time\">"+time+"</div>\n");
				htmlItem.add("   <div class=\"video_bt\">\n");
				htmlItem.add("    <div class=\"video_dt\">"+data+"</div>\n");
				//htmlItem.add("    <div class=\"video_bot\"><img class=\"imgbot\" src=\"img/bt_playB1.gif\" /><img class=\"imgbot\" src=\"img/bt_sinopseB1.gif\" /></div>\n");
				htmlItem.add("    <div class=\"video_bot\"><img onclick=\"TVapi.getVideoDetalhe("+channelId+","+videoId+")\" class=\"imgbot\" src=\"img/bt_sinopseB1.gif\" /></div>\n");
				htmlItem.add("   </div>\n");
				htmlItem.add("  </div>\n");
				htmlItem.add(" </div>\n");
				htmlItem.add("</li>\n");
				htmlPlaylist.add(htmlItem.toString());
			});
			htmlPlaylist.add("</ul>");
			$("#areaLateral").html(htmlPlaylist.toString());
			this.setEventLateral();
	},
	getPlaylist: function(id, interval, start){
		id = id.substr(1);
		this.changeFundoAba(id,"img/aba_on.gif");
		$("#areaVideoList").html(" ");
		$.ajax({
        	type: "GET",
            url: this.getXMLurl+id+"&s="+start+"&i="+interval,
            dataType: "xml",
            beforeSend: function(){$("#areaLoading2").show()},
            complete: function(){$("#areaLoading2").hide()},
            success: function(data){
				TVapi.geraPlaylist(data, interval, start, 'getPlaylist');
			}
		});
	},
	geraPlaylist: function(data, interval, start, srcFunc){
		var i = 1;
		var htmlPlaylist = new StringBuffer();
		htmlPlaylist.add("<ul>");
		channelId = $(data).find('idcanal').text();
		if(channelId == "") channelId = 0;
		qtd_itens = $(data).find('qtdItens').text();
		itens_start = $(data).find('listItensStart').text();
		itens_limit = $(data).find('listItensLimit').text();
//		if($(data).find('item').length) > 0){
			$(data).find('item').each(function(){
				if($(this).attr('tp') !="institucional"){
					//var title = reduzTexto($(this).find('title').text(), 20);
					var title = $(this).find('title').text();
					var cutdescription = reduzTexto($(this).find('description').text(), 110);
					var description = $(this).find('description').text();
					var videoId = $(this).find('cod').text();
					var imgUrl = $(this).find('link').text();
					var data = dataFormatoBr($(this).find('pubDate').text());
					var time = $(this).find('time').text();
					time = time.substring(time.indexOf(':')+1);
					if(channelId == 0) channelId = $(this).find('channel_cod').text();
					var htmlItem = new StringBuffer();
					if (i % 2 == 0) n = 2; else n = 1;
					htmlItem.add("<li class=\"ld0"+n+"\">");
					htmlItem.add("<div>");
					htmlItem.add("<div style=\"background-image: url("+imgUrl+");\" class=\"video_img\" id=\"v"+channelId+"-"+videoId+"\"><img height=\"54\" width=\"54\" alt=\"\" src=\"img/h_play2.png\" class=\"imgthumb\"/></div>");
					htmlItem.add("<div class=\"video_desc\">");
					htmlItem.add("<div class=\"video_tit\">"+title+"</div>");
					htmlItem.add("<div class=\"video_detalhe\" title=\""+description+"\">"+cutdescription+"</div>");
					htmlItem.add("<div class=\"video_time\">"+time+"</div>");
					htmlItem.add("<div class=\"video_bt\">");
					htmlItem.add("<div class=\"video_dt\">"+data+"</div>");
					//htmlItem.add("<div class=\"video_bot\"><img src=\"img/bt_sinopseA1.gif\" class=\"imgbot\"/></div>");
					htmlItem.add("    <div class=\"video_bot\"><img onclick=\"TVapi.getVideoDetalhe("+channelId+","+videoId+")\" class=\"imgbot\" src=\"img/bt_sinopseA1.gif\" /></div>\n");		
					htmlItem.add("</div>");
					htmlItem.add("</div>");
					htmlItem.add("</div>");
					htmlItem.add("</li>");
					htmlPlaylist.add(htmlItem.toString());
					i++;
				}
			});
			htmlPlaylist.add("</ul>");
			/*****************************
			 Paginador JS
			*/
			var paginador = new StringBuffer();
			paginador.add("<div id=\"paginador\">");
		    var total = Math.ceil(qtd_itens/itens_limit);
			var	inimostra = (Math.ceil(itens_start/itens_limit));
		    var maxmostra = 17;
		 	if (total > maxmostra+1) {
			    maxmostra = maxmostra+(inimostra*1);
			    if (maxmostra > total)
			 	   maxmostra = total;
		    } else
		 	    maxmostra = total;
			if(inimostra != 0) {
				paginador.add("<span onClick=\"TVapi."+srcFunc+"('c"+channelId+"', 16, 0);\")\" title=\"primeira\">&lt;&lt;</span>&nbsp;&nbsp;&nbsp;");
				paginador.add("<span onClick=\"TVapi."+srcFunc+"('c"+channelId+"', 16, "+((inimostra-1)*5)+");\")\" title=\"anterior\">&lt;</span>&nbsp;&nbsp;");
			}	
			for (i = inimostra+1; i<=maxmostra;i++) {
				if(i == inimostra+1){
					var classSelected = "class=\"activePage\"";
				} else
					var classSelected = "";
				paginador.add(" <span "+classSelected+" onClick=\"TVapi."+srcFunc+"('c"+channelId+"', 16, "+((i-1) * 16)+");\")\" title=\"ir para página "+i+"\">"+i+"</span> |");
			}
			if(maxmostra != total){
				paginador.add("&nbsp;&nbsp;<span onClick=\"TVapi."+srcFunc+"('c"+channelId+"', 16, "+((inimostra+1)*itens_limit)+");\")\" title=\"próxima\">&gt;</span> ");
				paginador.add("&nbsp;&nbsp;&nbsp;<span onClick=\"TVapi."+srcFunc+"('c"+channelId+"', 16, "+((total-1)*16)+");\")\" title=\"última\">&gt;&gt;</span> ");
			}
			paginador.add("</div>")
			htmlPlaylist.add(paginador.toString());
			$("#areaVideoList").html(htmlPlaylist.toString());
			this.setEventInferior();
//		}
	},
	getVideoDetalhe: function(id,cod){
		window.scroll(0,0);
		$("#areaLateralVideoDetalhe").html(" ");
		this.changeLateral("areaLateralVideoDetalhe");
		$.ajax({
        	type: "GET",
            url: this.getVideoDetalheurl+id+"&cod="+cod,
            dataType: "xml",
            beforeSend: function(){$("#areaLoading").show()},
            complete: function(){$("#areaLoading").hide()},
            success: function(data){
				TVapi.geraVideoDetalhe(data);
			}
		});
	},
	geraVideoDetalhe: function(data){
		var htmlPlaylist = new StringBuffer();
		//var title = reduzTexto($(this).find('title').text(), 50);
		var title = $(data).find('title').text();
		var urlstream = $(data).find('urlstream').text();
		var description = $(data).find('shortdesc').text();
		var sinopse = $(data).find('description').text();
		var channelId = $(data).find('canal').text();
		var videoId = $(data).find('cod').text();
		var imgUrl = $(data).find('urlimg').text();
		var time = $(data).find('time').text();
		time = time.substring(time.indexOf(':')+1);
		var data = dataFormatoBr($(data).find('pubDate').text());
		var htmlItem = new StringBuffer();
		htmlItem.add("<div class=\"detalhe_tit\">"+title+"</div>");
		htmlItem.add("<div onclick=\"TVapi.prePlayFLV(this.id)\" onmouseover=\"TVapi.showHplay(this.id,true)\" onmouseout=\"TVapi.showHplay(this.id,false)\" id=\"v"+channelId+"-"+videoId+"\" class=\"detalhe_video_img\" style=\"background-image: url("+imgUrl+");\"><img height=\"54\" width=\"54\" class=\"imgthumb\" src=\"img/h_play2.png\" alt=\"\"/></div>");
		htmlItem.add("<div class=\"detalhe_dd\">");
		htmlItem.add("<div class=\"detalhe_hr\">"+time+"</div>");
		htmlItem.add("<div class=\"detalhe_dt\">"+data+"</div>");
		htmlItem.add("<div class=\"detalhe_volta\"><div onclick=\"TVapi.changeLateral('areaLateral')\" style=\"cursor:pointer\">&laquo;voltar&raquo;</div></div>");
		htmlItem.add("</div>");
		htmlItem.add("<div class=\"detalhe_desc\">"+description+"</div>");
		htmlItem.add("<div class=\"detalhe_sino\">");
		htmlItem.add("<p>Sinopse</p>");
		htmlItem.add("<p>"+sinopse+"</p>");
		htmlItem.add("</div>");
		htmlPlaylist.add(htmlItem.toString());
		htmlEmbed = new StringBuffer();
		htmlEmbed.add(this.getLinksURL(channelId, videoId));
		htmlEmbed.add(this.getLinksEmbeded(urlstream));
		$("#areaLateralVideoDetalhe").html(htmlPlaylist.toString());
		$("#areaEmbed").html(htmlEmbed.toString());
	}
}
