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) {
	$(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 {
		$(element_id+'_hidden').value = href;
		$(element_id).innerHTML = href.truncate(30);
		$(element_id).title = href;
		$(element_id+'_delete').show();
	}
}

var slideshow = {
	/* Tweakables */
	id: "gallery_inner", /*wrapper id containing only <img>s */
	quality:    10,    /* smaller is better. but harder for poor computer. */
	duration:   1000,  /* length of time the fading takes in milliseconds*/
	autoSpeed:  5000, /* length of time between fades */
	/* inits */
	currentlyVisible: 0,
	images: [],
	fadeIn_i: "",
	fadeOut_i: "",
	auto_i: "",
	out_t: 0,
	in_t: 0,
	/* go */
	init: function(){
		if(document.getElementById && document.getElementById(slideshow.id)){
			var container = document.getElementById(slideshow.id);
			var imgs = container.getElementsByTagName('img');
			var i=0;
			for(z in imgs){
				//nodelist to array. there's probably a better way to do it, but I'm not a js guru.
				if (imgs[z].style){
					slideshow.images[i] = imgs[z];

					if (slideshow.images[i].style.display == 'block'){

						slideshow.currentlyVisible = i;
					}
					i++;
				}
			}
			if(slideshow.images.length > 1){
				for(n in slideshow.images){
					if(!isNaN(n)){
						if(n == slideshow.currentlyVisible){
							slideshow.show(n);
						} else {
							slideshow.hide(n);
						}
					}
				}
				slideshow.auto()
			}
		}
	},
	show: function(n){
		slideshow.currentlyVisible = n;
		if(slideshow.fadeIn_i){clearInterval(slideshow.fadeIn_i)}
		slideshow.images[n].style.display = 'block';
		slideshow.images[n].style.opacity = '1';
		slideshow.images[n].style.filter = "alpha(opacity=100)";

	},
	hide: function(n){
		if(slideshow.fadeOut_i){clearInterval(slideshow.fadeOut_i)}
		slideshow.images[n].style.display = 'none';
		slideshow.images[n].style.opacity = '0';
		slideshow.images[n].style.filter = "alpha(opacity=0)";
	},
	auto: function(){
		if(slideshow.auto_i){clearInterval(slideshow.auto_i)}
		slideshow.auto_i = setInterval("slideshow.fadeInOut()", slideshow.autoSpeed);
	},
	jumpto: function(n){
		if(slideshow.currentlyVisible != n){
			/*otherwise it tries hiding && showing at the same time. not a good look.*/
			if(slideshow.auto_i){clearInterval(slideshow.auto_i)}
			slideshow.fadeOut(slideshow.currentlyVisible);
			slideshow.fadeIn(n);
		}
	},
	fadeInOut: function(){
		var cur = Number(slideshow.currentlyVisible);
		var nxt = cur == slideshow.images.length - 1 ? 0 : cur + 1;
		slideshow.fadeOut(cur);
		slideshow.fadeIn(nxt);
	},
	fadeIn: function(n){
		slideshow.images[n].style.display = 'block';
		if(slideshow.fadeIn_i){clearInterval(slideshow.fadeIn_i)}
		slideshow.in_t = 0;
		slideshow.fadeIn_i = setInterval("slideshow.run("+n+", 1)", slideshow.quality);
	},
	fadeOut: function(n){
		if(slideshow.fadeOut_i){clearInterval(slideshow.fadeOut_i)}
		slideshow.out_t = 0;
		slideshow.fadeOut_i = setInterval("slideshow.run("+n+", -1)", slideshow.quality);
	},
	run: function(n, dir){
		var o = 0;
		if (dir > 0) {
			o = slideshow.easeOut(slideshow.in_t+=slideshow.quality, 0, 1, slideshow.duration);
		} else if (dir < 0) {
			o = slideshow.easeIn(slideshow.out_t+=slideshow.quality, 1,  -1, slideshow.duration);
		}
		if(dir > 0 && slideshow.in_t >= slideshow.duration){
			slideshow.show(n);
		} else if(dir < 0 && slideshow.out_t >= slideshow.duration){
			slideshow.hide(n);
		} else {
			//if (dir > 0){o*=slideshow.easing}
			slideshow.images[n].style.opacity = o;
			slideshow.images[n].style.filter = "alpha(opacity="+o*100+")";
		}
	},
	/* FROM http://www.robertpenner.com/easing/penner_chapter7_tweening.pdf */
	easeOut: function(t, b, c, d){return c * (Math.pow (t/d-1, 3) + 1) + b;},
	easeIn:  function(t, b, c, d){return c * Math.pow (t/d, 3) + b;}
}
