// JavaScript Document

addLoadEvent(blurLinks);
addLoadEvent(globalSearchEvents);
addLoadEvent(preloadImages);


function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	}
	else {
		window.onload = function() {
			oldonload();
			func();
		}
	}
}


/* remove all focus lines from links when clicked 
***************************************************/
function blurLinks() {
	var links = document.getElementsByTagName('a');
	for (i=0;i<links.length;i++) {
		links[i].onfocus = function() {
			this.blur();
		}
	}
}


/* focus, blur events for global search
*************************************/
function globalSearchEvents() {
	if ($('txtKeywordSearch')) {
		
		var txtField = $('txtKeywordSearch');
		
		txtField.onfocus = function() {
			if (this.value=='Enter Keyword(s)') {
				this.value = '';
			}
		}
		
		txtField.onblur = function() {
			if (this.value=='') {
				this.value='Enter Keyword(s)'
			}
		}
		
	}
}





/* centered pop up window
***************************************************/
function openWindow(URL, intWidth, intHeight) {
		var left = ((parseInt(screen.availWidth)/2)-(intWidth/2));
		var top = ((parseInt(screen.availHeight)/2)-(intHeight/2));

		newWindow = window.open(URL, "loading", "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width="+intWidth+",height="+intHeight+",left="+left+",top="+top+"")
		newWindow.focus();
}




/* show / hide sitemap
***************************************************/
function showHideSitemap() {
	
	var sitemap = $('sitemap');
	var sitemapBtn = $('btnSitemap');
	
	if (sitemap.style.display=='none') {
		sitemapBtn.className='sitemapToggleOpen';
		Effect.BlindDown(sitemap, { duration: .75 });
	} else {
		sitemapBtn.className='sitemapToggle';
		Effect.BlindUp(sitemap, { duration: .75 });
	}
	
}



/* FAQ show / hide answers 
*************************************/
var currentFAQ = '';
function faqShowAnswer(id) {
	
	// hide all open faq
	var faq = $$('#faq p span');
	
	// turn off existing div
	for (i=0;i<faq.length;i++) {
		
		//faq[i].style.display='none';
		if (currentFAQ != '') {
			if (faq[i].id == currentFAQ) {
				Effect.BlindUp(faq[i].id, { duration: .25 });
			}	
		} 
	}
	
	Effect.BlindDown(id, { duration: .25 });
	currentFAQ = id;
	
}




/* image slideshow (main sections
***************************************************/
var imgCounter = 0;
var timerStarted = 0;
var numImages=0;


function rotateImages() {
	
	//alert('rotation queue: '+imgCounter);
	
	var images = $$('#slideshow img');
	// numImages = images.length;
		
	// if timer is not yet set
	if (timerStarted==0) {
		
		timer = setInterval(rotateImages, 6000);
		timerStarted = 1;
		$(images[imgCounter]).appear({ duration: .25 });
		imgCounter++;		
	}
	
	
	// test value of imgCounter vs. number of images in array
	else {
				
		// fade out previous image
		if (imgCounter==0) {
			$(images[images.length-1]).style.display='none';
		} else {
			$(images[imgCounter-1]).style.display='none';
		}
		
		
		// show new image
		$(images[imgCounter]).appear({ duration: .25 });
		
		// if we have reached end of loop start over
		if (imgCounter == (images.length-1) ) {
			imgCounter = 0;
		} else {
			imgCounter++;	
		}
	}
	
}



function nextImage(){
	
	clearInterval(timer);
	
	//alert('stopping show at image array index: '+imgCounter);
	timerStarted = 0;
	
	// check which image to show next
	if (imgCounter == numImages - 1) {
		imgCounter = 0;
	} else {
		imgCounter++;
	}
	
	//alert('up next: '+imgCounter);
	
	// restart slideshow
	rotateImages();
	
}



/* toggle for viewing image control options in slideshow
***************************************************/
function showController() {
	$('controller').style.visibility='visible';	
}

function hideController() {
	$('controller').style.visibility='hidden';
}




/* news / events ajax examples
***************************************************/
function getNewsArticles(value) {
	
	// var value can be replaced / configured as needed for individual script
	
	//show processing animation if desired while ajax call being made
	$('news').className='loading';
	
	// ajax call
	new Ajax.Updater('news', 'assets/ajax_examples/next_news.php?foo='+value, 
		{
		onSuccess: function(transport) 
			{
				// turn off processing animation
				$('news').className='';
			}
		}
	);
	
}


function getEvents(value) {
	
	// var value can be replaced / configured as needed for individual script
	
	//show processing animation if desired while ajax call being made
	$('events').className='loading';
	
	// ajax call
	new Ajax.Updater('events', 'assets/ajax_examples/next_events.php?foo='+value, 
		{
		onSuccess: function(transport) 
			{
				// turn off processing animation
				$('events').className='';
			}
		}
	);
	
}







var imgArray = Array();

function preloadImages() {
	if (imgArray.length > 0) {
		
		 // path to the directory with images
		 var path = 'assets/images/bg/';
		 
		 if(document.images) {
			
			var preload_image = new Array ();
			for(var i=0; i<imgArray.length; i++) {
				preload_image[i]= new Image();
				preload_image[i].src = path + imgArray[i];
			}
			
		 }
		 
		
	}
}

