/* *** PLAYLIST *** */
    /// this fills a div, of the ENPORTADA section, on pageload with the newest item of the onload playlist
function playlistOnload(index)
{

      var currentPlaylist2  = playlistID;
	if(currentPlaylist2 == ""){return;}


	$.ajax({
		type: "POST",
		url: "vzService/content.php",
		data: "playlistID="+currentPlaylist2+"",
		contentType: CONTENT_TYPE,
		success: function(msg){ 
	//	alert(msg);
			try{
				eval(msg);
				if(contentArray[0].Title != ""){
					currentlyShowing = 'content';
						
  					buildEnportada();
					playVideo(0, currentlyShowing);
					
				}else{
					alert("No hay resultados que mostrar.");
				}
			}catch(exception){
				alert("No hay resultados que mostrar. Error:" + exception);
			}
		}
	});
	

}

function switchPlaylist(index){
	currentPlaylist  = playlists[index];
	if(currentPlaylist == ""){return;}


	$.ajax({
		type: "POST",
		url: "vzService/content.php",
		data: "playlistID="+currentPlaylist+"",
		contentType: CONTENT_TYPE,
		success: function(msg){ 
			try{
				eval(msg);
				if(contentArray[0].Title != ""){
					currentlyShowing = 'content';
						
            /////////////////rellena de <li> estaticos como cajas para el carousel ///////////////
            c.reset();
            
             contentLength = 0; //aaaa
  					 var hazLista="";
	           contentLength = contentArray.length; 
	          
            	for(var i=0;i < contentLength;i++){
	        
              hazLista +=	"<li></li>";
              
         	}
           	$("#playlistInner").html(hazLista);
	           ///////////////////////////////////////////////////////////////////////////////////


					buildPlaylistContent();
					buildEnportada();
					playVideo(0, currentlyShowing);
					
				}else{
					alert("No hay resultados que mostrar.");
				}
			}catch(exception){
				alert("No hay resultados que mostrar. Error:" + exception);
			}
		}
	});
					
}

function highlightPlaylistItem(index) {
	try{
		$(".playlist_item").removeClass("playlist_item selected"); //remove all the previous playlist highlights
		$("#item" + index).addClass("playlist_item selected"); // find which playlist item to highlight
		

	}catch(ex){
		// we might not be have to highlight this, if it's an advert.
	}
}

function displayPlayingItem(index, showing){
	var playingItem;
	currentlyShowing = showing;

	//Reset the vars used, so we can start fresh.
	$("#video h2 span").html(""); //current_video_title 
	$("#video .sidebar p").html(""); // current video description
	$('.playlist_item_link').unbind('click'); 

	videoPlayer_ControlBar = 'over';

	if(currentlyShowing == 'content'){ playingItem = contentArray[index]; }
	if(currentlyShowing == 'search'){ playingItem = searchContentArray[index]; }
	$("#video h2 span").html(playingItem.Title); //current_video_title
	$("#video .sidebar p").html(playingItem.LongDesc); // current video description
	

	if(playingItem.NoClickAway == 'true'){
		$(".playlist_item_link").click(function() { return false; });
		videoPlayer_ControlBar = 'none';
	}


}


