// Pocket Lab Assistant
// Service scripts (main page "tabs" etc.)

var bFlashCreated = false;
var bFlashPlaying = false;

function setZIndex(strID, iValue) {
  var objElement;
  if (document.getElementById) {
    objElement = document.getElementById(strID);
  } else if (document.all) {
    objElement = document.all[strID];
  }
  if (objElement) {
    objElement.style.zIndex = iValue;
  }
}

function setVisibility(strID, strValue) {
  var objElement;
  if (document.getElementById) {
    objElement = document.getElementById(strID);
  } else if (document.all) {
    objElement = document.all[strID];
  }
  if (objElement) {
    objElement.style.visibility = strValue;
  }
}

function setMainTab(strID) {
  // Dev. Note: Firefox will reload Flash when recovering element from display: none to display: block. IE won't. Opera 7.2x will crash.
  var arrTabs = new Array('tabMain', 'tabFlashMovie', 'tabProjectManager', 'tabMyExperiments', 'tabProtocolDatabase', 'tabSolutionDesigner');
  if (!bFlashCreated && (strID == 'tabFlashMovie')) {
    strID = 'tabMain';
  } else if (bFlashPlaying) {
    // Locked.
    strID = 'tabFlashMovie';
  }
  for (var i = 0; i < arrTabs.length; i++) {
    setZIndex(arrTabs[i], strID == arrTabs[i] ? 100 : 0);
  }
  for (var i = 0; i < arrTabs.length; i++) {
    setVisibility(arrTabs[i], strID == arrTabs[i] ? 'visible' : 'hidden');
  }
  setVisibility('tabDefaultFlashAlt', strID == 'tabFlashMovie' ? 'visible' : 'hidden');
}

function startupTabs() {
  if (document.getElementById) {
    if (document.getElementById('tabMain')) {
      setMainTab('tabMain');
    }
  } else if (document.all) {
    if (document.all['tabMain']) {
      setMainTab('tabMain');
    }
  }
}

function getFlashMovieObject(movieName) {
  if (window.document[movieName])  {
    return window.document[movieName];
  }
  if ((navigator.appName.indexOf("Microsoft Internet") == -1) && document.embeds && document.embeds[movieName]) {
    return document.embeds[movieName]; 
  } else {
    return document.getElementById(movieName);
  }
}

function ftFlashDone() {
  // Flash movie should call this function to unlock tabs UI
  bFlashPlaying = false;
  return false;
}

function ftStartMovie() {
  var bOk = true;
  if (!bFlashCreated) {
    ftCreateMovie();

    // HACK: Movie load is not instant.
    setMainTab('tabFlashMovie');
    //bFlashPlaying = true;
    return false;
  }
  if (bFlashPlaying) { return true; } // HACK: Two clicks = separate page.
  var objFM = getFlashMovieObject('flashMovie');
  if (objFM) {
    setMainTab('tabFlashMovie');
    try {
      objFM.GotoFrame(1);
      objFM.Play();
      bOk = false;
      bFlashPlaying = true;
    } catch(e) {
    }
  }
  return bOk;
}

function ftCreateMovie() {
  if (navigator.userAgent.toLowerCase().indexOf('opera 7.2') <= 0) {
    var fo = new FlashObject("/4.swf", "flashMovie", "420", "366", "5", "#ffffff");
    fo.write("tabDefaultFlashAlt");
    bFlashCreated = true;
    //ftStopMovie();
  }
}

if (window.addEventListener) {
  window.addEventListener("load", startupTabs, false);
} else if (window.attachEvent) {
  window.attachEvent("onload", startupTabs);
}