/*
	Prototype version
*/
/* Check for IE */
var rv = -1;
if (navigator.appName == 'Microsoft Internet Explorer')
{
	var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
		rv = parseFloat( RegExp.$1 );
}

var tabs = new Array();
var cards = new Array();
var oTabs = new Array();
var over = false;
var current;
Event.observe(window, 'load', function(){
	contactMethod();
	tabs = $$('#navigation li');
	cards = $$('div.card');
	$('cards').absolutize();
	j = 0;
	for(var i = tabs.length-1; i>=0; i--){
		oTabs[j] = new Tab(tabs[i], i);
		j++;
	}
	swapSearch();
	formButton();
});

function swapSearch(){
	var search = document.getElementById('searchBox');
	if(search){
		search.onfocus = function(e){
			e = window.event || e;
			if (e.target) targ = e.target;
			else if (e.srcElement) targ = e.srcElement;
			targ.value = '';
		};
		search.onblur = function(e){
			e = window.event || e;
			if (e.target) targ = e.target;
			else if (e.srcElement) targ = e.srcElement;
			targ.value = 'Search';
		};
		}
};

var Tab = Class.create();
Tab.prototype = {

	initialize: function(t,i){
		this.tab = t;
		this.absTab = document.createElement('div');
		this.absTab.appendChild(document.createElement('span'));
		this.absTab.lastChild.appendChild(document.createTextNode(this.tab.childNodes[0].lastChild.nodeValue));
		document.body.appendChild(this.absTab);
		this.absTab.className = 'tab tab'+i;
		this.absTab.style.display = 'none';	
		this.card = cards[i];
		this.style = this.tab.className; 
		this.anchor = this.tab.immediateDescendants()[0];
		this.width = this.anchor.getWidth();
		this.animating = false;
		this.isOver = false;
		this.resized = false;
		this.resize();
		if(this.card.select('ul') != ''){
			new Navigation(this.card.select('ul')[0]);
		}
		Event.observe(this.absTab, 'mouseover', this.cardOver.bindAsEventListener(this), false);
		Event.observe(this.absTab, 'mouseout', this.cardOut.bindAsEventListener(this), false);
		Event.observe(this.card, 'mouseover', this.cardOver.bindAsEventListener(this), false);
		Event.observe(this.card, 'mouseout', this.cardOut.bindAsEventListener(this), false);
		Event.observe(this.anchor, 'mouseover', this.over.bindAsEventListener(this), false);
		Event.observe(this.anchor, 'mouseout', this.delay.bindAsEventListener(this), false);
		Event.observe(window, 'resize', this.resize.bindAsEventListener(this), false);
	},
	
	resize: function(){
		this.resized = true;
	},
	
	over: function(){
		if(this.resized){
			Element.clonePosition(this.absTab, this.tab, {offsetTop: -8,setWidth: false});
			this.resized = false;
		}
		if(current){
			current.forceOut();
		}
		current = this;
		this.card.style.display = 'block';		
		this.absTab.style.display = 'block';
	},
	
	delay:function(){
		if(!this.isOver){
			new PeriodicalExecuter(this.out.bindAsEventListener(this), 0.25);
		}
	},
	
	out: function(pe){
		pe.stop();
		if(!this.isOver){
			this.card.style.display = 'none';
			this.absTab.style.display = 'none';
		}
	},
	
	forceOut: function(){
		if(!this.isOver){
			this.card.style.display = 'none';
			this.absTab.style.display = 'none';
		}
	},
	
	cardOver: function(){
		this.isOver = true;
		this.over();
	},
	
	cardOut: function(){
		this.isOver = false;
		this.delay();
	}
};

var Navigation = Class.create();
Navigation.prototype = {

	initialize: function(u){
		this.topList = u;
		this.lis = this.topList.immediateDescendants();
		for(i=0;i<this.lis.length;i++){
			if(rv>-1){
				Event.observe(this.lis[i], 'mouseenter', this.over.bindAsEventListener(this, i), false);
				Event.observe(this.lis[i], 'mouseleave', this.out.bindAsEventListener(this, i), false);
			}else{
				Event.observe(this.lis[i], 'mouseover', this.over.bindAsEventListener(this, i), false);
				Event.observe(this.lis[i], 'mouseout', this.out.bindAsEventListener(this, i), false);
			}
		}
	},
	
	over: function(){
		over = $A(arguments)[1];
		this.lis[over].firstDescendant().className = 'hover';
		ul = this.lis[over].select('ul')[0];
		ul.style.display = 'block';
	},
	
	out: function(){
		over = $A(arguments)[1];
		this.lis[over].firstDescendant().className = '';
		ul = this.lis[over].select('ul')[0];
		ul.style.display = 'none';
	}
};

function contactMethod(){
	if($('webform-component-contactmethod')){
		options = $$('div#webform-component-contactmethod input.form-radio');
		if(options[0].checked){
			$('edit-submitted-fieldset-tel').disabled = true;
			$('edit-submitted-fieldset-email').disabled = false;
		}
		
		if(options[1].checked){
			$('edit-submitted-fieldset-email').disabled = true;
			$('edit-submitted-fieldset-tel').disabled = false;
		}
		Event.observe(options[0], 'click', function(){
			options = $$('div#webform-component-contactmethod input.form-radio');
			if(options[0].checked){
				$('edit-submitted-fieldset-tel').disabled = true;
				$('edit-submitted-fieldset-email').disabled = false;
			}
		});
		Event.observe(options[1], 'click', function(){
			options = $$('div#webform-component-contactmethod input.form-radio');
			if(options[1].checked){
				$('edit-submitted-fieldset-email').disabled = true;
				$('edit-submitted-fieldset-tel').disabled = false;
			}
		});
	}
}

function formButton(){
	inputs = $$('input[type="submit"]');
	for(var i=0; i<inputs.length; i++){
		Event.observe(inputs[i], 'mouseover', function(){
			this.className = this.className + 'hover';
		});
		Event.observe(inputs[i], 'mouseout', function(){
			this.className = this.className.replace(/hover/, '');
		});
	}
};
