/*
* DHTML crossbrowser-library
* General functions

by Triggerfish AB
*/

/* DHTML crossbrowser-library ******/

/* DHTML-object ---------------------
	METHODS:
		getPageTop(): int - absolute top position in pixels
		getPageLeft(): int - absolute left position in pixels
		getWidth(): int - width in pixels
		getHeight(): int - height in pixels
		setLeft(left) - sets left position in pixels
		setTop(top) - sets top position in pixels
		setWidth(width) - sets width in pixels
		setHeight(height) - sets height in pixels
		setPosition(left, top)
		setSize(width, height)
		setOpacity(opacity) - sets opacity, 0-100, where 0 is totally transparent.
		show() - sets visibility to visible
		hide() - sets visibility to hidden
		getInnerHTML(): String - gets inner HTML
		setInnerHTML(html) - sets inner HTML
	PROPERTIES:
		initialized: boolean - set to true when initialized by getObj

		style: The element's style object

*/

function getObj(id)
{	
	var obj = new Object();
	if(typeof(id) == 'string')
	{
		if(document.getElementById) //W3C
		{
			obj = document.getElementById(id);
		}
		else if(document.all) //IE
		{
			obj = document.all(id);
		}
		else //not IE or W3C
		{			
			obj.style = new Object();
		}
	}else{
		obj = id;
	}
	//custom methods
	if(typeof obj == 'object') {
		if(obj != null)
		if(!obj.initialized) {
			obj.getPageTop = obj_getPageTop;
			obj.getPageLeft = obj_getPageLeft;
			obj.getTop = obj_getTop;
			obj.getLeft = obj_getLeft;
			obj.getWidth = obj_getWidth;
			obj.getHeight = obj_getHeight;
			obj.setLeft = obj_setLeft;
			obj.setTop = obj_setTop;
			obj.setWidth = obj_setWidth;
			obj.setHeight = obj_setHeight;
			obj.setPosition = obj_setPosition;
			obj.setSize = obj_setSize;
			obj.setOpacity = obj_setOpacity;
			obj.show = obj_show;
			obj.hide = obj_hide;
			obj.getInnerHTML = obj_getInnerHTML;
			obj.setInnerHTML = obj_setInnerHTML;
			
			obj.initialized = true;
		}
	}

	return obj;
}
function obj_getPageTop() {
	return getAbsolutePos(this, 'Top');
}
function obj_getPageLeft() {
	return getAbsolutePos(this, 'Left');
}
function obj_getTop() {
	return this.offsetTop;
}
function obj_getLeft() {
	return this.offsetLeft;
}
function obj_getWidth() {
	return this.clientWidth;
}
function obj_getHeight() {
	return this.clientHeight;
}
function obj_setLeft(left) {
	this.style.left = left + 'px';
}
function obj_setTop(top) {
	this.style.top = top + 'px';
}
function obj_setWidth(width) {
	this.style.width = width + 'px';
}
function obj_setHeight(height) {
	this.style.height = height + 'px';
}
function obj_setPosition(left, top) {
	this.setLeft(left);
	this.setTop(top);
}
function obj_setSize(width, height) {
	this.setWidth(width);
	this.setHeight(height);
}
function obj_setOpacity(opacity) {
	if(opacity > 0) this.show();
	if(opacity <= 0) this.hide();
	opacity = (opacity == 100)?99.999:opacity; //Fixing a flicker-bug in some browser
	
	this.style.filter = 'alpha(opacity:' + opacity + ')'; //IE/Win
	this.style.KHTMLOpacity = opacity/100; //Safari<1.2, Konqueror
	this.style.MozOpacity = opacity/100; //Older Mozilla and Firefox
	this.style.opacity = opacity/100; //Safari 1.2, newer Firefox and Mozilla, CSS3
}
function obj_show() {
	this.style.visibility = 'visible';
}
function obj_hide() {
	this.style.visibility = 'hidden';
}
function obj_getInnerHTML() {
	if(typeof this.innerHTML != 'undefined') {
		return this.innerHTML;
	} else {
		//...
	}
}
function obj_setInnerHTML(html) {
	if(typeof this.innerHTML != 'undefined') {
		this.innerHTML = ''; //for Mac IE
		this.innerHTML = html;
	} else {
		//...
	}
}

// For backwards-compatibility ------
function setInnerHTML(id, html) {
	getObj(id).setInnerHTML(html);
}
function hideObj(id) {
	getObj(id).hide();
}
function showObj(id) {
	getObj(id).show();
}
function getAbsoluteTop(id) {
	return getAbsolutePos(id, 'Top');
}
function getAbsoluteLeft(id) {
	return getAbsolutePos(id, 'Left');
}
function setOpacity(id, opacity) {
	getObj(id).setOpacity(opacity);
}

// General DHTML functions ----------
function getAbsolutePos(id, which) {
	var obj = getObj(id), iPos = 0;
	while (obj!=null) {
	 	iPos += obj['offset' + which];
		if(obj['scroll' + which]) iPos -= obj['scroll' + which];
		obj = obj.offsetParent;
	}
	if(which == 'Top') {
		iPos += getScrollY();
	} else {
		iPos += getScrollX();
	}
	return iPos
}
function getScrollX() {
	if(window.scrollX) {
		return window.scrollX;
	} else if(document.body.scrollLeft) {
		return document.body.scrollLeft;
	} else if(document.documentElement && document.documentElement.scrollLeft) {
		return document.documentElement.scrollLeft;
	} else if(window.innerHeight) {
		return window.pageXOffset;
	}
	return 0;
}
function getScrollY() {
	if(window.scrollY) {
		return window.scrollY;
	} else if(document.body.scrollTop) {
		return document.body.scrollTop;
	} else if(document.documentElement && document.documentElement.scrollTop) {
		return document.documentElement.scrollTop;
	} else if(window.innerHeight) {
		return window.pageYOffset;
	}
	return 0;
}


/* General functions *************/
String.prototype.trim = function () {
	return this.replace(/^\s*|\s*$/g, '');
}
function trimString(sInString) {
	return sInString.trim();
}
function inArray(value, arr) {
	//if(typeof(arr) == 'array')
	for(var i=0; i<arr.length; i++)	if(arr[i] == value) return true;
	return false;
}
function max(value1, value2) {
	return value1>value2?value1:value2;
}
function min(value1, value2) {
	return value1<value2?value1:value2;
}

