/* (c) 2006 CaribMedia
* this module defines and initiates nice effects
* preconditions:
*  - topnav and domlibrary is loaded
*/

/*
 * @name  sfHover
 * @description  gives all input elements an effect
 * @author  Michiel van der Blonk
 * @date  Apr 6, 2006
*/

var lastActive;

sfHover = function(menus, sticky) {
  var n, i, tags;

  // check current environment
  if (!document.getElementById)
    return false;
  isIE = document.all?true:false;

  for (n in menus)
  {
    var menuName = menus[n];
    var menu = document.getElementById(menuName);
    if (menu)
    {
      tags = menu.getElementsByTagName("LI");
      for (i=0; i<tags.length; i++)
      {
        // add li:hover support to internet explorer 5 and 6
        if (isIE)
        {
          tags[i].onmouseover=function(){this.className+=" over";};
          tags[i].onmouseout=function(){this.className=this.className.replace(new RegExp(" over\\b"), "");};
        }

        // keep submenu in place after hover using 'active' class
        if (sticky)
          tags[i].onclick=function()
          {
            this.className += " active";
            if (lastActive)
            {
              lastActive.className=lastActive.className.replace(new RegExp(" active\\b"), "");
              lastActive.className=lastActive.className.replace(new RegExp(" over\\b"), "");
            }
            lastActive = this;
          };
      }
    }
  }

  // add :hover support to internet explorer 5 and 6
  var objContentPane = document.getElementById("contentPane");
  if (objContentPane && isIE)
  {
    tags = objContentPane.getElementsByTagName("INPUT");
    for (i=0; i<tags.length; i++)
    {
      tags[i].onmouseover=function()
      {
        this.className+=" over";
        return true;
      };
      tags[i].onmouseout=function()
      {
        this.className=this.className.replace(new RegExp(" over\\b"), "");
        return true;
      };
    }
  }
  return true;
};

/*
 * @name  tooltips
 * @description  initiate tooltips
 * @author  Michiel van der Blonk
 * @date  Apr 6, 2006
*/
function tooltips()
{
  if (window.hasTooltips)
    domTT_replaceTitles(null,"'styleClass', 'niceTitle', 'offsetX', 25, 'offsetY', -20, 'type', 'velcro', 'source', 'tt' ");
}

/*
 * @name  toggleMenu
 * @description  hide/show the nav menu
 * @author  Michiel van der Blonk
 * @date  May 31, 2006
*/
function toggleMenu()
{
  m = document.getElementById('header');
  if (m.className.match(/hide/))
  {
    re = new RegExp("hide\\b");
    m.className = m.className.replace(re, '');
  }
  else
    m.className += ' hide';
  this.className = m.className===''?'open':'close';
}

/*
 * @name  menuFx
 * @description  initiate menu toggle effect
 * @author  Michiel van der Blonk
 * @date  May 31, 2006
*/
function menuFx()
{
  var t = document.getElementById('menuToggle');
  if (t)
    t.onclick = toggleMenu;
}

/*
 * @name  popup
 * @description  initiate popup effect
 * @author  Michiel van der Blonk
 * @date  June 16, 2006
 * @source http://www.openhosting.co.uk/articles/webdev/5918/
*/
function popup()
{
  // check to see that the browser supports the getElementsByTagName method
  // if not, exit the loop
  if (!document.getElementsByTagName) {
    return false;
  }
  // create an array of objects of each link in the document
  var popuplinks = document.getElementsByTagName("a");
  // loop through each of these links (anchor tags)
  for (var i=0; i < popuplinks.length; i++)
  {
    // if the link has a class of "popup"...
    cName = popuplinks[i].getAttribute("class");
    re = new RegExp("popup","ig");
    if (cName && re.test(cName)) {

      options = null;
      if (new RegExp("popup-fullscreen","ig").test(cName))
        options = "fullscreen";
      if (new RegExp("popup-tiny","ig").test(cName))
        options = "tiny";
      if (new RegExp("popup-medium","ig").test(cName))
        options = "medium";
      if (new RegExp("popup-large","ig").test(cName))
        options = "large";

      // add an onclick event on the fly to pass the href attribute
      // of the link to our second function, openPopUp
      popuplinks[i].onclick = function() {
        openPopUp(this.getAttribute("href"), options);
        return false;
        };
    }
  }
  return false;
}

function openPopUp(linkURL, options, width, height) {
  // default position in options
  var position = 'screenX=300, screenY=250, top=250, left=300';
  var resizable = "resizable=yes";
  var scrollbars = "scrollbars=1";
  var defaultOptions = resizable + ',' + scrollbars + position;


  // define options
  var popupOptions = {
    'fullscreen': '',
    'tiny' : 'width=250, height=150, ' + defaultOptions,
    'small' : 'width=350, height=200, ' + defaultOptions,
    'medium' : 'width=450, height=225, ' + defaultOptions,
    'large' : 'width=800, height=600, ' + resizable + ',' + scrollbars
  };

  // get special ops
  if (!options)
  {
    var sOptions = '';
    if (width && height)
      sOptions += 'width='+width + ', height='+height + ',';
    sOptions +=  resizable + ',' + position;
  }
  else
    sOptions = popupOptions[options];

  var handle = window.open(linkURL, 'popup', sOptions);

  handle.focus();

  return false;
}

function doAddHover(){
  var menus = ['dynamicMenu'];
  var sticky = true;
  sfHover(menus, sticky);
}

addEvent(window, "load", doAddHover);
addEvent(window, "load", tooltips);
addEvent(window, "load", menuFx);
addEvent(window, "load", popup);