// JavaScript Document

// Globals

// Default Major version of Flash required
var requiredMajorVersion = 10;
// Minor version of Flash required
var requiredMinorVersion = 0;
// Revision of Flash required
var requiredRevision = 0;
// Base de URL para los Clips
var clipBaseUrlRaw = '';
var clipBaseUrl = encodeURIComponent(clipBaseUrlRaw);
// Base de URL para el reproductor
var playerBaseUrl = '';
var playerWidth = 400;
var playerHeight = 225;

/*****************************************************************/
/* toggleTab(baseName,number,total,classNormal,classActive) ver 1.0 - 28/05/09 */ 
/*****************************************************************/
function toggleTab(baseName,number,total,classNormal,classActive){
for (i=1;i<=total;i++)
	{
	document.getElementById(baseName+i).className = classNormal;
	};
document.getElementById(baseName+number).className = classActive;
};

/*****************************************************************/
/* switchDiv(divBaseName,divNumber,divTotal) ver 1.0 - 28/05/09 */ 
/*****************************************************************/
function switchDiv(divBaseName,divNumber,divTotal){
for (i=1;i<=divTotal;i++)
	{
	document.getElementById(divBaseName+i).style.display='none';
	} ;
document.getElementById(divBaseName+divNumber).style.display='block';
};

/*****************************************************************/
/* cclass(obj,id,newClass) ver 1.0 - 28/05/09 */ 
/*****************************************************************/
function cclass(obj,id,newClass) {
	if (obj==null) 
	{
	document.getElementById(id).className=newClass;
	}
	else { 
		obj.className=newClass;
		};
	};

/*****************************************************************/
/* changeDescription(idMain,idChild,content) ver 1.0 - 28/05/09 */ 
/*****************************************************************/
function changeDescription(idMain,idChild,content) {
document.getElementById(idChild).innerHTML = content;
if (!(document.getElementById(idMain).style.height=='0px')){
document.getElementById(idMain).style.height=document.getElementById(idChild).offsetHeight+"px";};
};

/*****************************************************************/
/* changeRating(valoracion,haSidoValorado) ver 1.0 - 20/08/09 */ 
/*****************************************************************/
function changeRating(valoracion,haSidoValorado){
var classTemp = '';
var valoracionInt = parseInt(valoracion);
var rateCode1  = '<div id="rateMe" style="margin:0px; padding:0px;">';
var rateCode2a = '<img src="plantillas/images/spcd.gif" width="12" height="13" class="';
var rateCode2b = '" onclick="rateIt(this)" id="_';
var rateCode2c = '" alt="current rating: ';
var rateCode2d = '" title="current rating: ';
var rateCode2e = '" onmouseover="ratingOver(this)" onmouseout="off(this,';
var rateCode2f = ')" />';
var rateCode3  = '</div>';

if(valoracion>0.5) {
	classTemp='starOn';
	var rateHTMLCode1 = rateCode2a+classTemp+rateCode2b+1+rateCode2c+valoracion+rateCode2d+valoracion+rateCode2e+valoracionInt+rateCode2f;
	} else {
	classTemp='';
	var rateHTMLCode1 = rateCode2a+classTemp+rateCode2b+1+rateCode2c+valoracion+rateCode2d+valoracion+rateCode2e+1+rateCode2f;
	};

if(valoracion>1.5){classTemp='starOn'} else {classTemp=''};
var rateHTMLCode2 = rateCode2a+classTemp+rateCode2b+2+rateCode2c+valoracion+rateCode2d+valoracion+rateCode2e+valoracionInt+rateCode2f;
if(valoracion>2.5){classTemp='starOn'} else {classTemp=''};
var rateHTMLCode3 = rateCode2a+classTemp+rateCode2b+3+rateCode2c+valoracion+rateCode2d+valoracion+rateCode2e+valoracionInt+rateCode2f;
if(valoracion>3.5){classTemp='starOn'} else {classTemp=''};
var rateHTMLCode4 = rateCode2a+classTemp+rateCode2b+4+rateCode2c+valoracion+rateCode2d+valoracion+rateCode2e+valoracionInt+rateCode2f;
if(valoracion>4.5){classTemp='starOn'} else {classTemp=''};
var rateHTMLCode5 = rateCode2a+classTemp+rateCode2b+5+rateCode2c+valoracion+rateCode2d+valoracion+rateCode2e+valoracionInt+rateCode2f;
document.getElementById('dynamicStat1').innerHTML = rateCode1+rateHTMLCode1+rateHTMLCode2+rateHTMLCode3+rateHTMLCode4+rateHTMLCode5+rateCode3;
if (haSidoValorado == null) { haSidoValorado = false};
if ((haSidoValorado)) {document.formRating.ratingField.value = 0} else {document.formRating.ratingField.value = parseInt(valoracion)};
	};

