/*
 * mismash of support functions
 *
 * should only be called AFTER window.load
 *
 * call init before doing anything
 *
 *
 *
 * Version: $Revision: 1.2 $, Date: $Date: 2008/07/11 08:48:43 $
 */

function support()
{
	/*
     * initial stuff to run for pointer location. IE offset
	 * appears to be -2,-2 from document 0,0!
     */
	this.init = function()
	{
		if (document.documentElement && document.documentElement.clientLeft) {
			this.mouseoffset_left = document.documentElement.clientLeft;
			this.mouseoffset_top = document.documentElement.clientTop;
		} else if (document.body.clientLeft) {
			this.mouseoffset_left = document.body.clientLeft;
			this.mouseoffset_top = document.body.clientTop;
		} else {
			this.mouseoffset_left = 0;
			this.mouseoffset_top = 0;
		}
	}
	/* get computed style */
	this.getWindowSize = function()
	{
		var myWidth;
		var myHeight;

		if( typeof( window.innerWidth ) == 'number' ) {
	    //Non-IE
			myWidth = window.innerWidth;
			myHeight = window.innerHeight;
		} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
	    //IE 6+ in 'standards compliant mode'
			myWidth = document.documentElement.clientWidth;
			myHeight = document.documentElement.clientHeight;
		} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
			myWidth = document.body.clientWidth;
			myHeight = document.body.clientHeight;
		}
		return [ myWidth, myHeight ];
	}
	this.getStyleById = function (item,what)
	{
        var id;

		id = document.getElementById(item);
		return this.getStyle(id,what);
	}
	this.getStyle = function (item,what)
	{
        var c;

        c = 0;
        if (item.currentStyle) {
                c = item.currentStyle[what];
        } else if (window.getComputedStyle) {
                c = window.getComputedStyle(item,null)[what];
        }
        return c;
	}
	/* get computed style as int */
	this.getIntStyle = function(item,what)
	{
		var c;

		try {
			c = parseInt(this.getStyle(item,what));
			if (isNaN(c)) c = 0;
		} catch(e) {
			c = 0;
		}
		return c;
	}
	/* get computed style as int */
	this.getIntStyleById = function(item,what)
	{
		var c;

		try {
			c = parseInt(this.getStyleById(item,what));
			if (isNaN(c)) c = 0;
		} catch(e) {
			c = 0;
		}
		return c;
	}
	/* get source of event */
	this.getEventSource = function(what)
	{
		var evt = what || window.event;
		var from = evt.target || evt.srcElement;
		return from;
	}
	this.stopDrag = function(what)
	{
		var evt = what || window.event;
		if (evt.preventDefault) evt.preventDefault();
		else evt.returnValue = false;
		if (evt.stopPropagation) evt.stopPropagation();
		else evt.cancelBubble = true;
	}
	/* add a class into an item */
	this.addClass = function(what,item)
	{
		if (this.hasClass(what,item)) return;
		if (what.className == "") {
			what.className = item;
		} else {
			what.className += " " + item;
		}
	}
	/* remove a class from an item */
	this.removeClass = function(what,item)
	{
		var items;
		var i;
		var arr;
		var str;

		items = what.className.split(/ /);
		if (items) {
			arr = new Array;
			for(i=0;i<items.length;i++) {
				if (items[i] != item && items[i] != "") {
					arr[arr.length] = items[i];
				}
			}
			for(str="",i=0;i<arr.length;i++) {
				str += (i?" ":"") + arr[i];
			}
			what.className = str;
		}
		return;
	}
	/* true if we have this class */
	this.hasClass = function(what,item)
	{
		var items;
		var i;

		items = what.className.split(/ /);
		for(i=0;i<items.length;i++) {
			if (items[i] == item) return true;
		}
		return false;
	}
	/*
   	 * debug - you need a div with id=debug for this to work
	 * if append is true then it appends to the current text within debug
	 * if you just give a single argument, it assumes its append
 	 */
	this.debug = function(append,what)
	{
		var id;

		id = document.getElementById("debug");
		if (typeof(what) == "undefined" ||typeof(append) == "string") {
			what = append;
			append = 1;
		}
		if (append) id.innerHTML += what + "<br>";
		else id.innerHTML = what + "<br>";
	}
	/* get the offset of an element */
	this.getOffset = function(item)
	{
		var x;
		var y;

		x = y = 0;
		if (item && item.offsetParent) {
			while(item) {
				x += item.offsetLeft;
				y += item.offsetTop;
				item = item.offsetParent;
			}
		}
		return [x,y];
	}

	/* combination of above. returns x,y, width, height */
	this.getCoordinates = function(item)
	{
		var xy;
		var h;
		var w;

		w = item.offsetWidth;
		h = item.offsetHeight;
		xy = this.getOffset(item);
		return [ xy[0], xy[1], w, h ];
	}
	this.getSizeById = function(item)
	{
		return getSize(document.getElementById(item));
	}
	/* return size of item */
	this.getSize = function(item)
	{
		var w;
		var h;

		h = w = 0;
		w = this.getIntStyle(item,"width");
		w += this.getIntStyle(item,"paddingLeft");
		w += this.getIntStyle(item,"paddingRight");
		w += this.getIntStyle(item,"marginLeft");
		w += this.getIntStyle(item,"marginRight");
		w += this.getIntStyle(item,"borderLeftWidth");
		w += this.getIntStyle(item,"borderRightWidth");

		h = this.getIntStyle(item,"height");
		h += this.getIntStyle(item,"paddingTop");
		h += this.getIntStyle(item,"paddingBottom");
		h += this.getIntStyle(item,"marginTop");
		h += this.getIntStyle(item,"marginBottom");
		h += this.getIntStyle(item,"borderTopWidth");
			h += this.getIntStyle(item,"borderBottomWidth")
			return [ w, h ];
		}
		this.getAbsMouseXY = function(what)
		{
			var m;
			var s;

			m = this.getMouseXY(what);
			s = this.getScrolling();
		/* get the current mouse position */
			m[0] += s[0];
			m[1] += s[1];
			return m;
		}
		this.getButton = function(what)
		{
			var evt = what || window.event;

			return (evt.button)?evt.button:evt.which;
		}
		this.getMouseXY = function(what)
		{
			var evt = what || window.event;
			var x;
			var y;

			if (evt.pageX) {
				x = evt.pageX;
				y = evt.pageY;
			} else {
				x = evt.clientX;
				y = evt.clientY;
			}
			return [ x - this.mouseoffset_left, y - this.mouseoffset_top ];
		}
		/* get scrolling region */
		this.getScrolling = function()
		{
			var x = 0;
			var y = 0;

			if(typeof(window.pageYOffset ) == "number" ) {
			/* we don't use these in firefox? */
			//	y = window.pageYOffset;
			//	x = window.pageXOffset;
				x = y = 0;
			} else if( document.body && (document.body.scrollLeft || document.body.scrollTop)) {
				x = document.body.scrollLeft;
				y = document.body.scrollTop;
			} else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
				x = document.documentElement.scrollLeft;
				y = document.documentElement.scrollTop;
				}
			return [ x, y ];
		}
		this.getWindowViewport = function()
		{
			var w;
			var s;

			w = this.getWindowSize();
			s = this.getScrolling();
			return [ s[0], s[1], s[0] + w[0], s[1] + w[1] ];
		}
		/* parseint - returns 0 if it can't work out what the number is */
		this.parseInt = function(what)
		{
			var c;

			if (what == "") return 0;
			try {
				c = parseInt(what);
				if (isNaN(c)) c = 0;
			} catch(e) {
				c = 0;
			}
			return c;
		}
		this.documentHeight = function()
		{
			var h;

			h = 0;
			if (document.all) {
				h = Math.max(Math.max(document.documentElement.offsetHeight, document.documentElement.scrollHeight), Math.max(document.body.offsetHeight, document.body.scrollHeight));
			} else if (document.body) {
				h = document.body.scrollHeight;
			} else if (document.documentElement.scrollHeight != 0) {
				h = document.documentElement.scrollHeight;
			}
			return h;
		}
}
