/* *** VIDEO CONTROL *** */

function replaceAll(stringValue, replaceValue, newValue)
{
	var functionReturn = new String(stringValue);
	while ( true )
	{
		var currentValue = functionReturn;
		functionReturn = functionReturn.replace(replaceValue, newValue);

		if ( functionReturn == currentValue )
			break;
	}
	return functionReturn;
}

function controlTogglePlay()
{
	var thePlayer = 'player1';

	player = document.getElementById(thePlayer);
	player.sendEvent("PLAY");
}

//comparte con facebook
function encodeface(index)
{
	var currentVideoIndex = index;
    var s = encodeURI(laurlface); 
    abreface = window.open(s);
    return abreface;
}	     
 
//comparte con twitter
function twit(index)
{
	var videoPlayingIndex = index;
		
	BitlyCB.shortenResponse = function(data) 
	{//alert("Viendo");
    	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=Algo Contigo: " + first_result['shortUrl'];
        twitee = window.open(twite)

        return twitee;  
	}

	BitlyClient.shorten(laurlNumero, 'BitlyCB.shortenResponse');
}

function playVideo(index, showing)
{
	currentVideoIndex = index;
	currentlyShowing = showing;
	highlightPlaylistItem(index);
	displayPlayingItem(index, showing);
	initPlayer(currentVideoIndex);

	/** Herramientas de descargar embeber y demás**/
	var herramientas = '';
	var title = contentArray[index].Title;
	var atitle = title.replace(":", "");
	var aatitle = atitle.replace("\"", "");
	var aaatitle = aatitle.replace("\"", "");
	var des = contentArray[index].LongDesc;
	
	var URLFriendlyTitle = replaceAll(contentArray[index].Title, " ", "-");
    var URLFriendlyTitle = URLFriendlyTitle.replace(/\"/g, "");
   	var descargoVideo = contentArray[index].VideoPath;
  	var elmetaID = contentArray[index].ContentMetaDataID;
  	
  	var urlgenerica = "http://www.giraldatv.es/";
  	
  	//////
  	
  	var urldelvideo = contentArray[index].VideoPath;
	var descripcionlarga = contentArray[index].LongDesc;

	//facebook
	var ladesc = descripcionlarga;
	var tbimg = contentArray[index].ThumbnailPath; 
	
	urlgrande = "http://www.giraldatv.es/algocontigo/faceshare.php?title="+URLFriendlyTitle+"&dir=algocontigo&meta="+elmetaID+"&img="+tbimg+"&desc="+ladesc+"&u="+urldelvideo;

  	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);
  		
  	/////
  	
  	laurl= baseurl+"video/"+ contentArray[index].ContentMetaDataID +"/"+ URLFriendlyTitle +"/index.php";
    laurlNumero = baseurl+"index.php?MetaDataID="+ elmetaID;
    laurlface = "http://facebook.com/share.php?u=" + laurlNumero;

   	herramientas += "<div class='DivCol'><a href='"+urlgenerica+"link.php?link="+baseurl+"index.php?MetaDataID="+ elmetaID+"' onclick=\"return GB_showCenter('Enlace al v&iacute;deo', this.href , 140, 540)\"><img src='http://www.giraldatv.es/img/herramientas/enlace.jpg' border=0 alt='enlace'></a></div>";
    herramientas += "<div class='DivCol'><a href='#' onclick='javascript:twit(); return false'><img src='http://www.giraldatv.es/img/herramientas/twitter.jpg' border=0 alt='twitter'></a></div>";
    herramientas += "<div class='DivCol'><a href='"+urlgenerica+"comenta.php?theid="+elmetaID+"&thetitle="+title+"&thedescription="+des+"' onclick=\"return GB_showCenter('Comenta', this.href , 400, 600)\"><img src='http://www.giraldatv.es/img/herramientas/comenta.jpg' border=0 alt='comenta'></a></div>";
    herramientas += "<div class='Sep'>&nbsp;</div>";
   
    herramientas += "<div class='DivCol'><a href='"+urlgenerica+"embed.php?path="+descargoVideo+"&baseurl="+urlgenerica+"&imagen="+tbimg+"' onclick=\"return GB_showCenter('Embeber v&iacute;deo', this.href , 230, 500)\"><img src='http://www.giraldatv.es/img/herramientas/embed.jpg' border=0 alt='embed'></a></div>";
    //herramientas += "<div class='DivCol'><a href='#' onclick='javascript:encodeface(); return false'><img src='img/herramientas/facebook.jpg' border=0 alt='facebook'></a></div>";
  	herramientas += "<div class='DivCol'><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;\" class='shareFacebook' title='Compartir en Facebook'><img src='http://www.giraldatv.es/img/herramientas/facebook.jpg' border=0 alt='facebook'></a></div>";
  	
  	//herramientas += "<div class='DivCol'><a href='#' onclick=''><img src='img/pixel.png' border=0 width='49px' height='29px'></a></div>";
    herramientas += "<div class='DivCol'><img src='http://www.giraldatv.es/img/pixel.png' border=0 alt='pixel'></div>";
    herramientas += "<div class='Sep'>&nbsp;</div>";
   
   	herramientas += "<div class='DivCol'><a href='"+urlgenerica+"popup_enviar.php?link="+baseurl+"index.php?MetaDataID="+ elmetaID+"&titulo="+ aaatitle +"' onclick=\"return GB_showCenter('Enviar a un amigo', this.href , 400, 500)\" target='_blank'><img src='http://www.giraldatv.es/img/herramientas/email.jpg' border=0 alt='email'></a></div>";
    herramientas += "<div class='DivCol'><a target='_blank' href='http://meneame.net/submit.php?url="+laurl+"'><img src='http://www.giraldatv.es/img/herramientas/menealo.jpg' border=0 alt='menealo'></a></div>";
    herramientas += "<div class='DivCol'><a href='vzService/rss.php'><img src='http://www.giraldatv.es/img/herramientas/rss.jpg' border=0 alt='rss'></a></div>";
    herramientas += "<div class='Sep'>&nbsp;</div>";

	$("#herramientas").html(herramientas);
}
