/*
 * QTObject embed
 * http://blog.deconcept.com/2005/01/26/web-standards-compliant-javascript-quicktime-detect-and-embed/
 *
 * by Geoff Stearns (geoff@deconcept.com, http://www.deconcept.com/)
 *
 * v1.0.2 - 02-16-2005
 *
 * Embeds a quicktime movie to the page, includes plugin detection
 *
 * Usage:
 *
 *	myQTObject = new QTObject("path/to/mov.mov", "movid", "width", "height");
 * myQTObject.altTxt = "Upgrade your Quicktime Player!"; // optional
 
 *  myQTObject.addParam("controller", "false");              // optional
 *	myQTObject.write();
 *
 */

//http://www.sean.co.uk/a/webdesign/javascriptdelay.shtm
//http://www.codeproject.com/jscript/quotesfix.asp
//delayclosewindow('video', 1000);
QTObject = function(jpg, id, w, h, mov) {
	this.mov = jpg;
	this.id = id;
	this.width = w;
	this.height = h;
	this.redirect = "";
	this.sq = document.location.search.split("?")[1] || "";
	regexp = /mov$/i;
	wmv = mov.replace(regexp, "wmv");
//	this.altTxt = "<center><table><tbody><tr><td onclick='swap_in_video(this, \""+wmv+"\", 275, 196)' style=\"cursor: pointer;\"><img src=\""+jpg+"\" border=\"0\"><br></td></tr></tbody></table></center>"
	this.altTxt = "<center><table><tbody><tr><td onclick='swap_in_video(this, \""+wmv+"\", 320, 240)' style=\"cursor: pointer;\"><img src=\""+jpg+"\" border=\"0\"><br></td></tr></tbody></table></center>"
	this.bypassTxt = ""
	this.params = new Object();
	this.params['href'] = mov;
	this.doDetect = getQueryParamValue('detectqt');
}

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.getParamTags = function() {
    var paramTags = "";
    for (var param in this.getParams()) {
        paramTags += '<param name="' + param + '" value="' + this.getParam(param) + '" />';
    }
    if (paramTags == "") {
        paramTags = null;
    }
    return paramTags;
}

//onMouseover="alert(\'Hi there!\');"
QTObject.prototype.getHTML = function() {
    var qtHTML = "";
	if (navigator.plugins && navigator.plugins.length) { // not ie
        qtHTML += '<embed type="video/quicktime" src="' + this.mov + '" width="' + this.width + '" height="' + this.height + '" id="' + this.id + '"';

// dave messing around <--
//        qtHTML += '<embed type="video/quicktime" scale="aspect" controller="true" width="275" src="' + this.mov + '"';
// -->

        for (var param in this.getParams()) {
            qtHTML += ' ' + param + '="' + this.getParam(param) + '"';
        }
        qtHTML += '></embed>';
    }
    else { // pc ie
        qtHTML += '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" width="' + this.width + '" height="' + this.height + '" id="' + this.id + '">';

// dave messing around <--
//        qtHTML += '<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" scale="aspect" width="275" ' + '" id="' + this.id + '">';
// -->

        this.addParam("src", this.mov);
        if (this.getParamTags() != null) {
            qtHTML += this.getParamTags();
        }
        qtHTML += '</object>';
    }
    return qtHTML;
}


QTObject.prototype.getVariablePairs = function() {
    var variablePairs = new Array();
    for (var name in this.getVariables()) {
        variablePairs.push(name + "=" + escape(this.getVariable(name)));
    }
    if (variablePairs.length > 0) {
        return variablePairs.join("&");
    }
    else {
        return null;
    }
}

QTObject.prototype.write = function(elementId) {
	if(isQTInstalled() || this.doDetect=='false') {
		if (elementId) {
			document.getElementById(elementId).innerHTML = this.getHTML();
		} else {
			document.write(this.getHTML());
		}
	} else {
		if (this.redirect != "") {
			document.location.replace(this.redirect);
		} else {
			if (elementId) {
				document.getElementById(elementId).innerHTML = this.altTxt +""+ this.bypassTxt;
			} else {
				document.write(this.altTxt +""+ this.bypassTxt);
			}
		}
	}
}

function isQTInstalled() {
//uncomment the next line to temporarily disable qt support for testing
//	return false;

	var qtInstalled = false;
	qtObj = false;
	if (navigator.plugins && navigator.plugins.length) {
		for (var i=0; i < navigator.plugins.length; i++ ) {
         var plugin = navigator.plugins[i];
         if (plugin.name.indexOf("QuickTime") > -1) {
			qtInstalled = true;
         }
      }
	} else {
		execScript('on error resume next: qtObj = IsObject(CreateObject("QuickTimeCheckObject.QuickTimeCheck.1"))','VBScript');
		qtInstalled = qtObj;
	}
	return qtInstalled;
}

