/* *** PLAYLIST *** */


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



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

function highlightPlaylistItem(index) { 
	
				try{
			/*$(".uvlTitle").removeClass("uvlSelected"); //remove all the previous playlist highlights
		$("#item" + index).addClass("uvlSelected"); // find which playlist item to highlight*/
					$(".titulo").removeClass("selected"); //remove all the previous playlist highlights
		$("#item" + index).addClass("selected"); // find which playlist item to highlight
				if(index > 0){
				
				if (contentArray.length== 3 || contentArray.length== 2 ||contentArray.length== 1 || searchContentArray.length== 3 || searchContentArray.length== 2 ||searchContentArray.length== 1){
	
          $('#pane')[0].scrollTo(1*index); //si sólo hay 1 2 ó 3 vídeos no hagas scroll para centrar el highlight en la playlist 
            }else{
					$('#pane')[0].scrollTo(72*index); //-18 //88
          }
							                                
					}
			}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
	$('.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 #slider li#descripcion").html(playingItem.LongDesc); // current video description
	

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


}


// comparte con twitter
function twit(index)
{
	var videoPlayingIndex = index;
		
	BitlyCB.shortenResponse = function(data) 
	{
    	var s = '';
        var first_result;
        
        // Results are keyed by longUrl, so we need to grab the first one.
        for (var r in data.results) 
        {
        	first_result = data.results[r]; 
        	break;     
        }          
        
        for (var key in first_result) 
        {
        	s += key + ":" + first_result[key].toString() + "\n";
        }

        var twite= "http://twitter.com/home?status=Viendo en BBVA Externo: " + first_result['shortUrl'];
        twitee = window.open(twite)
  
        return twitee;          
	}
	BitlyClient.shorten(laurl, 'BitlyCB.shortenResponse');
}	

//Build Playlist.
function buildPlaylistContent()
{
	var playingItem;
	$("#playlistInner").html(""); //clear contents.
	var playlistContentBuilder = "";

	contentLength = 0; //aaaa
	  	
	if(currentlyShowing == 'content')
	{ 
		contentLength = contentArray.length; 
	}
	
	if(currentlyShowing == 'search')
	{ 
		contentLength = searchContentArray.length; 
	}
	
	var thumbnailpath = '';
	var title = '';
	var doNotShowInPlaylist = 'false';
	var adTag = '';
	var shortDesc = '';
	var longDesc ='';
	var playlistid='';
	var playlistname='';
	var duration = '';
	var addnuevo='';
	var i=''; 
	var imagen='';
	var elmetaID='';
	var URLFriendlyTitle='';
	var descargas='';
	var urlgrande='';
	var urlmeneame='';
	var socialmedia='';
	var ancho=720;
  	var alto=361;
  	
  	//Colocamos el popup arriba a la izquierda
  	var posx=(screen.width/2)-(ancho);
	var posy=(screen.height/2)-(alto);
	
	for(i=0;i < contentLength;i++)
	{	
    	playingItem = contentArray[i]; 

		if(currentlyShowing == 'content')
		{ 
			playingItem = contentArray[i];
			metaItem = metaArray[i]; 
		}
		
		if(currentlyShowing == 'search')
		{ 
			playingItem = searchContentArray[i];
			metaItem = metaArray[i];
		}

		thumbnailpath = playingItem.ThumbnailPath;
		title = playingItem.Title;
		atitle = title.replace(":", "");
		aatitle = atitle.replace("\"", "");
		aaatitle = aatitle.replace("\"", "");
		shortDesc = playingItem.ShortDesc; 
    	doNotShowInPlaylist = playingItem.DoNotShowInPlaylist;
		adTag = playingItem.AdTag;
		longDesc = playingItem.LongDesc; 
		playlistid = playingItem.PlaylistID;
 	 	descargas = playingItem.VideoPath;
 	 	duration = playingItem.Duration;
 	 	elmetaID = playingItem.ContentMetaDataID;
 	 	
 	 	URLFriendlyTitle = replaceAll(playingItem.Title, " ", "-");
     	URLFriendlyTitle = URLFriendlyTitle.replace(/\"/g, "");
 	 	
 	 	//laurl = baseurl+"video/"+ playingItem.ContentMetaDataID +"/"+ URLFriendlyTitle +"/index.php";
  	 	
  	 	urlgrande = "http://externo.bbva.tv/faceshare.php?title="+URLFriendlyTitle+"&meta="+elmetaID+"&img="+thumbnailpath+"&desc="+shortDesc+"&u="+descargas;
  	 	urlmeneame = "http://externo.bbva.tv/index.php?MetaDataID="+elmetaID;
  	 	laurl = "http://externo.bbva.tv/index.php?MetaDataID="+elmetaID;
			
		socialmedia = metaItem.EsSocial;
		  	 
  	 	// minutes and seconds for duration (comes from Lenght all in seconds)		
		minVar = Math.floor(duration/60);  // minutes
    	secVar = duration % 60;              //  seconds
      
      	// add a zero to the seconds   
      	secVarS = secVar+''; // turn integer into string for .length to work
      	secVarS = secVarS.length; // how many characters?
      
      	if (secVarS==1) //if it has just one decimal, just one charater
      	{
        	secVar= secVarS*10; //add a zero 
      	}

      	if(adTag!='')
      	{ 
        	addnuevo ="&#161;NUEVO!"; 
      	}
      	else
      	{
        	addnuevo="";
      	}

		// deal with missing thumbnails
		if(thumbnailpath == "")
		{ 
			thumbnailpath = THUMBNAIL_MISSING; 
		}
	
		if(doNotShowInPlaylist == 'false')
		{
	      	playlistContentBuilder +="<div class='uvlItemBox'>";
			playlistContentBuilder +="<a href=\"javascript:playVideo("+ i +", '"+ currentlyShowing +"');\" onclick=\"_gaq.push(['_trackEvent', 'Videos', '"+aaatitle+"']);\"><img id='rptUVL__ctl2_Thumb' title='"+title+"' class='uvlThumb uvlNormal' src='"+thumbnailpath+"' border='0' style='height:"+THUMBNAIL_HEIGHT+"px;width:"+THUMBNAIL_WIDTH+"px;' /></a><div id='rptUVL__ctl2_Text' class='uvlTextBox'>";
			 
			playlistContentBuilder +="<a title='"+title+"' id='item"+i+"' class='titulo' href=\"javascript:playVideo("+ i +", '"+ currentlyShowing +"');\" onclick=\"_gaq.push(['_trackEvent', 'Videos', '"+aaatitle+"']);\">"+title+"</a><div id='highlightNuevo'>"+addnuevo+"</div><span id='rptUVL__ctl2_Abs' class='uvlShortAbstract uvlNormal'>"+shortDesc+"</span>";
				 
			if (socialmedia == 1)
			{
				playlistContentBuilder += "<span class='socialmedia'><a href='"+urlgrande+"' target='popup' onClick=\"window.open(this.href, this.target, 'width="+ancho+",height="+alto+",toolbar=no,directories=no,menubar=no,status=no,left="+posx+",top="+posy+"'); return false;\"><img src='images/facebook.png' title='Facebook' alt='Facebook' /></a>&nbsp;&nbsp;<a href='#' onclick='javascript:twit(); return false;'><img src='images/twitter.png' title='Twitter' alt='Twitter' /></a>&nbsp;&nbsp;<a href='http://www.meneame.net/submit.php?url="+urlmeneame+"'><img src='images/meneame.png' title='Meneame' alt='Meneame' /></a></span>";
			}
				 
			playlistContentBuilder +="</div><div id='rptUVL__ctl2_Times' class='uvlTimesBox'>";
			playlistContentBuilder +="<span id='rptUVL__ctl2_StartDt' title='"+ minVar+':'+secVar +"' class='uvlStartDt uvlNormal'>"+ minVar+':'+secVar +"</span><span class='sep'>/</span></div><div id='rptUVL__ctl2_DetailsBox' class='uvlDetailsBox uvlNormal'></div></div>";
		}
	} //final for 
	
	$("#pane").html(playlistContentBuilder);
	$('#pane').jScrollPane({showArrows:true, scrollbarWidth:10, scrollbarMargin:30});  
}//final funcion 

