/* --------------- STUDIO CONNECTIONS EXTERNAL JAVASCRIPT FILE -------------- */
/*                                                                            */
/*    Description: JavaScript for Upper frame - Main Menu, JukeBox            */
/*         Client: Cloud Studios - info@cloudstudios.com                      */
/*        Website: www.cloudstudios.com                                       */
/*         Author: Colin Abrahams - colin@studioconnections.net.au            */
/*      Copyright: 2008 Colin Abrahams, Studio Connections                    */
/*       Location: Wyong, NSW, Australia                                      */
/*        Version: SC-5.8                                                     */
/*   Last Updated: 2008-11-25                                                 */


// Global defaults
JukeAddr = "";
JukeAddP = "";
JukePNxt = true;
JukeAuto = true;
JukeFile = "";
JukeMode = "Play";
JukeSong = -1;
JukeLast = -1;
JukeStat = "";
JukeTAdr = false;
JukeTBuf = 0;
JukeTDur = 0;
JukeTPos = 0;
JukeType = null;
JukeVolu = 0;
JukeWait = 0;
JukeVQini = 255;                                    // QT default volume setting
JukeVQmin = 1;                        // QT minimum volume setting (>= 0 to < 8)
JukeVWini = 0;                                     // WMP default volume setting
JukeVWmin = -4980;           // WMP minimum volume setting (>= -5120 to < -4960)


// Ensure re-initialisation if Upper frame is reloaded
top.SiteInit = false;


// Validate and process cookie JukeBox data
if (top.CKRet && top.CKDataGet) {
  if (top.CKDataGet("JUKEMODE") == "Stop") {
    JukePNxt = false;
    JukeMode = "Stop";
  }
  CKLast = parseInt(top.CKDataGet("JUKELAST"));
  if (!isNaN(CKLast) && CKLast >= 0) {
    JukeLast = CKLast;
  }
  CKVolu = parseInt(top.CKDataGet("JUKEVOLU"));
  if (!isNaN(CKVolu) && CKVolu >= JukeVWmin && CKVolu <= 0) {
    JukeVWini = CKVolu;
  }
  if (!isNaN(CKVolu) && CKVolu >= JukeVQmin && CKVolu <= 256) {
    JukeVQini = CKVolu;
  }
}

// Detect browser
ua = navigator.userAgent.toLowerCase();
n = ua.indexOf("msie");
hasMac = ua.indexOf("mac") != -1;
hasWin = ua.indexOf("win")!=-1 || ua.indexOf("16bit") != -1;
hasIE = n > 0 && window.ActiveXObject ? true : false;
hasIEver = hasIE ? parseFloat(ua.substring(n + 5)) : 0;
hasIEMac = hasIE && hasMac;
hasNonStdBox = hasIE && hasWin && hasIEver < 6;
hasActiveX = window.ActiveXObject ? true : false;
hasGeckoActiveX = window.GeckoActiveXObject ? true : false;
hasPlugArray = navigator.plugins && navigator.plugins.length > 0;

// Detect QuickTime plugin
hasQTpl = false;
if (hasPlugArray) {
  for (i = 0; i < navigator.plugins.length; i++ ) {
    if (navigator.plugins[i].name.indexOf("QuickTime") >= 0) {
      hasQTpl = true;
    }
  }
}

function CKDataSet() {
  if (top.CKSet && top.VisitCurr) {
    Data = "&JUKEMODE=" + (JukePNxt ? "Play" : "Stop")
         + "&JUKELAST=" + JukeLast
         + "&JUKEVOLU=" + JukeVolu;
  top.CKSet(Data);
  }
}

