/**
 * Forces browser to correctly re-render an element
 *
 * stretch_children() corrects overflow problems occuring when a child of an element
 * is floating (ie. have their CSS `float` property set to something
 * other than 'none') and end up having a calculated height greater than
 * that of the containing element.
 *
 * Date Created: 2005-02-22
 *
 * @author   Peter-Paul Koch (taken from his site, www.quirksmode.org)
 * @author   Sean McCann (modifications to the original code)
 * @version  1.2
 *
 * @param string element_id   The `id` attribute of the element needing to be resized
 *
 * Changelog
 * - 2008-06-05:  stretch_children( ) now accepts named parameters and will optionally invoke a callback method
 * - 2006-08-29:  Changed name of this function for general use (McCann)
 * - 2005-07-27:  Generalized names used in this function (McCann)
 */
function stretch_children( params )
{
	var container;
	var y_offset;

	if (typeof(params['elementId']) === 'undefined'){
		return false;
	}

	// Manually set the height of the element to "auto"
	container = document.getElementById(params['elementId']);
	container.style.height = 'auto';

	// After it is set to "auto", the offsetHeight of the element
	// is properly calculated.  Use that to set the height of this
	// element once again.
	y_offset = container.offsetHeight;
	container.style.height = y_offset + "px";

	if (typeof(params['onComplete']) === 'function'){
		params['onComplete']();
	}
	return true;
}


/**
 * Adjusts two columns of variable height so that their
 * resulting height is identical.
 *
 * NOTE: relies on the Prototype javascript library
 */
function fix_two_column_height(c1, c2) {
	function debug() {
		var msg="c1:  " + c1 + "\n" +
				"c1.getHeight:  " + $(c1).getHeight() + "\n" +
				"c1.adjust:  " + c1_adjust + "\n" +
				"\n" +
				"c2:  " + c2 + "\n" +
				"c2.getHeight:  " + $(c2).getHeight() + "\n" +
				"c2.adjust:  " + c2_adjust;
		alert(msg);
	}

	// For each column, get the values of the properties listed below.  If
	// that property is not a number, treat it as 0.  Add up the values, and
	// keep track of them so that we can adjust the height of each column to
	// account for these height-adding properties.
	var p = ['padding-top', 'padding-bottom', 'margin-top', 'margin-bottom'];
	var c1_adjust = p.inject(0, function(sum, prop) { var pv = parseInt($(c1).getStyle(prop)); return sum + ((isNaN(pv)) ? 0 : pv) ; });
	var c2_adjust = p.inject(0, function(sum, prop) { var pv = parseInt($(c2).getStyle(prop)); return sum + ((isNaN(pv)) ? 0 : pv) ; });

	if ( $(c2).getHeight() + c2_adjust > $(c1).getHeight() + c1_adjust) {
		$(c1).style.height = 'auto';
		$(c1).style.height = parseInt($(c2).offsetHeight + c2_adjust - c1_adjust) + 'px';
	}
}


/**
 * A callback to fix an IE6 rendering bug after stretch_children() is invoked
 */
function contact_stretch_ie6_fix() {
	var map = document.getElementById('contact_map_hotspot');

	if (!map) return false;
	// Since IE6 doesn't do its job correctly, we need
	// to manually reposition this element at the bottom
	// of its parent after the parent gets resized.
	map.parentNode.style.position = 'relative';
	map.style.position = 'absolute';
	map.style.bottom = '0px';
	return true;
}


/**
 * The image Preloader class
 */
var Preloader = {
	loaded_images: new Array(),
	originals: new Array()
}

Preloader.load = function ( list ){
	list.each(function ( img ) {
		Preloader.loaded_images[img.id] = new Image();
		Preloader.loaded_images[img.id].src = img.src;
	});
};

Preloader.swap = function ( id, state ) {
	if (!Preloader.loaded_images[id]) return false;

	switch(state) {
		case 'hover':
			Preloader.originals[id] = $(id).src;
			$(id).src = Preloader.loaded_images[id].src;
			break;
		case 'original':
			if (Preloader.originals[id]) {
				$(id).src = Preloader.originals[id];
			}
			break;
		default: return false;
	}

	return true;
};