/* *** PLAYLIST *** */


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);
	           ///////////////////////////////////////////////////////////////////////////////////
		           if (currentPlaylist != 88)
					{
						$("#main").html("<div id='videoHolder'></div>");
					}
					else
					{
						$("#main").html("<div id='widgetPlayer' align='center'><a href='http://www.adobe.com/go/getflashplayer'>Get Flash Player</a></div>");
						loadWidgetPlayer();
					}
	           
					buildPlaylistContent();
					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.
	//$("#sidebar h2").html(""); //current_video_title 
	//$("#sidebar #slider li#descripcion").html(""); // current video description
	
	$("#sidebar #slider1 li#cabecera").html("");
	$("#sidebar #slider1 li#descripcion").html("");
	$('.playlist_item_link').unbind('click'); 

	videoPlayer_ControlBar = 'over';

	if(currentlyShowing == 'content'){ playingItem = contentArray[index]; }
	if(currentlyShowing == 'search'){ playingItem = searchContentArray[index]; }
	//$("#sidebar h2").html(playingItem.Title); //current_video_title
	$("#sidebar #slider1 li#cabecera").html(playingItem.Title); //current_video_title
	
if(playingItem.Lnk != ""){ 
 var todo =playingItem.LongDesc+" <a target='_blank' href='"+playingItem.Lnk+"'>M&aacute;s informaci&oacute;n</a>";
		$("#sidebar #slider1 li#descripcion").html(todo); // current video description + mas infomacion 
	}else{
	$("#sidebar #slider1 li#descripcion").html(playingItem.LongDesc); // current video description
 	}


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

function cierraBusqueda(){
	$("#searchResults").html(""); //clear contents. Para el boton cerrar resultados de busqueda
}

//Build Playlist.
function buildPlaylistContent(){

	if(currentlyShowing == 'content'){ 
  
	var playingItem;
	$("#playlistInner").html(""); //clear contents.
	var playlistContentBuilder = "";

	 contentLength = 0; //aaaa
	  contentLength = contentArray.length; 

	for(var i=0;i < contentLength;i++){
		var thumbnailpath = '';
		var title = '';
		var doNotShowInPlaylist = 'false';
		var adTag = '';
		var shortDesc = '';
		var longDesc ='';
		
    playingItem = contentArray[i]; 
    
		thumbnailpath = playingItem.ThumbnailPath;
		title = playingItem.Title;
		atitle = title.replace(":", "");
		aatitle = atitle.replace("\"", "");
		aaatitle = aatitle.replace("\"", "");
		aaatitle = replaceAll(aaatitle, "\"", "");
		shortDesc = playingItem.ShortDesc; 
	    doNotShowInPlaylist = playingItem.DoNotShowInPlaylist;
		adTag = playingItem.AdTag;
		longDesc = playingItem.LongDesc;
		var URLFriendlyTitle = replaceAll(aaatitle, " ", "-");
		var elmetaID = playingItem.ContentMetaDataID;
		var theuri = baseurl+"video/"+ elmetaID +"/"+ URLFriendlyTitle +"/video.php";
		
		//quita herramientas cuando el titulo es Directo
				if (title=="Directo"){$("#tools").hide(); $("#relatedVideos").hide();}
		
		 // deal with missing thumbnails
		if(thumbnailpath == ""){ thumbnailpath = THUMBNAIL_MISSING; }
	
		if(doNotShowInPlaylist == 'false'){

	  	var i2 = i+1;
	    var someImage = "<a onclick=\"_gaq.push(['_trackEvent', 'Videos', '"+aaatitle+"', 'player']);playVideo("+ i +", '"+ currentlyShowing +"');return false;\" id='item"+i+"'  onmouseover=\"Tip('"+aaatitle+"')\" onmouseout=\"UnTip()\" class='playlist_item' href=\""+theuri+"\"><img  name='imagen' Width='"+THUMBNAIL_WIDTH+"' Height='"+THUMBNAIL_HEIGHT+"' src='"+ thumbnailpath +"' /><span></span></a>";
      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>";
		
    		  
      wordsToSearch2 = wordsToSearch.replace("%20", " ");
    	playlistContentBuilderSearch +="<h3>Resultados de b&uacute;squeda: "+wordsToSearch2+"</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;
		atitle = title.replace(":", "");
		aatitle = atitle.replace("\"", "");
		aaatitle = aatitle.replace("\"", "");
		aaatitle = replaceAll(aaatitle, "\"", "");
		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 onclick=\"_gaq.push(['_trackEvent', 'Videos', '"+aaatitle+"', 'player']);\" 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 