function JukeInit() {

  // Set initial player volume
  if (JukeType == "WMPax") {
    JukeVolu = JukeVWini;
    JukeW.Volume = JukeVolu;
    JukeVolShow();
  }
  if (JukeType == "QTax" || JukeType == "QTpl") {
    JukeVolu = JukeVQini;
    // QT volume cannot be set until AFTER a file has been specified
    JukeVolShow();
  }

  // Pick first song to play
  if (JukeType && window.JukeIntro && JukeIntro != "" && JukeMode == "Play") {
    JukeAddr = JukeIntro;
  }
  else if (window.JukeList) {
    JukeSong = JukeLast < JukeList.length - 1 ? JukeLast + 1 : 0;
    JukeAddr = JukeList[JukeSong].Addr;
  }
  else {
    JukePNxt = false;
    JukeMode = "Stop";
    JukeStat = "No List!";
    if (document.getElementById) {
      document.getElementById("JukeDSMode").innerHTML = JukeMode;
      document.getElementById("JukeDSStat").innerHTML = JukeStat;
    }
    CKDataSet();
    return;
  }

  // Check for player
  if (!JukeType) {
    JukePNxt = false;
    JukeMode = "Stop";
    JukeStat = "External player";
    if (document.getElementById) {
      document.getElementById("JukeDSStat").innerHTML = JukeStat;
    }
    CKDataSet();
    return;
  }

  // Play song
  if (JukeMode == "Play") {
    JukeControl();
  }
  else if (JukeType) {
    JukeStat = "Ready";
    if (document.getElementById) {
      document.getElementById("JukeDSMode").innerHTML = JukeMode;
      document.getElementById("JukeDSStat").innerHTML = JukeStat;
    }
  }
}

function JukeDrive() {

  // Update music playlist
  JukeUpdtMusic();

  // Find next song that hasn't been played
  if (JukePNxt && JukeAuto) {
    for (i = 0; i < JukeList.length; i++ ) {
      n = JukeLast + 1 + i;
      n = n < JukeList.length ? n : n - JukeList.length;
      if (JukeList[n].Stat != "Played") {
        JukeAddr = JukeList[n].Addr;
        JukeControl();
        return;
      }
    }
  }

  // If no more unplayed songs or not AutoPlay, set stop mode
  JukeMode = "Stop";
  JukeControl();
}

function JukePlay() {
  if (JukeAuto) {
    JukePNxt = true;
  }
  JukeMode = "Play";
  JukeControl();
}

function JukeStop() {
  if (JukeAuto) {
    JukePNxt = false;
  }
  JukeMode = "Stop";
  JukeControl();

  // Update music playlist
  JukeUpdtMusic();
}

function JukePause() {
  if (JukeMode == "Play" && JukeStat == "Complete" && JukeTAdr) {
    JukeMode = "Pause";
    JukeControl();
  }
  else if (JukeMode == "Pause") {
    JukeMode = "Play";
    JukeControl();
  }
}

function JukePrevious() {
  if (window.JukeList) {
    JukeSong = JukeSong > 0 ? JukeSong - 1 : JukeList.length -1;
    JukeAddr = JukeList[JukeSong].Addr;
    JukePNxt = true;
    JukeAuto = true;
    JukeMode = "Play";
    JukeControl();
  }
  else if (document.getElementById) {
    JukeStat = "No List!";
    document.getElementById("JukeDSStat").innerHTML = JukeStat;
  }
}

function JukeNext() {
  if (window.JukeList) {
    JukeSong = JukeSong < JukeList.length - 1 ? JukeSong + 1 : 0;
    JukeAddr = JukeList[JukeSong].Addr;
    JukePNxt = true;
    JukeAuto = true;
    JukeMode = "Play";
    JukeControl();
  }
  else if (document.getElementById) {
    JukeStat = "No List!";
    document.getElementById("JukeDSStat").innerHTML = JukeStat;
  }
}

function JukeBack() {
  if (JukeMode == "Stop" || !JukeType || !JukeTAdr) {
    return;
  }

  // Get track data from plugin
  JukeTDataGet();

  // Pause player and shift track time backwards
  JukeTPos = Math.max(parseInt(JukeTPos - JukeTDur / 124), 0);
  if (JukeType == "WMPax") {
    if (JukeMode == "Play") {
      JukeW.Pause();
    }
    JukeW.CurrentPosition = JukeTPos;
  }
  if (JukeType == "QTax" || JukeType == "QTpl") {
    if (JukeMode == "Play") {
      document.JukeQ.Stop();
    }
    document.JukeQ.SetTime(JukeTPos * JukeQTim);
  }

  // Update track data displays
  JukeTDataShow();

  // Restart function
  if (window.JukeBackTim) {
    clearTimeout(JukeBackTim);
  }
  JukeBackTim = setTimeout("JukeBack()", 25);
}