/*****************************************************************/
/* changeSharing(idMain,idClip,title) ver 1.0 - 28/05/09 */ 
/*****************************************************************/

function changeSharing(idMain,idClip,title) {
if (idClip==null) {idVideo=0};
var sharePreTitle = encodeURIComponent('Video: ');
var shareTitle = encodeURIComponent(title);
var shareButton1 = '<a rel="nofollow" href="http://www.facebook.com/sharer.php?u='+clipBaseUrl+idClip+'&amp;t='+sharePreTitle+shareTitle+'" target="_blank" ><img class="iconSocialFacebook" src="plantillas/images/spcd.gif" alt="Share on Facebook" title="Share on Facebook"/><\/a> ';
var shareButton2 = '<a rel="nofollow" href="http://www.myspace.com/Modules/PostTo/Pages/?u='+clipBaseUrl+idClip+'&amp;t='+sharePreTitle+shareTitle+'" target="_blank" ><img class="iconSocialMyspace" src="plantillas/images/spcd.gif" alt="Share on MySpace" title="Share on MySpace"/><\/a> ';
var shareButton3 = '<a rel="nofollow" href="http://digg.com/submit?phase=2&amp;url='+clipBaseUrl+idClip+'&amp;title='+sharePreTitle+shareTitle+'" target="_blank" ><img class="iconSocialDigg" src="plantillas/images/spcd.gif" alt="Digg it" title="Digg it"/><\/a> ';
var shareButton4 = '<a rel="nofollow" href="http://meneame.net/login.php?return=/submit.php?url='+clipBaseUrl+idClip+'&amp;title='+sharePreTitle+shareTitle+'" target="_blank" ><img class="iconSocialMeneame" src="plantillas/images/spcd.gif" alt="Share on Meneame" title="Share on Meneame"/><\/a> ';
var shareButton5 = '<a rel="nofollow" href="http://bookmarks.yahoo.com/toolbar/savebm?opener=tb&amp;u='+clipBaseUrl+idClip+'&amp;t='+sharePreTitle+shareTitle+'&amp;d=&amp;ei=UTF" target="_blank" ><img class="iconSocialYahoo" src="plantillas/images/spcd.gif" alt="Save in Yahoo" title="Save in Yahoo"/><\/a> ';
var shareButton6 = '<a rel="nofollow" href="http://twitter.com/home?status='+sharePreTitle+shareTitle+' '+clipBaseUrl+idClip+'" target="_blank" ><img class="iconSocialTwitter" src="plantillas/images/spcd.gif" alt="Tweet this" title="Tweet this"/><\/a> ';
var shareButton7 = '<a rel="nofollow" href="https://favorites.live.com/quickadd.aspx?marklet=1&amp;mkt=es&amp;url='+clipBaseUrl+idClip+'&amp;title='+sharePreTitle+shareTitle+'" target="_blank" ><img class="iconSocialLive" src="plantillas/images/spcd.gif" alt="Share on Live" title="Share on Live"/><\/a> ';
var shareButton8 = '<a rel="nofollow" href="http://www.google.com/bookmarks/mark?op=edit&output=popup&bkmk='+clipBaseUrl+idClip+'&amp;title='+sharePreTitle+shareTitle+'" target="_blank" ><img class="iconSocialGoogle" src="plantillas/images/spcd.gif" alt="Share on Google" title="Share on Google"/><\/a> ';
document.getElementById(idMain).innerHTML = shareButton1+shareButton2+shareButton3+shareButton4+shareButton5+shareButton6+shareButton7+shareButton8;
};


/*****************************************************************/
/* changeEmbed(idMain) ver 1.0 - 28/05/09 */ 
/*****************************************************************/

