/* (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() {
  // check current environment
  isIE = navigator.userAgent.toLowerCase().match(/msie 5.5/i);
  isIE = isIE || navigator.userAgent.toLowerCase().match(/msie 6/i);

  var supportsID = (document.getElementById)?true:false;

  // document.all checks for IE. Al other browsers are OK
  if (supportsID && document.getElementById("nav"))
  {
    // hack to make IE navigation work, this can be removed later on
    var sfEls = document.getElementById("nav").getElementsByTagName("LI");
    for (var i=0; i<sfEls.length; i++)
    {
      if (isIE)
      {
        sfEls[i].onmouseover=function(){this.className+=" over";};
        sfEls[i].onmouseout=function(){this.className=this.className.replace(new RegExp(" over\\b"), "");};
      }
      sfEls[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;
      };
    }
  }

  if (!document.getElementById("contentPane")) return;
  sfEls = document.getElementById("contentPane").getElementsByTagName("INPUT");
  for (i=0; i<sfEls.length; i++)
  {
    sfEls[i].onmouseover=function()
    {
      this.className+=" over";
    };
    sfEls[i].onmouseout=function()
    {
      this.className=this.className.replace(new RegExp(" over\\b"), "");
    };
  }

};

/*
 * @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;
}

/* changed to be compatible with moo (not mootools)
e.g. $$=>$S, no more addEvent
see visitaruba code for newer version
changed: 20080827 by MB
original version (c): http://www.nuff-respec.com/technology/google-track-outbound-links-automatically
*/
function addLinkTracking()
{
		//no tracker then quit
		if(!window.pageTracker) return;

		//suggested by remy
		var host = window.location.host;

		//go thru each link
		$S('a').each(function(el)
		{
			//if rel tag is google then track what's inside brackets
			//ex: google[download/thisfile]
			if(el.rel && el.rel.match(/^google/i) )
			{
				if (el.href)
				{
					addEvent(el, 'click',function()
					{
						var track_id = this.rel.match(/^google\[(.*)\]$/);
						if(track_id)
						{
							//we have a match use array[1] for tracking
							pageTracker._trackPageview(track_id[1]);
						}

					});
				}
			}

			//if has http, must be external link, track as
			//outbound/domainname
			//else if(el.href && el.href.test(/^http:\/\//))
			else if(el.host != host)
			{
				if (el.href)
				{
					addEvent(el, 'click',function()
					{
						var track_id = el.href.match(/^http:\/\/([^\/]+)/);
						if(track_id)
						{
							//we have a match use array[1] for tracking
							pageTracker._trackPageview('outgoing/'+track_id[1]);
						}

					});
				}
			}
		});
}

addEvent(window, "load", sfHover);
addEvent(window, "load", tooltips);
addEvent(window, "load", menuFx);
addEvent(window, "load", popup);
addEvent(window, "load", addLinkTracking);
