/* ---------------------------------------------------
 * Function library for North Carolina SOE Web.
 * 
 * Created by: dZ.
 *
 * Copyright (c) 2008 - SOE Software, Inc.
 * ---------------------------------------------------
*/

// Initialize global values
	var imgIdx     = 1;  // Starts from 1.
	var imgCount   = 7;  // Number of images to rotate
	var frameDelay = 15; // seconds!
	var intervalId = -1;
	var imgPath    = 'images/nc_state/';
	var imgPrefix  = 'masthead_';
	var imgType    = 'jpg';

// getNextImageUrl
function getNextImageUrl() {
	if (imgIdx >= imgCount) {
		imgIdx = 1;
	} else {
		imgIdx++;
	}

	return (imgPath + imgPrefix + imgIdx +'.' + 'jpg');
}

// enableAnimation
function enableAnimation() {
	// initialize with a random image
	// and start the timer.
	imgIdx = Math.floor(Math.random() * imgCount) + 1;
	setRotatingImage();

	intervalId = setInterval('setRotatingImage()', (frameDelay * 1000));
}

// disableAnimation
function disableAnimation() {
	clearInterval(intervalId);
	intervalId = -1;
}

// setRotatingImage
function setRotatingImage() {
	var elm = document.getElementById('RotatingImg');
	if (elm != null) {
		elm.style.backgroundImage = 'url('+ getNextImageUrl() +')';
	} else {
		disableAnimation();
	}
}

// controlAnimation
function controlAnimation() {
	if (intervalId == -1) {
		enableAnimation();
	} else {
		disableAnimation();
	}
}

// initSearchLabel
function initSearchLabel() {
	if (!document.getElementById) return;

	var label = document.getElementById('SiteSearchLabel');
	var field = document.getElementById('SiteSearchField');

	if ((label != null) && (field != null)) {
		// hide the field if it has text already
		if (field.value !== '') {
			hideSearchLabel(field, true);
		}

		// Wire field events to event-handlers
		field.onfocus = function() {
			if (this.value !== '')
				setTimeout('document.getElementById(\'SiteSearchField\').select()', 50);

			hideSearchLabel(field, true);
			};
		field.onblur  = function() {
			if (this.value === '')
				hideSearchLabel(field, false);
		};
	}
}

// hideSearchLabel
function hideSearchLabel(field, hide) {
	var label = document.getElementById('SiteSearchLabel');
	var id    = (label.htmlFor) || (label.getAttribute('for'));

	// If we have a valid label, set
	if ((label != null) && (id == field.id)) {
		label.style.zIndex = (hide)
							? -1  // Go behind the field
							: +1; // Go in front
	}

	return true;
}

