function linkspecial() {
   if(document.getElementById && document.createTextNode) {
      var as=document.getElementsByTagName('a');
      for (var i=0;i<as.length;i++) {
         var href = as[i].getAttribute('href');
         var rel = as[i].getAttribute('rel');
         var rev = as[i].getAttribute('rev');
         var type = as[i].getAttribute('type');
	 if (rel == 'hidden'){
	    as[i].onclick=function(){show(this); return false}
	    as[i].onkeypress=function(){show(this);return false}
	    }
	 if (rel == 'clip'){
	    as[i].onclick=function(){play(this, true, false);return false}
	    as[i].onkeypress=function(){play(this, true, false);return false}
            }
         if (rel == 'track'){
            as[i].onclick=function(){play(this, true, true); return false}
            as[i].onkeypress=function(){play(this, true, true); return false}
            }
         if (rel == 'background'){
            as[i].onclick=function(){play(this, false);return false}
            as[i].onkeypress=function(){play(this, false);return false}
            }
         if (rel == 'movie' || href.match(/http:\/\/www.youtube.com\/watch.*/)){
            as[i].onclick=function(){film(this);return false}
            as[i].onkeypress=function(){film(this);return false}
            }
         if (rel == 'download'){
            var url = as[i].href;
            as[i].href = 'http://www.veritones.com/cgi-bin/download.php?filename=' + url;  
            }
         if (rel == 'ajax'){
            as[i].onclick=function(){load(this);return false;}
            as[i].onkeypress=function(){load(this);return false;}              
            }
         if (rev == 'thumbnail'){
            as[i].onclick=function(){display(this);return false}
            as[i].onkeypress=function(){display(this);return false}
            }
         if (type == 'mouseover'){
            as[i].onmouseover=function(){rollover(this, true);}
            as[i].onmouseout=function(){rollover(this, false);}
            }
         }
      hidem();
      if (document.getElementById('start')){
         var start = document.getElementById('start');
         start.style.display = 'block';
         start.style.position = 'relative';
         start.style.visibility = 'visible';
         }
      }
   }

function rollover(anchor, on){
   var images = anchor.getElementsByTagName('img');
   var image = images[0].src.match(/[\/|\\]([^\\\/]+)$/);
   var position = images[0].src.length - image[1].length;
   var root = images[0].src.substring(0, position);
   var id = images[0].id;
   var image_split = image[1].split('.');
   if (on){
      var new_image = id + '_hover.' + image_split[1];
      } else {
      var new_image = id + '.' + image_split[1];
      }
   var new_source = root + new_image;
   images[0].src = new_source;
   }


