var swapswap = {
	suffix : "2", // default image file name suffix (before file type suffix, like .jpg)

	/**
	 * init()
	 * Initializes swapswap functionality, finding all img elements with a class of swap 
	 * and attaching the swap() function to them.
	 * @param suff	Optional suffix for mouseover state images. The default is 2, but can be anything you want.
	 */
	init : function(suff) {
		this.suffix = (suff == "") ? this.suffix : suff;			// check for supplied suffix. use default if not found
		var imgs = document.getElementsByTagName("img");			// get all the images on the page
		for (var i=0; i<imgs.length; i++) {
			if(imgs[i].className.indexOf("swap") != -1) {			// find the img.swap elements
				
				var img_path_array = imgs[i].src.split("/");
				var img_file_name = img_path_array[img_path_array.length - 1];	// the image file name
				var img_type = img_file_name.split(".")[1];		// the image type (jpg, gif, png)
	
				var img_alternate = new Image();					// preload the OVER state
				img_alternate.src = imgs[i].src.substring(0, imgs[i].src.length - 4) + this.suffix + "." + img_type;
				
				// attach the swap function to the img.swap elements
				imgs[i].onmouseover = function() { swapswap.swap(this); }
				imgs[i].onmouseout = function() { swapswap.swap(this); }
			}
		}
	},
	
	/**
	 * swap()
	 * Interchanges the src of the supplied image with an alternate image
	 * @param img	An img element
	 */
	swap : function(img) {
		var src = img.src;
		var img_path_array = img.src.split("/");
		var img_file_name = img_path_array[img_path_array.length - 1];
		var img_type = img_file_name.split(".")[1];
		var newSrc = "";																			// Variable to hold the new image to swap in
		if (src.indexOf(this.suffix + "." + img_type) == -1) {										// Check if current image NOT is the MouseOver state image
			newSrc = src.substring(0, src.length - 4) + this.suffix + "." + img_type;				// Swap image src attribute to the MouseOver state image
		} else {																					// current image IS the mouseOver state image
			newSrc = src.substring(0, src.length - (4 + this.suffix.length)) + "." + img_type;		// swap src attribute to the NORMAL state image
		}
		img.src = newSrc;																			// Set the image src attribute
	}
}

var navBGSwapper = {
	init: function() {
		if (document.body.id != 'body_index') { return; }
		
		this.imgs = [
			'lashSplashBlink',
			'lashSplashBlink',
			'lashSplashBlink',
			'lashSplashBlink',
			'lashSplashBlink'
		];
		this.imgs2 = {};
		
		var nav = document.getElementById('level_1');
		var j = 0;
		for (var i=0; i<nav.childNodes.length; i++) {
			var node = nav.childNodes[i];
			if (node.nodeName == 'LI') {
				var lnk = node.getElementsByTagName('a')[0];
				var key = this.toKey(lnk.href);
				var value = this.imgs[j];
				this.imgs2[key] = value;
				j++;
				$event.add(lnk,'mouseover',this.swap);
				$event.add(lnk,'mouseout',this.swapOff);
			}
		}
		this.imgs = this.imgs2;
	},
	swap: function() {
		var img = rootVirtual + '/public/img/' + navBGSwapper.imgs[navBGSwapper.toKey(this.href)] + '.jpg';
		var body = document.getElementsByTagName('body')[0];
		body.style.backgroundImage = 'url(' + img + ')';
	},
	swapOff: function() {
		var img = rootVirtual + '/public/img/lashSplash.jpg';
		var body = document.getElementsByTagName('body')[0];
		body.style.backgroundImage = 'url(' + img + ')';
	},
	toKey: function (sValue) {
		return sValue.replace(/\//g,'_').replace('http:','');
	}
}

var Ticker = {
	msg: "This is the default message.",
	separator: "     ",
	txt: "",

	build: function(sElementID, sMessage, sSeparator) {
		this.el = document.getElementById(sElementID);
		this.msg = sMessage || this.msg;
		this.separator = sSeparator || this.separator;
		if (!this.el) { return; }
		while (this.txt.length < 1000) {
			this.txt += this.msg + this.separator;
		}
		this.el.innerHTML = this.txt;
		this.go();
	},
	update: function() {
		var first = this.txt.substr(0,1);
		var rest = this.txt.substr(1);
		this.txt = rest + first;
		this.el.innerHTML = this.txt;
	},
	go: function() {
		this.timerID = setInterval("Ticker.update()",90);
	},
	pause: function() {
		clearInterval(this.timerID);
	}
}

$event.add(window,'load',function() {
	swapswap.init("2");

	// Move div out of current spot and to the top of the document
});