var WM_v7 = 7;
var WM_v8 = 8;
var WM_v9 = 9;
var WM_v10 = 10;

MODEM = 56;
RDSI = 128;
ADSL_128 = 128;
ADSL_512 = 512;
ADSL_1024 = 1024;
ADSL_2048 = 2048;
ADSL_4096 = 4096;

function Speed(fileLocation, fileSize, imgTagProperties)
{
  this.fileLocation = fileLocation;
  this.fileSize = fileSize;
  this.imgTagProperties = imgTagProperties;
  this.connectionSpeed = -1;
  this.Speed_drawImage = Speed_drawImage;
  this.Speed_computeConnectionSpeed = Speed_computeConnectionSpeed;
  this.connectionMode ="";
  return this;
}


function Speed_drawImage() {
	start = (new Date()).getTime();
	loc = this.fileLocation + '?t=' + escape(start);
	var oInnerHtml = document.getElementById('speedConnect');
	oInnerHtml.innerHTML = '<img border="0" width="0" height="0" src="' + loc + '" ' + this.imgTagProperties + ' onload=Speed_computeConnectionSpeed(' + start + ',' + this.fileSize + ');>';
	return;
}

function connectionType(speed) {
	
	if (speed) {
		if (speed.connectionSpeed <= MODEM) {
			this.connectionMode=56; //"Slow Modem";
		} else if (speed.connectionSpeed <= RDSI) {
			this.connectionMode=128; //"Fast Modem";
		} else if (speed.connectionSpeed <= ADSL_128) {
			this.connectionMode=128; //"Fast Modem";
		} else if (speed.connectionSpeed <= ADSL_512) {
			this.connectionMode=512; // "ADSL 128";
		} else if (speed.connectionSpeed <= ADSL_1024) {
			this.connectionMode=1024; //"ADSL 1024";	
		} else if (speed.connectionSpeed <= ADSL_2048) {
			this.connectionMode=2048; // "ADSL 2048";
		} else if (speed.connectionSpeed <= ADSL_4096) {
			this.connectionMode=4096; // "ADSL 4096";	
		} else {
			this.connectionMode=512; //"ADSL > 4Mb";
		}
	} else {
		this.connectionMode=512;
	}
	return 
}

function Speed_computeConnectionSpeed(start, fileSize ) {
	end = (new Date()).getTime();
	this.connectionSpeed = (Math.floor((((fileSize * 8) / ((end - start) / 1000)) / 1024) * 10) / 10);
	connectionType(this);
	return;
	//this.connectionSpeed;
}

function showSpeed() {	
	return this;
}


var windowsmedia=new Object();
windowsmedia.installed=false;
windowsmedia.version='0.0'; // Internet Explorer-only

var gkoaxwma=false;
if (navigator.plugins && navigator.plugins.length) { for (x=0; x<navigator.plugins.length; x++) { if (navigator.plugins[x].name.indexOf('ActiveX') != -1 && window.GeckoActiveXObject) { gkoaxwma=true; break; } } }

// Create an ActiveX/GeckoActiveX constructor
function AXO(id)
{
	var error; var control = null;
	try {
		if (window.ActiveXObject && navigator.userAgent.indexOf('Win') != -1) control = new ActiveXObject(id);
		else if (gkoaxwma) control = new GeckoActiveXObject(id);
	}
	catch (error) {}
	return control;
}

if (window.ActiveXObject || gkoaxwma){
	try{
		oWMP=new AXO('WMPlayer.OCX.7');
		if (oWMP)
		{
			windowsmedia.installed=true;
			parseFloat(oWMP.versionInfo);

			windowsmedia.version=parseFloat(oWMP.versionInfo);
			if (windowsmedia.version.toString().length == 1) windowsmedia.version+='.0';
		}
	}
	catch(e) {}
}

