
if ( document.createEvent) { // create click() function if necessary (for FF etc)
  HTMLElement.prototype.click = function() {
  var evt = this.ownerDocument.createEvent('MouseEvents');
  evt.initMouseEvent('click', true, true, this.ownerDocument.defaultView, 1, 0, 0, 0, 0, false, false, false, false, 0, null);
  this.dispatchEvent(evt);
  }
}


function chooseClip(e){

  var targ;
  if (!e) var e = window.event; // define e under ms event accessing model
  if (e.target) targ = e.target; // define target under w3c/netscape model
  else if (e.srcElement) targ = e.srcElement; // define target under microsoft model
  if (targ.nodeType == 3) { targ = targ.parentNode; } // defeat Safari bug

  // set title
  document.getElementById('cliptitle').innerHTML = targ.innerHTML;

  // Set alternative (no flash) content
  document.getElementById('flashcontent').innerHTML = 'Sorry, you need to <a href="http:\/\/www.adobe.com\/shockwave\/download\/download.cgi?P1_Prod_Version=ShockwaveFlash">install the free Adobe Flash Plugin<\/a> to view this content.'

  // Derive correct link to flash movie from youtube href
  var clipRef;
  rExp = /watch\?/gi; clipRef = targ.href.replace(rExp, '');
  rExp = /=/gi;       clipRef = clipRef.replace(rExp, '/');

  // set up required clip using swf object
  var so = new SWFObject(clipRef, "mymovie", "425", "355", "8", "");
  so.addParam("wmode", "transparent");
  so.addVariable("rel", "0");           // for YouTube clips; value 0 is used to prevent display of stupid links screen when clip ends.
  so.addVariable("color1", "0x330000"); // for YouTube clips; 1st color for controlbar gradient
  so.addVariable("color2", "0x880000"); // for YouTube clips; 2nd color for controlbar gradient
  so.addVariable("autoplay", "1");      // for YouTube clips; 1=autoplay, 0=no autoplay
  so.write("flashcontent");

  // display clip block, which is initially hidden
  document.getElementById('flashcontent').style.display = 'block';

}


// initialisation function
function init() {
  // attach onclick=play function to each clip link
  var cliplinks = getElementsByClass('vidclip','a');
  for (i=0; i<cliplinks.length; i++) {
    cliplinks[i].onclick = function(e) { chooseClip(e); return false; }
  }
  // check if we're using an urlstring to autoplay a specific clip
  if (location.search > '') {
    qsplit=location.search.split('=');
    urlstring1=qsplit[0];
    urlstring2=qsplit[1];
    o=document.getElementById(urlstring2);
    if (o && urlstring1 == '?clip') {
      o.click();
    }
  }
}


// generic getElementsByClass function
function getElementsByClass(searchClass,tag,node) {
  var classElements = new Array();
  if ( node == null ) { node = document; }
  if ( tag == null )  { tag = '*';       }
  var els = node.getElementsByTagName(tag);
  var elsLen = els.length;
  var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
  for (i = 0, j = 0; i < elsLen; i++) {
    if ( pattern.test(els[i].className) ) {
      classElements[j] = els[i];
      j++;
    }
  }
  return classElements;
}


// generic add event function
function addEvent(obj, evType, fn) {
 if (obj.addEventListener) { obj.addEventListener(evType, fn, false);  return true; }
 else if (obj.attachEvent) { var r = obj.attachEvent("on"+evType, fn); return r;    }
 else                      { return false; }
}


addEvent(window, 'load', init); // add initialisation function onload
