//sagient's common utilities objects and operations
//version 1.1 collected from examples and added to by andy mascaro june 2007 andym@sagient.com

var CookieCutter = {
		//CookieCutter: Object to contain cookie functions
		
	create: function(name,value,days,path) {
			//creates a new cookie
			//takes cookie's save name, cookie's save data, # days to store, and path to store as
			//returns nothing
		//if running on a local machine, ignore path
		if (!path || window.location.toString().substr(0,4) == "file") path = "/";
		if (days) {
		    var date = new Date();
		    date.setTime(date.getTime()+(days*24*60*60*1000));
		    var expires = "; expires="+date.toGMTString();
		}
		else expires = "";
		document.cookie = name+"="+value+expires+"; path=" + path;
	},
	read: function(name, commasToObject) {
			//reads a saved cookie
			//takes name of cookie, and a boolean to return an object if cookie's data is a comma-separated string of values
			//returns cookie value or if commasToObject an Object with all values or null if no cookie found
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0)	{
				if (commasToObject) {
					//creating a return Object based on the comma-separated string
					var returnArray = c.substring(nameEQ.length,c.length).split(',');
					var returnObject = new Object();
					for(var d=0;d<returnArray.length;d++) {
						var pair = returnArray[d].split('=');
						returnObject[pair[0]] = pair[1];					
					}
					return returnObject;
				}
			return (c.substring(nameEQ.length,c.length));
			}
		}
		return null;
	}
};

var EventManager = {
		//EventManager: Object to contain managing js event functions
		
	add: function(obj,type,fn) {
		//attaches a new event listener to an element
		//takes which element to attach to, which event, and function to call when event is triggered
		//returns true if addEventListener, result of attachEvent for non-addEventListener-compatible, or false if no compatability
	  if (obj.addEventListener){
	    obj.addEventListener(type, fn, false);
		return true;
	  }
	  else if (obj.attachEvent){
		var r = obj.attachEvent("on"+type, fn);
	    return r;
	  }
	  else {
		return false;
	  }
	},
	
	remove: function(obj,type,fn) {
		//deletes an event listener from an element
		//takes which element to remove from, which event, and (function the event calls when triggered)??
		//returns true if removeEventListener, result of detachEvent for non-removeEventListener-compatible, or false if no compatability
	  if (obj.removeEventListener){
	    obj.removeEventListener(type, fn, false);
		return true;
	  }
	  else if (obj.detachEvent){
		var r = obj.detachEvent("on"+type, fn);
	    return r;
	  }
	  else {
		return false;
	  }	
	}
};
 
var Dom = {
		//Dom: Object to contain dealing with elements simplification functions
		
	create: function(newElement) {
			//creates an element based on an input Object's parameters
			/*takes an Object with structure:			
			 {
				tag: String - element's html tag name
				attributes: Array - pairs of [String, (String / Int)] to use to created elements attributes
				src: String - quicker access to src attribute
				text: String - text to add as new element's inner text - TextNode
			 }
			 */
			 //returns new element or false if input Object had no tag specified
		if (newElement.tag) {
			var element = document.createElement(newElement.tag);
			if (!newElement.attributes) newElement.attributes = new Array();
			if (newElement.src) newElement.attributes.push("src",newElement.src);
			if (newElement.attributes.length) {			
				for (var c=0; c<newElement.attributes.length; c+=2) {
					element.setAttribute(newElement.attributes[c], newElement.attributes[c+1]);
				}
			}
			if (newElement.text) element.appendChild(document.createTextNode(newElement.text));
			return element;
		}
		return false;
	},
	
	add: function(newElement, id) {
			//more usable access to create function, creates a new element then adds it to the DOM
			//takes input Object as specified for create(), and the id of the receiving element
			//returns nothing
		var element = Dom.create(newElement);
		document.getElementById(id).appendChild(element);
	},
	
	remove: function(obj) {		
			//removes an element from the DOM -- slightly simpler function call			
		obj.parentNode.removeChild(obj);
	}
};

function preloadImages(Images) {
		//loads images into the cache
		//takes an Array of images' src's
		//returns nothing
	for(var i=0;i<Images.length;i++){
	   var newImage = {tag: "img", src: Images[i]};
	   Dom.create(newImage);
	}
}

function preloadImage(image) {preloadImages(Array(image));}

function getElementsByClass(searchClass,node,tag) {
// !! currently this doesn't find duel-classed elements "findclass otherclass" won't return for searching "findclass"
		//similar to getElementsByTagName, finds DOM elements by class
		//takes the class to look for
		//returns Array of found elements
	var classElements = new Array();
	if ( node == null ) node = document;
	if ( tag == null ) tag = '*';
	var elements = node.getElementsByTagName(tag);
	for (var c=0;c<elements.length;c++) {
		if (elements[c].className == searchClass) classElements.push(elements[c]);
	}
	return classElements;
}

function cloneObject(what) {
		//http://www.irt.org/script/879.htm
		//creates a duplicate of an object
    for (i in what) {
        if (typeof what[i] == 'object' && typeof what[i].length!='number') {
            this[i] = new cloneObject(what[i]);
        }
        else
            this[i] = what[i];
    }
}

var DropDownMenu = Class.create();

DropDownMenu.prototype = {
	initialize:function(menuElement) {
		menuElement.childElements().each(function(node){
			node.onmouseover = function() {
				node.addClassName('hover');
			}
			node.onmouseout = function() {
				node.removeClassName('hover');
			}
		});
	}
};