/* (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(addFocusFx) {
  // check current environment
  if (!document.getElementById)
    return false;

  // add :hover and :focus support to internet explorer 5 and 6
  // remove this code as soon as most common browsers support this
  if (isIE5or6)
  {
    var tagNames = ['p','li','ul','input','button','select','img'];
    tagNames.each( function(tagName) {
      $S(tagName).each( function(tag)
      {
        addEvent(tag, "mouseover", function() {Element.addClassName(tag, 'over');});
        addEvent(tag, "mouseout", function() {Element.removeClassName(tag, 'over');});
        if (addFocusFx)
        {
          addEvent(tag, "focus", function() {Element.addClassName(tag, 'focus');});
          addEvent(tag, "blur", function() {Element.removeClassName(tag, 'focus');});
        }
      });
    });
  }

  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('summary');
  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 = function () { toggleMenu('header') };
  var s = document.getElementById('summary');
  if (s)
    s.onclick = function () { toggleMenu('summary') };
}

/*
 * @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");
    if (!cName)
      cName = popuplinks[i].className;
    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-picture","ig").test(cName))
        options = "picture";
      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,
    'picture' : 'width=680, height=530, ' + 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 addFocusFx = true;
  sfHover(addFocusFx);
}

addEvent(window, "load", doAddHover);
addEvent(window, "load", tooltips);
//addEvent(window, "load", menuFx);
addEvent(window, "load", popup);