function JukeBackOff() {
  if (window.JukeBackTim) {
    clearTimeout(JukeBackTim);
  }

  // Restart player if in PLAY mode
  if (JukeMode == "Play" && JukeTAdr) {
    if (JukeType == "WMPax") {
      JukeW.Play();
    }
    if (JukeType == "QTax" || JukeType == "QTpl") {
      document.JukeQ.Play();
    }
  }
}

function JukeFwd() {
  if (JukeMode == "Stop" || !JukeType || !JukeTAdr) {
    return;
  }

  // Get track data from plugin
  JukeTDataGet();

  // Pause player and shift track time forwards
  JukeTPos = Math.min(parseInt(JukeTPos + 1 + JukeTDur / 124), JukeTDur - 1);
  if (JukeType == "WMPax") {
    if (JukeMode == "Play") {
      JukeW.Pause();
    }
    JukeW.CurrentPosition = JukeTPos;
  }
  if (JukeType == "QTax" || JukeType == "QTpl") {
    if (JukeMode == "Play") {
      document.JukeQ.Stop();
    }
    document.JukeQ.SetTime(JukeTPos * JukeQTim);
  }

  // Update track data displays
  JukeTDataShow();

  // Restart function
  if (window.JukeFwdTim) {
    clearTimeout(JukeFwdTim);
  }
  JukeFwdTim = setTimeout("JukeFwd()", 25);
}

function JukeFwdOff() {
  if (window.JukeFwdTim) {
    clearTimeout(JukeFwdTim);
  }

  // Restart player if in PLAY mode
  if (JukeMode == "Play" && JukeTAdr) {
    if (JukeType == "WMPax") {
      JukeW.Play();
    }
    if (JukeType == "QTax" || JukeType == "QTpl") {
      document.JukeQ.Play();
    }
  }
}

