/*
v 1.53, 
Made by Mattias Henell, Large Medium AB, mhenell@largemedium.com
*/
var cStart = "";
var cEnd = "";
var os = navigator.userAgent;

if (isNAV4) {
	cStart = "layers['";
	cEnd = "']";
	cStyle = "";
} else if (isNAV6) {
	cStart = "getElementById('";
	cEnd = "')";
	cStyle = ".style";
} else if (isOPERA) {
	cStart = "all.";
	cEnd = "";
	cStyle = ".style";
} else {
	cStart = "all.";
	cEnd = "";
	cStyle = ".style";
}
		
//Convert Object name String or object reference into a valid object reference
function getObject(obj, nestedLayer){
	var theObj
	if (typeof obj == "string") {
		if(nestedLayer && isNAV4) { 
			theObj = eval("document.layers."+nestedLayer+".document."+obj)
		} else {

			theObj = eval("document."+cStart+obj+cEnd);
			
			//if (DEBUG && !theObj) alert("getObject(not found: '"+obj+"')");
			// get the style portion for theObj, if theObj exists
			if (theObj && (cStyle != "")) {
				theObj = eval("document."+cStart+obj+cEnd+cStyle);
			}
		}
	} else {
		theObj = obj;
	}
	return theObj
}

function getObjectNew(obj, nestedLayer){
	var theObj
	if (typeof obj == "string") {
		theObj = document.getElementById (obj);
		if (theObj && (cStyle != "")) {
			//theObj = eval("document."+cStart+obj+cEnd+cStyle);
			theObj = document.getElementById (obj).style;
		}
	} else {
		theObj = obj;
	}
	return theObj
}

function getImage(imgname, nestedLayer){
	var theObj
	if (typeof imgname == "string") {
		if(nestedLayer && isNAV4) { 
			theObj = eval("document.layers."+nestedLayer+".document.images."+imgname)
		} else { 
			theObj = eval("document.images['"+imgname+"']");
		}
	} else {
		theObj = imgname;
	}
	
	return theObj
}

//positioning an object at a specific pixel coordinate
function shiftTo(obj, x, y, nestedLayer) {
	nestedLayer = nestedLayer
	var theObj = getObject(obj, nestedLayer)
	theObj.left = x;
	theObj.top = y;
}

//Moving an object by x and / or y pixels
function shiftBy(obj, deltaX, deltaY, nestedLayer) {
//alert('move '+obj+'('+deltaX+','+deltaY+')');
	nestedLayer = nestedLayer
	var theObj = getObject(obj, nestedLayer)
	deltaX = parseInt(deltaX) + parseInt(getObjectLeft(obj))
	deltaY = parseInt(deltaY) + parseInt(getObjectTop(obj))
	theObj.left = deltaX
	theObj.top = deltaY
}

//Setting the z-order of an object
function setZIndex(obj, zOrder, nestedLayer){
	nestedLayer = nestedLayer
	var theObj = getObject(obj, nestedLayer)
	theObj.zIndex = zOrder
}

//setting the visibility of an object
function setVisibility(obj, visibility, nestedLayer) {
	nestedLayer = nestedLayer
	var theObj = getObject(obj, nestedLayer)
	if (theObj) theObj.visibility = visibility;
	else debug("setVisibility('"+obj+"'): object not found", 'ERROR');
}

//setting the visibility of an object
function setVisibilityNew(obj, visibility, nestedLayer) {
	nestedLayer = nestedLayer
	var theObj = getObjectNew(obj, nestedLayer)
	if (theObj) theObj.visibility = visibility;
	else debug("setVisibility('"+obj+"'): object not found", 'ERROR');
}

//setting the visibility of an object
function setDisplay(obj, visibility, nestedLayer) {
	nestedLayer = nestedLayer
	var theObj = getObjectNew(obj, nestedLayer)
	if (theObj) theObj.display = visibility;
	else debug("setDisplay('"+obj+"'): object not found", 'ERROR');
}

//Retrieving the x-cordinate of a positionable object
function getObjectLeft(obj, nestedLayer) {
	nestedLayer = nestedLayer
	var theObj = getObject(obj, nestedLayer)
	return parseInt(theObj.left)
}

//Retrieving the y-cordinate of a positionable object
function getObjectTop(obj, nestedLayer){
	nestedLayer= nestedLayer
	var theObj = getObject(obj, nestedLayer)
	return parseInt(theObj.top)
}

