/*  © 2007 - All Rights Reserved */
String.prototype.trim = function() { return this.replace(/^[\s\u3000]+|[\s\u3000]+$/g, ''); }

/**
* The Delegate is an object that helps to keep events within scope
*/
var Delegate = {};
Delegate.create=function(obj, func){
	var f = function(){
		var target = arguments.callee.target;
		var func = arguments.callee.func;
		return func.apply(target, arguments);
	};
	f.target = obj;
	f.func = func;
	return f;
};


/**
* Utility function which toggles the visibility of an element
* I'd like to boost the functionality of this to see if the element has a style
* selector that makes the display of it "none" so I don't have to hard-code any css into the html.
* But in the interest of not spending the hour to figure it out, I am just going to set the elements style="dispaly:none;"
* inline. 
*/
var toggle = function(id, force){
	var element = document.getElementById(id);
	var show = false;
	if(force != undefined){
		show = force;
	}else{
		if(element.style.display){
			show = element.style.display=="none";
		}else if(window.getComputedStyle){
			var cs = window.getComputedStyle(element,"");
			var prop = cs.getPropertyValue("display");
			show = prop=="none";
		}else{
			show = element.currentStyle.display=="none";
		}
	}
	if(show){
		try{
			switch(element.nodeName){
				case "TABLE":
					element.style.display="table";
					break;
				case "TR":
					element.style.display="table-row";
					break;
				case "TD":
					element.style.display="table-cell";
					break;
				case "TH":
					element.style.display="table-cell";
					break;
				default:
					element.style.display = "block";
					break;
			}
		}catch(e){
			element.style.display = "block";
		}
	}else{
		var good = element.style.display = "none";
	}
	
	resetFooter();
	return show;
}

function resetFooter(){
	document.getElementById('footer').style.bottom = '1px';
	setTimeout("document.getElementById('footer').style.bottom = '0px'",50);
}

//Does a vertical height show / hide using jquery 
function showhide(id, replacementText, rate){
	var container=(typeof id == 'string') ? $("#"+id) : $(id);
	var link = window.event ? window.event.srcElement : arguments.callee.caller[0] ? arguments.callee.caller[0].target : undefined;
	
	var changeText = function(text){
		if(link && text){
			if(link._showhidetext){
				text = link._showhidetext;
				link._showhidetext = false;
			}else{
				link._showhidetext = $(link).html();
			}
			$(link).html(text);
			var linkClass=$(link).attr("class");
			if(linkClass && linkClass.match(/^(.*\s)*(showhide_(show|hide))(\s.*)*$/)){
				if(!isShowing){
					$(link).removeClass("showhide_show");
					$(link).addClass("showhide_hide");
				}else{
					$(link).addClass("showhide_show");
					$(link).removeClass("showhide_hide");
				}		
			}		 
		}
	};
	
	
	var wrapper = container.find(".showhide-wrapper");
	if(wrapper == undefined || wrapper.length < 1){
		container.wrapInner('<div class="showhide-wrapper"></div>');
		var pt = container.css("padding-top");
		var pb = container.css("padding-bottom");
		wrapper=container.css("overflow","hidden").find(".showhide-wrapper");
		wrapper.css("padding-top",pt).css("padding-bottom",pb);
		container.css("padding-top","0px").css("padding-bottom","0px");
	}
	
	var isShowing = container.css('display') != "none";//.andSelf().length > 0;
	rate = rate ? rate : 400;
	var numvalRegex = /(\d+).*/;
	var paddings = new Number(numvalRegex.exec($(wrapper).css("padding-top"))[1])
		+ new Number(numvalRegex.exec($(wrapper).css("padding-bottom"))[1]);
	var margins = new Number(numvalRegex.exec($(wrapper).css("margin-top"))[1])
		+ new Number(numvalRegex.exec($(wrapper).css("margin-bottom"))[1]);
	if(isShowing){
		var h = $(wrapper).height()+paddings+margins;
		container.height(h).css('display','block');
		container.animate(
			{height:0},
			Math.round((h/rate)*1000),
			null,
			function(){
				$(this).css("display","none");
				resetFooter();
				changeText(replacementText);
			}
		);
	}else{
		container.height(0).css('display','block');
		var h = $(wrapper).height()+paddings+margins;
		container.animate(
			{height:h},
			Math.round((h/rate)*1000),
			null,
			function(){
				$(this).css("height","1%");
				resetFooter();
				changeText(replacementText);
			}
		);
	}
}


