/********************/
/* Control playlist */
/********************/
function loadboton(index)
{
	if (index==1)
	{
		var boton = "";
	
		boton += "<li><a onclick='switchPlaylist(0); return false;' href='#'><img src='http://www.psoetv.es/embed/img/banner_elecciones2011_back.jpg' alt='Volver'/></a></li>";	
		
		volver = 0;
	
		$("ol#elecciones").html(boton);
	}
	else
	{
		var boton = "";
	
		boton += "<li><a onclick='switchPlaylist(1); return false;' href='#'><img src='http://www.psoetv.es/embed/img/banner_elecciones2011.jpg' alt='Elecciones 2011'/></a></li>";
		
		volver = 1; 	
	
		$("ol#elecciones").html(boton);
	}
}

function switchPlaylist(index)
{
	currentPlaylist  = playlists[index];

	if(currentPlaylist == ""){return;}
	
	loadboton(index);

	$.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();
			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 #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;
		var playlistContentBuilder = "";

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

	 	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, " ", "-");
	   		URLFriendlyTitle = replaceAll(aaatitle, "/", "-");
			
			//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+"', 'tira']);\" id='item"+i+"'  onmouseover=\"Tip('"+aaatitle+"')\" onmouseout=\"UnTip()\" class='playlist_item' href=\""+baseurl+"video/"+ contentArray[i].ContentMetaDataID +"/"+ URLFriendlyTitle +"/index.php\" target=\"_blank\"><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>";
		/*  alert(wordsToSearch);
      wordsToSearch2 = wordsToSearch.replace("%20", " ");*/
    
    	playlistContentBuilderSearch +="<h3>Resultados de bú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;
		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 onclick=\"_gaq.push(['_trackEvent', 'Videos', '"+aaatitle+"', 'embed']);\" 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 



