/* 
	VideoObject
	
		7/05, Jeff Bellsey
		Based on code and ideas from Geoff Stearns:
			http://blog.deconcept.com/2005/01/26/web-standards-compliant-javascript-quicktime-detect-and-embed/
	
	javascript code for dynamically placing videos.
	currently handles Windows Media and Quicktime; can
	easily be extended to include RealPlayer.
	
	Usage:

		function placeVideo() {
			var vid = new VideoObject( 'myvideoID', 240, 180),
				wm = vid.addWindowsMedia( 'my-movie.wmv'),
				qt = vid.addQuicktime( 'my-movie.mov');
			vid.write('video');
		}
	
	this function can then be called directly in a SCRIPT tag,
	or on an onclick event handler.

	the order you add videos in becomes the order of preference.
	i.e., if the user has both plugins, it will choose the WM version
	first in the above example.
	
	params can be added to the sub-objects (WMObject, etc), but not
	currently to the main parent object.
	
	in the WRITE method, pass in the ID of the elt whose innerHTML
	you want to replace. if you don't pass in an ID, we use doc.write (in place).
	
	this hasn't been tested yet, but you should be able to 
	override the showErrorMsg method, if needed.
	
	
	TO DO:
	
		- add a master "checkPlugins" function, to test before clicking
		- quicktime plugin doesn't work in IE/windows. it just hangs.
			(REVISED: it works when called asynchronously: at least via ajax)
		- windows media doesn't autostart in IE/windows.
			(REVISED: it works when called asynchronously: either setTimeout, or ajax)
*/

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) {
	// 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 h = obj.getHTML( !(navigator.plugins && navigator.plugins.length));
				if (elementId)
					document.getElementById(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)
	else if( this.written == 'this.wm' && false) {
		try {
			var vo = document.getElementById(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)
					document.getElementById(elementId).innerHTML += errStr;
				else
					document.write(errStr);
			}
		}
		catch(e) {}
	}
}

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)
		document.getElementById(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
		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) + 44;	// 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;
}


