// showpic.js

var mediaIsVideo = true;

// 0: stopped
// 1: paused
// 2: playing
// 6: loading

function PlayClick() {
 updateStatus();
 if (!MediaPlayer1.HasError && loaded) {
  document.all.b_play.src="../common/play_n.gif";
  document.all.b_pause.src="../common/pause_d.gif";
  document.all.b_stop.src="../common/stop_d.gif";
  document.MediaPlayer1.Play();
 }
}

function StopClick() {
 updateStatus();
 if (!MediaPlayer1.HasError && loaded) {
  document.all.b_play.src="../common/play_d.gif";
  document.all.b_pause.src="../common/pause_d.gif";
  document.all.b_stop.src="../common/stop_n.gif";
  document.MediaPlayer1.Stop();
  document.MediaPlayer1.CurrentPosition=0;
 }
}

function PauseClick(){
 updateStatus();
 if (!MediaPlayer1.HasError && loaded) {
  if (document.MediaPlayer1.PlayState == 2) {
   document.all.b_play.src="../common/play_d.gif";
   document.all.b_pause.src="../common/pause_n.gif";
   document.all.b_stop.src="../common/stop_d.gif";
   document.MediaPlayer1.Pause();
  }
 }
}

function incVol (level) {
 var inc = 500;
 var new_volume = MediaPlayer1.volume + (inc * level);
 if (new_volume < -4000)
  new_volume = -4000;
 else if (new_volume > -0)
  new_volume = 0;
 MediaPlayer1.volume = new_volume;
 return;
}			

function updateStatus() {

 origHeight = 200;
 origWidth = 200;

 if (MediaPlayer1.HasError) {
//  alert(MediaPlayer1.ErrorDescription+' - Please wait for video to load before pressing play.');
  document.status = "Error: " + MediaPlayer1.ErrorCode + " - " + MediaPlayer1.ErrorDescription;
  document.status = "<a name='#' title='Error: " + MediaPlayer1.ErrorCode + " - " + MediaPlayer1.ErrorDescription + "'>Play Failed - Media Error</a>";
//  setTimeout("updateStatus()", 1000);
  return;
 }
 var sStatus;
 // Store correct video size
 if (origHeight==0)
  origHeight = MediaPlayer1.ImageSourceHeight;
 if (origWidth==0)
  origWidth = MediaPlayer1.ImageSourceWidth;
 // Oversize protection
 if ((origWidth > 360) || (origHeight > 270)) {
  origWidth = 360;
  origHeight = 270;
 }

 // Initial resize
 if ((origWidth > 0) && (origHeight > 0) && (MediaPlayer1.height == 200) && (MediaPlayer1.width == 200)) {
  MediaPlayer1.width = origWidth;
  MediaPlayer1.height = origHeight;
 }
 if (MediaPlayer1.PlayState == 1) {
  sStatus = "Paused";
  nMode = 2;
 }
 else if (MediaPlayer1.PlayState == 0) {
  sStatus = "Stopped 00:00";
  bInitBuffer = true;
  if (false && mediaIsVideo) {
   if (cover.style.visibility != 'visible')
    cover.style.visibility  = 'visible';
   player.style.visibility = 'hidden';
  }
  nMode = 0;
 }
 else if (((MediaPlayer1.BufferingProgress < 100) && (MediaPlayer1.BufferingProgress > 0)) && bInitBuffer) {
  sStatus = "Buffering: " + MediaPlayer1.BufferingProgress + "%";
  if (false && mediaIsVideo) {
   if (cover.style.visibility != 'visible')
    cover.style.visibility  = 'visible';
   player.style.visibility = 'hidden';
  }
  nMode = 1;
 }
 else if ((MediaPlayer1.PlayState == 2)) {
  if ((MediaPlayer1.BufferingProgress == 100) && (MediaPlayer1.CurrentPosition > 0))
   bInitBuffer = false;
  sStatus = formatTimePad(MediaPlayer1.CurrentPosition) + " (" + formatTimePad(MediaPlayer1.Duration) + ")" + " - " + Math.round((MediaPlayer1.Bandwidth / 1024)) + "K Bits/s";
  player.style.visibility = 'visible';
  cover.style.visibility  = 'hidden';
  nMode = 1;
 }
 else if (MediaPlayer1.OpenState < 6) {
  sStatus = "Connecting...";
  nMode = 1;
 } 
 else {
  sStatus = "";
 }

 if (MediaPlayer1.PlayState > 2)
  loaded = false;
 else
  loaded = true;

 if (loaded && !started) {
  if (document.MediaPlayer1.PlayState == 2) {
   document.all.b_play.src="../common/play_n.gif";
   document.all.b_pause.src="../common/pause_d.gif";
   document.all.b_stop.src="../common/stop_d.gif";
  }
  started = 1;
 }

 // Update the status display
 // document.all.divStatus.innerHTML = sStatus;
 status = sStatus;
 setTimeout("updateStatus()", 500);
}

function formatTimePad(nS) {
 var n = nS;
 var sT1, sT2;
 var nMinutes = Math.floor(n / 60);
 n = n - (nMinutes * 60);
 var nSeconds = Math.floor(n);
 // append an extra zero where applicable
 if (nMinutes < 10) { sT1 = "0";  } else { sT1 = ""; }
 if (nSeconds < 10) { sT2 = "0";  } else { sT2 = ""; }
 return sT1 + nMinutes + ":" + sT2 + nSeconds;
}


