/*
		
			Dynamic Object Library 1.0 ~ dynobject.js
			Mattias Henell, Accor Services 2009
		
			Tested: PC IE7.05, PC FF3.07
			
			first use:
			mynewDynamicObject = new dynObject(HTMLelementId);
			
			for moving operations, first set mynewDynamicObject.gotoX, mynewDynamicObject.gotoY
			
			for resizing operations, first set mynewDynamicObject.newSizeX, mynewDynamicObject.newSizeY
			
			The following functions are available:
			mynewDynamicObject.move()
			mynewDynamicObject.initSmoothMove()
			mynewDynamicObject.resize()
			mynewDynamicObject.show()
			mynewDynamicObject.hide()
			mynewDynamicObject.changeContent(newContent)
			mynewDynamicObject.topRightAlignWith(referingObject, distance) ~referingObject = HTMLElement.id
			mynewDynamicObject.topLeftAlignWith(referingObject, distance) ~referingObject = HTMLElement.id
			mynewDynamicObject.upperLeftAlignWith(referingObject, distance) ~referingObject = HTMLElement.id
			mynewDynamicObject.upperRightAlignWith(referingObject, distance) ~referingObject = HTMLElement.id
			mynewDynamicObject.bottomLeftAlignWith(referingObject, distance) ~referingObject = HTMLElement.id
			mynewDynamicObject.bottomRightAlignWith(referingObject, distance) ~referingObject = HTMLElement.id
			mynewDynamicObject.ajaxRequest(openThis) ~ openThis = url
*/
		
		
		
		
		//main 'class' for dynamic objects 				
			function dynObject(objId){
				
				this.oid = document.getElementById(objId);
				this.oname = objId;
				this.posX = "";
				this.posY = "";
				this.newX = "";
				this.newY = "";
				this.gotoX = "";
				this.gotoY = "";
				this.speed = 50;
				this.direction = "forward";
				this.Ydirection = "down";
				this.posX = this.oid.offsetLeft;
				this.posY = this.oid.offsetTop;
				this.shouldMoveX=1;
				this.shouldMoveY=1;
				this.done = false;
				this.donext = "";
			}
			
			//setting the left and top properties of the object
			function setXY(){
				this.posX = this.oid.offsetLeft;
				this.posY = this.oid.offsetTop;
			}
			
			//moves an object
			function move(){
				if(this.shouldMoveX==1)	this.oid.style.left = this.gotoX;
				if(this.shouldMoveY==1)	this.oid.style.top = this.gotoY;
			}
			
			//call this object function to start a sliding movement ~ object.initSmoothMove()			
			function initSmoothMove(){
				if(this.newX < this.posX){
					this.direction = "backward";
				}
				if(this.newX > this.posX) {
					this.direction = "forward";
				}
				if(this.newY < this.posY){
					this.Ydirection = "up";
				}
				if(this.newY > this.posY) {
					this.Ydirection = "down";
				}
				
				this.smoothMove();
			}
			
			//function for making sliding movements of elements,dont call this function directly. ~ uses object.move()			
			function smoothMove(){			
				
				this.shouldMoveX = 0;
				this.shouldMoveY = 0;
				
				
				if(this.direction == "forward"){
					if(this.posX < this.newX){
						this.gotoX = this.posX + this.speed;
						this.shouldMoveX=1;
					}
				}
				if(this.direction == "backward") {
					if(this.posX > this.newX){
						this.gotoX = this.posX - this.speed;
						this.shouldMoveX=1;				
					}
				}
				
				if(this.Ydirection=="down"){
					if(this.posY < this.newY){
						this.gotoY = this.posY + this.speed;
						this.shouldMoveY=1;
					}
				}
				
				if(this.Ydirection=="up"){
					if(this.posY > this.newY){
						this.gotoY = this.posY - this.speed;
						this.shouldMoveY=1;
					}
				}
				if(this.shouldMoveX==1 || this.shouldMoveY==1){
					this.setXY();
					this.move();
					this.returnSmoothMove(); 
				}
				
			}
			
			//function to call back to smoothMove after timeout has passed, do not call this function	directly		
			function returnSmoothMove(){			
				obj = this;
			  this.timerS = setTimeout("obj.smoothMove()", 10);
			}
			
			//function to grow an object
			function resize(){
				if(!this.newSizeX) this.newSizeX = 400;
				if(!this.newSizeY) this.newSizeY = 400;
				
				if(this.newSizeX < this.oid.clientWidth){
					this.shrinkX = true;
				}
				else this.shrinkX = false;
				
				if(this.newSizeY < this.oid.clientHeight){
					this.shrinkY = true;
				}
				else this.shrinkY = false;
					this.done = false;
					this.grow();
			}
			
			//function to make a nice growing/shrinking object
			function grow(){				
				this.callagain = 0;
				
				if((this.shrinkY == false) && (this.oid.clientHeight < this.newSizeY)){
					this.oid.style.height = this.oid.clientHeight + this.speed;
					this.callagain = 1;
				}
				
				else if((this.shrinkY == true) && (this.oid.clientHeight > this.newSizeY)){
					this.oid.style.height = this.oid.clientHeight - this.speed;
					this.callagain = 1;
				}
				
				if((this.shrinkX == false) && (this.oid.clientWidth < this.newSizeX)){
						this.oid.style.width = this.oid.clientWidth + this.speed;
						this.callagain = 1;
				}
				
				else if((this.shrinkX == true) && (this.oid.clientWidth > this.newSizeX)){
					this.oid.style.width = this.oid.clientWidth - this.speed;
					this.callagain = 1;
				}
								
				if(this.callagain==1){
					this.returnGrow();
				}
				else {
				
					
					if(this.donext!=""){
						eval(this.donext);
						this.done = true;
					}
				
					}
			}
			//calling object.grow after timer is done
			function returnGrow(){
				obj = this;
			  this.timerG = setTimeout("obj.grow()", 10);
			}

			function hide(){
				this.oid.style.visibility = "hidden";
			}
			
			function show(){
				this.oid.style.visibility = "visible";
			}
				
			function showHide(){
				(this.oid.style.visibility=='hidden') ?  this.oid.style.visibility='visible' : this.oid.style.visibility='hidden';
			}	
			
			function changeContent(newContent){
				this.oid.innerHTML = newContent;
			}
			
			//function for dynamicly align object to the right of the refering object, with option to add distance in "pixels"
			function topRightAlignWith(referingObject, distance){
				if(distance=="") distance = 1;
				ref = document.getElementById(referingObject);
				
				Ol = getRealPosLeft(ref);
				Ot = getRealPosTop(ref);
				
				rightCorner = Ol + ref.clientWidth;
				topLine = Ot;
				
				distance = parseInt(distance);
				
				this.shouldMoveY=1;
				this.shouldMoveX=1;
				this.gotoX = rightCorner + distance;
				this.gotoY = topLine;
				this.move();
			}
			
			function topLeftAlignWith(referingObject, distance){
				if(distance=="") distance = 1;
				ref = document.getElementById(referingObject);
				
				distance = parseInt(distance);
				
				Ol = getRealPosLeft(ref);
				Ot = getRealPosTop(ref);
				
				newLeft = Ol - this.oid.clientWidth - distance;
				topLine = Ot;
				
				this.shouldMoveY=1;
				this.shouldMoveX=1;
				this.gotoX = newLeft;
				this.gotoY = topLine;
				this.move();
			}
			
			function upperLeftAlignWith(referingObject, distance){
				if(distance=="") distance = 1;
				ref = document.getElementById(referingObject);
				
				Ol = getRealPosLeft(ref);
				Ot = getRealPosTop(ref);
				
				
				newLeft = Ol;
				topLine = Ot - distance - this.oid.clientHeight;
				
				distance = parseInt(distance);
				
				this.shouldMoveY=1;
				this.shouldMoveX=1;
				this.gotoX = newLeft;
				this.gotoY = topLine;
				this.move();
			}
			
			function upperRightAlignWith(referingObject, distance){
				if(distance=="") distance = 1;
				ref = document.getElementById(referingObject);
				
				Ol = getRealPosLeft(ref);
				Ot = getRealPosTop(ref);
				
				
				rightCorner = Ol + ref.clientWidth;
				newLeft = rightCorner - this.oid.clientWidth;
				
				topLine = Ot - distance - this.oid.clientHeight;
				
				distance = parseInt(distance);
				
				this.shouldMoveY=1;
				this.shouldMoveX=1;
				this.gotoX = newLeft;
				this.gotoY = topLine;
				this.move();
			}
			
			function bottomLeftAlignWith(referingObject, distance){
				if(distance=="") distance = 1;
				ref = document.getElementById(referingObject);
				Ol = getRealPosLeft(ref);
				Ot = getRealPosTop(ref);
				newLeft = Ol;
				topLine = Ot + distance + ref.clientHeight;
				
				distance = parseInt(distance);
				
				this.shouldMoveY=1;
				this.shouldMoveX=1;
				this.gotoX = newLeft;
				this.gotoY = topLine;
				this.move();
			}
			
			function bottomRightAlignWith(referingObject, distance){
				if(distance=="") distance = 1;
				ref = document.getElementById(referingObject);
				Ol = getRealPosLeft(ref);
				Ot = getRealPosTop(ref);
				
				newLeft = Ol +ref.clientWidth - this.oid.clientWidth;
				topLine = Ot + distance + ref.clientHeight;
				
				distance = parseInt(distance);
				
				this.shouldMoveY=1;
				this.shouldMoveX=1;
				this.gotoX = newLeft;
				this.gotoY = topLine;
				this.move();
			}
			
			function ajaxRequest(openThis) {			   
			   curObj = this;
			   
			   var AJAX = null; 
			   if (window.XMLHttpRequest) { 
			      AJAX=new XMLHttpRequest(); 
			   } else {
			      AJAX=new ActiveXObject("Microsoft.XMLHTTP"); 
			   }
			   if (AJAX==null) { 
			      alert("Din browser stödjer inte AJAX."); 
			      return false 
			   }
			   AJAX.onreadystatechange = function() { 
			      if (AJAX.readyState==4 || AJAX.readyState=="complete") { 
			         curObj.changeContent(AJAX.responseText);
			      }  
			   }
			   
			   ieCacheParam = Math.random()*111;
			   
			   AJAX.open("GET", openThis+ "?d="+ieCacheParam, true);
			   AJAX.send(null);
			}
			
			function getRealPosLeft(obj){			
				curL = 0;

				if(obj.offsetParent){
							do {
								curL+= obj.offsetLeft;
								
							} while (obj = obj.offsetParent)
				}
				return curL;
			}

			
			function getRealPosTop(obj){			
				curT = 0;

				if(obj.offsetParent){
							do {
								curT+= obj.offsetTop;
								
							} while (obj = obj.offsetParent)
				}
				return curT;
			}
						
			//assingning functions to be subroutines to the dynObject "class" 
			dynObject.prototype.returnSmoothMove = returnSmoothMove; 
			dynObject.prototype.initSmoothMove = initSmoothMove;		
			dynObject.prototype.smoothMove = smoothMove;
			dynObject.prototype.move = move;
			dynObject.prototype.setXY = setXY;
			dynObject.prototype.resize = resize;
			dynObject.prototype.grow = grow;
			dynObject.prototype.returnGrow = returnGrow;
			dynObject.prototype.hide = hide;
			dynObject.prototype.show = show;
			dynObject.prototype.showHide = showHide;
			dynObject.prototype.changeContent = changeContent;
			dynObject.prototype.topRightAlignWith = topRightAlignWith;
			dynObject.prototype.topLeftAlignWith = topLeftAlignWith;
			dynObject.prototype.upperLeftAlignWith = upperLeftAlignWith;
			dynObject.prototype.upperRightAlignWith = upperRightAlignWith;
			dynObject.prototype.bottomLeftAlignWith = bottomLeftAlignWith;
			dynObject.prototype.bottomRightAlignWith = bottomRightAlignWith;
			dynObject.prototype.ajaxRequest = ajaxRequest;
			
