var goldenrod
if (!goldenrod) goldenrod = {}

/*Importing classes
var Dom = goldenrod.Dom;
*/

goldenrod.Utility = new Object;

goldenrod.Utility.NAME = "goldenrod.Utility";

goldenrod.Utility.getAttr = function(elt, attrname) {
	if(typeof elt == "string") elt = document.getElementById(elt);
	
	attrname = attrname || "utility";
	var utility  = elt.getAttribute(attrname);
	if(!utility) return null;	//If attrname does not exist or is an empty string we return null
	else return utility;
}

goldenrod.Utility.hasAttr = function(elt, attrparm, /*optional*/ attrname) {
	
	attrname = attrname || "utility";
	var utility  = goldenrod.Utility.getAttr(elt, attrname);
	if(!utility) return null;	//If attrname does not exist or an emtpy string we return null
	
	var pattern = new RegExp(attrparm + "\\s*\=")	//search for attrparm=
	
	return (utility.search(pattern) != -1 );	//Otherwise we return true of false
	}

goldenrod.Utility.getAttrValue = function(elt, attrparm, /*optional*/ attrname) {

	var returnvalue = "";
	if(typeof elt == "string") elt = document.getElementById(elt);
	attrname = attrname || "utility";
	
	
	if(goldenrod.Utility.hasAttr(elt, attrparm, attrname) ) {
		var utility  = elt.getAttribute(attrname);
		/************************************************************************************************
		RegExp for: attrparm(space)=(space) followed by one or more non-semicolon characters followed by 
			zero or more occurances of (a semicolon followed by one or more non-semicolon word characeters ) 
			ending with either two consecutive semicolons or a semicolon-(space) end of line.
		************************************************************************************************/
		
		var pattern = new RegExp(attrparm + "\\s*\=\\s*([^\;]+(?:\;[^\;]+)*)(\;{2}|\;\\s*$)");
		var result = pattern.exec(utility);
		//alert("The result is: " + result[1]);
		if(result[1]) {
			returnvalue = result[1];
				
		}
	}
		
	return returnvalue;
}
	
goldenrod.Utility.setAttrValue = function(elt, attrparm, attrvalue, /*optional*/ attrname) {

	if(typeof elt == "string") elt = document.getElementById(elt);
	
	var newString = attrparm + "=" + attrvalue + ";;";
	attrname = attrname || "utility";
	
	var utility  = elt.getAttribute(attrname);
	if(!utility) {	//The attribute doesn't exist
	}
	else if(!goldenrod.Utility.hasAttr(elt, attrparm, attrname) ) {	//The attribute exists but not the parm
		newString = utility + newString;
	}
	else {
		var pattern = new RegExp(attrparm + "\\s*\=\\s*[^\;]+(?:\;[^\;]+)*(\;{2}|\;\\s*$)");
		newString = utility.replace(pattern, newString);
	}
	elt.setAttribute(attrname, newString);
}
	
goldenrod.Utility.centerElt = function(elt, parent) {
	var parentWidth;
	
	if(typeof elt == "string") elt = document.getElementById(elt);
	
	if(parent == null) parentWidth = goldenrod.Dom.getViewportWidth();
	else {
		if(typeof parent == "string") parent = document.getElementById(parent);
		if( (parentWidth = goldenrod.Dom.getStyle(parent).width) == goldenrod.Dom.auto)  return;
		if( isNaN(parentWidth = goldenrod.Dom.getPixel(parentWidth)) ) return;
		}
	
	
	
	var elt_style = goldenrod.Dom.getStyle(elt);
	var	elt_width = elt_style.width;
	var	changeWidth = false;
	
	/********************************************************************************************************
	In the case that the element has overflow set to scroll and max width is a string and max width != "auto" 
	and the parseInt of max width great than or equal to 0
	First we make the width equal to the maximum then we try to center it.
	In the centering algorithm we add and extra length for the a scroll bar using the Dom initialized
	scroll bar pixel size that is currently set to 1/4 inch. 
	If the current width plus 25 is to small for the parent we make is small enought to fit.
	*********************************************************************************************************/
		
	if ( ( (elt_style.overflow == "scroll") || (elt_style.overflow == "auto") ) && 
		 (typeof elt_style.maxWidth == "string") &&
	     (elt_style.maxWidth != goldenrod.Dom.auto) && 
		 (goldenrod.Dom.getPixel(elt_style.maxWidth) >= 0) ) {changeWidth = true; elt.style.width = elt_style.maxWidth; }
	
	if(elt_width == "100%") 
		elt.style.left = "0px"; 
	else {
		//alert("elt_width = " +elt_width + "\nDom.getPixel(elt_width) = " + goldenrod.Dom.getPixel(elt_width) + "\n" +
		//"goldenrod.Dom.toPixelRatio = " + goldenrod.Dom.toPixelRatio + "\nDom.DPI_X = " + goldenrod.Dom.DPI_X)
		
		goldenrod.Dom.setDimension(Math.max(0, Math.round( (parentWidth - goldenrod.Dom.getPixel(elt_width) - goldenrod.Dom.scroll_bar_pixel )/2) ), elt, "left")
		//elt.style.left = Math.max(0, Math.round( (parentWidth - goldenrod.Dom.getPixel(elt_width))/2) ) + "px";	
	}
		
		
		
	if(changeWidth) {
		if(goldenrod.Dom.getPixel(elt_style.width )+ goldenrod.Dom.scroll_bar_pixel > (parentWidth)) 
			goldenrod.Dom.setDimension((parentWidth - goldenrod.Dom.scroll_bar_pixel), elt, "width");
	}
		
}
	