function changeEmbed(textField,idClip) {
// NOTA: Depende de las variables globales: playerWidth, playerHeight, playerBaseUrl
if (idClip==null) {idClip=0};
var embedHTMLCode1 = '<object width="'+playerWidth+'" height="'+playerHeight+'" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0" style="border:none" >';
var embedHTMLCode2 = '<param name="movie" value="'+playerBaseUrl+idClip+'"></param>';
var embedHTMLCode3 = '<param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><param name="SCALE" value="ExactFit"></param><param name="quality" value="High"></param><param name="loop" value="false"></param><param name="swliveconnect" value="true"></param><param name="wmode" value="opaque"></param>';
var embedHTMLCode4 = '<embed width="'+playerWidth+'" height="'+playerHeight+'" src="'+playerBaseUrl+idClip+'" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" allowscriptaccess="always" quality="High" loop="false" allowfullscreen="true" scale="exactFit" swliveconnect="true"></embed>';
var embedHTMLCode5 = '</object>';
textField.value = embedHTMLCode1+embedHTMLCode2+embedHTMLCode3+embedHTMLCode4+embedHTMLCode5;
};

/*****************************************************************/
/* changeVideoData(titulo,valoracion,reproducciones,fecha,descripcion,idClip,haSidoValorado,urlClip,urlPlayer) ver 1.1 - 12/02/10 */ 
/*****************************************************************/

function changeVideoData(titulo,valoracion,reproducciones,fecha,descripcion,idClip,haSidoValorado,urlClip,urlPlayer){
if (idClip==null) {idClip="indefinido"};
if (urlClip==null) {urlClip="*url clip indefinido* id="};
if (urlPlayer==null) {urlPlayer="*url player indefinido* id="};
clipBaseUrlRaw = urlClip;
clipBaseUrl = encodeURIComponent(clipBaseUrlRaw);
playerBaseUrl = urlPlayer;
// NOTA: Depende de las variables globales: clipBaseUrl
if ( titulo == null) titulo ="Clip not found";
tmpTitle = titulo;
tmpTitle = tmpTitle.replace (/&aacute;/g,"á");
tmpTitle = tmpTitle.replace (/&Aacute;/g,"Á");
tmpTitle = tmpTitle.replace (/&eacute;/g,"é");
tmpTitle = tmpTitle.replace (/&Eacute;/g,"É");
tmpTitle = tmpTitle.replace (/&iacute;/g,"í");
tmpTitle = tmpTitle.replace (/&Iacute;/g,"Í");
tmpTitle = tmpTitle.replace (/&oacute;/g,"ó");
tmpTitle = tmpTitle.replace (/&Oacute;/g,"Ó");
tmpTitle = tmpTitle.replace (/&uacute;/g,"ú");
tmpTitle = tmpTitle.replace (/&Uacute;/g,"Ú");
tmpTitle = tmpTitle.replace (/&agrave;/g,"à");
tmpTitle = tmpTitle.replace (/&Agrave;/g,"À");
tmpTitle = tmpTitle.replace (/&egrave;/g,"è");
tmpTitle = tmpTitle.replace (/&Egrave;/g,"È");
tmpTitle = tmpTitle.replace (/&igrave;/g,"ì");
tmpTitle = tmpTitle.replace (/&Igrave;/g,"Ì");
tmpTitle = tmpTitle.replace (/&ograve;/g,"ò");
tmpTitle = tmpTitle.replace (/&Ograve;/g,"Ò");
tmpTitle = tmpTitle.replace (/&ugrave;/g,"ù");
tmpTitle = tmpTitle.replace (/&Ugrave;/g,"Ù");
tmpTitle = tmpTitle.replace (/&ntilde;/g,"ñ");
tmpTitle = tmpTitle.replace (/&Ntilde;/g,"Ñ");
tmpTitle = tmpTitle.replace (/&uuml;/g,"ü");
tmpTitle = tmpTitle.replace (/&Uuml;/g,"Ü");
tmpTitle = tmpTitle.replace (/&iexcl;/g,"¡");
tmpTitle = tmpTitle.replace (/&iquest;/g,"¿");
//document.title = tmpTitle;
document.getElementById('dynamicTitle').innerHTML = titulo;
if (!( fecha == null)) changeRating(valoracion,haSidoValorado);
document.formRating.ratingIdClip.value = idClip;
if ( reproducciones == null) reproducciones ="";
document.getElementById('dynamicStat2').innerHTML = reproducciones;
if ( fecha == null) fecha ="";
document.getElementById('dynamicStat3').innerHTML = fecha;
if ( descripcion == null) descripcion ="";
changeDescription('videoDescriptionCont','dynamicDescription',descripcion);
document.videoShareForm.shareLink.value = clipBaseUrlRaw+idClip;
document.videoShareForm.shareTitle.value = titulo;
changeEmbed(document.videoShareForm.shareEmbed,idClip);
changeSharing('dynamicShare',idClip,titulo);
if ( fecha == "") document.videoShareForm.shareLink.value ="";
if ( fecha == "") document.videoShareForm.shareEmbed.value ="";
};