function JukeControl() {

  // Handle plugin error
  if (JukeStat.indexOf("Error") != -1) {
    JukeMode = "Stop";
    JukePNxt = false;
  }

  // Handle missing file error
  if (JukeMode == "Play" && JukeAddr == "" && JukeStat.indexOf("Error") == -1) {
    JukeMode = "Stop";
    JukeStat = "No file!";
  }

  // STOP mode actions
  if (JukeMode == "Stop") {
    if (JukeType == "WMPax" && JukeTAdr) {
      JukeW.Stop();
    }
    if ((JukeType == "QTax" || JukeType == "QTpl") && JukeTAdr) {
      document.JukeQ.Stop();
      document.JukeQ.Rewind();
    }
    if (window.PlayWindow) {
      PlayWindow.close();
      return;
    }
  }

  // PAUSE mode actions
  if (JukeMode == "Pause") {
    if (JukeType == "WMPax") {
      JukeW.Pause();
    }
    if (JukeType == "QTax" || JukeType == "QTpl") {
      document.JukeQ.Stop();
    }
  }

  // PLAY mode actions
  if (JukeMode == "Play" && JukeAddr != "") {

    // Extract filename from URL
    var n = 0;
    for (i = 0; i < JukeAddr.length; i++ ) {
      if (JukeAddr.substr(i, 1) == "/" || JukeAddr.substr(i, 1) == "\\") {
        n = i;
      }
    }
    JukeFile = JukeAddr.substr(n + 1);

    // Try to find song in playlist
    JukeSong = -1;
    if (window.JukeList) {
      for (i = 0; i < JukeList.length; i++ ) {
        if (JukeList[i].Addr.indexOf(JukeFile) != -1) {
          JukeSong = i;
        }
      }
    }

    // Reset track status displays for new song
    if (JukeType && JukeAddr != JukeAddP) {
      if (window.JukeTim) {
        clearTimeout(JukeTim);
      }
      JukeStat = "Standby";
      JukeTPos = 0;
      JukeTDur = 1;
      JukeTBuf = 0;
      JukeTDataShow();
    }
    JukeAddP = JukeAddr;

    // Initiate PLAY mode
    if (JukeType == "WMPax") {
      if (escape(JukeW.FileName).indexOf(escape(JukeFile)) == -1) {
        setTimeout("JukeW.Open(JukeAddr)", 250);
      }
      else {
        JukeW.Play();
      }
    }
    else if (JukeType == "QTax" || JukeType == "QTpl") {
      if (document.JukeQ.GetURL() == "") {
        cmd = 'document.JukeQ.SetURL(JukeAddr); '
            + 'document.JukeQ.SetVolume(JukeVolu)';
        setTimeout(cmd, 250);
      }
      else if (escape(document.JukeQ.GetURL()).indexOf(escape(JukeFile)) == -1){
        cmd = 'document.JukeQ.SetURL(JukeFile); '
            + 'document.JukeQ.SetVolume(JukeVolu)';
        setTimeout(cmd, 250);
      }
      else if (JukeTAdr) {
        document.JukeQ.Play();
      }
    }
    else {
      cmd = 'PlayWindow = '
          + 'window.open("Player2.html", "Play", "width=200, height=100"); '
          + 'PlayWindow.focus()';
      setTimeout(cmd, 250);
    }

    // Start track monitoring
    if (window.JukeTim) {
      clearTimeout(JukeTim);
    }
    if (JukeType) {
      JukeWait = 0;
      JukeTim = setTimeout("JukeMon()", 1000);
    }
  }

  // Update Track Status display
  if (document.getElementById) {
    document.getElementById("JukeDSStat").innerHTML = JukeStat;
  }

  // Update Control Mode display
  if (JukeType && document.getElementById) {
    document.getElementById("JukeDSMode").innerHTML = JukeMode;
  }

  // Update Auto display
  if (JukeType && document.getElementById) {
    document.getElementById("JukeDSAuto").innerHTML =
      JukePNxt && JukeAuto ? 'Auto' : '&nbsp;';
    document.getElementById("JukeDSAuto").title =
      JukePNxt && JukeAuto ? 'AutoPlay is on' : 'AutoPlay is off';
  }

  // Update artist, song title and Artist link
  if (JukeMode == "Play" && document.getElementById) {
    if (JukeSong >= 0 && JukeList[JukeSong].Info != "") {
      document.getElementById("JukeInfo").innerHTML = '<a href="'
        + JukeList[JukeSong].Info + '" title="View more about '
        + JukeList[JukeSong].Arti + '" target="Main" '
        + 'onclick="this.blur ? this.blur() : null">'
        + '<b>' + JukeList[JukeSong].Arti + '</b></a><br />'
        + JukeList[JukeSong].Song;
    }
    else if (JukeSong >= 0) {
      document.getElementById("JukeInfo").innerHTML = '<b>'
        + JukeList[JukeSong].Arti + '</b><br />'
        + JukeList[JukeSong].Song;
    }
    else {
      document.getElementById("JukeInfo").innerHTML = '';
    }
  }

  // Update Picture and Picture Link
  if (JukeMode == "Play" && document.getElementById) {
    if (JukeSong >= 0 && JukeList[JukeSong].Pict != "" &&
      JukeList[JukeSong].Info != "") {
      document.getElementById("JukePict").innerHTML = '<a href="'
        + JukeList[JukeSong].Info + '" ' + 'target="Main" '
        + 'onclick="this.blur ? this.blur() : null"><img '
        + 'src="' + JukeList[JukeSong].Pict + '" '
        + 'alt="' + JukeList[JukeSong].Arti + '" '
        + 'width="50" height="50" border="0" /></a>';
    }
    else if (JukeSong >= 0 && JukeList[JukeSong].Pict != "") {
      document.getElementById("JukePict").innerHTML = '<img '
        + 'src="' + JukeList[JukeSong].Pict + '" '
        + 'alt="' + JukeList[JukeSong].Arti + '" '
        + 'width="50" height="50" border="0" />';
    }
    else {
      document.getElementById("JukePict").innerHTML = '<img '
        + 'src="Styles/Cloudwht.gif" alt="" '
        + 'width="50" height="50" border="0" />';
    }
  }

  // Update Track Number display
  if (JukeMode == "Play" && document.getElementById) {
    if (JukeSong >= 0) {
      document.getElementById("JukeTrackNo").innerHTML = (JukeSong + 1)
        + '/' + JukeList.length;
    }
    else {
      document.getElementById("JukeTrackNo").innerHTML = '';
    }
  }

  // Update cookie with startup mode for next visit
  CKDataSet();

  // Update JukeBox Diagnostics page
  JukeUpdtDiag();
}

