/**
 * Over/out Event Management
 */
var Element_SwapClass = {

  classes: new Array(),

  set: function(object, classname) {
    if (!object)
      return;
    this.classes[object] = object.className;
    object.className = classname ? classname : 'over';
  },

  restore: function(object) {
    if (!object)
      return;
    object.className = this.classes[object];
  },

  override: function(object, name) {
    object.className = this.classes[object] = name;
  }
};

/**
 * Popup Window
 */

var _TOKEN_BLOCKED_WINDOW = "Your browser is blocking popup window. Disable it in order to proceed";

function OpenWindow(url, name) {
  var width  = 640;
  var height = 480;
  var top    = (screen.height - height) / 2;
  var left   = (screen.width  - width) / 2;
  var params = 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width=' + width + ',height=' + height + ',top=' + top + ',left=' + left;

  if (name == null)
    name = 'ekpopup';

  var win = window.open(url, name, params);
  if (win)
    win.focus();
  else
    alert(_TOKEN_BLOCKED_WINDOW);
  return win;
}

/**
 * Event management
 */
function OnLoad_addCode(func) {
  if (window.onload != null) {
    var oldOnLoad = window.onload;
    window.onload = function (e) { oldOnLoad(e); func() }
  } else {
    window.onload = func;
  }
}

/**
 * Lightbox and popup handling inside the page
 */
function LightBox() {
}

LightBox.prototype = {

  body_changed: false,

  activate: function(id) {
    this.updateHtml();

    if (this.isBuggedIE()) {
      this.IE_saveScroll();
      this.IE_updateStyles('100%', 'hidden');
    }

    var lightbox = document.getElementById('lightbox');
    var element = document.getElementById(id);
    lightbox.innerHTML = element.parentNode.innerHTML;
    this.setOpacity(lightbox, 0);
    lightbox.style.display = 'block';

    // Find the image div and the name div to set the width of the popup
    var divs = lightbox.getElementsByTagName('div', true);
    var element;
    for (i = 0; i < divs.length; i++)
      if (divs[i].className == 'name') {
	if (divs[0].offsetWidth) {
	  var length = divs[0].offsetWidth;
	  divs[i].style.display = 'none';
	  if (divs[0].offsetWidth < length)
	    divs[i].style.width = divs[0].offsetWidth + 'px';
	  divs[i].style.display = 'block';
	}
	break;
      }

    // Center
    if (lightbox.offsetWidth && lightbox.offsetHeight) {
      var offset_left = Math.round(lightbox.offsetWidth / 2);
      var offset_top  = Math.round(lightbox.offsetHeight / 2);
      lightbox.style['top'] = '50%';
      lightbox.style['left'] = '50%';
      lightbox.style['margin'] = '-' + offset_top + 'px 0 0 -' + offset_left + 'px';
    }
    this.setOpacity(lightbox, 100);

    var overlay = document.getElementById('overlay');
    overlay.style.display = 'block';
    this.opacity_current = -1;
    this.opacity_value = 85;
    this.opacity_max = 15;
    this.opacity_timeout = 1;
    this.fadeOverlay();
    this.fadeOverlay();
    this.fadeOverlay();
  },

  fadeOverlay: function() {
    this.opacity_current++;
    if (this.opacity_current > this.opacity_max)
      this.opacity_current = this.opacity_max;
    this.setOpacity(document.getElementById('overlay'), this.opacity_value * Math.log((this.opacity_current / this.opacity_max * 99) + 1) / Math.log(100));
    if (this.opacity_current == this.opacity_max)
      return;
    var self = this;
    setTimeout(function() { self.fadeOverlay() }, this.opacity_timeout);
  },

  setOpacity: function(element, value) {
    value = Math.round(value);
    if (value < 0)
      value = 0;
    else if (value > 100)
      value = 100;
    element.style['opacity'] = value / 100;
    element.style['moz-opacity'] = value / 100;
    element.style['filter'] = 'alpha(opacity=' + value + ')';
  },

  updateHtml: function() {
    if (this.body_changed)
      return;
    this.body_changed = true;
    document.body.innerHTML = '<div id="overlay" onclick="myLightBox.deactivate()"></div><div id="lightbox"></div>' + document.body.innerHTML;
    document.getElementById('overlay').style.height = document.body.clientHeight + 'px';
  },

  deactivate: function() {
    if (this.isBuggedIE()) {
      this.IE_restoreScroll();
      this.IE_updateStyles('auto', 'auto');
    }
    document.getElementById('lightbox').style.display = 'none';
    document.getElementById('overlay').style.display = 'none';
  },

  isBuggedIE: function() {
    var _version = navigator.userAgent.split("MSIE");
    return _version.length > 1 && parseFloat(_version[1]) >= 5.5 && parseFloat(_version[1]) < 7;
  },

  IE_saveScroll: function() {
    if (self.pageYOffset)
      this.scroll_ypos = self.pageYOffset;
    else if (document.documentElement && document.documentElement.scrollTop)
      this.scroll_ypos = document.documentElement.scrollTop; 
    else if (document.body)
      this.scroll_ypos = document.body.scrollTop;
    window.scrollTo(0, 0);
  },

  IE_restoreScroll: function() {
    window.scrollTo(0, this.scroll_ypos);
  },

  IE_updateStyles: function(height, overflow) {

    bod = document.getElementsByTagName('body')[0];
    bod.style.height = height;
    bod.style.overflow = overflow;
  
    htm = document.getElementsByTagName('html')[0];
    htm.style.height = height;
    htm.style.overflow = overflow; 
  }
};

var myLightBox = new LightBox();

/**
 * Layer/Div Management
 *
 * This section defines functions to hide or show layers.
 * It also provide function to check whether a layer is visible
 * or not.
 */

function showLayer(id) { 
  var object = document.getElementById(id);
  if (!object)
    return;
  
  if (object.style['visibility']) {
    object.style.display = "block";
    object.style.visibility = "visible";
  } else {
    object.style.display = "inline";
  }
} 

function hideLayer(id) {
  var object = document.getElementById(id);
  if (!object)
    return;
  
  if (object.style['visibility']) {
    object.style.display = "none";
    object.style.visibility = "collapse";
  } else {
    object.style.display = "none";
  }
} 

/**
 * String manipulation
 */

function String_trim(str) {
  return str.replace(/^\s+|\s+$/, '');
}
