function add_onload(element, new_function) {
	var old_onload = element.onload;

	if (typeof old_onload != 'function') {
		old_onload = function(){};
	}

	element.onload = function (){
		old_onload();
		new_function();
	}
}

function add_onunload(element, new_function) {
	var old_onunload = element.onunload;

	if (typeof old_onunload != 'function') {
		old_onunload = function(){};
	}

	element.onunload = function (){
		old_onunload();
		new_function();
	}
}

function add_onfocus(element, new_function) {
	var old_onfocus = element.onfocus;

	if (typeof old_onfocus != 'function') {
		old_onfocus = function(){};
	}

	element.onfocus = function (){
		old_onfocus();
		new_function();
	}
}

function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}

function limitTextarea(elm,limit,display_id,limit_id) {
  $(limit_id).value = limit - (elm.value.length);
  if ($(limit_id).value > 1) {
  	$(display_id).innerHTML = limit - elm.value.length+' characters remaining.';
  }
  if ($(limit_id).value == 1) {
  	$(display_id).innerHTML = '1 character remaining.';
 	}
  if ($(limit_id).value == 0) {
  	$(display_id).innerHTML = '0 characters remaining.';
 	}
 	if ($(limit_id).value < 0) {
 		$(display_id).innerHTML = limit - elm.value.length+' characters remaining, excess characters will be truncated.';
 	}
}

function callbackLink(href, element_id, inner_html) {
	$(element_id).href = href;
	if (href == '' || href == 'javascript:;') {
		$(element_id+'_hidden').value = '';
		$(element_id).innerHTML = 'Add a Link';
		$(element_id).title = 'Add a Link';
		$(element_id+'_delete').hide();
	} else {
		if (inner_html === undefined) {
			inner_html = href.truncate(30);
		}
		$(element_id+'_hidden').value = href;
		$(element_id).innerHTML = inner_html;
		$(element_id).title = href;
		$(element_id).target = '_blank';
		$(element_id+'_delete').show();
	}
}

var flow = {
	/* values */
		wrapperId: "home-banner",
		selectedClassName: "selected-item",
		initial: 0, /* index of initial expanded item */
		/*dimensions*/
		expandedWidth: 636,
		contractedWidth: 65,
		totalWidth: 900,
		spacing: 1,
		/*subHeight*/
		subHeight:30,
		initSubHeight:0,
		/*animations*/
		quality: 50,
		duration: 5000,
	/* init vars*/
	items: [],
	orderedItems: [],
	interval: 0,
	i_time:   0,
	inited: false,
	/**/
	init: function () {
		if(document.getElementById && document.getElementById(flow.wrapperId)){
			flow.inited = true;
			var container = document.getElementById(flow.wrapperId);
			var divs = container.getElementsByTagName('div');
			var i=0;
			for(j in divs){
				if (divs[j].nodeType && divs[j].nodeType == 1 && divs[j].parentNode.id == flow.wrapperId) {
					if (!divs[j].id) {divs[j].id = "flow-banner-"+i;}
					flow.items[i] = divs[j].id;
					if (flow.initial == i){
						flow.initial = divs[j].id;
					}
					divs[j].style.width = divs[j].offsetWidth+"px";
					divs[j].getElementsByTagName('ul')[0].offsetHeight+"px";
					if(divs[j].addEventListener){
						divs[j].addEventListener('mouseover', function(){flow.slide(this.id, 1);},false);
						divs[j].addEventListener('mouseout', function(){flow.slide(this.id, -1);},false);
					} else {
						/* stupid backward IE */
						divs[j].onmouseover = flow.flow_out;
						divs[j].onmouseout = flow.flow_in;
					}
					i++;
				}
			}
		}
	},
	/* What's wrong with anonymous functions, huh, IE? nothing.
	   I have some quite good friends who happen to be anonymous functions.
	   so stop with your judging. */
	flow_out: function(event){
		flow.slide(this.id, 1);
	},
	flow_in: function(event){
		flow.slide(this.id,-1);
	},
	slide:function(itemId, dir){
		/* it looks better if they don't slide back to default. */
		if (dir > 0 && flow.inited) {
			if(flow.interval){clearInterval(flow.interval)}
			flow.i_time = 0;
			/*reorder our flow.items to get the one being expanded last.*/
			flow.orderedItems = []; /*zero*/
			for (var n=0; n < flow.items.length;n++) {
				if ((dir > 0 && flow.items[n] != itemId) || (dir < 0 && flow.items[n] != flow.initial)){
					flow.orderedItems.push(flow.items[n]);
				}
			}
			flow.orderedItems.push(dir > 0 ? itemId : flow.initial);
			/*start loop*/

			flow.interval = setInterval("flow.slide_i('"+itemId+"', "+dir+")", flow.quality);
		}
	},

	slide_i:function(itemId, overallDir){
		var otherWidths = 0;

		for (var n=0; n < flow.orderedItems.length;n++){
			var dir = overallDir;
			var newWidth = 0;
			var newSubHeight = 0;
			var item = document.getElementById(flow.orderedItems[n]);
			var itemSub = item.getElementsByTagName('ul')[0];
			if ((dir > 0 && flow.orderedItems[n] != itemId) || (dir < 0 && flow.orderedItems[n] == flow.initial)){
				dir = dir*-1;
			}
			var currentWidth = Number(item.style.width.match(/\d*/));
			var currentSubHeight = Number(itemSub.style.height.match(/\d*/));
			if (dir > 0) {
				/*if we are growing this sucker make it's size the leftovers. this means I can do fancy tweens without the edge being ragged. */
				newWidth = flow.totalWidth - (flow.spacing * (flow.items.length - 1)) - otherWidths;
				//newWidth = Math.floor(flow.easeInOutCirc(flow.i_time+=flow.quality, currentWidth, flow.expandedWidth-currentWidth, flow.duration));
				newSubHeight = Math.floor(flow.easeInOutCirc(flow.i_time+=flow.quality, currentSubHeight, flow.subHeight-currentSubHeight, flow.duration));
			} else if (dir < 0){
				newWidth = Math.floor(flow.easeInOutCirc(flow.i_time+=flow.quality, currentWidth, flow.contractedWidth-currentWidth, flow.duration));
				newSubHeight = Math.floor(flow.easeInOutCirc(flow.i_time+=flow.quality, currentSubHeight, flow.initSubHeight-currentSubHeight, flow.duration));
			}
			if(dir > 0 && flow.i_time >= flow.duration){
				newWidth = flow.expandedWidth;
				newSubHeight = flow.subHeight;
			} else if(dir < 0 && flow.i_time >= flow.duration){
				newWidth = flow.contractedWidth;
				newSubHeight = flow.initSubHeight;
			}
			//item.className = newWidth > flow.expandedWidth - 100 ? flow.selectedClassName : "" ;
			item.style.width = newWidth+"px";
			itemSub.style.height = newSubHeight+"px";
			otherWidths = n==0 ? newWidth : otherWidths + newWidth;
		}
	},
	/*#^ FROM http://www.robertpenner.com/easing/penner_chapter7_tweening.pdf ^#*/
	easeInOutCirc: function (t, b, c, d) {
		if ((t/=d/2) < 1) return c/2 * (1 - Math.sqrt(1 - t*t)) + b;
		return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
	}
}