// Internet Explorer or GeckoActiveXObject-compatible browsers only.
if(windowsmedia.installed && parseInt(windowsmedia.version) >= 10){
	windowsmedia.ver = WM_v10;	
}else if(windowsmedia.installed && parseInt(windowsmedia.version) >= 9){
	windowsmedia.ver = WM_v9;
}else if(windowsmedia.installed && parseInt(windowsmedia.version) >= 8){
	windowsmedia.ver = WM_v8;
}else if(windowsmedia.installed && parseInt(windowsmedia.version) >= 7){
	windowsmedia.ver = WM_v7;
}

//Dectecta el navegador
function Browser(){
	this.BrowserGet = BrowserGet;
}

function BrowserGet(){
	browser = false;
	if (navigator.appName.indexOf('Microsoft') != -1)
		browser = "IE";
	else if(navigator.appName.indexOf('Netscape') != -1)
		browser = "NS";
	else
		browser ="OTHER";
	return browser;
	
}

detectableWithVB = false;

// Here we write out the VBScript block for MSIE Windows
if ((navigator.userAgent.indexOf('MSIE') != -1) && (navigator.userAgent.indexOf('Win') != -1)) {    
    document.writeln('<scr' + 'ipt language="VBScr' + 'ipt">');

    document.writeln('\'do a one-time test for a version of VBScript that can handle this code');
    document.writeln('detectableWithVB = False');
    document.writeln('If ScriptEngineMajorVersion >= 2 then');
    document.writeln('  detectableWithVB = True');
    document.writeln('End If');

    document.writeln('\'this next function will detect most plugins');
    document.writeln('Function detectActiveXControl(activeXControlName)');
    document.writeln('  on error resume next');
    document.writeln('  detectActiveXControl = False');
    document.writeln('  If detectableWithVB Then');
    document.writeln('     detectActiveXControl = IsObject(CreateObject(activeXControlName))');
    document.writeln('  End If');
    document.writeln('End Function');
    document.writeln('</scr' + 'ipt>');
}


//Detecta Plugins
function DetecPlugins(){
	this.detectFlash = detectFlash;
	this.detectReal = detectReal;
	this.detectWindowsMedia = detectWindowsMedia;
}

function detectFlash() {
    pluginFound = detectPlugin('Shockwave','Flash'); 
    // if not found, try to detect with VisualBasic
    if(!pluginFound && detectableWithVB) {
		pluginFound = detectActiveXControl('ShockwaveFlash.ShockwaveFlash.1');
    }
    return pluginFound;
}


function detectReal() {
    pluginFound = detectPlugin('RealPlayer');
    if(!pluginFound && detectableWithVB) {
	pluginFound = (detectActiveXControl('rmocx.RealPlayer G2 Control') ||
		       detectActiveXControl('RealPlayer.RealPlayer(tm) ActiveX Control (32-bit)') ||
		       detectActiveXControl('RealVideo.RealVideo(tm) ActiveX Control (32-bit)'));
    }	
    return pluginFound;
}

function detectWindowsMedia() {
    pluginFound = detectPlugin('Windows Media');
    if(!pluginFound) {
		pluginFound = windowsmedia.installed;
    }
    return pluginFound;
}

function detectPlugin() {
    // allow for multiple checks in a single pass
    var daPlugins = detectPlugin.arguments;
    // consider pluginFound to be false until proven true
    var pluginFound = false;
    // if plugins array is there and not fake
    if (navigator.plugins && navigator.plugins.length > 0) {
	var pluginsArrayLength = navigator.plugins.length;
	// for each plugin...
	for (pluginsArrayCounter=0; pluginsArrayCounter < pluginsArrayLength; pluginsArrayCounter++ ) {
	    // loop through all desired names and check each against the current plugin name
	    var numFound = 0;
	    for(namesCounter=0; namesCounter < daPlugins.length; namesCounter++) {
		// if desired plugin name is found in either plugin name or description
		
		if( (navigator.plugins[pluginsArrayCounter].name.indexOf(daPlugins[namesCounter]) >= 0) || 
		    (navigator.plugins[pluginsArrayCounter].description.indexOf(daPlugins[namesCounter]) >= 0) ) {
		    // this name was found
		    numFound++;
		}   
	    }
	    // now that we have checked all the required names against this one plugin,
	    // if the number we found matches the total number provided then we were successful
	    if(numFound == daPlugins.length) {
		pluginFound = true;
		// if we've found the plugin, we can stop looking through at the rest of the plugins
		break;
	    }
	}
    }
    return pluginFound;
} // detectPlugin



