	
	
	//detect IE
	function isMSIE() {
		return '\v' == 'v';
	}
	
	// image fade cycle counter.
	var count = 0;
	var newBgCount;
	function countBg(){
		// queue all
		$('div.images img').each(function(index) {
			newBgCount = index;
		});		
	}
	
	// This function updates the z-index properties.
	function updateZindex(){
		// The CSS method can take a function as its second argument
		// 'i' is the zero-based index of the element.
		$("div.images").find('img').css('z-index',function(i){
			return (bgCount-i)
		})		
	}
	
		
	// This function fades the elements.
	function fadeIt() {
		countBg();
		// when the last image is displayed
		if (bgCount > 1){
			if (count == newBgCount){
				if (!isMSIE()){
					// anything non IE gets a nice fade transition.
					$("#bg0").fadeIn('slow');
					$("#bg"+newBgCount).fadeOut('show');
				} else {
					$("#bg0").css('display', 'block');
					$("#bg"+newBgCount).css('display', 'none');
					
				}
				// reset the count
				count = 0;
			} 
			// else when other images are displayed
			else {
				if (!isMSIE()){
					// anything non IE gets a nice fade transition.
					$("#bg"+(count+1)).fadeIn('slow');
					$("#bg"+(count)).fadeOut('slow');
				}else {
					$("#bg"+(count+1)).css('display', 'block');
					$("#bg"+(count)).css('display', 'none');
						
				}
					// increment the current count.
					count = count+1;
				
			}
			// recursive fading of the background elements.
			setTimeout(fadeIt, 6000); //delays next call for 3 seconds
		}
		
	}
	
	
			
	var newsOpen = false;
	var jobsOpen = false;
	
	function openNews(active) {
		var counter;
		$("."+active+" ul li").each(function(index) {
			// count how many items there are.
			counter = index;
		});	
		
		var slideDist = (((counter*49)+26)-55);
		var slideHeight = (((counter*49)+26));
		$("."+active+" ul")
			.css('display', 'block')
			.stop(true, true)
			.animate({
				top: -slideDist+"px",
				height: slideHeight+"px"
			}, 1000);
	}
	
	function closeNews(active) {
			
			$("."+active+" ul").stop(true, true).animate({
				top: '13px',
				height: "43px"
			}, 1000)
			.fadeOut('fast')
			
	}
	
	
	$(window).load(function() {
		
		
		
		// execute the update of images z-indicies.
		//updateZindex();
		
		// initiate the fading of the background elements.
		//setTimeout(fadeIt, 3000); //delays next call for 3 seconds


		$("#newsOpen").click(function(ev) {
			ev.preventDefault();
			newsOpen = true;
			openNews("latestNews");
			if(jobsOpen){
				closeNews("latestJobs");
			}
		});
		
		$("#newsClose").click(function(ev) {
			ev.preventDefault();
			closeNews("latestNews");
			newsOpen = false;
		});
		
		$("#jobsOpen").click(function(ev) {
			ev.preventDefault();
			if(newsOpen){
				closeNews("latestNews");
			}
			openNews("latestJobs");
			jobsOpen = true;
		});
		
		$("#jobsClose").click(function(ev) {
			ev.preventDefault();
			closeNews("latestJobs");
			jobsOpen = false;
		});
		
		
		
		
		
	});
