﻿/* Queue the Site Load Events
===================================================================================== */
 
addLoadEvent(initCaseStudies) ;
addLoadEvent(initMenu) ;
addLoadEvent(initLabels) ;

function initLabels() {
	$(".moveLbls label").hide();

	$(".moveLbls input:text").each(function() {
		setText($(this));
		$(this).focus(function() { clearText($(this)); });
		$(this).blur(function() { setText($(this)); });
	});
	
	$(".moveLbls select").each(function() {
		setList($(this));
	});
	
	$(".moveLbls input:submit,.moveLbls input:image").click(function() {
		$(".moveLbls input:text").each(function() { clearText($(this)); });
	});
	
	
	function setText(txtBox) {
		if ($(txtBox).val() == '') $(txtBox).val($(txtBox).siblings("label").text());
	}
	function clearText(txtBox) {
		var val = $(txtBox).siblings("label").text();
		if ($(txtBox).val() == val) $(txtBox).attr('value', '');
	}

	function setList(list) {
		var val = $(list).siblings("label").text();
		if ($(list).find("option:first").val() != val) $(list).find("option:first").text(val);
	}
}

function initMenu() {
	var menu = document.getElementById("menu") ;
	
	/* set all heights to 0 and store original height */
	var subs = menu.getElementsByTagName('ul') ;
	var i ;
	for (i=0; i < subs.length; i++) {	
		var sub = subs[i] ;
		sub.id = 'sub' + i ;
		sub.style.display = 'block' ;
		sub.oHeight = parseInt(sub.offsetHeight) ;
		if ((getElementsByClass('on',sub,'li').length == 0) && (sub.parentNode.className.lastIndexOf('on') == -1)) {
			sub.style.height = '0' ;
		} else {
			sub.style.height = 'auto' ;
		}
	}
	subs[0].style.height = subs[0].oHeight ;
	
	/* reference all handles */
	var links = menu.getElementsByTagName('a') ;
	for (i=0; i < links.length; i++) {
		if (links[i].parentNode.getElementsByTagName('ul').length > 0) {
			links[i].onclick = expand ;
		}
	}
	/* force top level to shrink in IE  */	
	/*@cc_on @*/
	/*@if (@_win32)
		menu.style.height = '1%' ;
	/*@end @*/
	
}

function expand() {
	var sub = this.parentNode.getElementsByTagName('UL')[0] ; 	
/* set current height */	
	var ch = (sub.style.height == 'auto' ? sub.offsetHeight : parseInt(sub.style.height)) ;
/* set all parents to auto height */
	var p = sub.parentNode ;
	while (p.nodeName != 'DIV') {
		if (p.nodeName == 'UL') p.style.height = 'auto' ;
		p = p.parentNode ;
	}
/* Determine target height */
	var finH = 0 ;
	if (ch == 0) {
		sub.style.height = 'auto' ;
		finH = sub.offsetHeight ;
		sub.style.height = 0 ;
	} else {
/* contract all immediate children */	
		var childSubs = sub.parentNode.childNodes ;
		for (i=0; i < childSubs.length; i++) {
			if (childSubs[i].nodeName == 'UL') childSubs[i].style.height = 'auto' ;
		}
	}
	
/* Finshing moving if clicked again */
	if (sub.movement) {		
		sub.style.height = (sub.finH == 0 ? 0 : sub.finH) + 'px' ;
/* Finish moving others if clicked */
	} else {
		var p = sub.parentNode.parentNode ;
		while (p.nodeName != 'DIV') {
			if (p.movement) {	
				clearTimeout(p.movement) ;
				p.movement = null ;
				p.style.height = 'auto' ;
			}
			p = p.parentNode ;
		}
/* Call Animation */
		animate(sub.id, finH, 30) ;
		sub.finH = finH ;
	}
	return false ;
}

function animate(subId, finH, interval) {
	var sub = document.getElementById(subId) ;
	var ch = (sub.style.height == 'auto' ? sub.offsetHeight : parseInt(sub.style.height)) ;	
	//if (sub.movement) clearTimeout(sub.movement) ;	
	if (ch == finH) {
		sub.movement = null ;		
		return true ;
	}	
	if (ch < finH) {
		var dist = Math.ceil((finH - ch)/3) ;
		ch += dist ;
	}
	if (ch > finH) {
		var dist = Math.ceil((ch - finH)/3) ;
		ch -= dist ;
	}
	sub.style.height = ch + 'px' ;
	sub.movement = setTimeout(function() {animate(subId, finH, interval)}, interval) ;		
}


function initCaseStudies() {
	var g = 1 ;
	var j = 0 ;
	
	$("#sectors img").hide() ;
	$.each (
		$("#sectors li"),
		function(i, n){
			if (j == 3) {g++ ; j = 0 ; }
			j++ ;
			$(n).attr("g", g) ;
		}
	)
	$("#sectors li[@g=1] img").show() ;
	
	$("#sectors li a").click (
		function() {
			if ($(this).children("img").css("display") == "block") return ;
			var a = $(this).parent().attr("g") ;
			$("#sectors img:visible").slideUp("slow") ;
			$("#sectors li[@g=" + a + "] img").slideDown("slow") ;	
			return false;
		}
	) ;
}