/*
		Copyright 2002 Konjin Technologies
		http://www.konjintech.com

		Authors:	johan@konjintech.com
					david@konjintech.com
					andreas@konjintech.com
					
		Version: 	2.0 (amore.se)
		Created:		00-00-00 David
		Edit: 		02-09-28 David
						02-10-10 Andreas
						02-10-28 Andreas & David
*/

	//world timeout
	worldRefresh = 30;
	//worldRefresh = 1000;
	worldArray = new Array();
	worldtimer = null;
	
	function worldAdd(addString, selectedIndex, runWorld) {
		//alert('worldadd');
		var newIndex = null;
		if (selectedIndex || selectedIndex == 0) {
			worldArray[selectedIndex] = addString;
			newIndex = selectedIndex;
		} else {
			worldArray[worldArray.length] = addString;
			newIndex = worldArray.length-1;
		}
		//alert('ADD: string: '+worldArray[newIndex]+'\n      index: '+(newIndex)+'\n     length: '+(worldArray.length));
		if (runWorld) {
			world();
		}
		return newIndex;
	}
	
	function worldDelete(deleteIndex) {
		//alert('DELETE: string: '+worldArray[deleteIndex]+'\n         index: '+(deleteIndex)+'\n        length:'+(worldArray.length));
		worldArray[deleteIndex] = '';
	}
	
	function world() {
		for (var worldIndex=0; worldIndex < worldArray.length; worldIndex++) {
			if (worldArray[worldIndex] != '') {
				//alert('RUN: string: '+worldArray[worldIndex]+'\n      index: '+(worldIndex)+'\n     length: '+(worldArray.length));
				eval(worldArray[worldIndex]);
			}
		}
		worldtimer = setTimeout('world()', worldRefresh);
	}
	
	moveobjectArray = new Array();
	function moveAdd(addObj, selectedIndex) {
		var newIndex = null;
		if (selectedIndex || selectedIndex == 0) {
			moveobjectArray[selectedIndex] = addObj;
			newIndex = selectedIndex;
		} else {
			moveobjectArray[moveobjectArray.length] = addObj;
			newIndex = moveobjectArray.length-1;
		}
		//alert('ADD: string: '+worldArray[newIndex]+'\n      index: '+(newIndex)+'\n     length: '+(worldArray.length));
		return newIndex;
	}
	
	function moveDelete(deleteIndex) {
		//alert('DELETE: string: '+worldArray[deleteIndex]+'\n         index: '+(deleteIndex)+'\n        length:'+(worldArray.length));
		moveobjectArray[deleteIndex] = '';
	}
	
	function moveAllMoveableElements() {
		//alert('moveAllMoveableElements');
		var i = 0;
		for (i = 0; i < moveobjectArray.length; i++) {
			if (moveobjectArray[i]) {
				if (moveobjectArray[i] != '') {
					moveobjectArray[i].move();
				}
			}
		}
	}
	
	var moveAllMoveobjectsAdded = false;
	/*
		Moveobject moves objects, use world() to start the movement.
	
		obj 			= the object to move
		xFrom 			= xPosition to move from
		yFrom 			= yPosition to move from 
		xTo				= X position to move to (nullable)
		yTo 			= Y position to move to (nullable)
		maxVelocity 	= maxvelocity (nullable)
		acceleration	= acceleration (can be nullable if maxvelocity is set)
		deAcceleration  = deacceleration (uses acceleration when not set)
		startWorld		= start world with this object? (bool)
		endMoveEval 	= eval to execute when object is in final position (needs xto or yto)
		yTop			= top y delimiter (kills the movement if lower) (nullable)
		yBottom			= bottom y delimiter (kills the movement if lower) (nullable)
		xLeft			= left x delimiter (kills the movement if lower) (nullable)
		xRight			= right x delimiter (kills the movement if lower) (nullable)
		atLimiterEval	= eval to run when object has crossed a limitation.
		setDirection	= set direction if continous movement (pos/neg)
	*/
	function moveObject(obj, xFrom, yFrom, xTo, yTo, maxVelocity, acceleration, deAcceleration, startWorld, endMoveEval, yTop, yBottom, xLeft, xRight, atLimiterEval, setDirection) {
		var currentObj = obj;
		if (endMoveEval) {
			currentObj.endMoveEval = endMoveEval;
		}
		if (!xFrom && xFrom != 0) {
			var xFrom = getXpos(currentObj);
		}
		if (!yFrom && yFrom != 0) {
			var yFrom = getYpos(currentObj);
		}
		if (!maxVelocity && maxVelocity != 0) {
			var maxVelocity = null;
		}
		if (!acceleration) {
			var acceleration = null;
		}
		if (!deAcceleration) {
			var deAcceleration = null;
		}
		if (!yTop && yTop != 0) {
			var yTop = null;
		}
		if (!yBottom && yBottom != 0) {
			var yBottom = null;
		}
		if (!xLeft) {
			var xLeft = null;
		}
		if (!xRight) {
			var xRight = null;
		}
		if (!atLimiterEval) {
			var atLimiterEval = null;
		}

		currentObj.xFrom =	xFrom;
		currentObj.yFrom =	yFrom;
		currentObj.xTo =	xTo;
		currentObj.yTo =	yTo;
		currentObj.maxVelocity = maxVelocity;
		currentObj.constantAcceleration = acceleration;
		currentObj.deAcceleration = deAcceleration;
		currentObj.yTop = yTop;
		currentObj.yBottom = yBottom;
		currentObj.xLeft = xLeft;
		currentObj.xRight = xRight;
		currentObj.atLimiterEval = atLimiterEval;
		currentObj.direction = setDirection;
		
		//if ((currentObj.direction && (currentObj.yFrom && currentObj.yTo == null) || (currentObj.xFrom && currentObj.xTo == null))) {
		if (currentObj.direction != null) {
			currentObj.moveIndex = moveAdd(currentObj, currentObj.moveIndex);
			if (moveAllMoveobjectsAdded == false) {
				worldAdd('moveAllMoveobjects()', null, startWorld);		
				moveAllMoveobjectsAdded = true;
			}
		}
		//moveObjectCalculation(currentObj);
	}
	
	
	var moveObjectError = false
	function moveObjectReset (obj) {
		var currentObj = obj;
		currentObj.isDeAccelerating = false;
		currentObj.moving = false;
		currentObj.velocity = 0;
		currentObj.d = 0;
		currentObj.time = null;
		currentObj.lastTime = 0;
		currentObj.atMaxVelocity = false;
		//currentObj.startTravelDistance = Math.sqrt(((currentObj.xTo - currentObj.xFrom)*(currentObj.xTo - currentObj.xFrom))+((currentObj.yTo-currentObj.yFrom)*(currentObj.yTo-currentObj.yFrom)));
		currentObj.travelDistance = currentObj.startTravelDistance;
		currentObj.travelDistanceLeft = null;
		currentObj.lastTravelDistanceLeft = null;
		if (!currentObj.constantAcceleration) {
			currentObj.constantAcceleration = (currentObj.maxVelocity/10);
		}
		currentObj.acceleration = currentObj.constantAcceleration;
		if (!currentObj.deAcceleration) {
			currentObj.deAcceleration = 0 - currentObj.acceleration;
		} else if (currentObj.deAcceleration > 0) {
			currentObj.deAcceleration = 0 - currentObj.deAcceleration;
		}
		if (currentObj.maxVelocity) {
			/*if (parseInt((currentObj.acceleration / currentObj.maxVelocity)*100) > 10) {
				alert('acceleration versus maxacceleration is too high (max 10%)');
				moveObjectError = true;
			}*/
		} else if (!currentObj.maxVelocity && !currentObj.acceleration) {
			alert('Neither accelleration or maxvelocity is set for this object: '+ currentObj.innerText);
			moveObjectError = true;
		}
		
		if (currentObj.xTo == null) {
			currentObj.horisontal = true;
			currentObj.contignousMovement = true;
			//default is left->right
			if (!currentObj.direction) {
				currentObj.direction = 0;
			}
			currentObj.startTravelDistance = null;
		} else if (currentObj.yTo == null) {
			currentObj.vertical = true;
			currentObj.contignousMovement = true;
			//default is top->down
			if (!currentObj.direction) {
				currentObj.direction = 0;
			}
			currentObj.startTravelDistance = null;
		} else if (currentObj.xTo - currentObj.xFrom != 0) {
			currentObj.contignousMovement = false;	
			currentObj.horisontal = true;
			currentObj.startTravelDistance = currentObj.xTo - currentObj.xFrom;
			if (currentObj.xTo - currentObj.xFrom > 0) {
				currentObj.direction = 1;
			} else if (currentObj.xTo - currentObj.xFrom < 0) {
				currentObj.direction = -1;
			} else {
				currentObj.direction = 0;
			}
		} else if (currentObj.yTo - currentObj.yFrom != 0) {
			currentObj.contignousMovement = false;	
			currentObj.vertical = true;
			currentObj.startTravelDistance = currentObj.yTo - currentObj.yFrom;
			if (currentObj.yTo - currentObj.yFrom > 0) {
				currentObj.direction = 1;
			} else if (currentObj.yTo - currentObj.yFrom < 0) {
				currentObj.direction = -1;
			} else {
				currentObj.direction = 0;
			}
		} else {
			currentObj.startTravelDistance = 0;
			currentObj.direction = 0;
		}
		
		//temp
		currentObj.ld = 0;
	}


	
	function moveObjectCalculation(obj) {
		var currentObj = obj;
		if (!currentObj.moving) {
			if (moveObjectError == false) {
				moveObjectReset(currentObj);
				if (moveObjectError == false && currentObj.startTravelDistance != 0) {
					var tempDate = new Date();
					currentObj.startTime = tempDate.getTime();
					currentObj.moving = true;					
				}
			} else {
				//stop timer
				//kill();
			}
		}
		if (currentObj.moving) {
			//set time
			var tempDate = new Date();
			currentObj.time = (tempDate.getTime() - currentObj.startTime)/1000;
			var diffTime = currentObj.time - currentObj.lastTime;
			currentObj.lastTime = currentObj.time;
			var newVelocity;
			if (currentObj.isDeAccelerating) {
				newVelocity = currentObj.velocity + currentObj.deAcceleration * diffTime;
			} else {
				newVelocity = currentObj.velocity + currentObj.acceleration * diffTime;
			}
			
			if (currentObj.maxVelocity != null) {
				if (Math.round(newVelocity) >= currentObj.maxVelocity) {
					newVelocity = currentObj.maxVelocity;
					currentObj.atMaxVelocity = true;
				} else {
					currentObj.atMaxVelocity = false;
				}
			} else {
				//accelerate forever
			}
			
			if (currentObj.maxVelocity) {
				if (!currentObj.atMaxVelocity) {
					currentObj.acceleration = (1-(newVelocity/currentObj.maxVelocity))*currentObj.constantAcceleration; 
				}
			} else {
				currentObj.acceleration = currentObj.constantAcceleration;
			}
			
			//calculate angle
			//currentObj.angle = (currentObj.xTo-currentObj.xFrom)/(currentObj.yTo-currentObj.yFrom);
			
			//calculate place from start to move object
			//currentObj.apa = currentObj.d;
			//currentObj.d = currentObj.velocity*currentObj.time + ((currentObj.deAcceleration*currentObj.time*currentObj.time)/2);
			currentObj.d = currentObj.d + newVelocity * diffTime * currentObj.direction;
			//debugForm(currentObj.d, 'asasa');			
			//alert(currentObj.d);
			//currentObj.apa = currentObj.d - currentObj.apa;
			
			if (!currentObj.contignousMovement) {
				//calculate traveldistance
				if (currentObj.startTravelDistance == null) {
					currentObj.startTravelDistance = currentObj.d + (currentObj.velocity * currentObj.velocity) / (-2 * currentObj.deAcceleration);
				}
				currentObj.travelDistanceLeft = currentObj.startTravelDistance - currentObj.d;
								
				if (!currentObj.isDeAccelerating) {
					currentObj.deAccellerationTest = ((currentObj.velocity * currentObj.velocity + 2 * (currentObj.deAcceleration) * Math.abs(currentObj.travelDistanceLeft)));
					if (currentObj.deAccellerationTest >= 0) {
						currentObj.isDeAccelerating = true;
					}
				}
				
				//if ((currentObj.d > currentObj.startTravelDistance && currentObj.startTravelDistance > 0) || 
				//		(currentObj.d < currentObj.startTravelDistance && currentObj.startTravelDistance < 0)) {
				if (currentObj.lastTravelDistanceLeft == null) {
					currentObj.lastTravelDistanceLeft = currentObj.travelDistanceLeft + currentObj.direction;
				}
				if ((currentObj.travelDistanceLeft > currentObj.lastTravelDistanceLeft && currentObj.direction == 1)
						|| (currentObj.travelDistanceLeft < currentObj.lastTravelDistanceLeft && currentObj.direction == -1)
						|| (currentObj.d > currentObj.startTravelDistance && currentObj.direction == 1)
						|| (currentObj.d < currentObj.startTravelDistance && currentObj.direction == -1)) {
					currentObj.d = currentObj.startTravelDistance;
					currentObj.moving = false;
				}
				currentObj.lastTravelDistanceLeft = currentObj.travelDistanceLeft;
			}
			
			var isDelimited = false;
			var doPlacement = false;
			if (currentObj.horisontal) {
				//fix the left/right as below for up down
				placeObject(currentObj, (currentObj.xFrom + currentObj.d), null, true);	
			} else if (currentObj.vertical) {
				var placeY = 0;
				placeY = (currentObj.yFrom + currentObj.d);
				if (currentObj.yTop != null && currentObj.direction < 0) {
					if (placeY < currentObj.yTop) {
						moveObjectKill (currentObj);
						placeObject(currentObj, null, currentObj.yTop, true);
					} else {
						doPlacement = true;	
					}
				} else if (currentObj.yBottom != null && currentObj.direction > 0) {
					if (placeY >= currentObj.yBottom) {
						moveObjectKill (currentObj);
						placeObject(currentObj, null, currentObj.yBottom, true);						
					} else {
						doPlacement = true;	
					}
				} else {
					doPlacement = true;	
				}
				if (doPlacement) {
					placeObject(currentObj, null, placeY, true);				
				} else if (currentObj.atLimiterEval && !doPlacement) {
					eval(currentObj.atLimiterEval);
				}
			}
			currentObj.velocity = newVelocity;
			/*
			if (currentObj.direction == -1) {
				debugForm('time: ' + currentObj.time, 't', 90, 500);
				debugForm('deAccellerationTest: ' + currentObj.deAccellerationTest, 'v');
				debugForm('velocity: ' + Math.round(currentObj.velocity), 'vp');
				debugForm('constantacceleration: ' + currentObj.constantAcceleration, 'ca');
				debugForm('d: ' + currentObj.d, 'd');
				debugForm('acceleration: ' + currentObj.acceleration, 'a');
				debugForm('start traveldistance: ' + currentObj.startTravelDistance, 'std');
				debugForm('traveldistanceLeft: ' + currentObj.travelDistanceLeft, 'td');
				debugForm('deAccelleration: ' + currentObj.deAcceleration, 'dec');
				debugForm('isDeAcc: ' + currentObj.isDeAccelerating, 'da');
				debugForm('diffTime: ' + diffTime, 'dt');
			}*/
			
		}
		if (moveObjectError == false && currentObj.moving) {
			//testTT = setTimeout('moveObjectCalculation(currentObj)', 10);
		} else {
			moveDelete(currentObj.moveIndex);
			if (currentObj.endMoveEval) {
				eval(currentObj.endMoveEval);
			}
			//kill();
		}
	}	
	
	function moveObjectKill (obj) {
		var currentObj = obj;
		//debugForm('killin: ' + currentObj.innerText, 'fjlsdhfjksd');
		//alert('killing: '+ currentObj.innerText+ ' index: ' +currentObj.moveIndex);
		currentObj.moving = false;
		moveDelete(currentObj.moveIndex);
	}
	
	function stopObjectLinear(obj, xStop, yStop, dInterval, endMoveEval) {
		var currentObj = obj;
		var dStop = null;
		if (!endMoveEval) {
			endMoveEval == null;
		}
		currentObj.endMoveEval = endMoveEval;
		if (xStop && currentObj.horisontal) {
			dStop = xStop - currentObj.xPos;
		} else if (yStop && currentObj.vertical) {
			dStop = yStop - currentObj.yPos;
		} else if (yStop || xStop) {
			alert('Object confused, direction error');
		}
		if (dInterval) {
			//currentObj.startTravelDistance = currentObj.d + (currentObj.velocity * currentObj.velocity) / (-2 * currentObj.deAcceleration);
			var breakDistance = (currentObj.velocity * currentObj.velocity) / (-2 * currentObj.deAcceleration);
			var multiplications = parseInt((currentObj.yFrom + currentObj.d + breakDistance) / dInterval);
			if (parseInt(breakDistance / dInterval) < (breakDistance / dInterval)) {
				multiplications++;
			}
			currentObj.startTravelDistance = multiplications * dInterval - currentObj.yFrom;
			if (multiplications < 0) {
				//debugForm(currentObj.innerText + '#' + (currentObj.yFrom + currentObj.startTravelDistance), 'dfd' + Math.abs(multiplications));
			} else {
				//debugForm(currentObj.innerText + '#' + (currentObj.yFrom + currentObj.startTravelDistance), 'df' + multiplications);
			}
		} else if (dStop) {
			currentObj.startTravelDistance = currentObj.d + dStop;
			var deAccelerationForce = (currentObj.velocity * currentObj.velocity) / (-2 * dStop);
			if (deAccelerationForce > currentObj.deAcceleration) {
				currentObj.deAcceleration = deAccelerationForce;
			}
		}
		currentObj.contignousMovement = false;
	}
	
	function setObjectClip(obj, t, w, h, l) {
//		rect(offsetTOP, BREDD, HÖJD, OffsetLEFT);
		var currentObj = obj;
		if (document.getElementById || document.all) {
			obj.style.clip = 'rect('+t+'px '+w+'px '+h+'px '+l+'px)';
		}
		else if (document.layers) {
			obj.clip.top = t;
			obj.clip.bottom = h;
		}
		else { alert('This browser does not support this script.'); }
	}
	
	function splitClip(obj) {
		var currentObj = obj;
		var currentClip = currentObj.style.clip;
		if (currentClip) {
			clipObject = new Object();
			var nextStart = 5;
			var nextEnd = currentClip.indexOf('px', 0);
			clipObject.clipOffsetTop = currentClip.substring(nextStart, nextEnd);
			nextStart = (currentClip.indexOf('px', 0)+2);
			nextEnd = currentClip.indexOf('px', nextStart);
			clipObject.clipWidth = currentClip.substring(nextStart, nextEnd);
			nextStart = (currentClip.indexOf('px', nextEnd)+2);
			nextEnd = currentClip.indexOf('px', nextStart);
			clipObject.clipHeight = currentClip.substring(nextStart, nextEnd);
			nextStart = (currentClip.indexOf('px', nextEnd)+2);
			nextEnd = currentClip.indexOf('px', nextStart);
			clipObject.clipOffsetLeft = currentClip.substring(nextStart, nextEnd);					
			return clipObject;
		} else {
			return false;
		}
	}
	
	function getObjectXClip(obj) {
		var currentObj = obj;
		currentObj.xPos = getXpos(currentObj);
		var clipObj = splitClip(currentObj);
		if (clipObj) {
			return parseInt(currentObj.xPos+clipObj.clipOffsetLeft);
		} else {
			return null;
		}
	}
	
	function getObjectYClip(obj) {
		var currentObj = obj;
		currentObj.yPos = getYpos(currentObj);
		var clipObj = splitClip(currentObj);
		alert(clipObj);
		if (clipObj) {
			return parseInt(currentObj.yPos+clipObj.clipOffsetTop);
		} else {
			return null;
		}
	}	

	function getObjectClipWidth(obj) {
		var currentObj = obj;
		var clipObj = splitClip(currentObj);
		if (clipObj) {
			return parseInt(clipObj.clipWidth);
		} else {
			return null;
		}
	}	
	
	function getObjectClipHeight(obj) {
		var currentObj = obj;
		var clipObj = splitClip(currentObj);
		if (clipObj) {
			return parseInt(clipObj.clipHeight);
		} else {
			return null;
		}
	}		
	
	function stopTimeOut(timeOutObj) { 
		if (timeOutObj != null) { 
			clearTimeout(timeOutObj);
		} 
		timeOutObj = null;
	}
	
	function setObjectWidth(obj, objWidth) {
		if (obj.style) { obj.style.width = objWidth; obj.width = objWidth; }
		else if (obj.width) { obj.width = objWidth; }
	}

	function setObjectHeight(obj, objHeight) {
		if (obj.style) { obj.style.height = objHeight; obj.height = objHeight; }
		else if (obj.height) { obj.height = objHeight; }
	}	
	
	function getObjectWidth(obj) {
		oWidth = false;
		if (obj.style) { oWidth = parseInt(obj.style.width); }
		else if (obj.width) { oWidth = parseInt(obj.width); }
		return oWidth;
	}

	function getObjectHeight(obj) {
		oHeight = false;
		if (obj.style) { oHeight = parseInt(obj.style.height); }
		else if (obj.height) { oHeight = parseInt(obj.height); }
		return oHeight;
	}

	function popup(popupUrl, windowName, width, height, top, left, resizable, scrollbars, menubar, status, toolbar, location, directories) {
		page = getPageSize();
		if (!width) { width=page.width; }
		if (!height) { height=page.height; }
		if (!top) { top = 100; }
		if (!left) { left = 100; }

		if (!resizable) { resizable = 'yes'; }
		if (!scrollbars) { scrollbars = 'yes'; }
		if (!menubar) { menubar = 'yes'; }

		if (!status) { status = 'yes'; }
		if (!toolbar) { toolbar = 'yes'; }
		if (!location) { location = 'yes'; }
		if (!directories) { directories = 'yes'; }

		properties = 'width='+width+', height='+height+', left='+left+', top='+top+', resizable='+resizable+', scrollbars='+scrollbars+', menubar='+menubar+', status='+status+', toolbar='+toolbar+', location='+location+', directories='+directories
		window.open(popupUrl, windowName, properties);
	}

	function changeObjectContent(obj, content) {
		if (obj) {
			var currentObj = obj;
			currentObj.innerHTML = content;
		}
	}

	function getPageSize() {
		var getPageSizeObj = new Object();
		if (document.all) { getPageSizeObj.width = document.body.clientWidth; }
		else if (document.layers) { getPageSizeObj.width = window.innerWidth; }
		else if (document.getElementById) { getPageSizeObj.width = self.innerWidth; }

		if (document.all) { getPageSizeObj.height = document.body.clientHeight; }
		else if (document.layers) { getPageSizeObj.height = window.innerHeight; }
		else if (document.getElementById) { getPageSizeObj.height = self.innerHeight; }
		return getPageSizeObj;
	}

	function showObject(obj) {
		if (document.all || document.getElementById) { obj.style.visibility = "visible"; }
		else if (document.layers) { obj.visibility = "show"; }
	}
	
	function hideObject(obj) {
		if (document.all || document.getElementById) { obj.style.visibility = "hidden"; }
		else if (document.layers) { obj.visibility = "hide"; }
	}

	// to use this function in netscape with nested layers, use it like this:
	// getObject('layerid', 'parenttolayerid', 'parenttoparentlayerid', and so on);
	function getObject(layerName, referenceLayer) {
		//alert('ii'+layerName);
		var referenceLayerString = '';
		var currentObj = null;
		if (document.getElementById) {currentObj = document.getElementById(layerName);}
		else if (document.all) { currentObj = document.all(layerName); }
		else if (document.layers) {
			if (referenceLayer) {
				for (i=(arguments.length-1); i > 1; i--) {
					referenceLayerString = referenceLayerString + 'document.layers[\''+arguments[i]+'\'].';
				}
				referenceLayerString = referenceLayerString+'document.layers[\''+referenceLayer+'\'].';
			}
			currentObj = eval(referenceLayerString+'document.layers[\''+layerName+'\']');
		}
		//alert(referenceLayerString+'pp'+currentObj);
		return currentObj;
	}
	
	function placeObject(obj, xPos, yPos, setPositions, zPos) {
	// Lägg inte till argument till funkltioner i default.js om de inte läggs till sist! 
	// funktionerna här används av flera andra funktioner!
	
		var currentObj = obj;
		if (currentObj.style) {
			if (xPos || xPos == 0) { currentObj.style.left = xPos; }
			if (yPos || yPos == 0) { currentObj.style.top = yPos; }
			if (zPos) { currentObj.style.zIndex = zPos; }
		}
		else if (document.layers) {
			if (xPos || xPos == 0) { currentObj.left = xPos; }
			if (yPos || yPos == 0) { currentObj.top = yPos; }
			if (zPos) { currentObj.zIndex = zPos; }
		}
		if (setPositions) {
			currentObj.xPos = xPos;
			currentObj.yPos = yPos;
		}
	}
	
	highestZ = 5;
	function setZindex(obj, zIndex) {
		if (!zIndex) { highestZ += 1; zIndex = highestZ; }
		if (document.getElementById || document.all) {
			obj.style.zIndex = zIndex;
		}
		else if (document.layers) {
			obj.zIndex = zIndex;
		}
	}

	function getXpos(obj) {
		var currentObj = obj;
		var xPos = null;
		if (currentObj.style) { xPos = parseInt(currentObj.style.left); }
		else { xPos = parseInt(currentObj.left); }
		return xPos;
	}

	function getYpos(obj) {
		if (obj.style) { yPos = parseInt(obj.style.top); }
		else if (obj.top) { yPos = parseInt(obj.top); }
		else { yPos = false; }		
		return yPos;
	}
	
	function toggleObjectDisplay(obj, setStatus, isInline) {
		if (!document.layers) {
			var currentObj = obj;
			if (setStatus) {
				currentObj.style.display = setStatus;
			} else {
				if (currentObj.style.display != 'block' && currentObj.style.display != 'inline') {
					if (isInline) { currentObj.style.display = 'inline'; }
					else { currentObj.style.display = 'block'; }
				} else {
					currentObj.style.display = 'none';
				}
			}
		}
	}


	function chImgSrc(imgName, imgSrc) {
		if (!document.layers) {
			document.images[imgName].src = imgSrc;
		}
	}

	fadeIndex = null;
	function fade(layerFrom, imgSrc) {
		if (document.getElementById) {
			var obj = document.getElementById(layerFrom);
	
			//Change Pic
			// FIXA DYNAMISKA IMG - NAMN
			// FIXA SÅ DEN FUNKAR I NS
			if (obj.filters.alpha.opacity == 100) {
				document.images["imageLayerBack"].src = imgSrc;
			} else if (obj.filters.alpha.opacity == 0) {
				document.images["imageLayerFront"].src = imgSrc;
			}
	
			//Fade layer
			if (obj.filters.alpha.opacity == 100) { fadeValue = -5; }
			else if (obj.filters.alpha.opacity == 0) { fadeValue = 5; }
			obj.filters.alpha.opacity += fadeValue;
	
			if (obj.filters.alpha.opacity > 0 && obj.filters.alpha.opacity < 100) {
			} else { worldDelete(fadeIndex); }
		}
	}

	imageN = 0;
	function preloadImage(imageUrl) {
		eval('image'+imageN+' = new Image()');
		eval('image'+imageN+'.src = imageUrl');
		imageN++;
	}

	function getIframeBody(obj) {
		var currentObj = obj;
		var returnVal = null;
		if (currentObj.contentDocument) {
			returnVal = currentObj.contentDocument.body;
		} else if(currentObj.contentWindow) {
			returnVal = currentObj.contentWindow.body;
		} else if (currentObj.document) {
			returnVal = currentObj.document.body;
		}
		return returnVal;
	}

	function getIframeWindow(obj) {
		var currentObj = obj;
		var returnVal = null;
		if(currentObj.contentWindow) {
			// IE 6 (verified)
			// Mozilla 1 (verified)
			returnVal = currentObj.contentWindow;
		} else if (currentObj.document) {
			returnVal = currentObj;
		}
		return returnVal;
	}
	
	function getIframeDocument(obj) {
		//alert('getIframeDocument');
		var currentObj = obj;
		var returnVal = null;
		if (currentObj.contentDocument) {
			// Mozilla 1 (verified)
			returnVal = currentObj.contentDocument;
		} else if(currentObj.contentWindow) {
			// IE 6 (verified)
			// Mozilla 1 (verified)
			returnVal = currentObj.contentWindow.document;
		} else if (currentObj.document) {
			returnVal = currentObj.document;
		}
		//alert('returning');
		//alert(returnVal);
		return returnVal;
	}
	
	function getIframe(name) {
		return window.frames[name];
	}
	
	/*
		Creates or updates a debugform (use debugStart first)
		
	*/

	debugActive = false;
	function debugStart(inputValue, inputName, inputTop, inputLeft) {
		debugActive = true;
		debugForm(inputValue, inputName, inputTop, inputLeft);
	}
	
	function debugForm(inputValue, inputName, inputTop, inputLeft) {
		if (debugActive) {
			if (window.document.forms.debugForm) {
			} else {
				window.document.write('<form name="debugForm" style="position: absolute;" class="debugForm" onselectstart="return true" ondragstart="return false"></form>');
			}
			var formObj = window.document.forms.debugForm;
			if (!inputName) {
				inputName = 'debug';
			}
			if (!eval('formObj.'+inputName)) {
				formObj.innerHTML = formObj.innerHTML + '<input type="text" name="'+inputName+'" style="width:500px;"><br />';
			}
	
			eval('formObj.'+inputName+'.value = inputValue');
			if (inputTop != null && inputLeft != null) { 
				placeObject(formObj, inputLeft, inputTop, true);
			}
		} else {
			alert('debugForm() ERROR: Use debugStart() in body first..');
		}
	}
	
	/*
		object can be a string or whatever object you like to test
	*/
	function isInArray(array, object) {
		for (var i = 0; i < array.length; i++) {
			if (array[i] == object) {
				return true;
				break;
			}
		}
	}

	/*
		object can be a string or whatever object you like to test
	*/
	function whereInArray(array, object) {
		var i = null;
		for (i = 0; i < array.length; i++) {
			if (array[i] == object) {
				break;
			}
		}
		return i;
	}
	
	/**
	 * Make the option with the value optionValue  selected.
	 **/
	function selectOption (form, selector, optionValue) {
		var objSelector = eval('document.forms.'+form+'.'+selector);
		for (var i = 0; i < objSelector.length; i++) {
			if (objSelector.options[i].value == optionValue) {
				objSelector.options[i].selected = true;
				break;
			}
		}
	}
	
	function instanceOf(object, constructorFunction) {
		while (object != null) {
			if (object == constructorFunction.prototype) {
				return true;
			}
			object = object.__proto__;
	 	}
		return false;
	}	
	
	function propertyNames(o) {
		var tempStr;
		var i = 0;
		tempStr = 'Properties in object:\n';
		for (property in o) {
			tempStr = tempStr + property + ' ';
			if (i == 4) {
				tempStr = tempStr + '\n';
				i = 0;
			}
			i++;
		}
		alert(tempStr);
	}

	/* 
	name - name of the cookie
	value - value of the cookie
	[expires] - expiration date of the cookie (defaults to end of current session)
	[path] - path for which the cookie is valid (defaults to path of calling document)
	[domain] - domain for which the cookie is valid (defaults to domain of calling document)
	[secure] - Boolean value indicating if the cookie transmission requires a secure transmission
	* an argument defaults when it is assigned null as a placeholder
	* a null placeholder is not required for trailing omitted arguments
	*/
	function setCookie(name, value, expires, path, domain, secure) {
	  var curCookie = name + "=" + escape(value) +
	      ((expires) ? "; expires=" + expires.toGMTString() : "") +
	      ((path) ? "; path=" + path : "") +
	      ((domain) ? "; domain=" + domain : "") +
	      ((secure) ? "; secure" : "");
	  document.cookie = curCookie;
	}
	
	// name - name of the desired cookie
	// * return string containing value of specified cookie or null if cookie does not exist
	function getCookie(name) {
	  var dc = document.cookie;
	  var prefix = name + "=";
	  var begin = dc.indexOf("; " + prefix);
	  if (begin == -1) {
	    begin = dc.indexOf(prefix);
	    if (begin != 0) return null;
	  } else
	    begin += 2;
	  var end = document.cookie.indexOf(";", begin);
	  if (end == -1)
	    end = dc.length;
	  return unescape(dc.substring(begin + prefix.length, end));
	}
	
	// name - name of the cookie
	// [path] - path of the cookie (must be same as path used to create cookie)
	// [domain] - domain of the cookie (must be same as domain used to create cookie)
	// * path and domain default if assigned null or omitted if no explicit argument proceeds
	function deleteCookie(name, path, domain) {
	  if (getCookie(name)) {
	    document.cookie = name + "=" + 
	    ((path) ? "; path=" + path : "") +
	    ((domain) ? "; domain=" + domain : "") +
	    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
	  }
	}