/* Images rollover script.
 * ----------------------------------------------------------------------------
 *
 * Copyright (C) 2009 Raphaël Bois.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to
 * deal in the Software without restriction, including without limitation the
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 * sell copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 * ----------------------------------------------------------------------------
 * rollover.js
 */

var om = null;
var rollcount = 0;
var ROLL_HEIGHT = 900; /* Need to know how much height we have. */
var IMG_HEIGHT = 510;
var rollActivated = true;

function roll_setom(selected) {
  if (!om) {
    return;
  }
  selected = selected <= 0 ? 1 : selected;
  step = (1.0 * (ROLL_HEIGHT - IMG_HEIGHT)) / Math.max(1, rollcount - 1);
  for (var i = 1; i <= rollcount; i++) {
    if (!om[i]) { break; } /* No tab-like div. */
    if (i == selected) {
      om[i].style.left = "-999em";
    } else {
      om[i].style.left = "900px";
      if (i < selected) {
        om[i].style.top = (parseInt((i - 1) * step)).toString() + "px";
      } else {
        om[i].style.top = (parseInt((i-2) * step) + IMG_HEIGHT).toString() + "px";
      }
    }
  }
}

function rollover(tmm) {
  if (!rollActivated || !tmm) { return; }
  mmid = parseInt(tmm.id.substr(2));
  imgs = document.getElementsByTagName("img");
  for (var i = 0; i < imgs.length; i++) {
    im = imgs[i];
    if (im.id.substr(0,2) == "im") {
      cid = parseInt(im.id.substr(2));
      mm = document.getElementById("mm" + cid.toString());
      if (cid < mmid) {
        cZ = 98 + 2 * cid;
        im.style.zIndex = cZ.toString();
        mm.style.zIndex = (cZ+1).toString();
      } else if (cid == mmid) {
        cZ = 98 + 2 * cid;
        mm.style.zIndex = cZ.toString();
        im.style.zIndex = (cZ+1).toString();
      } else {
        cZ = 100 + 2 * (mmid - cid);
        im.style.zIndex = cZ.toString();
        mm.style.zIndex = (cZ+1).toString();
      }
    }
  }
  roll_setom(mmid);
}

function initroll() {
  lastid = 0;
  lastim = null;
  lastmm = null;
  lastZ  = 100;
  imgs = document.getElementsByTagName("img");
  rollcount = 0;
  om = [];
  for (var i = 0; i < imgs.length; i++) {
    im = imgs[i];
    if (im.id.substr(0,2) == "im") {
      rollcount++;
      cid = parseInt(im.id.substr(2));
      cZ = 98 + 2 * cid;
      mm = document.getElementById("mm" + cid.toString());
      om[cid] = document.getElementById("om" + cid.toString());
      im.style.zIndex = cZ.toString();
      mm.style.zIndex = (cZ + 1).toString();
      if (cid > lastid) {
        lastid = cid;
        lastim = im;
        lastmm = mm;
        lastZ  = cZ;
      }
    }
  }

  if (lastmm && lastim) {
    lastmm.style.zIndex = lastZ.toString();
    lastim.style.zIndex = (lastZ+1).toString();
  }
  rollover(document.getElementById("im1"));
}

function rollActivate(active) {
  rollActivated = active;
}

