//defined in the template and is a hack anyway.
//var active_clip_mid = false;

function toggle_hover(elem,hover) {
    if (elem.id == active_clip_mid) {
        return;
    } else if (hover) {
        elem.style.borderBottom = '3px solid #c7d0dd';
    } else {
        elem.style.borderBottom = '3px solid white';
    }
}

function playVideo(id, qt_url, win_url, w, h)
{
    if (isQTInstalled()) {
        showQuickTime(id, qt_url, w, h);
    } else {
        showWindowsMedia(id, win_url, w, h);
    }
}

function DB_selectClip(mid, id, play)
{
    // gets the clip info and sets it to an array
    // info[0] = title
    // info[1] = caption
    // info[2] = image path
    // info[3] = link to mov preview
    // info[4] = link to wmv preview
    // info[5] = link to download mov
    // info[6] = link to download WMV
    // info[7] = link to download DV
    // info[8] = media clip id
    // info[9] = size of clip DV
    // info[10] = runtime of video
    // info[11] = type of video
    var info = DB_getClipInfo(mid);

    //document.getElementById(active_clip_mid).style.borderBottom = '3px solid white';
    //document.getElementById(mid+'_thumb').style.borderBottom = '3px solid red';
    //active_clip_mid = mid+'_thumb';

    // default arg
    play = typeof(play) != 'undefined' ? play : false;

    // create the image tag
    var img = document.createElement('img');
    img.src = info[2];
    // define the image tag
    //ugh
    var imgHTML = '<img onclick="DB_selectClip(\''+mid+'\',\''+id+'\','+'true)" src="'+info[2]+'"/>';
    // write the new image into the page
    var preview = document.getElementById('preview');
    preview.innerHTML = imgHTML;

    // change the title text to the clips title
    var title = document.getElementById('videoTitle');
    //title.innerHTML = info[0];

    // change the caption to the clips caption
    var caption = document.getElementById('caption');
    //caption.innerHTML = info[1];

    var pvplay = document.getElementById('pvPlay');
    var self = window.location.href+'#';
    self = self.replace('##', '#');
    //pvplay.innerHTML = '<span><a href="'+self+'" onclick="DB_selectClip(\''+mid+'\',\''+id+'\',true)"><img alt="Preview Video" src="/design/images/icons/actions/play.gif" title="Preview Video" /> Play Preview ('+info[10]+'m)</a></span>'

    // change the QT Preview action to view the clip
    var dlMOV = document.getElementById('dlMOV');
    //dlMOV.innerHTML = '<span><a href="'+info[5]+'"><img alt="Download Quicktime streaming" src="/design/images/icons/actions/download_mp4.gif" title="Download Quicktime streaming" /> Quicktime [Streaming]</a></span>';

    // change the QT Preview action to view the clip
    var dlWMV = document.getElementById('dlWMV');
    //dlWMV.innerHTML = '<span><a href="'+info[6]+'"><img alt="Download Windows Media Streaming" src="/design/images/icons/actions/download_wmv.gif" title="Download Windows Media streaming" /> Windows Media [Streaming]</a></span>';

    // change the DV download action to download the clip
    var dlDV = document.getElementById('dlDV');
    if ( info[11] == 'broadcast') {
    //dlDV.innerHTML = '<span><a href="'+info[7]+'"><img alt="Broadcast" src="/design/images/icons/actions/download_dv.gif" title="Download Broadcast ('+info[9]+')" /> Broadcast ('+info[9]+')</a></span>';
    } else {
        //dlDV.innerHTML = '<span><div class="unavailable"><img alt="Broadcast icon" src="/design/images/icons/actions/download_dv.gif" />Broadcast</div></span>';
    }

    // window.alert(document.getElementById(info[8]).attributes[0].properties);

    var ids = DB_getIDs();
    for( var nid in ids ) {
        var el = document.getElementById(ids[nid]);
        el.setAttribute("style", "color: #555555");
        // document.getElementById(ids[nid]).style.color="grey";
        if (info[8] == ids[nid]) {
            var la = document.getElementById(ids[nid]);
            la.setAttribute("style", "color: #FF0000");
            // document.getElementById(ids[nid]).style.color="red";
        }
    }

    var installed = isQTInstalled();

    if (play == true) {
        if (installed == true) {
            showQuickTime(id, info[3]);
        } else {
            showWindowsMedia(id, info[4]);
        }
    }

}

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

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.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 + '" autoplay="true" 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;
    }
}

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;
}

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 = "<a href='http://www.apple.com/quicktime/download/'><img src='/images/video/no_plugin.gif' width='320' height='240' alt='You do not have QuickTime installed'></a>";
            } else {
                document.write("<a href='http://www.apple.com/quicktime/download/'><img src='/images/video/no_plugin.gif' width='320' height='240' alt='You do not have QuickTime installed'></a>");
            }
        }
    }
}

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

function showQuickTime(id, mov_url) {

    var myQTObject = new QTObject(mov_url ,1 , "360", "304");
    myQTObject.addParam("target", "myself");
    myQTObject.addParam("controller", "true");
    myQTObject.addParam("bgcolor","#FFFFFF");
    myQTObject.write(id);

}

/*******************************************************
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/
*******************************************************/

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;
            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;


function showWindowsMedia(id, wmv_url) {
    var elementId = id;
    var thefile = "<embed src= '"+wmv_url+"', width='360' height='333' autostart='true'></embed>";

    if(windowsmedia.installed) {
        if (elementId) {
            document.getElementById(elementId).innerHTML = thefile;
        } else {
            document.write(thefile);
        }
    } else {
            if (elementId) {
                document.getElementById(elementId).innerHTML = "<a href='http://www.windowsmediaplayer.com'><img src='/images/video/no_plugin.gif' width='320' height='240' alt='You do not have Windows Media Player installed'></a>";
            } else {
                document.write("<a href='http://www.windowsmediaplayer.com'><img src='/images/video/no_plugin.gif' width='320' height='240' alt='You do not have Windows Media Player installed'></a>");
            }
        }
}