function JukeMon() {

  // Get track data from plugin
  JukeTDataGet();

  // Update track data displays
  JukeTDataShow();

  // Detect end of song (QuickTime only)
  if ((JukeType == "QTax" || JukeType == "QTpl") &&
    JukeStat == "Complete" && JukeTPos / JukeTDur == 1) {

    // Pick next song to play
    JukeDrive();
  }

  // Thank you, come again!
  if (window.JukeTim) {
    clearTimeout(JukeTim);
  }
  if (JukeMode != "Stop") {
    JukeTim = setTimeout("JukeMon()", 1000);
  }
}

function JukeTDataGet() {
  if (JukeType == "WMPax") {
    var OSta = JukeW.OpenState;
    var PSta = JukeW.PlayState;
    var RSta = JukeW.ReadyState;
    var PErr = JukeW.ErrorCode;
    var TBuf = JukeW.BufferingProgress;

    // Track status
    JukeStat = PErr != 0 ? "Error" :
               RSta == 0 ? "Initialising" :
               OSta  < 4 ? "Locating server" :
               OSta  < 5 ? "Connecting" :
               OSta  < 6 ? "Waiting" :
               TBuf  < 100 && PSta == 0 ? "Download aborted!" :
               TBuf == 100 ? "Complete" : "Buffering";

    // Track addressable status
    JukeTAdr = RSta >= 3 && PErr == 0;

    if (JukeTAdr) {

      // Current track position in seconds
      JukeTPos = JukeW.CurrentPosition;

      // Current track duration in seconds
      JukeTDur = JukeW.Duration;

      // Current track buffering progress (0-100%)
      JukeTBuf = OSta < 6 ? 0 : TBuf;
    }
  }
  if (JukeType == "QTax" || JukeType == "QTpl") {
    var PSta = document.JukeQ.GetPluginStatus();

    // Track status
    JukeStat = PSta == "Playable" ? "Buffering" : PSta;

    // Track addressable status
    JukeTAdr = PSta == "Playable" || PSta == "Complete";

    // Catch errors when QT does not report error in Netscape
    if (escape(document.JukeQ.GetURL()).indexOf(escape(JukeFile)) == -1) {
      if (JukeTAdr) {
        JukeStat = "NSQT Error";
        document.JukeQ.Stop();
      }
      if (PSta == "Waiting") {
        if (JukeWait > 0) {
          JukeStat = JukeStat + " " + JukeWait;
        }
        if (JukeWait > 30) {
          JukeStat = "NSQT Load Error";
        }
        JukeWait++;
      }
    }

    if (JukeTAdr) {

      // QT time scale (units per second) for current media
      JukeQTim = document.JukeQ.GetTimeScale();

      // Current track position in seconds
      JukeTPos = document.JukeQ.GetTime() / JukeQTim;

      // Current track duration in seconds
      JukeTDur = document.JukeQ.GetEndTime() / JukeQTim;

      // Current track buffering progress (0-100%)
      var TBuf = document.JukeQ.GetMaxTimeLoaded() / JukeQTim;
      JukeTBuf = parseInt(TBuf / JukeTDur * 100);

      // Refresh volume setting for erratic browsers or plugin
      document.JukeQ.SetVolume(JukeVolu);
    }
  }

  // Handle plugin error
  if (JukeStat.indexOf("Error") != -1) {
    JukeTAdr = false;
    JukeControl();
  }

  // Reset track data if track not addressable
  if (!JukeTAdr) {
    JukeTPos = 0;
    JukeTDur = 1;
    JukeTBuf = 0;
  }
}

