// NOTE A NEW FIRST ARGUMENT.
// IT IS REQUIRED TO GIVE PROPER CONTROL INSTRUCTIONS TO THE QTOBJECT THAT THE MOVIE IS PLAYING
// IF ANYBODY CAN THINK OF A BETTER WAY OF PUSHING THE QTOBJECTS INSTANCE NAME IN ITSELF PLEASE LET ME KNOW
QTObject = function(instanceName, mov, id, w, h) {
	this.mov 			= mov;
	this.id 			= id;
	this.width 			= w;
	this.height 		= h;
	this.redirect 		= "";
	this.sq 			= document.location.search.split("?")[1] || "";
	this.altTxt 		= "This content requires the QuickTime Plugin. <a href='http://www.apple.com/quicktime/download/'>Download QuickTime Player</a>.";
	this.bypassTxt 		= "<p>Already have QuickTime Player? <a href='?detectqt=false&"+ this.sq +"'>Click here.</a></p>";
	this.params 		= new Object();
	this.doDetect 		= getQueryParamValue('detectqt');
	// additions to qtobject js
	this.loopDaLoop		= false;
	this.instanceName   = instanceName;
	// qt controll extensions
	this.started		= false;
	this.playing 		= false;
	this.paused			= false;
	this.currentVolume  = 255;
	this._lastElapsed   = 0;
	this._monitorSOD = {
		_destroy:false, 
		_destroyed:true,
		ping : 500,
		onComplete: function(){},
		onPlayable: function(){},
		onError: function(){},
		onLoading: function(){},
		onWaiting: function(){},
		onPlaceholder: function(){}
	};
	this._monitorSO 	= {_destroy:false, _destroyed:true};
	this._monitorTOD 	= {
		_destroy:false, 
		_destroyed:true,
		ping : 100,
		onPlay: function(){},
		onPlaying: function(){},
		onStop: function(){},
		onStopped: function(){},
		onPause: function(){},
		onPaused: function(){},
		onEnd: function(){},
		keyframes: {}
	};
	this._monitorTO 	= {_destroy:false, _destroyed:true};
	this.monitoringStatus = false;	
	this.monitoringTimeline = false;	
	this.$movie			= null;
};

QTObject.prototype.addParam = function(name, value) {
	switch(name.toLowerCase())
	{
		case 'loop' :
			this.loopDaLoop = value;
			break;
	}
	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;
};

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 + '"';
        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 + '">';
        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());
			this.$movie = document.getElementById(this.id);
		}
	} 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() {
	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 "";
	}
};
