/*
 * 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();
 *
 */

var QTLoadedInterval;
var QTMAXTRIES=10;
var QTIntervalCount=0;
var timeOUTID;
var QTObject = function(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');

};

QTObject.prototype.addParam = function(name, value) {
    this.params[name] = value;
};

QTObject.prototype.getParams = function() {
    return this.params;
};

QTObject.prototype.setDefaultParams = function() {
    this.addParam("target", "myself");
    this.addParam("style","background-color:transparent;");
    this.addParam("autoplay","true");
    this.addParam("pluginspage","http://www.apple.com/quicktime");
};

//this must be called prior to the write
QTObject.prototype.showControls = function(val) {
    var ctrlVal = (val)?"true":"false";
    this.addParam("controller", ctrlVal);
};

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.refreshVideo = function(newMovie){
  this.mov = newMovie;
};

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(QTObject.isQTInstalled(elementId) || 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);
            }
        }
    }

    QTLoadedInterval = setInterval("QTObject.waitforQTAvail('"+this.id+"',"+this.width+","+this.height+")",500);
};


//moving the player div back in place
QTObject.setMovieVisible = function(qtPlayer,w,h)
{
     if(qtPlayer)
        try{qtPlayer.width=w; qtPlayer.height=h;}//the show hide is there for IE repaint issues}
         catch(ex){qtPlayer.setMovieInVisible()} //if there are no controls or movie, hide it.
};


//hiding the movie
QTObject.setMovieInVisible = function(qtPlayer)
{     if(qtPlayer)
        try{qtPlayer.width= qtPlayer.height=0;}
          catch(ex){}
};


QTObject.IsQTMovieLoaded = function(qtPlayer)
  {
return (qtPlayer)?(qtPlayer.GetMovieSize() == qtPlayer.GetMaxBytesLoaded()) :false; //GetMaxBytesLoaded() is a qt ref to the internal functions
  };

//isMovieValid checks to see if the movie kb is larger than 0
QTObject.isMovieValid = function(qtPlayer){
if(qtPlayer){
  var movieSize = new Number(qtPlayer.GetMovieSize());
//alert(qtPlayer.GetPluginStatus()+" GetMovieSize() "+qtPlayer.GetMovieSize()+" movieSize "+movieSize+" test "+!isNaN(movieSize)+" "+( movieSize>0));
  return (!isNaN(movieSize)&&( movieSize>0));
  }
    else return false;
};

 //i hate doing this but IE and 3rd party software such as QT & flash ... i feel so dirty
//function calls itself every half second and returns true when the qt object and its property is availble
QTObject.waitforQTAvail= function(playerID,w,h){
    var player= QTObject.getQTPlayer(playerID);
    if(QTIntervalCount>=QTMAXTRIES){clearInterval(QTLoadedInterval); QTIntervalCount=0; }
    else{//try to get a std property of the qt movie
    if(player)
        try{
            if(QTObject.isMovieValid(player)) {
                QTObject.setMovieVisible(player,(w > 0?w:'304'),(h > 0?h:'268'));
                clearInterval(QTLoadedInterval);
            }
            else {
                QTIntervalCount++;
            }
        }
        catch(ex){ if(QTIntervalCount==0)  QTIntervalCount++; }

        }
};//end function
   

QTObject.getQTPlayer = function(playerID)
{
  if(playerID==""||playerID==null)playerID='qtplayer';
    return document.getElementById(playerID);
}

QTObject.isQTInstalled = function() {
    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;
}
/*external utility functions*/

/* 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 "";
    }
}

