// JavaScript Document
function getvideos(treeStructure,pageNum,pageSize,sortClause,filterClause,vidFilterClause){

	matchdomain =window.location.href.match('(.*)\.com');
	domainURL=matchdomain[0];

//We can use one Request object many times.
			var req = new Request.HTML({
			url:'http://www.tvolucion.com/cocacolatv/indexajax_mc.php?treeStructure='+treeStructure+'&sortClause='+sortClause+'&pageSize='+pageSize+'&pageNum='+pageNum+'&filterClause='+filterClause+'&vidFilterClause='+vidFilterClause,  
				onSuccess: function(html) {
					//Clear the text currently inside the results div.
					$('mainContainerHalf').set('text', '');
					//Inject the new DOM elements into the results div.
					$('mainContainerHalf').adopt(html);
					
					setTips();
				},
				//Our request will most likely succeed, but just in case, we'll add an
				//onFailure method which will let the user know what happened.
				
				onFailure: function(xhr) {
					$('mainContainerHalf').set('text', 'ERROR');
				}
				
			}
			
			).send();	
}





function getvideosCom(treeStructure,pageNum,pageSize,sortClause,filterClause,vidFilterClause,type){

	matchdomain =window.location.href.match('(.*)\.com');
	domainURL=matchdomain[0];

//We can use one Request object many times.
			var req = new Request.HTML({
			url:'http://www.tvolucion.com/cocacolatv/indexcomentariosajax_mc.php?type='+type+'&treeStructure='+treeStructure+'&pageSize='+pageSize+'&pageNum='+pageNum+'&filterClause='+filterClause,  
				onSuccess: function(html) {
					//Clear the text currently inside the results div.
					$('mainContainerHalf').set('text', '');
					//Inject the new DOM elements into the results div.
					$('mainContainerHalf').adopt(html);
					
					setTips();
				},
				//Our request will most likely succeed, but just in case, we'll add an
				//onFailure method which will let the user know what happened.
				
				onFailure: function(xhr) {
					$('mainContainerHalf').set('text', 'ERROR');
				}
				
		}
			
	).send();
}

var cache;

function getvideosCarrusel(tags,pageNum,pageSize,videoId){

	matchdomain =window.location.href.match('(.*)\.com');
	domainURL=matchdomain[0];

new Ajax.Request('http://www.tvolucion.com/pagcarruselajax.php?tags='+tags+'&videoId='+videoId, {
  method:'get',
  onSuccess: function(transport){
     var videos = transport.responseText.evalJSON();
	 	
		cache = videos.Videos;
		var backHTML = "";
		var itemsHTML = "";
		var nextHTML = "";
		
		backHTML+="<img src=\"http://i2.esmas.com/tvolucion/img/arrow_carrusel_back.gif\" onclick=\"Javascript:backnextVids(8);\" />";
		
		for (i=0; i<cache.length && i<4; i++){
		var tmp = cache[i];
		
			if (tmp.Program.length>40)
				titulo=tmp.Program.substring(0,37)+'...';
			else 
				titulo=tmp.Program;
			if (tmp.EditorialTitle.length>26)
				capitulo=tmp.EditorialTitle.substring(0,23)+'...';
			else 
				capitulo=tmp.EditorialTitle;
		
		
		itemsHTML+="<li>";
		itemsHTML+="<a href=\""+tmp.UrlPublic+"\"><img src=\""+tmp.ThumbnailFilename+"\"/></a>";
		itemsHTML+="<span class=\"itemOptions\">"+titulo+"</span>";
		itemsHTML+="<span class=\"itemOptions\">Duraci&oacute;n:"+tmp.Duration+"</span>";
		itemsHTML+="<span class=\"itemOptions\">"+capitulo+"</span>";
		itemsHTML+="</li>";
		}
		
		nextHTML+="<img src=\"http://i2.esmas.com/tvolucion/img/arrow_carrusel_foward.gif\" onclick=\"Javascript:backnextVids(4);\" />";
		
	$("btnCarruselBack").update(backHTML);	
    $("VidcarruselItems").update(itemsHTML);
	$("btnCarruselNext").update(nextHTML);    
   }
});

}