/* get value of querystring param */
function getQueryParamValue(param) {
	var q = document.location.search;
	var detectIndex = q.indexOf(param);
	var endIndex = (q.indexOf("&", detectIndex) != -1) ? q.indexOf("&", detectIndex) : q.length;
	if(q.length > 1 && detectIndex != -1) {
		return q.substring(q.indexOf("=", detectIndex)+1, endIndex);
	} else {
		return "";
	}
}

/*******************************************************
WINDOWS MEDIA DETECT
All code by Ryan Parman, unless otherwise noted.
(c) 1997-2003, Ryan Parman
http://www.skyzyx.com
Distributed according to SkyGPL 2.1, http://www.skyzyx.com/license/
http://www.skyzyx.com/scripts/windowsmedia.js
*******************************************************/

// warning, this may not be the original version of this code, please
// get the original code from:
//   http://www.skyzyx.com/scripts/windowsmedia.js

var windowsmedia=new Object();

// Set some base values
windowsmedia.installed=false;
windowsmedia.version='0.0'; // Internet Explorer-only

// Check for GeckoActiveXObject and co-inciding Plug-In
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;

			// A wierd bug in the Gecko ActiveX plug-in will return
			// undefined at the first call, but the correct value on the second.
			// This "fix" doesn't seem to hurt IE at all.
			parseFloat(oWMP.versionInfo);

			windowsmedia.version=parseFloat(oWMP.versionInfo);
			if (windowsmedia.version.toString().length == 1) windowsmedia.version+='.0';
		}
	}
	catch(e) {}
}
else if (navigator.plugins && navigator.plugins.length)
{
	for (x=0; x<navigator.plugins.length; x++)
	{
		if (navigator.plugins[x].name.indexOf('Windows Media') != -1)
		{
			windowsmedia.installed=true;
			break;
		}
	}
}

// Internet Explorer or GeckoActiveXObject-compatible browsers only.
windowsmedia.ver7=(windowsmedia.installed && parseInt(windowsmedia.version) >= 7) ? true:false;
windowsmedia.ver8=(windowsmedia.installed && parseInt(windowsmedia.version) >= 8) ? true:false;
windowsmedia.ver9=(windowsmedia.installed && parseInt(windowsmedia.version) >= 9) ? true:false;

/*
 * code for swapping in wmv from:
 *   http://www.dembloggers.com/story/2005/7/19/232324/915
 * plus wmv detect code from:
 *   http://www.skyzyx.com/scripts/
 */

function swap_in_video(swap_target, video_url, w, h)
{
	//note: wmv is 240x180, we could probably stretch or something?
	//the 1x15 image and the break compensates for the 15 pixel quicktime control

	//the below code only works with IE, so don't use it
	// if (windowsmedia.ver9) { alert("ie only hack"); }
	// alert("windowsmedia.version");
	if (windowsmedia.installed) {
		swap_target.innerHTML = '<img src="/static/video/1x15.gif" />'+
			'<object id="MediaPlayer" width="'+w+'" height="'+h+'" '+
			'classid="CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95" '+
			'standby="Loading Windows Media Player components..." '+
			'type="application/x-oleobject"><param name="filename" '+
			'value="'+video_url+'"/><param name="autoStart" '+
			'value="true"/><param name="ShowStatusBar" value="true"/>'+
			'<param name="Volume" value="1"/><param name="ShowTracker" '+
			'value="false"/><param name="ShowAudioControls" '+
			'value="false"/><param name="ShowPositionControls" '+ 
			'value="false"/><embed type="application/x-mplayer2" src="'+
			video_url+'" width="'+w+'" height="'+h+'" autoStart="true" '+
			'ShowStatusBar="true" Volume="0" ShowTracker="false" '+
			'ShowAudioControls="false" '+
			'ShowPositionControls="false"></embed></object>';
	} else {
		regexp = /wmv$/i;
		mov_url = video_url.replace(regexp, "mov");
		// Windows Media is NOT installed.  Do something else.
		swap_target.innerHTML = '<div class="content">'+
			'<img src="/static/video/1x15.gif" />'+
			'<table border="1" width="'+w+'" height="'+h+'">'+
			'<p>We cannot detect your media player.'+
			'You must install either <a href="http://www.apple.com/quicktime/download/">Apple Quicktime</a> or <a href="http://www.microsoft.com/windows/windowsmedia/">Windows Media Player</a> to watch this and other <i>Media Matters for America</i> videos.</p>'+
			'<p>If you have one of these programs, please try one of the following links:'+
			'<ul><li><a href="'+video_url+'">Windows Media video</a></li>'+
			'<li><a href="'+mov_url+'">Quicktime video</a></li></ul></p></div>';
	}
}

