// see docs in this folder

var voCurrent = '';	// global: holds the ID of the object currently playing

VideoObject = function( id, w, h) {
	this.id = id;
	this.width = w;
	this.height = h;
	
	this.prefer = "";
	this.wm = this.qt = null;
	this.written = "";		// on output
}

VideoObject.prototype.addWindowsMedia = function( movFile) {
	// object name must match preference string
	this.prefer += "this.wm,";
	return this.wm = new WMObject( this, movFile);
}

VideoObject.prototype.addQuicktime = function( movFile) {
	this.prefer += "this.qt,";
	return this.qt = new QTObject( this, movFile);
}

VideoObject.prototype.write = function(elementId) {
	// first delete any current object playing
	if ( voCurrent.length > 0) {
		var vo = $(voCurrent);
		if ( vo)
			vo.parentNode.removeChild( vo);
	}
	
	// scan the preferences
	var prefs = this.prefer.split(","), pref, obj, gotIt=false;
	for ( var i=0, ct=prefs.length; i<ct && !gotIt; ++i) {
		pref = prefs[i];
		if ( pref.length > 0 && (obj = eval(pref))) {
			if ( obj.checkPlugin() ) {		// got it
				var ua = navigator.userAgent.toLowerCase(),
					isIE = (!(/opera|webtv/i.test(ua)) && /msie (\d)/.test(ua));
				var h = obj.getHTML( isIE);		// !(navigator.plugins && navigator.plugins.length)
				if (elementId)
					$(elementId).innerHTML = h;
				else
					document.write(h);
				gotIt = true;
				this.written = pref;
			}
		}
	}
	// no plugins -- show an error message
	if ( !gotIt)
		this.showErrorMsg();
	// windows media -- ensure we have version 9.0 (this only works in IE, and CRASHES!!!)
	else if( this.written == 'this.wm' && false) {
		try {
			var vo = $(this.id),
				vrs = vo.versionInfo, v, enough;
	
			// combine the first 2 version numbers into a 4-digit number
			vrsArray = vrs.split('.');
			v = vrsArray[0];
			if ( v.length == 1) v = '0'+v;
			vrs = v;
			v = vrsArray[1];
			if ( v.length == 1) v = '0'+v;
			vrs = vrs + v;
			
			enough = (parseInt(vrs) >= 900);

			if ( !enough) {
				var errStr = '<br><br><p>This site requires Windows Media Player version 9 or higher. ' +
							'Please download a free upgrade from ' +
							'<a href="http://www.microsoft.com/windows/windowsmedia/player/download/download.aspx" target="_blank">' +
							'Microsoft.com</a>.</p>';
				if (elementId)
					$(elementId).innerHTML += errStr;
				else
					document.write(errStr);
			}
		}
		catch(e) {}
	}
	
	if ( gotIt)	// remember the active object's ID
		voCurrent = this.id;
}

VideoObject.prototype.showErrorMsg = function(elementId) {
	var errStr = "This video requires ", objCt=0, pref, obj;
	for ( var i=0, ct=prefs.length; i<ct; ++i) {
		pref = prefs[i];
		if ( pref.length > 0 && (obj = eval(pref))) {
			if ( ++objCt > 1) errStr += " or ";
			errStr += "the " + obj.errorText();
		}
	}
	errStr += ".";
	if (elementId)
		$(elementId).innerHTML = errStr;
	else
		document.write(errStr);
}

// static member function -- pass in a WMObject or QTObject to get its params
VideoObject.prototype.getParamTags = function( vtypeObj) {
    var paramTags = "", allParams = vtypeObj.getParams();
    for (var param in allParams)
        paramTags += '<param name="' + param + '" value="' + vtypeObj.getParam(param) + '" />';
    return (paramTags == "" ? null : paramTags);
}