function backnextVids(indice){

		var backHTML = "";
		var itemsHTML = "";
		var nextHTML = "";
		
		var pagBack = indice-4;
		var pagNext = indice+4;
		
		if ((indice+4)>=12)
			pagNext=0;
			
		if ((indice-4)<0)
			pagBack=8; 
		
		backHTML+="<img src=\"http://i2.esmas.com/tvolucion/img/arrow_carrusel_back.gif\" onclick=\"Javascript:backnextVids("+pagBack+");\" />";		
		for (i=indice; i<=(indice+3); i++){
		var tmp = cache[i];
		
			if (tmp.Program.length>40)
				titulo=tmp.Program.substring(0,37)+'...';
			else 
				titulo=tmp.Program;
			if (tmp.EditorialTitle.length>26)
				capitulo=tmp.EditorialTitle.substring(0,23)+'...';
			else 
				capitulo=tmp.EditorialTitle;
		
		itemsHTML+="<li>";
		itemsHTML+="<a href=\""+tmp.UrlPublic+"\"><img src=\""+tmp.ThumbnailFilename+"\"/></a>";
		itemsHTML+="<span>"+titulo+"</span>";
		itemsHTML+="<span class=\"itemOptions\">Duraci&oacute;n:"+tmp.Duration+"</span>";
		itemsHTML+="<span class=\"itemOptions\">"+capitulo+"</span>";
		itemsHTML+="</li>";

		}
		
		nextHTML+="<img src=\"http://i2.esmas.com/tvolucion/img/arrow_carrusel_foward.gif\" onclick=\"Javascript:backnextVids("+pagNext+");\" />";
		
	$("btnCarruselBack").update(backHTML);	
    $("VidcarruselItems").update(itemsHTML);
	$("btnCarruselNext").update(nextHTML);    


}


function getListRelated(treeStructure,pageNum,pageSize){

	matchdomain =window.location.href.match('(.*)\.com');
	domainURL=matchdomain[0];
new Ajax.Request('http://www.tvolucion.com/list_relatedajax.php?treeStructure='+treeStructure+'&pageSize='+pageSize+'&pageNum='+pageNum, {
  method:'get',
  onSuccess: function(transport){
     var videos = transport.responseText.evalJSON();
	 	
		var tmpHTML = "";
		var results = videos.Videos;
		tmpHTML+="<!--Pestanya de paginacion--><li class=\"rightNoTab\"><!--Paginacion--><div class=\"pagingCont\">";
				//paginacion 
		maxpage=5;
		
		if (videos.TotalResults>pageSize)
    	{
		    //back
            if(pageNum > 0)
            {
				if(pageNum>=maxpage)
            		tmpHTML+="<span><a href=\"Javascript:getListRelated('"+encodeURI(treeStructure)+"',0,"+pageSize+");\"><img src=\"http://i2.esmas.com/tvolucion/img/arrow_back.gif\" /><img src=\"http://i2.esmas.com/tvolucion/img/arrow_back.gif\" /></a></span><span><a href=\"Javascript:getListRelated('"+encodeURI(treeStructure)+"',"+(pageNum-1)+","+pageSize+");\"><img src=\"http://i2.esmas.com/tvolucion/img/arrow_back.gif\" /></a></span>";
				else
					tmpHTML+="<span><a href=\"Javascript:getListRelated('"+encodeURI(treeStructure)+"',"+(pageNum-1)+","+pageSize+");\"><img src=\"http://i2.esmas.com/tvolucion/img/arrow_back.gif\" /></a></span>";				
			}
			//numbers               
            j=0;
            while((j*pageSize)<videos.TotalResults)
            {   
                if(j>pageNum-maxpage && j<pageNum+maxpage)
                {
                    if(pageNum==j)
                        tmpHTML+="<span>"+(j+1)+"</span>";
                    else
                        tmpHTML+="<span><a href=\"Javascript:getListRelated('"+encodeURI(treeStructure)+"',"+(j)+","+pageSize+");\">"+(j+1)+"</a></span>";
                }
                j++;
            }
			var lastPage=Math.ceil(videos.TotalResults/pageSize);
			tmpHTML+="<span><a>...</a></span>";
			tmpHTML+="<span><a href=\"Javascript:getListRelated('"+encodeURI(treeStructure)+"',"+(lastPage-1)+","+pageSize+");\">"+lastPage+"</a></span>";
			//next
            if( !((pageNum==Math.ceil(videos.TotalResults/pageSize)) || (videos.TotalResults < pageSize)) )
				tmpHTML+="<span><a href=\"Javascript:getListRelated('"+encodeURI(treeStructure)+"',"+(pageNum+1)+","+pageSize+");\"><img src=\"http://i2.esmas.com/tvolucion/img/arrow_foward.gif\" /></a></span>";
		}
		tmpHTML+="</div><!--Termina paginacion--></li><!--Termina pestanya de paginacion-->";
	
	$("pag_top").update(tmpHTML);	
	$("pag_button").update(tmpHTML);	
		///  videos ****************
	if (results.length>0){	
		tmpHTML = "";
		
		tmpHTML+="<li class=\"formatTH\">";
		tmpHTML+="<span class=\"chaptersLongTitle\">T&iacute;tulo</span>";
		tmpHTML+="<span class=\"chaptersLen\">Duraci&oacute;n</span>";
		tmpHTML+="</li>";
		
		for (i=0; i<=results.length-1; i++){
			var tmp = results[i];
			tmpHTML+="<li><span class=\"chaptersLongTitle\" >";
			tmpHTML+="<a href='"+tmp.UrlPublic+"' class=\"txtColor4 txtMedium\">"+tmp.EditorialTitle+"</a>";
			tmpHTML+="</span>";
			tmpHTML+="<span class=\"chaptersLen\">"+tmp.Duration+"</span></li>";			
		}
	$("vid_related").update(tmpHTML);   
	} //end if >0   
  }
});

}



