var util = {};

/////////returns mouse position
util.getMousePos = function(e) {
	var e = e||window.event;
	if(e.pageX||e.pageY) return {x:e.pageX,y:e.pageY};
	return {x:e.clientX+document.body.scrollLeft-document.body.clientLeft,y:e.clientY+document.body.scrollTop-document.body.clientTop};
}

/////////returns current scroll position offset
util.getPageScroll = function() {
	var xScroll,yScroll;
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop) {
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;
	}
	return {x:xScroll,y:yScroll};
}

/////////returns page size & window size
util.getPageSize = function(){
	var xScroll,yScroll;
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	var windowWidth,windowHeight;
	if (self.innerHeight) {
		(document.documentElement.clientWidth)?windowWidth = document.documentElement.clientWidth:windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	}else if (document.documentElement && document.documentElement.clientHeight){
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	}else if (document.body){
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	(yScroll < windowHeight)?pageHeight = windowHeight:pageHeight = yScroll;
	(xScroll < windowWidth)?pageWidth = xScroll:pageWidth = windowWidth;
	return {"pageWidth": pageWidth, "pageHeight": pageHeight, "windowWidth": windowWidth, "windowHeight": windowHeight};
}

/////////returns top,left position of an element
util.getElPos = function(el) {
	var retTop=el.offsetTop, retLeft=el.offsetLeft;
	var elParent = el;
	while((elParent = elParent.offsetParent) != null){if(elParent.tagName!='HTML'){retTop+=elParent.offsetTop;retLeft+=elParent.offsetLeft;}}
	return {top:retTop,left:retLeft};
}

/////////old functions to return top,left position of element. DEPRECIATE WHEN THIS IS READY FOR GLOBAL USE
util.getTopPos=function(div){var retVal=div.offsetTop;var div_parent=div;while((div_parent=div_parent.offsetParent) != null) if(div_parent.tagName!='HTML') retVal+=div_parent.offsetTop;return retVal;}
util.getLeftPos=function(div){var retVal=div.offsetLeft;var div_parent=div;while((div_parent=div_parent.offsetParent) != null) if(div_parent.tagName!='HTML') retVal+=div_parent.offsetLeft;return retVal;}

/////////use this to clone objects
util.cloneObj = function(obj) {
	if((typeof(obj)!='object')||(obj == null)) return obj;
	var newobj = new Object();
	for(var i in obj) newobj[i]=util.cloneObj(obj[i]);
	return newobj;
}

/////////global expand/collapse function. can do one or both
util.expand = function(expdiv, coldiv, target_height, min_height) {
	min_height=(min_height||0);
	// calculate new height for collapsing div
	if(coldiv) {
		coldiv.style.overflow = "hidden";
		coldiv.style.height = (coldiv.style.height||coldiv.offsetHeight + "px");
		var newcolheight = parseFloat(coldiv.style.height) - 7;
		newcolheight = (newcolheight < min_height) ? min_height : newcolheight;
	}else{
		var newcolheight = min_height;
	}

	// calculate new height for expanding div
	if(expdiv) {
		if(!target_height) {
			expdiv.style.height = "";
			expdiv.style.visibility = "hidden";
			expdiv.style.position = "absolute";
			expdiv.style.display = "block";
			target_height = expdiv.offsetHeight;
			expdiv.style.height = min_height + "px";
			expdiv.style.position = "static";
			expdiv.style.visibility = "visible";
			expdiv.style.overflow = "hidden";
		}
		expdiv.style.height = expdiv.offsetHeight + "px";
		var newexpheight = parseFloat(expdiv.style.height) + 7;
		newexpheight = (newexpheight > target_height) ? target_height : newexpheight;
	}else{
		var newexpheight = min_height;
		var target_height = min_height;
	}

	// set new heights
	if(coldiv) coldiv.style.height = newcolheight + "px";
	if(expdiv) expdiv.style.height = newexpheight + "px";

	// check to see if we need to continue
	if((newcolheight > min_height) || (newexpheight < target_height)) { // ) && (newcolheight < 300) why was this here?????
		setTimeout(function() { util.expand(expdiv, coldiv, target_height, min_height); }, 5);
	}else{
		if(coldiv && min_height == 0) coldiv.style.display = "none";
		if(expdiv) expdiv.style.display = "block";
		if(util.expand_oncomplete) util.expand_oncomplete();
	}
}

/////////oncomplete listener for the expand function
util.expand_oncomplete = null;

/////////add a class name to an element
util.addClassName = function(d,c) {
	var classnames=d.className.split(/\s+/);
	var already = false;
	for(var i=0;i<classnames.length;i++) if(classnames[i]==c) already=true;
	return (already)?d.className:d.className+=" "+c;
}

/////////remove a class name from an element
util.removeClassName=function(d,c) {
	var classnames=d.className.split(/\s+/);
	for(var j=0;j<classnames.length;j++) if(classnames[j]==c) classnames.splice(j,1);
	return d.className=classnames.join(" ");
}

/////////fixes onmouseover bugs when one element overlays the target element
util.isMouseLeaveOrEnter = function(e,handler) {
	var reltg=e.relatedTarget?e.relatedTarget:e.type=='mouseout'?e.toElement:e.fromElement;
	while(reltg&&reltg!=handler) reltg=reltg.parentNode;
	return (reltg!=handler);
}

/////////cross browser event listener
util.addListener = function(el,type,expression,bubbling) {
	bubbling=bubbling||false;
	if(window.addEventListener){el.addEventListener(type,expression,bubbling); return true;
	}else if(window.attachEvent){el.attachEvent('on'+type,expression); return true;
	}else return false;
}
