
if (typeof window.Feature == 'undefined') {
	
	var Feature = {
		
		config: {
			'heroId':  		'hero_holder',
			'showClass':	'hero_show',
			'hideClass':	'hero_hide'
		},
		
		/** 
		 * Feature.init
		 * Grabs all images within specified div ID, and sets display according to default or param.
		 * Customize script's CSS parameters in Features.config.
		 * @example 
		 * 	{default} 	Feature.init(); 	- displays 1st img within the specified div
		 * 	{number} 	Feature.init(2); 	- displays Xth image within div; if not found, displays 1st img
		 * 	{string}	Feature.init('some-image-id'); - displays img w/ matching ID; if not found, displays 1st img
		 */
		init: function(setId) {
			if (! this.config['heroId'] || this.config['heroId'] == '') { 
				/* alert('Error: required ID not found.'); */
				return false;
			}
			this.holder = document.getElementById(this.config['heroId']);
			if (!this.holder) { return false; }
			
			this.images = this.holder.getElementsByTagName('img');
			this.images_len = this.images.length;
			/*	this.images[1].useMap; */
			
			this.areas = this.holder.getElementsByTagName('area');
			areaLen = this.areas.length;
			for (var i=0; i<areaLen; i++) {
				var href = this.areas[i].href || false;
				if (href) {
					var anchorTest = href.split('#');
					if (anchorTest.length > 1) {
						this.areas[i].href = "javascript:Feature.show('"+ anchorTest[1] +"');";
					}
				}
				href=null; 
			}
			
			
			/* handle function input */
			if (setId) {
				if (typeof setId == 'string') {
					setId = setId;
				} else if (typeof setId == 'number') {
					setId--;
					if (this.images[setId] && this.images[setId].id !== false) {
						setId = this.images[setId].id; /* recast setId as string */
					} else {
						setId = false;
					}
				} else {
					setId = false;
				}
			} else { setId = false; }
			
			/* set default display */
			this.show(setId);
			
		},
		
		/**
		 *  test
		 *  See if 
		 */
		test: function(sId){
			if (!this.images || !sId || sId=='' || sId==null) { return false; }
			var out = false;
			for (var i=0; i<this.images_len; i++) {
				if (sId === this.images[i].id) {
					out = i;
				}
			}
			return out;
		},
		
		show: function(sId) {
			if (!this.images || this.images.length === 0) {
				alert('Error: Cannot toggle - target images not found.');
				return false;
			}
			
			sId= (sId != '' && sId !== false) ? sId : false;
			var setIndex = false;
			
			if (sId) {
				var idx = this.test(sId);
				setIndex = idx || false;
			}
			setIndex = setIndex || 0;
			for (var i=0; i<this.images_len; i++) {
				if (i === setIndex) {
					/*	show it */
					Feature.dom.swapClass(this.images[i], this.config['hideClass'], this.config['showClass']);
				} else {
					/*	hide it */
					Feature.dom.swapClass(this.images[i], this.config['showClass'], this.config['hideClass']);
				}
			}
			
		}
		
	}; /* end Feature */
	
	/** 
	 * DOM utilities
	 * @requires Misc. helper Object augmentations.
	 */
	Feature.dom = {
		addClass: function(node, cls) {
			var c = node.className.split(' ');
			(!c.contains(cls)) ? c.push(cls) : true ;
			node.className = c.join(' ');
		},
		removeClass: function(node, cls) {
			var c = node.className.split(' ');
			(c.contains(cls)) ? c.remove(cls) : true ;
			node.className = c.join(' ');
		},
		swapClass: function(node, sOldClass, sNewClass) {
			if (this.hasClass(node,sOldClass)) {
				this.removeClass(node, sOldClass);
			};
			this.addClass(node, sNewClass);
		},
		hasClass: function(node, classNames) {
			var args = arguments;
			var start = 1;
			if (typeof arguments[1] == 'object') {
				args = arguments[1];
				start = 0;
			};
			var out = false;
			for (var i=start; i<args.length; i++) {
				if (node.className.split(' ').contains(args[i])) {
					out = true;
					break;
				};
			};
			return out;
		}
	};
	Feature.event = {
		add: function(obj, evType, fn){
			if (obj.addEventListener){
				obj.addEventListener(evType, fn, true);
				return true;
			} else if (obj.attachEvent){
				var r = obj.attachEvent("on"+evType, fn);
				return r;
			} else {
				return false;
			};
		},
		stop: function(e) {
			if (e) {
				if (e.preventDefault) {	/* W3C */
					e.preventDefault();
					e.stopPropagation();
				} else {				/* IE */
					e.returnValue = false;
					e.cancelBubble = true;
				};
			} else {
			};
			return false;
		}
		
	};
	
	/**
	 * Helpers for DOM utilities - augment the Array Object 
	 */
	Object.augment = function(oDestination, oSource) {
		for (var property in oSource) {
			if (typeof oDestination[property] == 'undefined') {
				oDestination[property] = oSource[property];
			}
		}
		return oDestination;
	};
	Object.augment(String.prototype, {
		contains: function(re, str) {
			if (str.search(re) != -1) { return true; } 
			else { return false; }
		}
	});
	Object.augment(Array.prototype, {
		push: function(item) { 
			this[this.length] = item;
			return this.length;
		},
		splice: function(index,count){
			if (arguments.length == 0) { return index; }
			if (typeof index != 'number') { index = 0; }
			if (index < 0) { index = Math.max(0,this.length + index); }
			if (index > this.length) {
				if (arguments.length > 2) { index = this.length; }
				else { return []; }
			}
			if (arguments.length < 2) { count = this.length-index; }
			count = (typeof count == 'number') ? Math.max(0,count) : 0;
			removeArray = this.slice(index,index+count);
			endArray = this.slice(index+count);
			this.length = index;
			for (var i=2;i < arguments.length;i++){
				this[this.length] = arguments[i];
			}
			for (var i=0;i < endArray.length;i++){
				this[this.length] = endArray[i];
			}
			return removeArray;
		},
		indexOf: function (obj, si) {
			if (si == null) {
				si = 0;
			} else if (si < 0) {
				si = Math.max(0, this.length + si);
			}
			for (var i=si; i < this.length; i++) {
				if (this[i] === obj) { return i; }
			}
			return -1;
		},
		contains: function (obj) {
			return this.indexOf(obj) != -1;
		},
		remove: function (obj) {
			var i = this.indexOf(obj);
			if (i != -1) {
				this.splice(i, 1);
			}
		}
	});

}; /* CLOSE OBJECT TEST */
/* ---------------------*/

if (typeof window.Feature == 'object') {
	Feature.event.add(window, 'load', function(){ 
		Feature.init() 
	});
};

