// yes this is straight from prototype... its handy but I don't want the overhead of loading all that JS when I don't need it
function $()
{
	var elements = new Array();
	
	for (var i = 0; i < arguments.length; i++)
	{
		var element = arguments[i];
		if (typeof element == 'string')
			element = document.getElementById(element);
	
		if (arguments.length == 1)
			return element;
		
		elements.push(element);
	}
	
	return elements;
}

String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

function popupwindow(url, w, h)
{
	if (! w) w = 600;
	if (! h) h = 400;
	mywindow = window.open(url, 'nutpopup', 'status=no,toolbar=no,location=no,menubar=no,scrollbars=yes,resizable=yes,width=' + w + ',height=' + h);
	mywindow.location = url;
}


function addRollover(obj, url)
{
	(new Image).src = url;
	var oldSrc = obj.src;
	obj.onmouseover = function () { this.src = url; }
	obj.onmouseout = function () { this.src = oldSrc; }
}

function addRollovers(objs)
{
	for(var i=objs.length-1; i>=0; i--)
	{
		var value = objs[i].attributes.rollover ? objs[i].attributes.rollover.value : objs[i].rollover || 0;
		if (value)
			addRollover(objs[i], value);
	}
}

// resizes Iframe according to content
function resizeIFrameByID(id)
{
	var obj = $(id);
	if (! obj) return;
	var docHeight = eval(obj.name + '.document.body.scrollHeight');
	obj.style.height = docHeight + 'px';
}

function truncateTestimonial()
{
	var temp = $('truncate-it');
	var dest = $('truncate-dest');
	var inner = $('truncate-quote');
	
	if (! temp || ! dest || ! inner) return;
	
	var str = inner.innerHTML;
	
	var destHeight = 35;
	
	if (temp.clientHeight > destHeight)
	{
		var suffix = '...';
		
		var nextOffset = Math.pow(2, Math.floor(Math.log(str.length) / Math.LN2));
		var len = nextOffset;
		
		do {
			nextOffset = nextOffset >> 1;
			inner.innerHTML = str.substr(0, len) + suffix;
			len = len + (temp.clientHeight > destHeight ? -nextOffset : nextOffset);
		} while (nextOffset > 1);
		
		inner.innerHTML = str.substr(0, len) + suffix;
		while (len > 0 && temp.clientHeight > destHeight)
		{
			len--;
			inner.innerHTML = str.substr(0, len) + suffix;
		}
		
		inner.innerHTML = str.substr(0, len).replace(/\s+\S*$/, '') + suffix;
		
		// if something has gone wrong and we couldn't fit 
		// anything, then don't show anything... better than just 
		// a quoted ellipsis :)
		if (len == 0)
			return;
	}
	
	dest.innerHTML = temp.innerHTML;
}

function buttonClicked(targ)
{
	targ.value = 'Please wait...';
	setTimeout('disableButtons()', 0);
}

function disableButtons()
{
	if (! document.getElementsByTagName) return true;
	var inputs = document.getElementsByTagName('input');
	
	for (i = 0; i < inputs.length; i++)
	{
		var type = inputs.item(i).getAttribute('type');
		if ((type == 'submit') || (type == 'button'))
			inputs.item(i).disabled = true;
	}
}

var tfData = {};

function tempFlashGo()
{
	tfData.element = $('tempFlashThis');
	
	if (! tfData.element)
		return;
	
	var s = '#ffeeae';
	var e = '#a0e3f6';
	
	tfData.base = [
		parseInt(s.slice(1,3),16),
		parseInt(s.slice(3,5),16),
		parseInt(s.slice(5),16) ];
	
	tfData.delta = [
		parseInt(e.slice(1,3),16) - tfData.base[0],
		parseInt(e.slice(3,5),16) - tfData.base[1],
		parseInt(e.slice(5),16) - tfData.base[2] ];
	
	tfData.frame = 0;
	tfData.interval = setInterval(tempFlashUpdate, 100);
};

function tempFlashUpdate() {
	tfData.frame += 0.2;
	var pos = (-Math.cos(tfData.frame*Math.PI)/2) + 0.5;
	if (tfData.frame > 5.999)
	{
		clearInterval(tfData.interval);
		pos = 0;
	}
	var color = '#';
	for (var i = 0; i < 3; i++)
	{
		var dec = Math.round(tfData.base[i] + (tfData.delta[i] * pos));
		if (dec < 16)
			color += '0';
		color += dec.toString(16);
	}
	
	tfData.element.style.backgroundColor = color;
}