//Guarda todos los parametros en un Objetos.
function Values(){
	var BROWSER = false;
	var FLASH = false;
	var WM = false;
	var WMVersion = 0;
	var RO = false;
	var WM_URLVideo = "";
	var RO_URLVideo = "";
	
	oBrowser = new Browser();
	oDetectPlugins = new DetecPlugins();
	this.BROWSER = oBrowser.BrowserGet();
	this.FLASH = oDetectPlugins.detectFlash(); 
	this.WM = oDetectPlugins.detectWindowsMedia();
	this.WMVersion = windowsmedia.ver;
	this.RO = oDetectPlugins.detectReal();
	return;
}


//	Windows Media para IE
function WM_IEObj(width, height, classid, autostart, showcontrols, oValues)
{
  this.width = width;
  this.height = height;
  this.classid = classid;
  this.autostart = autostart;
  this.showcontrols = showcontrols;
  this.oValues = oValues;
  this.WM_IEObjToHtml = WM_IEObjToHtml;
  return this;
}

function WM_IEObjToHtml()
{
  var codHTML = '';
  codHTML += '<OBJECT ID="windowmedia1" width=' + this.width + ' height=' +this.height+ ' classid="' +this.classid+'" ';
  if(oValues.WMVersion < WM_v9){
  	codHTML += 'CODEBASE="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715" ';
  }
  codHTML += 'standby="Loading Microsoft® Windows® Media Player components..." ';
  codHTML += 'type="application/x-oleobject"> ';
  codHTML += '<PARAM NAME="AutoStart" VALUE="' + this.autostart + '"> ';
  if(oValues.WMVersion < WM_v9){  	
  	codHTML += '<PARAM NAME="FileName" VALUE="' + this.oValues.WM_URLVideo + '"> ';
  }else{
  	codHTML += '<PARAM NAME="URL" VALUE="' + this.oValues.WM_URLVideo + '"> ';
	codHTML += '<PARAM NAME="uiMode" VALUE="none"> ';
  }
  codHTML += '<PARAM NAME="ShowControls" VALUE="' + this.showcontrols + '"> ';
  codHTML += '<PARAM NAME="ShowStatusBar" VALUE="false"> ';
  
  //NS
  var showcontrols = (this.showcontrols=="true") ? "1" : "0" ;
  var autostart = (this.autostart=="true") ? "1" : "0" ;
  codHTML += '<EMBED type="application/x-mplayer2" ';
  codHTML += 'pluginspage="http://www.microsoft.com/Windows/MediaPlayer/" ';
  codHTML += 'SRC="' + oValues.WM_URLVideo + '" ';
  codHTML += 'name="windowmedia1" ';
  codHTML += 'width="' + this.width + '" ';
  codHTML += 'height="' + this.height + '" ';
  codHTML += 'autostart="' + autostart + '" ';
  codHTML += 'showcontrols="' + showcontrols + '"> ';
  codHTML += '</OBJECT> ';
  return codHTML;
}

//	Real One
function RO_IEObj(width, height, classid, autostart, showcontrols, oValues)
{
  this.width = width;
  this.height = height;
  this.classid = classid;
  this.autostart = autostart;
  this.showcontrols = showcontrols;
  this.oValues = oValues;
  this.RO_IEObjToHtml = RO_IEObjToHtml;
  return this;
}