/*****************************************************************/
/* getUrlParams() ver 1.0 - 20/08/09 */
/*****************************************************************/

//Global
var urlParams = new Array();

function getUrlParams() {
var idx = document.URL.indexOf('?');
if (idx != -1) {
	var pairs = document.URL.substring(idx+1, document.URL.length).split('&');
	for (var i=0; i<pairs.length; i++) {
		nameVal = pairs[i].split('=');
		urlParams[nameVal[0]] = nameVal[1];
   };
};
return urlParams;
};

/*****************************************************************/
/* inclVideo(url,w,h,id,wmode) ver 1.0 - 20/08/09 */
/*****************************************************************/

function inclVideo(url,w,h,id,wmode,idForAlternateContent,requiredFlash){
	if (!(requiredFlash == null)) requiredMajorVersion = requiredFlash;
	var hasRightVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);
	if(hasRightVersion) {  
	// if we've detected an acceptable version
	if (wmode == null) wmode= "transparent";
	document.write('<object classid="clsid:D27CDB6E-AE6D-11CF-96B8-444553540000" id="');
	document.write(id);	
	document.write('" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=10,0,0,0" border="0" width="');
	document.write(w);
	document.write('" height="');
	document.write(h);
	document.write('" >\n');
	document.write('<param name="movie" value="');
	document.write(url);
	document.write('">\n');
	document.write('<param name="SCALE" value="ExactFit">\n');
	document.write('<param name="quality" value="High">\n');
	document.write('<param name="loop" value="false">\n');
	document.write('<param name="swliveconnect" value="true">\n');
	document.write('<param name="allowScriptAccess" value="always">\n');
	document.write('<param name="allowFullScreen" value="true">\n');
	document.write('<param name="wmode" value="');
	document.write(wmode);
	document.write('">\n');
	document.write('<embed src="');
	document.write(url);
	document.write('" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" name="');
	document.write(id);	
	document.write('" width="');
	document.write(w);
	document.write('" height="');
	document.write(h);
	document.write('" quality="High" loop="false" allowFullScreen="true" allowScriptAccess="always" scale="exactFit" swliveconnect="true" wmode="');
	document.write(wmode);
	document.write('"></object>\n');
	}
	else {  
		// flash is too old or we can't detect the plugin
		var alternateContent = '<br /><br /><br />You need Flash Player 10 or higher to playback videos, <a href="http://get.adobe.com/flashplayer" target="_blank">download it here</a><br /><br /><br /><br />';
		document.getElementById(idForAlternateContent).innerHTML=alternateContent;
		//document.writeln(alternateContent);  // insert non-flash content
	};
};

