// --------------------------------------------------------------------------------
// nspg_Application.js
// Travis Musika Aug 26, 2005
// Contains Javascript relevant to the NSMultipleImage application.
// --------------------------------------------------------------------------------

var nspg_fieldPrefix = "tmpl_nspgApp";
var nspg_images = new Array();
var nspg_current = 1;
var nspg_indexFormat;

// --- Attach JS to document elements and execute simple statements. ---
ow_f_AppendLoadEvent(
    function() {

		// add the rollovers
		var nspg_img = null;
		if (document.getElementById)
			nspg_img = document.getElementById("nspg_img");
		else if (document.all)
			nspg_img = document.all("nspg_img");
			
		if (nspg_img != null) {
			var nspg_thumb = null;
			for (var i=1;(nspg_thumb = nspg_get("nspg_thumb" + i)) != null;i++) {
				if (nspg_thumb.getAttribute("imageSrc") != null) {
					ow_f_AddEvent(nspg_thumb, (nspg_triggerEvent != null) ? nspg_triggerEvent : "mouseover", nspg_imageChange, false);

					// preload rollover images
					if (document.images) {
						nspg_images[i] = new Image(nspg_thumb.getAttribute("width"),nspg_thumb.getAttribute("height"));
						nspg_images[i].src = nspg_thumb.getAttribute("imageSrc");
					}
				}
			}
		}
		var nspg_indexbox =  nspg_get("nspg_indexbox");
		if (nspg_indexbox != null) {
			nspg_indexFormat = nspg_indexbox.childNodes[0].data;
			nspg_showCurrent();
		}
    }
);

// --------------------------------------------------------------------------------
// nspg_imageChange()
// Changes the current image index when the trigger event fires.
// --------------------------------------------------------------------------------
// Arguments:
//	- e - event object
// Returns:
//	- none
// --------------------------------------------------------------------------------
function nspg_imageChange(e) {
	if (!e) var e = window.event;
	var tg = (window.event) ? e.srcElement : e.target;
	nspg_current = parseInt(tg.getAttribute("id").replace("nspg_thumb", ""));
	nspg_showCurrent();
	
}

// --------------------------------------------------------------------------------
// nspg_showPrevious()
// Changes the current image to the previous in the array.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- none
// --------------------------------------------------------------------------------
function nspg_showPrevious() {
	//if (nspg_current <= 1) return;
	var len = nspg_images.length - 1;		// the array has an empty element at index 0
	nspg_current=(((nspg_current-2)+len)%len)+1;
	nspg_showCurrent();
}

// --------------------------------------------------------------------------------
// nspg_showNext()
// Changes the current image to the next in the array.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- none
// --------------------------------------------------------------------------------
function nspg_showNext() {
	//if (nspg_current >= nspg_images.length - 1) return;
	var len = nspg_images.length - 1;		// the array has an empty element at index 0
	nspg_current=(nspg_current%len)+1;
	nspg_showCurrent();
}

// --------------------------------------------------------------------------------
// nspg_showCurrent()
// Shows the currently selected image.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- none
// --------------------------------------------------------------------------------
function nspg_showCurrent() {
	var nspg_thumb = nspg_get("nspg_thumb" + nspg_current);
	if (nspg_thumb != null) {
		if (nspg_thumb.getAttribute("imageSrc") != null) {
			var nspg_img = nspg_get("nspg_img");
			if (nspg_img != null) {
				nspg_img.src = nspg_thumb.getAttribute("imageSrc");
				nspg_img.title = nspg_thumb.getAttribute("title").replace(/<\S[^><]*>/g,"") + " \n" + nspg_thumb.getAttribute("subtitle").replace(/<\S[^><]*>/g,"");
			}
		}
		if (nspg_thumb.getAttribute("title") != null) {
			var nspg_title = nspg_get("nspg_title");
			if (nspg_title != null)
				nspg_title.innerHTML = nspg_thumb.getAttribute("title");
		}
		if (nspg_thumb.getAttribute("subtitle") != null) {
			var nspg_subtitle = nspg_get("nspg_subtitle");
			if (nspg_subtitle != null)
				nspg_subtitle.innerHTML = nspg_thumb.getAttribute("subtitle");			
		}
	}
	nspg_setIndex();
}

// --------------------------------------------------------------------------------
// nspg_setIndex()
// Sets the index display box to the current index number.
// --------------------------------------------------------------------------------
// Arguments:
//	- none
// Returns:
//	- none
// --------------------------------------------------------------------------------
function nspg_setIndex() {
	var nspg_indexbox =  nspg_get("nspg_indexbox");
	if (nspg_indexbox != null) {
		var text = nspg_indexFormat;
		text = text.replace("{0}", nspg_current);
		text = text.replace("{1}", nspg_images.length - 1);
		nspg_indexbox.childNodes[0].data = text;
	}
}

// --------------------------------------------------------------------------------
// nspg_get()
// Cross-browser function to get an element by id.
// --------------------------------------------------------------------------------
// Arguments:
//	- id - the id of the element
// Returns:
//	- none
// --------------------------------------------------------------------------------
function nspg_get(id) {
	return (document.getElementById) ? document.getElementById(id) : (document.all) ? document.all(id) : (document.layers) ? document.layers[id] : null;
}