/**
* Looks for all anchors with a rel of "external" 
* and sets the 'target' attribute to '_blank'.
* This allows for XHTML strict compliance
*/
function modifyLinks() {
	if (document.getElementsByTagName) {
		var a = document.getElementsByTagName('a');
		for (var i = 0; i < a.length; i++) {
			if (a[i].getAttribute('href')){
				switch(a[i].getAttribute('rel')){
					case 'external':
						a[i].target = '_blank';
						break;
				}
			}
		}
	}
}

if(YAHOO && YAHOO.util && YAHOO.util.Event){
	var success = YAHOO.util.Event.addListener(window , "load" , modifyLinks );
}

/**
* Method for making forms compact
* Get the form to make compact
* Add the "compact" css class to the form
* Add the onblur function and onfocus function to the 
* input text and password fields 
*/
var makeCompact = function(form){
	form.setAttribute("class","compact");
	form.setAttribute("className","compact");
	for(var i = 0; i < form.elements.length; i++){
		var el = form.elements[i];
		if(el.nodeName == "INPUT"){
			var type = el.type;
			if(type == "text" || type == "password"){
				el.onblur = function(){
					var val = this.value;
					if(val == ""){
						this.style['z-index'] = 1;
						this.style['zIndex'] = 1;
					}
				}
				el.onfocus = function(){
					this.style['z-index'] = 3;
					this.style['zIndex'] = 3;
				}
				el.previousSibling.onclick = function(){
					var id, field;
			        id = this.getAttribute('for');
			        if (id && (field = document.getElementById(id))) {
						field.focus();
			        }
				}
				//if there is a value already, set the Zindex
				if(el.value != ""){
					el.onfocus();
				}
			}
		}
	}
}



/***********************************************
* Added December 4, 2007 MGB 
*  Enable form field hints
* Use via a span class="hint" with the text of the 
* hint between the span tags
***********************************************/

// returns an object with x and y values
// of the first parent with relative positioning
function relativeXY(element){
	var parent = element.parentNode;
	var nodeType = parent.nodeType;
	if(parent && nodeType != 9){
		var positioning = $(parent).css("position");
		if(positioning == "relative"){
			return $(parent).offset();
		}else{
			return relativeXY(parent);
		}
	}else{
		return {left:0,top:0};
	}
}
$(document).ready(function(){
  $("input,select")
  	.focus(
  		function(){
  			var hint = $(this).siblings(".hint");
  			if(hint.length > 0){
	  			var relative = relativeXY(this);
	  			var thisXY = $(this).offset();
	  			var rightEdge = thisXY.left - relative.left + $(this).width();
	  			var topEdge = thisXY.top - relative.top;
	  			hint.css("display","inline").css("left",(rightEdge+30)+"px").css("top",topEdge+"px");
	  		}
  		}
  	).blur(
  		function(){
  			$(this).siblings(".hint").css("display","none");
  		}
  	);
});


/***********************************************
* Added January 15, 200 MGB 
*  Enable tabbed interface for home page
***********************************************/


/* ================================================================ 
This copyright notice must be untouched at all times.

The original version of this script and the associated (x)html
is available at http://www.stunicholls.com/various/tabbed_pages.html
Copyright (c) 2005-2007 Stu Nicholls. All rights reserved.
This script and the associated (x)html may be modified in any 
way to fit your requirements.
=================================================================== */


onload = function() {
	var e, i = 0;
	var gallery = document.getElementById('gallery');
	if(gallery){
		while (e = gallery.getElementsByTagName ('DIV') [i++]) {
			if (e.className == 'on' || e.className == 'off') {
			e.onclick = function () {
				var getEls = document.getElementsByTagName('DIV');
					for (var z=0; z<getEls.length; z++) {
					getEls[z].className=getEls[z].className.replace('show', 'hide');
					getEls[z].className=getEls[z].className.replace('on', 'off');
					}
				this.className = 'on';
				var max = this.getAttribute('title');
				document.getElementById(max).className = "show";
				}
			}
		}
	}
}