function load_reel(){

  if (data == null){return true}
  var clipSelect = document.getElementById('clipSelect');

  var html = '';
  var img;
  for (var i = 0; i < data.length; i++ ) {
    var clip = data[i];
    var img_path = clip.img;
    html += '<img src="' + img_path + '" ';
    html += 'onmouseover=hover_info(' + i + '); ';
    html += 'onmouseout=hover_info(-1); ';
    if ( i == data.length - 1 ) {
      html += 'onload=showme(); ';
    }
    html += 'onclick=play_clip(' + i + ');>';

//      img = document.createElement('img');
//      img.setAttribute('onmouseover',"hover_info('"+i+"');");
//      img.setAttribute('onmouseout',"hover_info(-1);");
//      img.setAttribute('onclick',"play_clip('"+i+"');");
//      img.setAttribute('src',img_path);
//      img.src = img_path;
//      clipSelect.appendChild(img);
  }
  clipSelect.innerHTML = html;
  //  alert(html);

  var clip = data[data.length-1];
  //  img.setAttribute('onload',"showme();");
  //  document.player.SetResetPropertiesOnReload(false);
}

function showme() {

  var player = document.getElementById('player');
  player.style.visibility = 'visible';
  var mywindow = window.setInterval(update_player,1000);

  play_all();
  hover_info(-1);
}

function update_player(){

  // find out if we are at the end of a movie
  if ( document.player == null ) { return true }
  //  alert('stop');

  // get duration and current time and see if we are really close to end
  var status = document.player.GetPluginStatus();
  if ( ( status != "Playable" ) && ( status != "Complete" ) ) { return true };

  var duration = document.player.GetDuration();
  var time = document.player.GetTime();
  //window.status = "duration: " + duration + " time: " + time + " playone: " + playone;

  // if at the end of a clip and playall select update current and play
  if ( duration < time + 500 ) {
    document.player.Stop();
    if ( !playone ) {
      play_clip((current + 1), true);
    }
    //document.player.Stop();
    //document.player.Play();
  }
}

function clip_info(i) {

  if (data == null){return true}
  var clip = data[i];
  if (clip == null){return true}

  // update the clip information
  var product = document.getElementById('clipProduct');
  var title = document.getElementById('clipTitle');
  var editor_name = document.getElementById('editorName');
  var editor_email = document.getElementById('editorEmail');
  var director_name = document.getElementById('directorName');
  var director_title = document.getElementById('directorTitle');
  product.innerHTML = clip.product.toLowerCase();
  title.innerHTML = ('"' + clip.title + '"').toLowerCase();
  editor_name.innerHTML = clip.editor.toLowerCase();
  editor_email.innerHTML = clip.email.toLowerCase();
  editor_email.href = 'mailto:' + clip.email.toLowerCase();

  // clear out director if not set to anything
  if ( clip.director == null ) {
    director_title.innerHTML = '';
    director_name.innerHTML = '';
  }
  else {
    director_title.innerHTML = 'director';
    director_name.innerHTML = clip.director.toLowerCase();
  }
}

function play_clip(i, playall) {

  if (data == null){return true}
  if ( document.player != null ) { document.player.Stop() };

  // update current
  current = i;

  // if user clicked on spot only play that one spot
  if ( playall != true ) {playone = true;}

  // change clip information 
  clip_info(i);

  // change quicktime player movie
  var clip = data[i];
  if (clip == null){return true}
  if (clip.mov == null){return true}
  var qt = get_qt( clip.mov );
  var clipPlay = document.getElementById('clipPlay');
  //  var qtPlay = document.getElementById('qtPlay');
  //  if ( qtPlay != null ) {
    //    clipPlay.removeChild(qtPlay);
  clipPlay.style.visibility = 'hidden';
    //  }
  clipPlay.innerHTML = qt;
  clipPlay.style.visibility = 'visible';
}

function play_all() {

  if (data == null){return true}

  // reset current and play first clip
  playone = false;
  current = 0;
  play_clip(0, true);
}

function get_qt(mov) {

   var qt = '';
//   qt += '<object classid="clsid: 02BF25D5..." ...>\n';
//   qt += '<param name="src" value="' + mov + '"/>\n';
//   qt += '<param name="width value="480"/>\n';
//   qt += '<param name="height value="376"/>\n';
//   qt += '<param name="scale" value="aspect"/>\n';
//   qt += '<param name="enablejavascript" value="true"/>\n';
//   qt += '<param name="cache" value="true"/>\n';
//   qt += '<param name="kioskmode" value="true"/>\n';
//   qt += '<param name="showlogo" value="false"/>\n';
//   qt += '<param name="bgcolor" value="000000"/>\n';

//   qt += '<div id=qtPlay>';
   qt += '<embed name="player" width="480" height="376 scale="aspect"';
   qt += 'enablejavascript="true" cache="true" kioskmode="true"';
   qt += 'showlogo="false" bgcolor="000000" src="' + mov + '">';
   //   qt += '</div>';

//   qt += '</object>\n';

//  var qt = QT_GenerateOBJECTText( mov, '480', '376', '',
//                                  'emb#name', 'player');
  //  alert(qt);

  return qt;
}

function hover_info(i) {

  if (data == null){return true}

  // get heading elements to set
  var product = document.getElementById('hoverProduct');
  var title = document.getElementById('hoverTitle');
  if (i == -1 ) {
    product.innerHTML = '';
    title.innerHTML = '';
  }
  else {
    var clip = data[i];
    product.innerHTML = clip.product.toLowerCase();
    title.innerHTML = ('"' + clip.title + '"').toLowerCase();
  }
}

function download() {

  // stop the movie and download from download.php on cuttersmedia.com
  document.player.Stop();
  var mov = '' + data[current].mov;
  mov = mov.replace(/^.*[\/\\]/g,'');
  mov = "http://www.cuttersmedia.com/download.php?filename=" + mov;
  top.location.href = mov;
}

function go_back() {

  // stop the movie and switch to the home page
  document.player.Stop();
  window.location = "http://www.cutters.com/flash_web/main.html";
}

function send_link (){

  // stop the movie and open up the email interface
  document.player.Stop();
  (window.open('r?a=e&ea=c&reel=' + reel + '&title_input=' + title_input,
               '', 'height=600,width=600')).focus();
}

