
function extensionsObject() {

	//initialize....

	//add a groups object if it doesnt exist
	if (document.groups == null) { document.groups = new Object(); }

	this.extendButtons = extendButtons;
	this.extendElementsByTagName = extendElementsByTagName;
	this.extendElement = extendElement;

	this.addAttributeByTagName = addAttributeByTagName;
	this.setStyle = setStyle;

	function addAttributeByTagName(tag, att, val, filterAtt, filterVal) {


		//warning! NOT case specific


		var ar = document.getElementsByTagName(tag);


		for (var i = 0; i < ar.length; i++) {

			var a = ar[i];

			var bAddAtt = false;

			//if we are filtering by an attribute
			if (filterAtt != null && filterAtt != '') {

				//if this node has that attribute
				if (a.getAttribute(filterAtt)) {


					//if the filterAtt is equal to the filterVal 
					if (a.getAttribute(filterAtt).toString().toLowerCase() == filterVal.toString().toLowerCase()) {

						//then set the node to have the tagname added
						bAddAtt = true;
					}
				}
			} else {
			
				bAddAtt = true;
			}

			if (bAddAtt == true) {
				a.setAttribute(att, val);
			}

		}
	}

	function extendElementsByTagName(t, filterAtts, filterVals) {
		//extend all the elements collected with this tag

		var els = document.getElementsByTagName(t);

		if (filterAtts != null) {
			var filterAtts = filterAtts.split(',');
		} else {
			var filterAtts = new Array();
		}

		if (filterVals != null) {
			var filterVals = filterVals.split(',');
		} else {
			var filterVals = new Array();
		}

		for (var i = 0; i < els.length; i++) {
			var bExtend = false;

			var el = els[i];

			if (filterAtts.length > 0) {

				//if we are filtering by an attribute
				for (var k = 0; k < filterAtts.length; k++) {
			
					var filterAtt = filterAtts[k];

					//if this node has that attribute
					if (el.getAttribute(filterAtt)) {

						//if the filterAtt is equal to the filterVal 
							
						if (filterVals.length == 0) {
							bExtend = true;
						} else {
							for (var j = 0; j < filterVals.length; j++) {
								if (el.getAttribute(filterAtt).toString().toLowerCase() == filterVals[j].toString().toLowerCase()) {

									bExtend = true;
								}
							}
						}

					}

				}
			} else {

				bExtend = true;
			}

			if (bExtend) {
				this.extendElement(el);
			}

		}

	}

	function extendButtons() {
		//extend all the button tags
		extendTheseButtons(document.getElementsByTagName('button'));

		//extend all input/@type=button|submits|image tags
		extendTheseButtons(document.getElementsByTagName('input'));
	}

	function extendTheseButtons(btns) {

		for (var i = 0; i < btns.length; i++) {
			var btn = btns[i];

			if (btns.type) {
				if (btn.type.toLowerCase() == 'image' || btn.type.toLowerCase() == 'submit' || btn.type.toLowerCase() == 'button') {
					extendElement(btn);
				}
			} else {
				extendElement(btn);
			}
		}
	}

	function extend_src(o) {

		//get the classHover and classActive
		var srcHover = o.getAttribute("srcHover")
		var srcActive = o.getAttribute("srcActive");
		var srcOut = o.getAttribute("srcOut");

		//add the img
		o.imgNormal		= new Image();
		o.imgNormal.src	= o.src;

		//onmouseout	-- this is the default mouseout after a mouseover. any srcOut will overide this
		if (o.src != null && srcHover != null && srcOut == null) {

				//merge script with any existing onmouseout code
			o.script_onMouseOut	= mergeScript(o.script_onMouseOut, default_onMouseOut);

		}

		if (srcOut!=null) {
			o.imgOut		= new Image();
			o.imgOut.src	= srcOut;

				//merge script with any existing onmouseover code
			o.script_onMouseOut = mergeScript(o.script_onMouseOut, specific_onMouseOut);

		}

		//onmouseover
		if (srcHover!=null) {
			o.imgHover		= new Image();
			o.imgHover.src	= srcHover;


			//merge script with any existing onmouseover code
			o.script_onMouseOver = mergeScript(o.script_onMouseOver, onMouseOver);

			//default onmouseout
			//if no srcOut is specified, we want the onmouseout to restore what the onmouseover changed
			if (srcOut == null)
				o.script_onMouseOut = mergeScript(o.script_onMouseOut, default_onMouseOut);

			//default onmousedown for hovers
			//if no srcActive is specified, we want the onmousedown to set this image and the active image for this group.
			if (srcActive == null) {
				o.imgActive		= new Image();
				o.imgActive.src	= srcHover;

					//merge script with any existing onmousedown code
				o.script_onMouseDown = mergeScript(o.script_onMouseDown, onMouseDown);
			}

		}

		//onmousedown - if an active was set
		if (srcActive!=null) {
			o.imgActive		= new Image();
			o.imgActive.src	= srcActive;

				//merge script with any existing onmousedown code
			o.script_onMouseDown = mergeScript(o.script_onMouseDown, onMouseDown);
		}


		function onMouseDown() {

			if (this.groupName != 'nogroup') {
				if (this.group.activeImage != null) {
					this.group.activeImage.src = this.group.activeImage.imgNormal.src;
				}

				this.group.activeImage = this;
			}
			this.src = this.imgActive.src;
 
		}

		function onMouseOver() {


			if (this.groupName != 'nogroup') {

				//only set img active on mouseovers when there is a specfic imgOut
				if (this.imgOut != null) {

					if (this.group.activeImage != null && this.group.activeImage != this) {
						this.group.activeImage.src = this.group.activeImage.imgNormal.src;
					}

					if (this.group.activeImage != this) {
						this.src = this.imgHover.src;
					}

					this.group.activeImage = this;

				} else {
					this.src = this.imgHover.src;
				}


			} else {
				this.src = this.imgHover.src;
			}


		}

		function specific_onMouseOut() {

			if (this.groupName != 'nogroup') {

				if (this.group.activeImage != null) {

					if (this.group.activeImage != this) {
						this.group.activeImage.src = this.group.activeImage.imgNormal.src;
						this.src = this.imgOut.src;
					} 
					//if this item IS the active image, dont hide it...

				} else {
					this.src = this.imgOut.src;
				}

				this.group.activeImage = this;

			} else {
				this.src = this.imgOut.src;
			}
		}

		function default_onMouseOut() {
//alert ('out.. active? ' + (this != this.group.activeImage));

			if (this != this.group.activeImage) { this.src = this.imgNormal.src; }
		}
	}




	function extend_class(o) {

		//get the classHover and classActive
		var	classHover = o.getAttribute("classHover")

		var	classActive = o.getAttribute("classActive");


		//add the classNames to the object
		o.classNormal	= o.className;
		o.classHover	= classHover;
		o.classActive	= classActive;

		//onmouseout
			if (classHover != null || classActive != null) {

				o.script_onMouseOut = mergeScript(o.script_onMouseOut, onMouseOut);
			}

		//onmouseover
			if (classHover != null) {

				//build the onmouseover function, merge it with any existing onmouseover code
				o.script_onMouseOver = mergeScript(o.script_onMouseOver, onMouseOver);


			}
		//onmousedown
			if (classActive != null) {

				o.script_onMouseDown = mergeScript(o.script_onMouseDown, onMouseDown);

			}


			function onMouseDown() {

				if (this.groupName != 'nogroup') {
					if (this.group.activeClass != null) {
						this.group.activeClass.className = this.group.activeClass.classNormal;
					}
					//alert('setting active class');
					this.group.activeClass = this;
				}
				this.className = this.classActive;

			}

			function onMouseOver() {
				if (this != this.group.activeClass) { this.className = this.classHover; }
			}

			function onMouseOut() {
				if (this != this.group.activeClass) { this.className = this.classNormal; }

			}

	}



	function extend_show(o) {


			//set the classHover
		var showHover = o.getAttribute("showHover");

			//set showOut to overide the autohide of the hover!
		var showOut = o.getAttribute("showOut");

			//set showOut to overide the autohide of the hover!
		var showDefault = o.getAttribute("showDefault");

		
		//initialize default arrays
		o.showDefault = new Array();

		o.showHover = new Array();

		o.showOut = new Array();

		if (o.group.activeShow == null)
			o.group.activeShow = new Array();

		if (o.group.defaultShow == null)
			o.group.defaultShow = new Array();


			//show any specified default
		if (showDefault != null) {

			showDefault = showDefault.split(',');

			for (var i = 0; i < showDefault.length; i++)
				o.showDefault[i] = document.getElementById(showDefault[i]);

			if (o.groupName != 'nogroup') {

				o.group.defaultShow = o.showDefault;
				o.group.activeShow = o.showDefault;

				for (var i = 0; i < o.group.activeShow.length; i++)
					if (o.group.activeShow[i] != null)
						o.group.activeShow[i].style.display = '';
			}
		}

		if (showHover != null) {
			o.showHover = showHover.split(',');
			for (var i = 0; i < o.showHover.length; i++)
				o.showHover[i] = document.getElementById(o.showHover[i]);

			//default onmouseout	-- onmouseout hides the showHover by default!
						// if showOut is set, then it doesnt define the onmouseout here
						// otherwise, hovers get hidden onmouseout					
			if (showOut == null) {

				o.script_onMouseOut = mergeScript(o.script_onMouseOut, default_onMouseOut);
			}

			//onmouseover

			//build the onmouseover function, merge it with any existing onmouseover code
			o.script_onMouseOver = mergeScript(o.script_onMouseOver, onMouseOver);

		}

		if (showOut != null) {
			showOut = showOut.split(',');
			for (var i = 0; i < showOut.length; i++)
				o.showOut[i] = document.getElementById(showOut[i]);

			//specified onmouseout

			//build the onmouseover function, merge it with any existing onmouseover code
			o.script_onMouseOut = mergeScript(o.script_onMouseOut, specific_onMouseOut);
		}


		function onMouseOver() {
			if (this.groupName != 'nogroup') {

//				for (var i = 0; i < this.group.activeShow.length; i++)

setStyle(this.group.activeShow, 'display', 'none');

//					if (this.group.activeShow[i] != null)
//						this.group.activeShow[i].style.display = 'none';

setStyle(this.group.defaultShow, 'display', 'none');

//				for (var i = 0; i < this.group.defaultShow.length; i++)
//					if (this.group.defaultShow[i] != null)
//						this.group.defaultShow[i].style.display = 'none';

this.group.activeShow = this.showHover;
			}
setStyle(this.showHover, 'display', '');

//			for (var i = 0; i < this.showHover.length; i++)
//				if (this.showHover[i] != null)
//					this.showHover[i].style.display = '';
		}

		function specific_onMouseOut() {
			if (this.groupName != 'nogroup') {
				if (this.group.activeShow!=null) {
					for (var i = 0; i < this.group.activeShow.length; i++)
						if (this.group.activeShow[i] != null)
							this.group.activeShow[i].style.display = 'none';
				}
				this.group.activeShow = this.showOut;
			}
			for (var i = 0; i < this.showOut.length; i++) {
	
				if (this.showOut[i] != null) {
					this.showOut[i].style.display = '';
				}
			}
		}

		function default_onMouseOut() {
				//redisplay the active show if one exists
			if (this.groupName != 'nogroup') {
				if (this.group.defaultShow!=null) {
					for (var i = 0; i < this.group.defaultShow.length; i++)
						if (this.group.defaultShow[i] != null)
							this.group.defaultShow[i].style.display = '';
				}
			}
			for (var i = 0; i < this.showHover.length; i++)
				if (this.showHover[i] != null)
					this.showHover[i].style.display = 'none';
		}

	}

	function extend_initializeGroup(o) {

		//setup group settings in case this object is in a group of objects
		//that will act together.

		//get the group attribute setting, if no group, then it uses "nogroup".
		if (o.getAttribute('group') != null) {
			o.groupName = o.getAttribute('group');
		} else {
			o.groupName = 'nogroup';
		}
		var grp = o.groupName;

		//add the group to the document.groups object
		if (eval('document.groups.' + grp) == null) {
			eval('document.groups.' + grp + ' = new Object()');

			//intiailize special group setting for buttons, objects, etc
			eval('document.groups.' + grp + '.activeButton = null');
		}
		//add the document.groups.grp pointer to the btn
		o.group	= eval('document.groups.' + grp);

	}

	function extend_ellipsis(el) {

		var ellipsis = el.getAttribute('ellipsis');

		if (ellipsis != null) {
			ellipsis = document.getElementById(ellipsis);

			var scale = parseInt(el.getAttribute('ellipsisScale'));
			if (isNaN(scale)) {
				if (el.group.ellipsisScale > 0) {
					scale = 7;
				} else {
					scale = el.group.ellipsisScale;
				}
			}
			
			
			el.group.ellipsisScale = scale;

			var txt = ellipsis.innerText;
			var w = parseInt(el.clientWidth / scale);

			if (txt.length > w) {
				el.innerText = txt.substring(0, w) + '...';
				if (el.showHover == null) {
					el.showHover = ellipsis.id;
				} else {
					el.showHover += ',' + ellipsis.id;
				}
			} else {
				el.innerText = txt;
			}
		}
	}

	function extendElement(el) {		
//db('extending button ' + el.tagName);

		if (el.constructor == String) {
			el = document.getElementById(el);
			if (el == null) {
				return;
			}
		}

		//first, we setup group settings in case this element is in a group of elements
		//that will act together.

		extend_initializeGroup(el);

		el.script_onMouseOut = mergeScript(el.onmouseout, '');
		el.script_onMouseOver = mergeScript(el.onmouseover, '');
		el.script_onMouseDown = mergeScript(el.onmousedown, '');


		el.onmouseout  = extensions_onMouseOut;
		el.onmousedown = extensions_onMouseDown;
		el.onmouseover = extensions_onMouseOver;


		//setup rollover button class attributes
		extend_class(el);

		//setup rollover img src attributes
		extend_src(el);

		//do ellipsis first because it sets a showHover that we want extend_show to capture
		extend_ellipsis(el);

		//show rollovers
		extend_show(el);


	//	el.onmouseup = function() {
	//		if (this.group == 'nogroup') {
	//			el.onmouseover();
	//		}
	//	}

		if (el.getAttribute('link') != null) {
			
			//only take over onClick if there is a link attribute
//			el.script_onClick	= mergeScript(el.onclick, '');
			el.script_onClick = mergeScript(el.onclick, onclick_target);


//			if (el.getAttribute('target') == null) {
//				el.script_onClick = mergeScript(el.script_onClick, 'document.location = "' + el.getAttribute('link') + '";');
//			} else {
//				el.script_onClick = mergeScript(el.script_onClick, el.getAttribute('target') + '.location = "' + el.getAttribute('link') + '";');
//			}
			el.onclick			= extensions_onClick;

		}

		function onclick_target() {

			var t = this.getAttribute('target');
			if (t == null) {

				document.location = this.getAttribute('link');

			} else {

				if (document.frames != null) {
				
					if (document.frames[t]!=null) {

						document.frames[t].location = this.getAttribute('link');

					} else {

						var t = window.open(this.getAttribute('link'), t);
					}
				} else {

					var t = window.open(this.getAttribute('link'), t);
				}
			}
			
		}


		var extendWidthHeight = false;

		if (el.tagName != 'IMAGE') {
			if (el.type != null) {
				//if type is NOT null, then if it is not an image, 
				// then allow extension of the width and height

				if (el.type.toLowerCase() != 'image') {
					extendWidthHeight = true;
				}
			} else {
				//if type IS null, then allow extension of width and height if
				// it is a DIV or SPAN tag

				if (el.tagName == 'DIV' || el.tagName == 'SPAN') {
					extendWidthHeight = true;
				}
			}
		}

		if (extendWidthHeight) {
			//if width hasnt already been set
			if (el.width != 0) {

				// then if there is an attribute for width
				if (el.getAttribute('width') != null) {

					//then set the style.width
					el.style.width = el.getAttribute('width');
				}
			}

			//ditto
			if (el.height != 0) {

				if (el.getAttribute('height') != null) {
					el.style.height = el.getAttribute('height');
				}
			}
		}
	}

	function extensions_onMouseOut(event) {
		//alert(this.script_onMouseOut);
		return eval(this.script_onMouseOut);
	}
	function extensions_onMouseOver(event) {
		return eval(this.script_onMouseOver);
	}
	function extensions_onMouseDown(event) {
		return eval(this.script_onMouseDown);
	}
	function extensions_onClick(event) {
		return eval(this.script_onClick);
	}

	function setStyle(o, s, v) {
		
		if (o.constructor == String) {
			
			o = document.getElementById(o);
			
		}
	
		if (o.constructor == Array) {

			for (var i = 0; i < o.length; i++) {
				if (o[i] != null) {
					if (o[i].style != null) {
						eval('o[i].style.' + s + ' = "' + v + '"');
					}
				}
			}
		} else {

			if (o != null) {
				if (o.style != null) {
					eval('o.style.' + s + ' = "' + v + '"');
				}
			}
		}
			
	}


	function mergeScript(s1, s2) {
		
		if (s1) {
			s1 = s1.toString();
		} else {
			s1 = '';
		}
		
		if (s2) {
			s2 = s2.toString();
		} else {
			s2 = '';
		}

		if (s1.substring(0, 8) == 'function') {
			//replace the last curly brace, and the function prefix (usually function anonymous() { or function () {
//			s1 = s1.substring(0, s1.length-2).replace(/function\s*\w*\s*\(\s*\w*\s*\)\s*\{\s*/gi, '');
			s1 = s1.replace(/\s*function\s*\w*\s*\(\s*\w*\s*\)\s*\{\s*/gi, '');
			s1 = s1.replace(/\s*}\s*$/gi,'');
		}

		if (s1.substring(1, 9) == 'function') {
			//replace the last curly brace, and the function prefix (usually function anonymous() { or function () {
//			s1 = s1.substring(1, s1.length-2).replace(/function\s*\w*\s*\(\s*\w*\s*\)\s*\{\s*/gi, '');
			s1 = s1.replace(/\s*function\s*\w*\s*\(\s*\w*\s*\)\s*\{\s*/gi, '');
			s1 = s1.replace(/\s*}\s*$/gi,'');
		}


		if (s2.substring(0, 8) == 'function') {
			//replace the last curly brace, and the function prefix (usually function anonymous() { or function () {
//			s2 = s2.substring(0, s2.length-2).replace(/function\s*\w*\s*\(\s*\w*\s*\)\s*\{\s*/gi, '');
			s2 = s2.replace(/\s*function\s*\w*\s*\(\s*\w*\s*\)\s*\{\s*/gi, '');
			s2 = s2.replace(/\s*}\s*$/gi,'');
		}
		if (s2.substring(1, 9) == 'function') {
			//replace the last curly brace, and the function prefix (usually function anonymous() { or function () {
//			s2 = s2.substring(1, s2.length-2).replace(/function\s*\w*\s*\(\s*\w*\s*\)\s*\{\s*/gi, '');
			s2 = s2.replace(/\s*function\s*\w*\s*\(\s*\w*\s*\)\s*\{\s*/gi, '');
			s2 = s2.replace(/\s*}\s*$/gi,'');
		}



		if (s1 == '' || s1 == null) {
			return s2;
		} else {
			if (s2 == '' || s2 == null) {
				return s1;
			} else {
				return s1 + ';\n' + s2;
			}
		}

		
	}

	function db (t) {
		document.getElementById('divDebugger').innerHTML += '\n<pre>' + t + '</pre>';
	}
}
