/*
	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){
    if(cards[i].className == 'card')
    {
  		this.tab = t;
  		this.absTab = document.createElement('div');
  
      this.absTab.appendChild(document.createElement('div'));
  		// image stuff
  		var oImg=document.createElement("img");
  		oImg.setAttribute('src', '/images/tab_left.png');
  		oImg.setAttribute('height', '52px');
  		oImg.setAttribute('width', '8px');
  		
  		var href = this.tab.childNodes[0].href;
  		var oA=document.createElement("a");
  		oA.setAttribute('href', href);
  		
  		// append image
  		this.absTab.lastChild.appendChild(oImg);
  		this.absTab.appendChild(document.createElement('span'));
  		this.absTab.lastChild.appendChild(oA);
  		this.absTab.lastChild.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];
		if(ul != null)
		{
			ul.style.display = 'block';
		}
		
	},
	
	out: function(){
		over = $A(arguments)[1];
		this.lis[over].firstDescendant().className = '';
		ul = this.lis[over].select('ul')[0];
		if(ul != null)
		{
			ul.style.display = 'none';
		}
	}
};

function pleaseWait(btn, pId, text)
{	
	var isValid = false;
	if (typeof(Page_ClientValidate) == 'function')
	{   
		isValid = Page_ClientValidate();
	}
	if(isValid)
	{
		var p = document.getElementById(pId);
		btn.style.display = 'none';
		p.innerHTML = "<img src='/images/loader.gif' alt='' width='16' height='16'/>&nbsp;&nbsp;" + text;
		p.style.display = 'block';
	}
}

function gcsOnBlur(id)
{
	var gcs = document.getElementById(id);
	var bg = "url(http://www.google.com/coop/intl/en/images/google_custom_search_watermark.gif)";
	var temp = gcs.style.backgroundImage;
	if(gcs.style.backgroundImage=='none' && gcs.value =='')gcs.style.backgroundImage=bg;
}

function gcsOnFocus(id)
{
	var gcs = document.getElementById(id);
	var bg = "url(http://www.google.com/coop/intl/en/images/google_custom_search_watermark.gif)";
	var temp = gcs.style.backgroundImage;
	gcs.style.backgroundImage='none';
}

function gcsOnLoad(id)
{
	var gcs = document.getElementById(id);
	var bg = "url(http://www.google.com/coop/intl/en/images/google_custom_search_watermark.gif)";
	var temp = gcs.style.backgroundImage;
	if(gcs.value != '')
	{
		gcs.style.backgroundImage='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/, '');
		});
	}
};*/