function JukeTDataShow() {
  if (document.getElementById) {

    if (document.getElementById("JukeDSStat").innerHTML != JukeStat) {

      // Update Track Status display
      document.getElementById("JukeDSStat").innerHTML = JukeStat;

      if (JukeMode == "Play" && JukeSong >= 0) {
        if (JukeStat == "Complete") {

          // Log song being played
          JukeList[JukeSong].Stat = "Played";

          // Update cookie with last song played
          JukeLast = JukeSong;
          CKDataSet();

          // Update music playlist
          JukeUpdtMusic();
        }
      }

      // Update JukeBox Diagnostics page
      JukeUpdtDiag();
    }

    // Update Song Time display
    var TTimDisp = parseInt(JukeTPos /60) + ":"
                 + (JukeTPos % 60 < 10 ? "0" : "")
                 + parseInt(JukeTPos % 60) + " ";
    document.getElementById("JukeTime").innerHTML = TTimDisp;

    // Specify Buffer Bar internal length
    var TBarILen = 129;

    // Specify Song Position Pointer start position left
    var TBarPosL = 582;

    // Update Song Position Pointer
    var TPosDisp = TBarPosL + parseInt(JukeTPos / JukeTDur * (TBarILen + 1));
    TPosDisp = (!isNaN(TPosDisp) ? TPosDisp : 0);
    document.getElementById("JukePosit").style.left = TPosDisp + "px";

    // Update Buffer Level Bar
    var TBufDisp = parseInt(JukeTBuf / 100 * TBarILen - TBarILen);
    document.getElementById("JukeBufBar").style.backgroundPosition =
      TBufDisp + "px 0px";
    document.getElementById("JukeBufBar").title = "Buffering: " +
      JukeTBuf + "%";
  }
}

function JukeVolDn() {
  if (JukeType == "WMPax") {
    JukeVolu = Math.max(JukeVolu - 80, JukeVWmin);
    JukeW.Volume = JukeVolu;
  }
  if (JukeType == "QTax" || JukeType == "QTpl") {
    JukeVolu = Math.max(JukeVolu - 4, JukeVQmin);
    if (JukeMode != "Stop") {
      document.JukeQ.SetVolume(JukeVolu);
    }
  }
  JukeVolShow();
  if (window.JukeVolDnTim) {
    clearTimeout(JukeVolDnTim);
  }
  JukeVolDnTim = setTimeout("JukeVolDn()", 40);
}

function JukeVolDnOff() {
  if (window.JukeVolDnTim) {
    clearTimeout(JukeVolDnTim);
  }
  if (JukeType) {

    // Update cookie with current volume setting
    CKDataSet();

    // Update JukeBox Diagnostics page
    JukeUpdtDiag();
  }
}

function JukeVolUp() {
  if (JukeType == "WMPax") {
    JukeVolu = Math.min(JukeVolu + 80, 0);
    JukeW.Volume = JukeVolu;
  }
  if (JukeType == "QTax" || JukeType == "QTpl") {
    JukeVolu = Math.min(JukeVolu + 4, 256);
    if (JukeMode != "Stop") {
      document.JukeQ.SetVolume(JukeVolu);
    }
  }
  JukeVolShow();
  if (window.JukeVolUpTim) {
    clearTimeout(JukeVolUpTim);
  }
  JukeVolUpTim = setTimeout("JukeVolUp()", 40);
}

function JukeVolUpOff() {
  if (window.JukeVolUpTim) {
    clearTimeout(JukeVolUpTim);
  }
  if (JukeType) {

    // Update cookie with current volume setting
    CKDataSet();

    // Update JukeBox Diagnostics page
    JukeUpdtDiag();
  }
}

function JukeVolShow() {
  var VoluDisp = 0;
  if (JukeType == "WMPax") {
    VoluDisp = parseInt((JukeVolu + 5120) / 160);
  }
  if (JukeType == "QTax" || JukeType == "QTpl") {
    VoluDisp = parseInt(JukeVolu / 8);
  }
  if (document.getElementById) {
    document.getElementById("JukeVolBar").style.backgroundPosition =
      "0px " + (-VoluDisp) + "px";
    var VolumeLevel = VoluDisp == 32 ?
                      "Volume is set to fucking eleven" :
                      "Volume level: " + parseInt(VoluDisp / 32 * 100) + "%";
    document.getElementById("JukeVolBar").title = VolumeLevel;
  }
}

function JukeUpdtDiag() {
  if (top.Main && top.Main.location && top.Main.location.href &&
    escape(top.Main.location.href).indexOf("DiagPlay") != -1) {
      if (self.DiagTim) {
        clearTimeout(DiagTim);
      }
      DiagTim =
       setTimeout('top.Main.location = "Main/Diag/DiagPlay.html"', 120);
  }
}

function JukeUpdtMusic() {
  if (top.Main && top.Main.location && top.Main.location.href &&
    escape(top.Main.location.href).indexOf("Music") != -1) {
      if (self.MusicTim) {
        clearTimeout(MusicTim);
      }
      MusicTim = setTimeout('top.Main.location = "Site/Main/Music.html"', 2000);
  }
}