goldenrod.Utility.centerAll = function(elt) {
	goldenrod.Utility.centerAll.count++;
	//myAlert("Inside center All for the " + goldenrod.Utility.centerAll.count + "th time");
	if(typeof elt == "string") elt = document.getElementById(elt);
	
	var children = elt.childNodes;
	var attrValue; 
	//myAlert("The length of children = " + children.length);
	for(var i=0; i<children.length; i++) {
		if( (children[i].nodeType == 1) &&
			(goldenrod.Utility.hasAttr(children[i], "center")) ) {
			
			//myAlert("This element node has id = " + children[i].id);
			attrValue = goldenrod.Utility.getAttrValue(children[i], "center")
			switch	(attrValue) {
				case "yes" : {
					//myAlert("This element node has id = " + children[i].id);
					goldenrod.Utility.centerElt(children[i])
					break;
				}
				case "parent" : {
					
					break;
				}	
			}	//End of switch
			
		}	//End of if
		goldenrod.Utility.centerAll(children[i])	
	}	//End of for
	return;
}	
	
goldenrod.Utility.centerAll.count = 0;



goldenrod.Utility.getElementsByName = function(elt, name, value) {
	//alert("The name we are searching for is = " + n_ame)
	
	
	function inner(elt) {
		var children = elt.childNodes;
		var attrValue; 
		//alert("The length of children = " + children.length);
		for(var i=0; i<children.length; i++) {
			if( (children[i].nodeType == 1) &&
				(goldenrod.Utility.hasAttr(children[i], name)) ) {
			
				//myAlert("This element node has id = " + children[i].id);
				attrValue = goldenrod.Utility.getAttrValue(children[i], name)
				//alert("attrValue = " + attrValue + "\nattrValue length = " + attrValue.length + 
				//"\nname = " + name + "\nname length = " + name.length)
				if(attrValue == value) {
					//alert("found one")
					elt_array.push(children[i])
					}	
			
				}	//End of if
			inner(children[i]);	
			}	//End of for
		
		}
		
	goldenrod.Utility.getElementsByName.count++;
	var elt_array = new Array();
	if(typeof elt == "string") elt = document.getElementById(elt);
	inner(elt)
	
	return elt_array;
	}	
	
goldenrod.Utility.getElementsByName.count = 0;

/* This is constructor for an object that will hold three mouse handlers */
goldenrod.Utility.mousehandlers = function(over, out, click) {
	this.mouseover = over;
	this.mouseout = out;
	this.mouseclick = click;	
}
	/*An object that will hold dynamically setup handlers.*/
goldenrod.Utility.linkTypes = {}


goldenrod.Utility.setMousehandlers = function() {
var property, e, i;
	
	for(property in goldenrod.Utility.linkTypes) {
		
		e = goldenrod.Utility.getElementsByName(document, "name", property);
	
	if(e) {
		for(i=0; i < e.length; i++){
		if(goldenrod.Utility.linkTypes[property].mouseover) e[i].onmouseover = goldenrod.Utility.linkTypes[property].mouseover;
		if(goldenrod.Utility.linkTypes[property].mouseout) e[i].onmouseout = goldenrod.Utility.linkTypes[property].mouseout;
		if(goldenrod.Utility.linkTypes[property].mouseclick) e[i].onclick = goldenrod.Utility.linkTypes[property].mouseclick;
			}
		}
		
	}
	
	//If present - initialize to alreadymouse over first link 
	
	e = goldenrod.Utility.getElementsByName(document, "firstlink", "here");
	if(e.length) {
		e[0].onmouseover();
		e[0].scrollIntoView();	
	}
			
}