// static -- used by video classes
VideoObject.prototype.checkPlugin = function( pluginName, objectID) {
	var pluginInstalled = false;
	if (navigator.plugins && navigator.plugins.length) {
		for (var i=0, ct=navigator.plugins.length; i < ct; ++i ) {
			var plugin = navigator.plugins[i];
			if (plugin.name.indexOf(pluginName) > -1)
				pluginInstalled = true;
		}
	} else {
		plugObj = false;	// don't use VAR
		if ( window.execScript)
			window.execScript('on error resume next: plugObj = IsObject(CreateObject("' + objectID + '"))','VBScript');
		pluginInstalled = plugObj;
	}
	return pluginInstalled;
}

//==================

WMObject = function( vidObj, mov) {
	this.vObj = vidObj;	// parent
	this.mov = mov;		// filename
	this.params = new Object();
}

WMObject.prototype.addParam = function(name, value) { this.params[name] = value; }
WMObject.prototype.getParams = function() { return this.params; }
WMObject.prototype.getParam = function(name) { return this.params[name]; }
WMObject.prototype.checkPlugin = function() { return this.vObj.checkPlugin( 'Windows Media', 'MediaPlayer.MediaPlayer.1'); }
WMObject.prototype.errorText = function() { return "Windows Media plugin"; }

WMObject.prototype.getHTML = function( isIE) {
    var wmHTML = "", allParams = this.getParams(), ht=(this.vObj.height-0) + 83;	// size of controls
	if (isIE) {
		// wm6.4: CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95
		wmHTML += '<object classid="CLSID:6BF52A52-394A-11D3-B153-00C04F79FAA6"' +
			' codebase="http://www.microsoft.com/Windows/Downloads/Contents/MediaPlayer/"' +
			' width="' + this.vObj.width + '" height="' + ht + '" id="' + this.vObj.id + '"> ';
		this.addParam("URL", this.mov);
		this.addParam("uiMode", "full");
		wmHTML += this.vObj.getParamTags( this);
		wmHTML += ' </object>';
	}
	else {
		wmHTML += '<embed type="application/x-mplayer2" ' + 
			'pluginspage="http://www.microsoft.com/Windows/Downloads/Contents/MediaPlayer/" ' +
			'src="' + this.mov + '" width="' + this.vObj.width + '" height="' + ht + '" id="' + this.vObj.id + '"';
		this.addParam("URL", this.mov);
		this.addParam("uiMode", "full");
		for (var param in allParams)
			wmHTML += ' ' + param + '="' + this.getParam(param) + '"';
		wmHTML += '></embed>';
	}
	return wmHTML;
}

//==================


QTObject = function( vidObj, mov) {
	this.vObj = vidObj;	// parent
	this.mov = mov;		// filename
	this.params = new Object();
}

QTObject.prototype.addParam = function(name, value) { this.params[name] = value; }
QTObject.prototype.getParams = function() { return this.params; }
QTObject.prototype.getParam = function(name) { return this.params[name]; }
QTObject.prototype.checkPlugin = function() { return this.vObj.checkPlugin( 'QuickTime', 'QuickTimeCheckObject.QuickTimeCheck.1'); }
QTObject.prototype.errorText = function() { return "<a href='http://www.apple.com/quicktime/download/'>Quicktime plugin</a>"; }

QTObject.prototype.getHTML = function( isIE) {
    var qtHTML = "", allParams = this.getParams();
	if ( isIE) {
		qtHTML += '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" width="' + 
			this.vObj.width + '" height="' + (this.vObj.height + 16) + '" id="' + this.vObj.id + '">';
        this.addParam("src", this.mov);
		qtHTML += this.vObj.getParamTags( this);
        qtHTML += '</object>';
    }
    else {
		qtHTML += '<embed type="video/quicktime" src="' + this.mov + '" width="' +
			this.vObj.width + '" height="' + (this.vObj.height + 16) + '" id="' + this.vObj.id + '"';
		for (var param in allParams)
			qtHTML += ' ' + param + '="' + this.getParam(param) + '"';
        qtHTML += '></embed>';
    }
    return qtHTML;
}

