var copy, switches, sections;

function showSection(sec) {
	var i;
	copy.style.height = '';

	for (i = switches.length - 1; i > 0; i--) {
		switches[i].src = switches[i].offsrc;
	}
	if (sec > 0) {
		switches[sec].src = switches[sec].onsrc;
	}

	for (i = sections.length - 1; i >= 0; i--) {
		sections[i].style.display = 'none';
	}
	sections[sec].style.display = 'block';

	/* javascript min-height hack for browsers that need it (safari, mac ie), with extra hard-coded value goodness! */
	if (copy.offsetHeight < 334) {
		copy.style.height = '334px';
	}
}

function isDescendent(desc, asc) {
	while (desc != asc && desc.nodeName != 'BODY') {
		desc = desc.parentNode;
	}
	return (desc == asc);
}

function mouseOver() {
	showSection(this.section);
}

function mouseOut(e) {
	var tg, reltg;

	if (!e) {
		e = window.event;
	}
	tg = (window.event) ? e.srcElement : e.target;
	reltg = (e.relatedTarget) ? e.relatedTarget : e.toElement;

	if (tg && reltg && isDescendent(tg, copy) && !isDescendent(reltg, copy)) {
		showSection(0);

		e.cancelBubble = true;
		if (e.stopPropagation) {
			e.stopPropagation();
		}
	}
}

function onFocus() {
	if (this.value == this.default_value) {
		this.value = '';
	}
}

function init() {
	var sectionTrigger = 'section';
	var showTrigger = 'showsection';
	var els, el, className, sec, i;

	copy = document.getElementById('copy');
	if (copy) {
		sections = new Array();
		els = copy.getElementsByTagName('div');
		for (i = els.length - 1; i >= 0; i--) {
			className = els[i].className;
			if (className.indexOf(sectionTrigger) == 0 && className.length > sectionTrigger.length) {
				sections[parseInt(className.substring(sectionTrigger.length))] = els[i];
			}
		}

		if (sections.length > 0) {
			switches = new Array();
			els = copy.getElementsByTagName('img');
			for (i = els.length - 1; i >= 0; i--) {
				el = els[i];
				className = el.className;
				if (className.indexOf(showTrigger) == 0 && className.length > showTrigger.length) {
					sec = parseInt(className.substring(showTrigger.length));
					el.preload = new Image();
					el.onsrc = el.preload.src = el.src.replace('_off.', '_on.');
					el.offsrc = el.src;
					el.section = sec;
					el.onmouseover = mouseOver;
					switches[sec] = el;
				}
			}

			copy.onmouseout = mouseOut;
			showSection(0);
		}
	}

	el = document.getElementById('stay_updated_email');
	if (el) {
		el.default_value = el.value;
		el.onfocus = onFocus;
	}
}

window.onload = init;