function RO_IEObjToHtml()
{
  var codHTML = '';
  if(oValues.BROWSER == 'IE'){
    codHTML += '<OBJECT ID="realOne" width=' +this.width+ ' height=' +this.height+ ' classid="' +this.classid+ '"> ';
    codHTML += '<PARAM NAME="autostart" VALUE="' +this.autostart+ '"> ';
    codHTML += '<PARAM NAME="controls" VALUE="ImageWindow"> ';
    codHTML += '<PARAM NAME="console" VALUE="realOne"> ';
    codHTML += '<PARAM NAME="src" VALUE="' +this.oValues.RO_URLVideo+ '"> ';
  }else{
    codHTML += '<EMBED name=realOneEmbed ';
    codHTML += 'src="' +this.oValues.RO_URLVideo+'" ';
    codHTML += 'type="audio/x-pn-realaudio-plugin" console="one" controls="ImageWindow" ';
    codHTML += 'width="' +this.width +'" '; 
    codHTML += 'height="' +this.height +'" ';
    codHTML += 'autostart="' +this.autostart + '"></EMBED>';
    codHTML += '</OBJECT> ';
  }
  return codHTML;
}







//Arranca el video nada mas terminar de cargarlo
function WmStartVideo(oValues){
	if(oValues.BROWSER == 'IE'){
		if(oValues.WMVersion < WM_v9){
			document.windowmedia1.open(oValues.WM_URLVideo);
		}else{
			document.windowmedia1.controls.play();
		}
	}else{
		document.windowmedia1.src = oValues.WM_URLVideo;
	}
}

//Windows Media Controles de Video
function WMControls(){
	this.WMControlPlay =  WMControlPlay;
	this.WMControlStop =  WMControlStop;
	this.WMControlPause =  WMControlPause;
	this.WMControlFullScreen = WMControlFullScreen;
	oValues = new Values();
	return this;
}

function WMControlPlay(){
	if(oValues.BROWSER == 'IE'){
		if(oValues.WMVersion < WM_v9){
			document.windowmedia1.play();
		}else{
			document.windowmedia1.controls.play();
		}
	}else{
	    document.windowmedia1.controls.play();
	}
}

function WMControlStop(){
	if(oValues.BROWSER == 'IE'){
		if(oValues.WMVersion < WM_v9){
			document.windowmedia1.stop();
		}else{
			document.windowmedia1.controls.stop();
		}
	}else{
		document.getElementById(windowmedia1).stop();
	}
}

function WMControlPause(){
	if(oValues.BROWSER == 'IE'){
		if(oValues.WMVersion < WM_v9){
			try{
			document.windowmedia1.pause();
			}catch(e){}
		}else{
			document.windowmedia1.controls.pause()
		}
	}else{
		//document.windowmedia1.controls.stop();
		//document.windowmedia1.src = this.urlVideo;
	}
}

function WMControlFullScreen(){
	if(oValues.BROWSER == 'IE'){
		if(oValues.WMVersion < WM_v9){
			document.windowmedia1.DisplaySize = 3
		}else{
			document.windowmedia1.fullscreen = true;
		}
	}else{
		document.windowmedia1.fullscreen = true;
	}
}

//Real One Controles de Video
function ROControls(){
	this.ROControlPlay =  ROControlPlay;
	this.ROControlStop =  ROControlStop;
	this.ROControlPause =  ROControlPause;
	this.ROControlFullScreen = ROControlFullScreen;
	oValues = new Values();
	return this;
}

	    
function ROControlPlay(){
	if(oValues.BROWSER == 'IE'){
		document.realOne.DoPlay();
	}else{
	    document.realOneEmbed.DoPlay;
	}
}

function ROControlStop(){
	if(oValues.BROWSER == 'IE'){
		document.realOne.DoStop();
	}else{
	    document.realOneEmbed.DoStop;
	}
}

function ROControlPause(){
	if(oValues.BROWSER == 'IE'){
		document.realOne.DoPlayPause();
	}else{
	    document.realOneEmbed.DoPlayPause;
	}
}

function ROControlFullScreen(){
	if(oValues.BROWSER == 'IE'){
		document.realOne.SetFullScreen();
	}else{
	    document.realOneEmbed.SetFullScreen;
	}
}