//Changing content of a layer
function reWriteLayer(obj, contentString, nestedLayer){
	if (isNAV6)
	{
//		alert ("isNAV6");
		var div = document.getElementById(obj);
		if (div)
		{
			div.innerHTML = contentString
		}
	}
	else if (isIE)
	{
//		alert ("isIE");
		var div = document.all[obj];
		if (div)
			div.innerHTML=contentString
	}
	else if(isNAV4) 
	{
//		alert ("isNAV4");
		theObj = getObject(obj,nestedLayer)
		if (theObj)
		{
			theObj.document.open("text/html")
//			theObj.document.open();
			theObj.document.write(contentString);
			theObj.document.close();
		}
	}
	else if (isOPERA) 
		return false
}

// getting layer contents
function getLayerContents(obj, nestedLayer){
	if (isNAV6)
	{
		return document.getElementById(obj).innerHTML;
	}
	else if (isIE)
	{
		return document.all[obj].innerHTML;
	}
	else if(isNAV4) 
	{
/*
		theObj = getObject(obj, nestedLayer)
		theObj.document.open("text/html")
		theObj.document.write(contentString);
		theObj.document.close();
*/
		return "not supported";
	}
	else if (isOPERA) 
		return "not supported";
}

function addToLayerContents (obj, contents, nestedLayer){
	if (isNAV6)
	{
		document.getElementById(obj).innerHTML += contents;
	}
	else if (isIE)
	{
		document.all[obj].innerHTML += contents;
	}
	else if(isNAV4  ||  isOPERA) 
	{
		// not supported
	}
}

//clipping a layer
function clipLayer(obj, lengthTop, lenghtRight, lengthBottom, lengthLeft, nestedLayer){
	nestedLayer = nestedLayer
	var theObj = getObject(obj)
	
	if(!isNAV4) {
		var clipString = "rect(" +lengthTop +"," +lenghtRight +"," +lengthBottom +","+ lengthLeft +")"
		theObj.clip = clipString;
		}
	else {
		theObj.clip.top = lengthTop
		theObj.clip.right = lenghtRight
		theObj.clip.bottom = lengthBottom
		theObj.clip.left = lengthLeft
	}
}

//Changing a picture within a layer
function setImage(obj, imgName, imgSrc, nestedLayer) {
	var img;
	if (isNAV4) {
		var myObj = getObject(obj, nestedLayer)
		img = myObj.document.images[imgName];
	} else {
		img = document.images[imgName];
	}
	if (img) img.src = imgSrc;
	else debug("setImage(image not found: '"+imgName+"')", 'ERROR');
}

// LAF: Swaps a layer with the currently visible layer
// limited 1 per page, this could be changed be creating a group
var currentLayer;
function setLayer(layer) {
	if (currentLayer) setVisibility(currentLayer,'hidden');
	setVisibility(layer, 'visible');
	currentLayer = layer;
}

// LAF: submits a form, could be in a layer
function submitForm(name, layerName, nestedLayer) {
	var form = getForm(name, layerName, nestedLayer);
	if (form) form.submit();
	else debug("submitForm(form not found: '"+name+"')", 'ERROR');
}

function getForm(name, layerName, nestedLayer) {
	var form;
	if (isNAV4 && layerName) {
		var myObj = getObject(layerName, nestedLayer)
		form = myObj.document.forms[name];
	} else {
		form = document.forms[name];
	}
	return form;
}


//changing background color in a div
function changeBgColor(obj, color, nestedLayer) {
		obj = getObject(obj,nestedLayer)
		
		if(isNAV4) {
			obj.document.bgColor = color
			}
		
		else {
			obj.backgroundColor = color
		}
}

function swapImage(imgname, imgsrc) {
		eval("document." + imgname + ".src='" + imgsrc + "';");
}







/*FUNCTION TO RETRIEVE MOUSEPOSITION IN DOCUMENT */
// http://www.breakingpar.com/bkp/home.nsf/Doc?OpenNavigator&U=87256B14007C5C6A87256B4B0005BFA6

// Global variables
xMousePos = 0; // Horizontal position of the mouse on the screen
yMousePos = 0; // Vertical position of the mouse on the screen

if (document.all)   // Internet Explorer
{
	document.onmousemove = captureMousePosition;
}
else if (document.getElementById)   // Netscape 6
{
	document.onmousemove = captureMousePosition;
}


function captureMousePosition (e)
	{

/*
    if (document.layers)
    {
        // Netscape 4
        xMousePos = e.pageX;
        yMousePos = e.pageY;
    }
    else 
*/
    if (document.all)
    {
        // IE,
        xMousePos = window.event.x+document.body.scrollLeft;
        yMousePos = window.event.y+document.body.scrollTop;
    }
    else if (document.getElementById)
    {
        // Netscape 6+
        xMousePos = e.pageX;
        yMousePos = e.pageY;
    }
}