function show(section) {
   hidem();
   section = section.href.match(/#(\w.+)/)[1];
   section = document.getElementById(section);
   section.style.display = 'block';
   section.style.position = 'relative';
   section.style.visibility = 'visible';
   window.scrollTo(0,0);
   }

function hidem() {
   var dv=document.getElementsByTagName('div');
   for(var i=0;i<dv.length;i++){
      var x = dv[i].className;
      if(x.match('hidden')){
         dv[i].style.display = 'none';
         dv[i].style.visibility = 'hidden';
	}
      }
   }

function play(clip, controller, download) {
   hidem();
   var div = document.getElementById('clip');
   //while(div.hasChildNodes()){
   //   div.removeChild(div.firstChild);
   //   }
   div.innerHTML = "";
   
   
   var label = document.createTextNode('Download Clip');
   var addition = new Array(1);

   addition[0] = document.createElement('embed');
   addition[1] = document.createElement('a');

   addition[0].id = 'audio_player';

   addition[0].setAttribute('src', 'http://www.veritones.com/cgi-bin/streaming_audio.php?clip=' + clip);
   if(controller){ 
      addition[0].setAttribute('autostart','false'); 
      addition[0].setAttribute('width', '300');
      addition[0].setAttribute('height', '16');
      }
   else { 
      addition[0].setAttribute('autostart', 'true'); 
      addition[0].setAttribute('width', '0');
      addition[0].setAttribute('height', '0');
      addition[0].setAttribute('ShowControls', 'false');
      addition[0].setAttribute('ShowStatusBar', 'false');
      }
   addition[0].setAttribute('type', 'audio/mpeg');
   addition[0].setAttribute('loop','false');
   addition[0].setAttribute('wmode', 'transparent');
   addition[0].setAttribute('controller', controller);
   addition[1].setAttribute('href', 'http://www.veritones.com/cgi-bin/download.php?filename=' + clip);
   div.appendChild(addition[0]);
   if(download){ div.appendChild(addition[1]); }
   addition[1].appendChild(label);
   if(controller){
     div.style.display = 'block';
     div.style.visibility = 'visible';
     }
   }

function film(movie) {
  hidem();
   var screen = document.getElementById('movie');
   while(screen.hasChildNodes()){
      screen.removeChild(screen.firstChild);
      }
   screen.innerHTML = '';
   screen.style.display = 'block';
   screen.style.visibility = 'visible';
   
   var youtube_id = movie.href.match(/v=([^&]*)$|v=([^&]*)&/);

   var url = 'http://www.youtube.com/v/' + youtube_id[1];
   var object;
  
   if(navigator.plugins && navigator.mimeTypes && navigator.mimeTypes.length){
      var object = document.createElement('embed');
      object.setAttribute('type', 'application/x-shockwave-flash');
      object.setAttribute('src', url);
      object.setAttribute('width',425);
      object.setAttribute('height',335);
      object.setAttribute('wmode', 'transparent');
      screen.appendChild(object);
      }else{
      object = '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="425" height="335"><param name="movie" value="' + url + '" /><param name="wmode" value="transparent" /></object>';
      screen.innerHTML = object;
      }
   }

function load(request){
   var url = request.href;
   var area = document.getElementById(request.target);

   var ajax = (window.ActiveXObject) ? new ActiveXObject('Microsoft.XMLHTTP') : new XMLHttpRequest();
   ajax.open('GET', url, true);
   ajax.onreadystatechange = function(){
      if(ajax.readyState == 4 && ajax.status == 200){
         area.innerHTML = ajax.responseText;
         }
      }
   ajax.send(null);
   }
         

function display(linkedPhoto){
   var info = linkedPhoto.getElementsByTagName('span');
   var title = info[0].firstChild.nodeValue;
   var frame = document.getElementById('frame');
   var caption = document.getElementById('caption');
   var object = linkedPhoto.getAttribute('object');
   var id = linkedPhoto.id;
   var secret = linkedPhoto.getAttribute('secret');
   var description = linkedPhoto.getAttribute('description');
   var image_id = "photo#" + id;
   var image = document.getElementById(image_id);

   var message = document.getElementById('gallery_message');
   if (message) { frame.removeChild(message); }
 
   while(caption.hasChildNodes()){
      caption.removeChild(caption.firstChild);
      }
// These lines are required for Opera and Safari to refresh the 
// first-letter of the caption, which has been set apart by
// a CSS pseudo-class.
   caption.style.display = 'none';
   caption.style.display = 'block';

   if (description !== null){
      var message = document.createTextNode(description);
      caption.appendChild(message);
      }


   if (!image && (id && secret)) {
      var url = "http://www.hcs.harvard.edu/~veritone/gallery.php?photo=" + id + "&secret=" + secret;
      var request = (window.ActiveXObject) ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
      
      var image = document.createElement('img');
      image.src = linkedPhoto;
      image.id = image_id;
      image.setAttribute('name', title);
      image.setAttribute('alt', title);
      image.style.opacity = 0;
      var anchor = document.createElement('a');
      anchor.setAttribute('href', object);
      anchor.appendChild(image);

      request.open("GET", url, true);
      request.onreadystatechange = function(){
        if (request.readyState == 4 && request.status == 200){
            if (request.responseText !== null){
               var description = document.createTextNode(request.responseText);
               caption.appendChild(description);
               linkedPhoto.setAttribute('description', request.responseText);
               var check = update_height(image.id, 'calculate');
               if(check) { update_height(image.id, 'execute'); }
               }
            }
         }
      request.send(null);


      frame.appendChild(anchor);
      }
   var images = frame.getElementsByTagName('img');
   var browser = detect(navigator.userAgent);
   for (var i = 0; i < images.length ; i++) {
      if (images[i].style.opacity  == 1){
//         if (browser !== 'Safari'){
            fade(images[i].id, 0, 100, 'out');
//            }else{
//            images[i].style.opacity = 0;
//            } 
         }
      }
   var check = update_height(image.id, 'calculate');
   if (check) { 
      update_height(image.id, 'execute');
      }
   if (browser !== 'Safari'){
      fade(image.id, 0, 100, 'in');
      }else {
      image.style.opacity = 1;
      }
   if (!check) { 
      setTimeout("update_height('x', 'delay')", 2100);
      }
   }

function fade(id, startfade, endfade, direction) {
   timer = 0;
   var image = document.getElementById(id);
   if (startfade < endfade) {
      for (i = startfade; i <= endfade; i++) {
         if (direction == 'in'){
            setTimeout("setOpacity(" + i + ",'" + id + "')", (timer * 20));
            timer++;
            image.style.visibility = 'visible';
            }
         if (direction == 'out'){
            var opacity = 100 - i;
            setTimeout("setOpacity(" + opacity + ",'" + id + "')", (timer * 20));
            timer++;
            }
         }
      }
   }

function setOpacity(opacity, id) {
   var css = document.getElementById(id).style;
   css.opacity = (opacity / 100);
   css.MozOpacity = (opacity /100);
   css.KhtmlOpacity = (opacity / 100);
   css.filter = "alpha(opacity=" + opacity + ")";
   }

function update_height(id, action){
   if (action == 'delay'){
      var frame = document.getElementById('frame');
      var images = frame.getElementsByTagName('img');
      for (var i = 0; i < images.length ; i++){
         var opacity = images[i].style.opacity;
         if (opacity == 1){
         id = images[i].id;
         }
         }
      }
   var image = document.getElementById(id);
   var height = image.offsetHeight;
   var new_height = height + 10;
   var caption = document.getElementById('caption');
   var content = document.getElementById('content');
   var content_height = content.offsetHeight;
   var caption_height = caption.offsetHeight;

     caption.style.top = new_height + 'px';
     new_height = height + caption_height + 180;
     if (new_height < 550) { new_height = 550; }
   if (action == 'calculate'){
      if (new_height >= content_height) { return true; }
      if (new_height < content_height) { return false; }
      }
   if (action == 'execute' || action == 'delay'){
      content.style.height = new_height + 'px';
      }
   }

function detect(agent) {
   var browsers = new Array('Safari', 'Omniweb', 'Opera', 'Webtv', 'Icab', 'MSIE', 'Firefox', 'compatible');

   for(i = 0; i < browsers.length; i++){
      var browser = browsers[i];
      var detected = agent.indexOf(browser);
      if (detected !== -1){
         return browser;
         }
      }
   }
