///////////////////////////
// Basic DHTML-functions //
///////////////////////////

function S5_GetObject(id)
{
	if (document.all) {
		return document.all[id];
	}
	else if (document.getElementById) {
		return document.getElementById(id);
	}
	
	return null;
}

function S5_GetStyle(id)
{
	if (document.all) {
		return document.all[id].style;
	}
	else if (document.getElementById) {
		return document.getElementById(id).style;
	}

	return null;
}


/////////////////
// Show / Hide //
/////////////////

function S5_Hide(id)
{
	var o = S5_GetObject(id);
	
	if (o) {
		if (document.all) {
			o.style.visibility = 'hidden';
		}
		else {
			o.style.visibility = 'hidden';
		}
	}
}

function S5_Show(id)
{
	var o = S5_GetObject(id);

	if (o) {
		if (document.all) {
			o.style.visibility = 'visible';
		}
		else {
			o.style.visibility = 'visible';
		}
	}
}

function S5_HideDisplay(id)
{
	var o = S5_GetObject(id);
	
	if (o) {
		if (document.all) {
			o.style.display = 'none';
		}
		else {
			o.style.display = 'none';
		}
	}
}

function S5_Display(id)
{
	var o = S5_GetObject(id);
	
	if (o) {
		if (document.all) {
			o.style.display = '';
		}
		else {
			o.style.display = '';
		}
	}
}


function S5_ToggleVisibility(id)
{
	var o = S5_GetObject(id);
	var nextState = 0;
	
	if (o) {
		if (o.style.visibility == '') nextState = 1;
		if (nextState) S5_Show(id);
		else S5_Hide(id);
	}
}


function S5_ToggleDisplay(id)
{
	var o = S5_GetObject(id);
	var nextState = 0;

	if (o) {
		if (o.style.display == 'none') nextState = 1;
		if (nextState) S5_Display(id);
		else S5_HideDisplay(id);
	}
}


function S5_IsVisible(id)
{
	var o = S5_GetObject(id);
	
	if (o) {
		if (o.style.visibility == 'visible') return true;
		else return false;
	}
	
	return false;
}


///////////////////
// Color & Class //
///////////////////

function S5_SetBgColor(id, color)
{
	var o = S5_GetObject(id);
	
	if (o) o.style.background = color;
}


function S5_ToggleBgColor(id, color1, color2)
{
	var o = S5_GetObject(id);

	if (o) {	
		if (o.style.background == color1) o.style.background = color2;
		else o.style.background = color1;
	}
}


function S5_SetClass(id, className)
{
	var o = S5_GetObject(id);
	
	if (o) {
		o.className = className;
	}
}



/////////////////
// Positioning //
/////////////////

function S5_ElementArea(x, y, width, height)
{
	this.x = x;
	this.y = y;
	this.width = width;
	this.height = height;
	
	return this;
}

function S5_GetElementArea(id)
{
	var o, x=0, y=0, width=0, height=0;

	if (document.all) {
		o = document.all[id];
		width = o.offsetWidth;
		height = o.offsetHeight;
		x = S5_GetScreenX(o);
		y = S5_GetScreenY(o);
	}
	else if (document.getElementById) {
		o = document.getElementById(id);
		width = o.offsetWidth;
		height = o.offsetHeight;
		x = S5_GetScreenX(o);
		y = S5_GetScreenY(o);
	}

	return new S5_ElementArea(x, y, width, height);
}

function S5_GetElementAreaObj(o)
{
	var o, x=0, y=0, width=0, height=0;

	if (document.all) {
		width = o.offsetWidth;
		height = o.offsetHeight;
		x = S5_GetScreenX(o);
		y = S5_GetScreenY(o);
	}
	else if (document.getElementById) {
		width = o.offsetWidth;
		height = o.offsetHeight;
		x = S5_GetScreenX(o);
		y = S5_GetScreenY(o);
	}

	return new S5_ElementArea(x, y, width, height);
}


function S5_SetAt(id, x, y)
{
	var o = S5_GetObject(id);
	
	if (o) {
		if (document.all) {
			o.style.pixelLeft = x;
			o.style.pixelTop = y;
		}
		else if (document.getElementById) {
			o.style.left = x;
			o.style.top = y;
		}
	}
}

function S5_GetScreenX(o)
{
	// Returns the screen X coordinate of the speficied element.
	// This is calculated by adding 'offsetLeft' of the parentElements.
	var x = 0;
	var container = o;

	while (container) {
		if (container.offsetLeft) {
			x += container.offsetLeft;
		}
		container = container.parentNode;
	}
	return x;
}

function S5_GetScreenY(o)
{
	// Returns the screen Y coordinate of the speficied element.
	// This is calculated by adding 'offsetTop' of the parentElements.
	var y = 0;
	var container = o;

	while (o) {
		if (o.offsetTop) {
			y += o.offsetTop;
		}
		o = o.offsetParent;
	}
	return y;
}
	

function S5_SetAtBottom(id)
{
	var o = S5_GetObject(id);
	var y = 0, x = 0;
	var elArea, docArea;

	if (o) {
		elArea = S5_GetElementArea(id);
		docArea = S5_GetElementAreaObj(document.body);
		x = 0;
		if (document.all) {
			y = document.body.clientHeight - elArea.height + document.body.scrollTop;
			S5_SetAt(id, x, y);
		}
		else if (document.getElementById) {
			y = window.innerHeight - elArea.height;
			S5_SetAt(id, x, y);
		}
	}
}


// fix for netscape 6/7
function NN6Contains(container, e)
{
	while (e && e != container) e = e.parentNode;
	return e;
}
// endfix



///////////
// DEBUG //
///////////

function S5_DumpObject(o)
{
	var s = "";
	for(x in o) s += "[" + x + "=" + o[x] + "]    ";
	alert(s);
}

