/**
* Version: 2007.06.26.1
*/
function global_onload()
{

// adds a "loaded" classname to the <html> element
	initPageLoadCss();

// creates a native os x search field for Safari	
	//new searchField();

// tabs
//	var activeTab = $$('div.active-tab')[0];
//	if(activeTab!=null) { TabControl('tabs', { current: $$('div.active-tab')[0].id });	}

// round corner solution, currently turned off in favour of purely css solution	
//	fixRoundedCorners();

// creates a larger click area for class="pinball-effect" boxes	based on class="pinball-sinkhole" links
	//pinballEffect();
	
// adds a hover state for Internet Explorer to class="mouseover" unordered list list items
	addHoverState();

}

/**
 * Injects non-semantic div elements into elements with .rb classnames 
 * to allow for CSS-controlled rounded corners
 * Author: Dylan Smith <dylan.smith@ogilvy.co.za> & Luke Hardiman <hardiman@gmail.com>
 */
 
function fixRoundedCorners()
{
	var els = document.getElementsByClassName('rb');
	els.each(
		function(el)
		{
			// remove any padding
//			el.style.padding = '0';
      var elHeight = el.getHeight();
      elHeight = parseInt(elHeight-13);
      
      var elWidth = el.getWidth();
      elWidth = parseInt(elWidth-13);
			
			// top right element
			var tl = document.createElement('div');
			Element.addClassName(tl, 'tl');
			
			// top left element
			var tr = document.createElement('div');
			Element.addClassName(tr, 'tr');			
			
			// bottom right element
			var br = document.createElement('div');
			Element.addClassName(br, 'br');
			
			// bottom left element
			var bl = document.createElement('div');
			Element.addClassName(bl, 'bl');
			
			// sets the top: positioning value for ie, which is incapable of positioning corners properly using bottom:-1px;
			lessThanIE7 = oldBrowser();
			
			if(lessThanIE7==true) {

				br.style.top = elHeight;
				bl.style.top = elHeight;
				br.style.left = elWidth;
				tr.style.left = elWidth;
			
			}						
			// inject new elements into the host
			el.appendChild(tl);
			el.appendChild(tr);
			el.appendChild(bl);			
			el.appendChild(br);
		}
	);
}

function addLoadEvent(func) { 
	Event.observe(window, 'load', func); 
} 

/*
function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
	  window.onload = function() {
	  oldonload();
	  func();
	}
  }
}
*/

String.prototype.normalize = function(){
	return this.replace(/\s+/g, " ");
};

function appendToClassName(node, sAppend){
	var sClassName = node.className;
	if(sClassName == null){
		sClassName = sAppend;
	} else {
		sClassName = sClassName.normalize() + (sClassName == "" ? "" : " ") + sAppend;
	};
	node.className = sClassName;
}

/* IE5.5+ 24bit PNG TRANSPARENCY -- DREW McCLELLAN -- http://allinthehead.com AND YOUNGPUP -- http://boring.youngpup.net/2001/sleight / */

var bgsleight	= function() {
	
	function fnLoadPngs() {		
		var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
		var itsAllGood = (rslt != null && Number(rslt[1]) >= 5.5 && Number(rslt[1]) < 7);
		var transEls = $$('div.png24','span.png24','dt.png24');
		for (var i = transEls.length - 1, obj = null; (obj = transEls[i]); i--) {
			if (itsAllGood && obj.currentStyle.backgroundImage.match(/\.png/i) != null) {
				fnFixPng(obj);
				obj.attachEvent("onpropertychange", fnPropertyChanged);
			}
		}
	}

	function fnPropertyChanged() {
		if (window.event.propertyName == "style.backgroundImage") {
			var el = window.event.srcElement;
			if (!el.currentStyle.backgroundImage.match(/x\.gif/i)) {
				var bg	= el.currentStyle.backgroundImage;
				var src = bg.substring(5,bg.length-2);
				el.filters.item(0).src = src;
				el.style.backgroundImage = "url(images/bg/x.gif)";
			}
		}
	}

	function fnFixPng(obj) {
		var bg	= obj.currentStyle.backgroundImage;
		var src = bg.substring(5,bg.length-2);
		obj.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "', sizingMethod='scale')";
		obj.style.backgroundImage = "url(images/bg/x.gif)";
	}
	
	return {
		
		init: function() {
			
			var lessThanIE7 = oldBrowser();

			if(lessThanIE7 == true) {
				addLoadEvent(fnLoadPngs);
			}
		}
	}
	
}();

bgsleight.init();

// add a classname to tell css that js is available and page is loading
node = document.getElementsByTagName("html")[0];
appendToClassName(node, "jsEnabled");

// add a classname to activate onload styles
function initPageLoadCss() { 
	var html = document.getElementsByTagName('html')[0];
	html.className = 'jsEnabled loaded';
}

// add a classname to active form elements 	
function formFocus() {
  var formHoverEls = $$('input,textarea,select');
  if (formHoverEls != '') {
		formHoverEls = document.getElementsByClassName('mouseover');
		formHoverEls.each(function(node) {
			Event.observe(node,'click',function() {
				appendToClassName(node, 'focus');
			});
			Event.observe(node,'mouseout',function() {
				node.className=node.className.replace(new RegExp(" focus\\b"), "");
			});			
		});
	}
}

// For IE5.5 to 6 - get all list items that require a hover state, 
// add a .hover classname to them on mouseover

