function setSource(el, img) {
	if (((typeof window.usingPngfix) != 'undefined') && window.usingPngfix === true) {
		el.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader"
         + "(src=\'" + img + "\', sizingMethod='scale')";
	} else {
		el.src = img;
	}
}

function closeTabs() {
	var hic = document.getElementById('HomeImageContainer');
	var he = document.getElementById('homeboxes_expanded_1');
	var he2 = document.getElementById('homeboxes_expanded_2');
	var he3 = document.getElementById('homeboxes_expanded_3');
	var tabs = document.getElementById('hpTabsImg');
	setSource(tabs, 'images/tabs1-down.png');
	if ((typeof he.morph) == 'function') {
		he.morph('height: 48px;', { duration: 0.5 } );
		hic.morph('height: 399px;', { duration: 0.5 } );
	} else {
		he.style.height = '48px';
		hic.style.height = '399px';
	}
	start_bg_cycles();	
	he.style.display = 'block';
	he2.style.display = 'none';
	he3.style.display = 'none';
	document.getElementById('tabs_closebutton').style.display = 'none';
}

function hpTab(n) {
	var hic = document.getElementById('HomeImageContainer');
	var he = document.getElementById('homeboxes_expanded_1');
	var he2 = document.getElementById('homeboxes_expanded_2');
	var he3 = document.getElementById('homeboxes_expanded_3');
	var tabs = document.getElementById('hpTabsImg');
	switch(n) {
		case 1:
			hic.style.backgroundImage = 'url(images/home-backgrounds/rotate_1.jpg)';
			if ((/images\/tabs1\.png$/.test(tabs.src) === true) || (/images\/tabs1\.png/.test(tabs.style.filter) === true)) {
				// Tab 1 is open. Close it.
				setSource(tabs, 'images/tabs1-down.png');
				if ((typeof he.morph) == 'function') {
					he.morph('height: 48px;', { duration: 0.5 } );
					hic.morph('height: 399px;', { duration: 0.5 } );
				} else {
					he.style.height = '48px';
					hic.style.height = '399px';
				}
				document.getElementById('tabs_closebutton').style.display = 'none';
				start_bg_cycles();
			} else {
				// Tab 1 is closed. Open it.
				if ((/images\/tabs1\-down\.png$/.test(tabs.src) === true) || (/images\/tabs1\-down\.png/.test(tabs.style.filter) === true)) {
					// We're in a closed state. Morph open.
					setSource(tabs, 'images/tabs1.png');
					if ((typeof he.morph) == 'function') {
						he.morph('height: 192px;', { duration: 0.5 } );
						hic.morph('height: 263px;', { duration: 0.5 } );
					} else {
						he.style.height = '192px';
						hic.style.height = '263px';
					}
				} else {
					// We're in an open state already. Switch to the current tab.
					setSource(tabs, 'images/tabs1.png');
					he.style.height = '192px';
					hic.style.height = '263px';
				}
				document.getElementById('tabs_closebutton').style.display = 'block';
				stop_bg_cycles();
			}
			he.style.display = 'block';
			he2.style.display = 'none';
			he3.style.display = 'none';
			break;
		
		case 2:
			hic.style.backgroundImage = 'url(images/home-backgrounds/rotate_2.jpg)';
			if ((/images\/tabs1\-down\.png$/.test(tabs.src) === true) || (/images\/tabs1\-down\.png/.test(tabs.style.filter) === true)) {
				// We're in a closed state. Morph open.
				he2.style.height = '48px';
				he2.style.overflow = 'hidden';
			} else {
				he2.style.height = '192px';
			}
			setSource(tabs, 'images/tabs2.png');
			he.style.display = 'none';
			he2.style.display = 'block';
			he3.style.display = 'none';
			if ((typeof he2.morph) == 'function') {
				he2.morph('height: 192px;', { duration: 0.5 } );
				hic.morph('height: 263px;', { duration: 0.5 } );
			} else {
				he2.style.height = '192px';
				hic.style.height = '263px';
			}
			document.getElementById('tabs_closebutton').style.display = 'block';
			stop_bg_cycles();
			break;
		
		case 3:
			hic.style.backgroundImage = 'url(images/home-backgrounds/rotate_3.jpg)';
			if ((/images\/tabs1\-down\.png$/.test(tabs.src) === true) || (/images\/tabs1\-down\.png/.test(tabs.style.filter) === true)) {
				// We're in a closed state. Morph open.
				he3.style.height = '48px';
				he3.style.overflow = 'hidden';
			} else {
				he3.style.height = '192px';
			}
			setSource(tabs, 'images/tabs3.png');
			he.style.display = 'none';
			he2.style.display = 'none';
			he3.style.display = 'block';
			if ((typeof he3.morph) == 'function') {
				he3.morph('height: 192px;', { duration: 0.5 } );
				hic.morph('height: 263px;', { duration: 0.5 } );
			} else {
				he3.style.height = '192px';
				hic.style.height = '263px';
			}
			document.getElementById('tabs_closebutton').style.display = 'block';
			stop_bg_cycles();
			break;
			
		// Default, do nothing.
	}
}

function next_bg_image() {
	// Assumes there are 3 BG images.
	var hic = document.getElementById('HomeImageContainer');
	var bg = hic.style.backgroundImage;
	if (/rotate\_1\.jpg/.test(bg) === true) {
		hic.style.backgroundImage = 'url(images/home-backgrounds/rotate_2.jpg)';
	} else if (/rotate\_2\.jpg/.test(bg) === true) {
		hic.style.backgroundImage = 'url(images/home-backgrounds/rotate_3.jpg)';
	} else if (/rotate\_3\.jpg/.test(bg) === true) {
		hic.style.backgroundImage = 'url(images/home-backgrounds/rotate_1.jpg)';
	} else { 
		return false; 
	}
	return true;
}

function stop_bg_cycles() {
	if ((typeof window.bgCycler) != 'undefined') { clearInterval(window.bgCycler); }
}

function start_bg_cycles() {
	stop_bg_cycles();
	window.bgCycler = setInterval("next_bg_image()", 8000); // run every 8 seconds
}

Event.observe(window, 'load', start_bg_cycles);