// MAIN

document.write('<table border="0" cellpadding="0" cellspacing="0" width="200"><tr><td width="33%" align="left" class="photo_nav">');

// prev, index, next menu bar

 if (photoID > 0)
  document.write('<a href="showpic.htm?gallery='+photoGallery+'&lib='+photoLibrary+'&photo='+eval(photoID-1)+'">&lt;&lt;&lt; Previous</a>');
 document.write('&nbsp;</td><td width="34%" align="middle" class="photo_nav"><a href="javascript:photo_index();">Index</a></td><td width="33%" align="right" class="photo_nav">');
 if (photoID+1 < maxPhotos)
  document.write('<a href="showpic.htm?gallery='+photoGallery+'&lib='+photoLibrary+'&photo='+eval(photoID+1)+'">Next &gt;&gt;&gt;</a>');
 document.write('&nbsp;</td></tr><tr><td colspan="3" height="240">');

// is video?

 var extension = '.jpg';
 var i = photoData[photoID][0].length;
 if (photoData[photoID][0].charAt(i-4) == '.') {
  extension = photoData[photoID][0].substring(i-4, i);
  photoData[photoID][0] = photoData[photoID][0].substring(0, i-4);
 }
 if (extension == '.avi' || extension == '.mpg')
  video = true;
 else
  video = false;
 
 if (video) {
  document.write('<DIV id="player" style="LEFT: 0px; VISIBILITY: visible; POSITION: relative; TOP: 0px">');
  document.write('<OBJECT id=MediaPlayer1 codeBase=http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,0,02,902 ');
  document.write('classid=CLSID:22D6F312-B0F6-11D0-94AB-0080C74C7E95 width=320 height=240 type=application/x-oleobject VIEWASTEXT ');
  document.write('standby="Loading Microsoft® Windows® Media Player components...">');
  document.write('<PARAM NAME="FileName" VALUE="'+photoPath+photoLibrary+'/pic'+photoData[photoID][0]+extension+'">');
  document.write('<PARAM NAME="AutoStart" VALUE="true">');
  document.write('<PARAM NAME="ShowControls" VALUE="false">');
  document.write('<PARAM NAME="clickToPlay" VALUE="false">');
  document.write('<EMBED type="application/x-mplayer2" ShowControls=1 EnableContextMenu=0 ShowAudioControls=1 clickToPlay=0 ');
  document.write('ShowPositionControls=0 ShowTracker=1 ShowStatusBar=1');
  document.write('pluginspage = "http://www.microsoft.com/Windows/MediaPlayer/"');
  document.write('SRC="'+photoPath+photoLibrary+'/pic'+photoData[photoID][0]+extension+'" name="MediaPlayer1" AutoStart=true ShowControls=true>');
  document.write('</embed></OBJECT></DIV>');

  document.write('<DIV id=cover><img src="../common/x1.gif" height="1" width="1"></DIV>');

  document.write('</td></tr><tr><td colspan="3"><FORM>');
  document.write('<img name="b_play" src="../common/play_x.gif" alt="Play" onmouseup="PlayClick();" height="41" width="41">');
  document.write('<img id="b_pause" src="../common/pause_x.gif" alt="Pause" onmouseup="PauseClick();" height="41" width="41">');
  document.write('<img id="b_stop" src="../common/stop_x.gif" alt="Stop" onmouseup="StopClick();" height="41" width="41">');
  document.write('<img id="b_sound" src="../common/sound_x.gif" alt="Mute" onmouseup="incVol(-1000);" height="41" width="41">');
  document.write('<img id="b_dn" src="../common/v_dn_x.gif" alt="Increase Volume" onmouseup="incVol(-1);" height="29" width="29">');
  document.write('<img id="b_up" src="../common/v_up_x.gif" alt="Decrease Volume" onmouseup="incVol(+1);" height="29" width="29">');
  document.write('</FORM></TD></tr>');
 }

 else {
  document.write('<img src="'+photoPath+photoLibrary+'/pic'+photoData[photoID][0]+extension+'"></td></tr>'); 
 }


// comments

 for (i=1;i<6;++i) {
  if (!photoData[photoID][i] || photoData[photoID][i] == '')
   photoData[photoID][i]=' ';
 }

 if (photoData[photoID][4] != ' ' || photoData[photoID][5] != ' ') {
  document.write('<tr><td valign="top">');
  if (photoData[photoID][4] != ' ')
   document.write('<p class="photo_crew">'+photoData[photoID][4]+'</p>');
  if (photoData[photoID][5] != ' ')
   document.write('<p class="photo_car">'+photoData[photoID][5]+'</p>');
  document.write('</td><td>');
 }
 else
  document.write('<td colspan="2" valign="top">');

 document.write('<p class="photo_comment"><b>'+photoData[photoID][1]+'</b></p>');
 document.write('<p class="photo_comment">'+photoData[photoID][2]+'</p></td>');
 document.write('<td valign="top"><p class="photo_credit">'+photoData[photoID][3]+'</p></td>');
 document.write('</tr></table>');

 if (video) {
  started = 0;
  updateStatus();
 }