function oldBrowser() {
	var rslt = navigator.appVersion.match(/MSIE (\d+\.\d+)/, '');
	var lessThanIE7 = (rslt != null && Number(rslt[1]) >= 5.5 && Number(rslt[1]) < 7);
	return lessThanIE7;
}

function addHoverState() {

	var lessThanIE7 = oldBrowser();

	if (lessThanIE7==true) {
		var hoverEls = $$('ul.mouseover li','dl.mouseover dd','dl.mouseover dt');
		hoverEls.each(function(node){
			Event.observe(node,'mouseover',function(){
				Element.addClassName(node, "hover");
			});
			Event.observe(node,'mouseout',function(){
				Element.removeClassName(node, "hover");
			});		
		});
	}
}

// Takes the href from the element with className 'pinball-sinkhole' and applies it to the wrapper element 'pinball-effect' 
// Proceding from the concept of Fitts Law this function creates a bigger click area to promote usability

function pinballEffect() {	
	if (document.getElementsByClassName('pinball-effect')) {
		scoops = document.getElementsByClassName('pinball-effect');
		scoops.each(function(node) {
			Event.observe(node,'click',function() {
				var allThisAreasElements = node.getElementsByTagName('*');
				for (var j=0; j<allThisAreasElements.length; j++) {
					if (allThisAreasElements[j].className.indexOf('pinball-sinkhole') != -1) {
						var url = allThisAreasElements[j].href;
						window.status = url;
						window.location = url;
					}
				}
			});
		});
	}
}

// Replace the browser chrome search with a native OS X search field 
// For Safari only - adapted from Wolfgang Bartelme's script - http://www.bartelme.at */

var searchField = Class.create();
searchField.prototype = {
  initialize: function()
  {
    this.search_field  = $("searchInput");
    this.search_submit = $("searchSubmit");
    this.trans_buttons = $$('a.join-upgrade-button','a.need-help-button');
    this.default_value = "";
    this.is_safari     = ((parseInt(navigator.productSub)>=20020000)&&(navigator.vendor.indexOf("Apple Computer")!=-1));
    if (this.is_safari) {
      Element.addClassName(this.search_field, "safari");
      Element.addClassName(this.search_submit, "safari");      
      this.trans_buttons.each(function(node) {
      	Element.addClassName(node, "safari");
      });
      this.replace();      
    } else {
      if (this.search_field.value == "") this.search_field.value = this.default_value;
    }
    Event.observe(this.search_field, "focus", this.focus.bindAsEventListener(this), false);
    Event.observe(this.search_field, "blur",  this.blur.bindAsEventListener(this),  false);
  },
  replace: function()
  {
    this.search_field.setAttribute('type', 'search');
    this.search_field.setAttribute('autosave', 'search');
    this.search_field.setAttribute('results', '5');
    this.search_field.setAttribute('placeholder', this.default_value);
  },
  focus: function()
  {
    if (this.search_field.value == this.default_value) {
      this.search_field.value = "";
      Element.addClassName(this.search_field, "focus");
    }
  },
  blur: function()
  {
    if (this.search_field.value == "") {
      if (this.is_safari) {
        this.search_field.value = "";        
      } else {
        this.search_field.value = this.default_value;
        Element.removeClassName(this.search_field, "focus");
      }
    }
  }
}

// Tab controller - hides inactive and shows active tabs
// Minor presentational fixes to tab borders

TabControl = function(control_id, options) {    
	
	var id = "#" + control_id;
	$$(id+' li a').each(function(a) {    
		var page = a.getAttribute('href').match(/[-_\w]+$/i)[0];

// if not the active tab - then hide it		
		if (page != options['current']) { $(page).hide(); } 
		else { 
		
// this is the active tab - set the classname and create the border elements
			$(a.parentNode).addClassName('active'); 
			var brdr1 = document.createElement('span');
			brdr1.setAttribute('id', 'border1');
			$(a.parentNode).appendChild(brdr1);
			var brdr2 = document.createElement('span');
			brdr2.setAttribute('id', 'border2');
			$(a.parentNode).appendChild(brdr2);						
			
		}
		
		Event.observe(a, 'click', function(e) {

/* check for a pdf download link
			if($$('#'+page+' div.download')=="" && $$('div.download').length==1) {
				var downloadDiv = $$('div.download')[0];

// append the pdf download element to the tab
				$(page).appendChild(downloadDiv);
			}
*/

// remove other gap filling border elements if they exist		
			if($('border1')) {Element.remove('border1');}
			if($('border2')) {Element.remove('border2');}

// create the border elements to fill in the gaps in the tab bottom border
			$(a.parentNode).addClassName('active'); 
			var brdr1 = document.createElement('span');
			brdr1.setAttribute('id', 'border1');
			$(a.parentNode).appendChild(brdr1);
			var brdr2 = document.createElement('span');
			brdr2.setAttribute('id', 'border2');
			$(a.parentNode).appendChild(brdr2);	

// remove the active classname from the previously active tab
			$$(id+' li.active').each(function(e) { e.removeClassName('active');e.blur(); })
			$$(id+' div.active-tab').each(function(e) { e.removeClassName('active-tab'); })

// hide the other tabs
			var otherTabs = $$(id+' div.tab[id!='+page+']');
			otherTabs.each(function(e) { e.hide(); });
			$(a.parentNode).addClassName('active');
			$(page).addClassName('active-tab');

// show el pago
			$(page).show();
			Event.stop(e);
		});
	});
}

Event.observe(window, 'load', global_onload);
// clear event handlers on unload - prevents memory leaks
Event.observe(window, 'unload', Event.unloadCache, false);
