// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults
document.observe('dom:loaded', displayBanners);
document.observe('dom:loaded', displayImages);
document.observe('dom:loaded', resetSlideShow);


function displayBanners() {
	if ($('home_nav')) {
		Effect.multiple('home_nav', Effect.BlindDown, { speed: 0.15 });
	}
}

/*
** Method to hide homepage banner when clicked
** effect to blind up
** Load new location (url) after short delay to allow for animation to complete
*/
function hideBannerLoadPage(banner, url) {
	if ($(banner)) {
		Effect.BlindUp($(banner).parentNode);
		window.setTimeout(("window.location='" + url +"'"), 500);
	}
}

function displaySecondaryMenu(service_menu) {
	// Toggle display of submenu
	// Cannot use Effect.Toggle with Effect.multiple
	if($(service_menu).firstChild.style.display == "none") {
		Effect.multiple(service_menu, Effect.BlindDown);
	} else {
		Effect.multiple(service_menu, Effect.BlindUp);
	}
}

/* 
** Automatic slideshow functionality
** If multiple images exist in ul element
** Images will be fade in then out to reveal the next image
** If singular image, fade in occurs but nothing else
*/
function displayImages() {
	if ($('image_list')) {
		//reset image display style
		for(z=0; z < $$('.slideShowImage').length; z++) {		
			if($$('.slideShowImage').length > 1) {
				$$('.slideShowImage')[z].style.display = "none";
				$$('.slideShowImage')[z].style.position = "absolute";
				$$('.slideShowImage')[z].style.zIndex = 500 - z;
			}
		}
		
		var global_num = 0;
		
		/* set current image to fade out and next image to fade in */
		function switchImage() {
			
			//console.log($$('.slideShowImage').length + " :: " + global_num);
			
			Effect.Fade($$('.slideShowImage')[global_num], {duration: 1.75 });
			if (global_num >= $$('.slideShowImage').length-1) {
				global_num = 0;
				Effect.Appear($$('.slideShowImage')[global_num], {duration: 1.5 });
			} else {
				Effect.Appear($$('.slideShowImage')[global_num+1], {duration: 1.5 });
				global_num += 1;
			}
		}

		/* Set timed interval to call the next image in the slide show if more than one image exists */
		if ($$('.slideShowImage').length > 1) {
			new PeriodicalExecuter(switchImage, 3)
		}
		
		/* Set display of first image before animation begins */
		if (global_num == 0) {
			$$('.slideShowImage')[0].style.display = "block";
		}

	}
}

/* 
** Reset the slideshow to begin from first image
** Call periodically after all images have been displayed 
** length determined by total images * x
*/
function resetSlideShow() {
	if ($('image_list')) {
		var length = ($('image_list').childElements().length)*3 //set this value for the # of seconds you want the final image to hold before restarting the sequence

  	//reset image slide show after all image have been shown
		//new PeriodicalExecuter(displayImages, length);
	}
}