/*****************************************************************/
/* fixSingleQuote(field) ver 1.0 - 16/11/09 */
/*****************************************************************/
function fixSingleQuote(field) {
var tmpStr = field.value;
field.value = tmpStr.replace (/''/g,"'");
var tmpStr = field.value;
field.value = tmpStr.replace (/'/g,"''");
}

/*****************************************************************/
/* filterText(obj,objid) ver 1.1 - 29/4/10 */
/*****************************************************************/
function filterText(obj,objid) {
if((obj == null) && (objid != null)) 
	{ 
	textField = document.getElementById(objid); 
	}
else if ((obj != null) && (objid == null)) 
	{ 
	textField = obj; 
	}
var tmpStr = textField.value;
tmpStr = tmpStr.replace (/'/g," ");
tmpStr = tmpStr.replace (/%/g,"");
tmpStr = tmpStr.replace (/=/g,"");
tmpStr = tmpStr.replace (/http:/gi,"");
tmpStr = tmpStr.replace (/https:/gi,"");
tmpStr = tmpStr.replace (/ftp:/gi,"");
tmpStr = tmpStr.replace (/rtmp:/gi,"");
tmpStr = tmpStr.replace (/rtsp:/gi,"");
tmpStr = tmpStr.replace (/:/g,"");
tmpStr = tmpStr.replace (/,/g,"");
tmpStr = tmpStr.replace (/;/g,"");
tmpStr = tmpStr.replace (/"/g,"");
tmpStr = tmpStr.replace (/\//g,"");
tmpStr = tmpStr.replace (/\\/g,"");
tmpStr = tmpStr.replace (/\?/g,"");
tmpStr = tmpStr.replace (/\!/g,"");
tmpStr = tmpStr.replace (/\$/g,"");
tmpStr = tmpStr.replace (/\>/g,"");
tmpStr = tmpStr.replace (/\</g,"");
tmpStr = tmpStr.replace (/\#/g,"");
tmpStr = tmpStr.replace (/\@/g,"");
tmpStr = tmpStr.replace (/\(/g,"");
tmpStr = tmpStr.replace (/\)/g,"");
tmpStr = tmpStr.replace (/&/g,"");
tmpStr = tmpStr.replace (/\*/g,"");
tmpStr = tmpStr.replace (/\[/g,"");
tmpStr = tmpStr.replace (/\]/g,"");
tmpStr = tmpStr.replace (/\{/g,"");
tmpStr = tmpStr.replace (/\}/g,"");
textField.value = tmpStr;
}

/*****************************************************************/
/* adjustChannelButtons() ver 1.0 - 16/3/10 */
/*****************************************************************/
function adjustChannelButtons(){
	var channelBarWidth = document.getElementById("menuItems").offsetWidth;

	var moreChannelsButton = document.getElementById("buttonMoreChannels");
	var lessChannelsButton = document.getElementById("buttonLessChannels");

	if (moreChannelsButton.style.display == "block"){
		var w1 = moreChannelsButton.offsetWidth + 15;
		}
	else {
		var w1 = lessChannelsButton.offsetWidth + 5;
		}

	var homeButton = document.getElementById("homeButton");

	if (homeButton.style.display == "block"){
		var w2 = document.getElementById("homeButton").offsetWidth + 5;
		}
	else {
		homeButton.style.display == "block";
		var w2 = document.getElementById("homeButton").offsetWidth + 5;
		homeButton.style.display == "none";
		}
	var channelBarAvailWidth = channelBarWidth - w1 - w2;
	//alert(channelBarAvailWidth);
	
	var buttonsArray = document.getElementById("menuItems").getElementsByTagName("li");
	//alert(buttonsArray.length);
	var tmpButtonWidth = 0;
	var numberOfDisplayedButtons = 0;

	for (i=0; i<buttonsArray.length-4; i++){ 
    	buttonsArray[i].style.display="block"; 
		tmpButtonWidth = buttonsArray[i].offsetWidth;
		if ((channelBarAvailWidth - tmpButtonWidth) <= 0 ) 
			{
			buttonsArray[i].style.display="none"; 
			break;
			}
		else { 
			channelBarAvailWidth = channelBarAvailWidth - tmpButtonWidth; 
			numberOfDisplayedButtons++;
			}
		//alert(channelBarAvailWidth);
		//alert(numberOfDisplayedButtons);
	} 

	var secondaryChannelBar = document.getElementById("menuButtonGroupMore");
	var secondaryButtonsArray = secondaryChannelBar.getElementsByTagName("li");
	var numberOfSecondaryButtons = secondaryButtonsArray.length;

	if ( numberOfDisplayedButtons == numberOfSecondaryButtons ) 
		{
		document.getElementById("buttonMoreChannels").style.display="none";
		document.getElementById("buttonLessChannels").style.display="none";
		document.getElementById("menuItemsMoreCont").style.display="none";
		}

	for (i=0; i<numberOfDisplayedButtons; i++)
		{ 
	    	secondaryButtonsArray[i].style.display="none"; 
		}

}