function getListChapter(treeStructure,pageNum,pageSize){

	matchdomain =window.location.href.match('(.*)\.com');
	domainURL=matchdomain[0];
new Ajax.Request('http://www.tvolucion.com/list_chapterajax.php?treeStructure='+treeStructure+'&pageSize='+pageSize+'&pageNum='+pageNum, {
  method:'get',
  onSuccess: function(transport){
     var videos = transport.responseText.evalJSON();
	 	
		var tmpHTML = "";
		var results = videos.Videos;
		///  videos ****************
	if (results.length>0){	
		tmpHTML = "";
		
		tmpHTML+="<li class=\"formatTH\">";
		tmpHTML+="<span class=\"seasonInfo\" id=\"cap_completos\">Temporada : Cap&iacute;tulo</span>";
		tmpHTML+="<span class=\"chaptersTitle\">T&iacute;tulo</span>";
		tmpHTML+="<span class=\"chaptersLen\">Duraci&oacute;n</span>";
		tmpHTML+="</li>";
		tmpHTML+="<li class=\"parentTD\">";
		tmpHTML+="<p id=\"arrow_1\" class=\"opened\" onClick=\"Javascript:muestraTodos('objetToHide_1','arrow_1');\" >Temporada 1</p>";
		for (i=0; i<=results.length-1; i++){
			var tmp = results[i];
			tmpHTML+="<p class=\"objetToHide_1\">";
			tmpHTML+="<span class=\"seasonInfo\">"+tmp.Season+" : "+tmp.Chapter+"</span>";
			tmpHTML+="<span class=\"chaptersTitle\" >";
			tmpHTML+="<a href='"+tmp.UrlPublic+"' class=\"txtColor4 txtMedium\">"+tmp.EditorialTitle+"</a>";
			tmpHTML+="</span>";
			tmpHTML+="<span class=\"chaptersLen\">"+tmp.Duration+"</span>";
			tmpHTML+="</p>";			
		}
		tmpHTML+="</li>";
		
		//paginacion ******************
		maxpage=3;
		
		if (videos.TotalResults>pageSize)
    	{
		//tmpHTML+="<p class=\"objetToHide_1 rightPaging\">";
		tmpHTML+="<li class=\"objetToHide_1\">";
		tmpHTML+="<!--Paginacion--><div class=\"pagingCont rightPaging\">";
		    //back
            if(pageNum > 0)
            {
				if(pageNum>=maxpage)
            		tmpHTML+="<span><a href=\"Javascript:getListChapter("+encodeURI(treeStructure)+",0,"+pageSize+");\"><img src=\"http://i2.esmas.com/tvolucion/img/arrow_back.gif\" /><img src=\"http://i2.esmas.com/tvolucion/img/arrow_back.gif\" /></a></span><span><a href=\"Javascript:getListChapter("+encodeURI(treeStructure)+","+(pageNum-1)+","+pageSize+");\"><img src=\"http://i2.esmas.com/tvolucion/img/arrow_back.gif\" /></a></span>";
				else
					tmpHTML+="<span><a href=\"Javascript:getListChapter("+encodeURI(treeStructure)+","+(pageNum-1)+","+pageSize+");\"><img src=\"http://i2.esmas.com/tvolucion/img/arrow_back.gif\" /></a></span>";				
			}
			//numbers               
            j=0;
            while((j*pageSize)<videos.TotalResults)
            {   
                if(j>pageNum-maxpage && j<pageNum+maxpage)
                {
                    if(pageNum==j)
                        tmpHTML+="<span>"+(j+1)+"</span>";
                    else
                        tmpHTML+="<span><a href=\"Javascript:getListChapter("+encodeURI(treeStructure)+","+(j)+","+pageSize+");\">"+(j+1)+"</a></span>";
                }
                j++;
            }
			var lastPage=Math.ceil(videos.TotalResults/pageSize);
			//tmpHTML+="<span><a>...</a></span>";
			//tmpHTML+="<span><a href=\"Javascript:getListChapter("+encodeURI(treeStructure)+","+(lastPage-1)+","+pageSize+");\">"+lastPage+"</a></span>";
			//next
            if( !((pageNum+1==Math.ceil(videos.TotalResults/pageSize)) || (videos.TotalResults < pageSize)) )
				tmpHTML+="<span><a href=\"Javascript:getListChapter("+encodeURI(treeStructure)+","+(pageNum+1)+","+pageSize+");\"><img src=\"http://i2.esmas.com/tvolucion/img/arrow_foward.gif\" /></a></span>";
		tmpHTML+="</div><!--Termina paginacion-->";
		//tmpHTML+="</p>";
		tmpHTML+="</li>";
		}
		
		//tmpHTML+="<li id=\"pagChapter_button\" style=\"text-align:right\"></li>";

	$("vid_chapter").update(tmpHTML);   
	} //end if >0 
  }
});

}