//////////BUILDS EN PORTADA SECTION ///////////////////////////////////////////////////////
function buildEnportada(){
                  
	var playingItem;
//	$("#test").html(""); //clear contents.
     $("#blackBox p a").html("");
     $("#blackBox p .des").html("");
	 
   contentLength = 0; //aaaa
	 // contentLength = contentArray.length; 
  if(currentlyShowing == 'content'){ contentLength = 1; }
	if(currentlyShowing == 'search'){ contentLength = 1; }
	

	for(var i=0;i < contentLength;i++){

		var title = '';
		var doNotShowInPlaylist = 'false';
		var shortDesc = '';
		var longDesc ='';    
		var ponTitulo ='';
		var ponDes ='';
		
   	if(currentlyShowing == 'content'){ playingItem = contentArray[i]; }
		if(currentlyShowing == 'search'){ playingItem = searchContentArray[i]; }

		title = playingItem.Title;
		atitle = title.replace(":", "");
		aatitle = atitle.replace("\"", "");
		aaatitle = aatitle.replace("\"", "");

		shortDesc = playingItem.ShortDesc; 
		longDesc = playingItem.LongDesc; 
    doNotShowInPlaylist = playingItem.DoNotShowInPlaylist;
    var URLFriendlyTitle = replaceAll(title, " ", "-");
    var URLFriendlyTitle = URLFriendlyTitle.replace(/\"/g, "");
    var elmetaID = playingItem.ContentMetaDataID;
             
                   
              if(aaatitle.length >= 45)
              {
                aaatitle = aaatitle.substring(0,45) + "...";
              }
                if(shortDesc.length >= 49)
              {
                shortDesc = shortDesc.substring(0,49) + "...";
              }
              

	
		if(doNotShowInPlaylist == 'false'){
            

	   
    var str;

    $(".blackBox p a").each(function(j){
        
        str = "<a href=\""+baseurl+"video/"+elmetaID+"/"+URLFriendlyTitle+"/index.php\">"+aaatitle+"</a>";
        $(this).html(str);
    });

    $(".blackBox p .des").each(function(j){
        
        //str= ""+shortDesc+"";
        str= ""+longDesc+"";
        $(this).html(str);
    });
	   
	      
		}//final if do nor show inplaylist
  } //final for 
}// final funcion
  
  
  


///////////////////////////////////////////////////////////////////////////////////////







//Build Playlist.
function buildPlaylistContent(){

	//if(currentlyShowing == 'content'){ 
  


	var playingItem;
	$("#playlistInner").html(""); //clear contents.

	 contentLength = 0; //aaaa
	 // contentLength = contentArray.length; 
    	if(currentlyShowing == 'content'){ contentLength = contentArray.length; }
	if(currentlyShowing == 'search'){ contentLength = searchContentArray.length; }
	

	for(var i=0;i < contentLength;i++){
		var thumbnailpath = '';
		var title = '';
		var doNotShowInPlaylist = 'false';
		var adTag = '';
		var shortDesc = '';
		var longDesc ='';
	  var ponllelo ='';
        	
   		if(currentlyShowing == 'content'){ playingItem = contentArray[i]; }
   		
		if(currentlyShowing == 'search'){     playingItem = searchContentArray[i]; 
  		 $("#home h2 span").html("");
  	   ponllelo +="Resultados de la b&uacute;squeda: <br>"+wordsToSearch+"";
       $("#home h2 span").html(ponllelo);
  	   $(".blackBox").hide();

    }


		thumbnailpath = playingItem.ThumbnailPath;
		title = playingItem.Title;
		atitle = title.replace(":", "");
		aatitle = atitle.replace("\"", "");
		aaatitle = aatitle.replace("\"", "");
		var aaaatitle = aaatitle.replace("?", "");
		var aaaaatitle = aaaatitle.replace("¿", "");
		shortDesc = playingItem.ShortDesc; 
    doNotShowInPlaylist = playingItem.DoNotShowInPlaylist;
		adTag = playingItem.AdTag;
		longDesc = playingItem.LongDesc; 
		var URLFriendlyTitle = replaceAll(title, " ", "-");
     var URLFriendlyTitle = URLFriendlyTitle.replace(/\"/g, "");
     var URLFriendlyTitle = replaceAll(URLFriendlyTitle, "?", "");
     
     var URLFriendlyTitle = URLFriendlyTitle.replace(/\¿/g,"");
     var elmetaID = playingItem.ContentMetaDataID;
        var laplay = playingItem.PlaylistID;
   
              
                   
              if(aaatitle.length >= 45)
              {
                aaatitle = aaatitle.substring(0,45) + "...";
              }
                if(shortDesc.length >= 49)
              {
                shortDesc = shortDesc.substring(0,49) + "...";
              }
              
              
		 // deal with missing thumbnails
		if(thumbnailpath == ""){ thumbnailpath = THUMBNAIL_MISSING; }
	
		if(doNotShowInPlaylist == 'false'){
            
	  	var i2 = i+1;

	if(currentlyShowing == 'content'){
var someImage = "<h3 class='limitah3'><a href=\""+baseurl+"video.php?PlayList="+laplay+"&VideoPlay="+i+"\" onclick=\"_gaq.push(['_trackEvent', 'Videos', 'index', '"+aaatitle+"']);\">"+aaatitle+"</a></h3><div class='video marginTop'><a href=\""+baseurl+"video.php?PlayList="+laplay+"&VideoPlay="+i+"\" class='mini' onclick=\"_gaq.push(['_trackEvent', 'Videos', 'index', '"+ aaatitle +"']);\"><img name='Imagen' Width='"+THUMBNAIL_WIDTH+"' Height='"+THUMBNAIL_HEIGHT+"' src='"+ thumbnailpath +"' alt='"+aaaaatitle+"'  class='pngfix'/><span></span></a><p class='mail'><a href='"+baseurl+"popups/popup_enviar.php?link="+baseurl+"video/"+ elmetaID +"/"+ URLFriendlyTitle +"/index.php&titulo="+aaatitle +"' onclick=\"return GB_showCenter('Enviar a un amigo', this.href , 400, 500)\" target='_blank' title='Enviar por correo'>Enviar por correo</a></p><p class='text'>"+shortDesc+"</p></div>";	
}

	if(currentlyShowing == 'search'){
  var someImage = "<h3 class='limitah3'><a href=\""+baseurl+"video/"+elmetaID+"/"+URLFriendlyTitle+"/index.php\" onclick=\"_gaq.push(['_trackEvent', 'Videos', 'index', '"+aaatitle+"']);\">"+aaatitle+"</a></h3><div class='video marginTop'><a href=\""+baseurl+"video/"+elmetaID+"/"+URLFriendlyTitle+"/index.php\" class='mini' onclick=\"_gaq.push(['_trackEvent', 'Videos', 'index', '"+aaatitle+"']);\"><img name='Imagen' Width='"+THUMBNAIL_WIDTH+"' Height='"+THUMBNAIL_HEIGHT+"' src='"+ thumbnailpath +"' alt='"+aaaaatitle+"'  class='pngfix'/><span></span></a><p class='mail'><a href='"+baseurl+"popups/popup_enviar.php?link="+baseurl+"video/"+ elmetaID +"/"+ URLFriendlyTitle +"/index.php&titulo="+aaatitle +"' onclick=\"return GB_showCenter('Enviar a un amigo', this.href , 400, 500)\" target='_blank' title='Enviar por correo'>Enviar por correo</a></p><p class='text'>"+shortDesc+"</p></div>";	
  }
     
     
     
      c.add(i2,someImage);

		}
  } //final for 
  c.size(contentLength); 
  
/*	}else{ //*****************SEARCH MODE**************************
	


	var playingItem;
	$("#searchResults").html(""); //clear contents.
	$("li .searchResultsItem").html(""); //clear contents.
	var playlistContentBuilderSearch = "";


	  var contentLength = 0;
	  contentLength = searchContentArray.length; 

			playlistContentBuilderSearch +="<p id='close' class='floatRight clearFix'><strong><a href='javascript:cierraBusqueda();'>Cerrar</a></strong></p>";
			playlistContentBuilderSearch +="<h3>Resultados de b&uacute;squeda: "+wordsToSearch+"</h3>";
			playlistContentBuilderSearch +="<ul id='searchResultsList' class='clearAll clearFix'>";


	for(var i=0;i < contentLength;i++){
		var thumbnailpath = '';
		var title = '';
		var doNotShowInPlaylist = 'false';
		var adTag = '';
		var shortDesc = '';
		var longDesc ='';
		
    playingItem = searchContentArray[i]; 


		thumbnailpath = playingItem.ThumbnailPath;
		title = playingItem.Title;
		shortDesc = playingItem.ShortDesc; 
    doNotShowInPlaylist = playingItem.DoNotShowInPlaylist;
		adTag = playingItem.AdTag;
		longDesc = playingItem.LongDesc; 

		 // deal with missing thumbnails
		if(thumbnailpath == ""){ thumbnailpath = THUMBNAIL_MISSING; }
	
		if(doNotShowInPlaylist == 'false'){

 			playlistContentBuilderSearch +=	"<li class='searchResultsItem'><a href=\"javascript:playVideo("+ i +", '"+ currentlyShowing +"');\"  ><img Width='"+THUMBNAIL_WIDTH+"' Height='"+THUMBNAIL_HEIGHT+"' src='"+ thumbnailpath +"' /></a>";
			playlistContentBuilderSearch +=	"<h4><a href=\"javascript:playVideo("+ i +", '"+ currentlyShowing +"');\"  >"+title+"</a></h4>";
			playlistContentBuilderSearch +=	"<p>"+longDesc+"</p>";
			playlistContentBuilderSearch +=	"</li>";

		
		}
  } //final del for 


	$("#searchResults").html(playlistContentBuilderSearch);

  		$(function(){
				$("#searchResultsList li").quickpaginate({ perpage: 6, pager : $("#searchResults") });
			});

}		//final else search mode				

   */

}//final funcion 


