//+---------------+\\
//| DHTML-Support |\\
//| developed  by |\\
//| Frank Boukamp |\\
//+---------------+\\


//+-----------------+\\
//|   Constructor   |\\
//|       for       |\\
//| Browser-Objects |\\
//+-----------------+\\

function Browser() {
    var BrowserAgent = navigator.userAgent.toLowerCase();
    this.major = parseInt(navigator.appVersion);
    this.minor = parseFloat(navigator.appVersion);
    this.w3c = document.documentElement;
    this.ns = !this.w3c && ((BrowserAgent.indexOf('mozilla') != -1) && ((BrowserAgent.indexOf('spoofer')==-1) && (BrowserAgent.indexOf('compatible') == -1)));
    this.ns2 = (this.ns && (this.major == 2));
    this.ns3 = (this.ns && (this.major == 3));
    this.ns4b = (this.ns && (this.minor < 4.04)); //does not support image.onload-Events
    this.ns4 = (this.ns && (this.major >= 4));
    this.ie = (BrowserAgent.indexOf("msie") != -1);
    this.ie3 = (this.ie && (this.major == 2));
    this.ie4 = (this.ie && (this.major >= 4));
    this.op3 = (BrowserAgent.indexOf("opera") != -1);
    this.win = (BrowserAgent.indexOf("win") != -1);
    this.mac = (BrowserAgent.indexOf("mac") != -1);
    this.unix = (BrowserAgent.indexOf("x11") != -1);
	this.aol = (BrowserAgent.indexOf("aol") != -1);
	
	this.window = window;
	
	this.allowsDHTML = allowsDHTML;
	this.getScreenWidth = getScreenWidth;
	this.getScreenHeight = getScreenHeight;
	this.getAvailableWidth = getAvailableWidth;
	this.getAvailableHeight = getAvailableHeight;
	this.getStatus = getStatus;
	this.setStatus = setStatus;
	this.getActualTop = getActualTop;
	this.getActualLeft = getActualLeft;
}


//+-----------------+\\
//|    Functions    |\\
//|       for       |\\
//| Browser-Objects |\\
//+-----------------+\\


//+------------------------------+\\
//| Checking if DHTML is allowed |\\
//|  (YES = true | NO = false)   |\\
//+------------------------------+\\

function allowsDHTML() {
	return ((this.ns4 || this.ie4 || this.w3c) && (!this.aol));
}


//+------------------------------------+\\
//| determines the width of the actual |\\
//|      resolution of the screen      |\\
//+------------------------------------+\\

function getScreenWidth() {
	return screen.width;
}


//+-------------------------------------+\\
//| determines the height of the actual |\\
//|      resolution of the screen       |\\
//+-------------------------------------+\\

function getScreenHeight() {
	return screen.height;
}


//+-----------------------------------+\\
//| determines the available width in |\\
//|        the browsers window        |\\
//+-----------------------------------+\\

function getAvailableWidth() {
	if(this.ns4) {
		return this.window.innerWidth;
	} else if(this.ie4) {
	  	return this.window.document.body.clientWidth;
	}
	return false;
}


//+------------------------------------+\\
//| determines the available height in |\\
//|        the browsers window         |\\
//+------------------------------------+\\

function getAvailableHeight() {
	if(this.ns4) {
		return this.window.innerHeight;
	} else if(this.ie4) {
	  	return this.window.document.body.clientHeight;
	}
	return false;
}


//+--------------------------------------+\\
//| determines the text in the statusbar |\\
//|        of the browsers window        |\\
//+--------------------------------------+\\

function getStatus() {
	return this.window.status;
}


//+--------------------------------+\\
//| sets the text in the statusbar |\\
//|     of the browsers window     |\\
//+--------------------------------+\\

function setStatus(newStatus) {
	this.window.defaultStatus = newStatus;
	this.window.status = newStatus;
	return true;
}


function getActualTop() {
	Returner = this.ns? parseInt(this.window.pageYOffset) : this.window.document.body.scrollTop;
	return Returner;
}

function getActualLeft() {
	Returner = this.ns? parseInt(this.window.pageXOffset) : this.window.document.body.scrollLeft;
	return Returner;
}
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////

//+---------------+\\
//|  Constructor  |\\
//|      for      |\\
//| Layer-Objects |\\
//+---------------+\\

function Layer(id, bodyId, parentLayer) {
	this.Browser = new Browser();
	if(this.Browser.allowsDHTML()) {
		this.name = id;
		if(parentLayer==null) {
			this.layer = this.Browser.ns? document.layers[id] : null;
		} else {
		  	this.layer = this.Browser.ns? parentLayer.document.layers[id] : null;
		}
		this.parentArea = this.Browser.ns? null : this.Browser.ie? document.all[bodyId] : document.getElementById(bodyId);
		this.el = this.Browser.ns? this.layer : this.Browser.ie? document.all[id] : document.getElementById(id);
		this.cssStyle = this.Browser.ns? this.el : this.el.style;
		this.ignoreScreen = false; 
		this.visible = false;

		this.setVisible = setVisible;
		this.changeVisibility = changeVisibility;
		this.setLocation = setLocation;
		this.getLeft = getLeft;
		this.setLeft = setLeft;
		this.getTop = getTop;
		this.setTop = setTop;
		this.clipTo = clipTo;
		this.getWidth = getWidth;
		this.setWidth = setWidth;
		this.getHeight = getHeight;
		this.setHeight = setHeight;
		this.getZ = getZ;
		this.setZ = setZ;
		this.ignoreScreenSize = ignoreScreenSize;
		this.takeLocation = takeLocation;
		this.moveUp = moveUp;
		this.moveDown = moveDown;
		this.moveRight = moveRight;
		this.moveLeft = moveLeft;
		this.move = move;
		this.getLeftClip = getLeftClip;
		this.getRightClip = getRightClip;
		this.getTopClip = getTopClip;
		this.getBottomClip = getBottomClip;
		this.clipTo = clipTo;
		this.moveClip = moveClip;
		this.moveLeftClip = moveLeftClip;
		this.moveRightClip = moveRightClip;
		this.moveTopClip = moveTopClip;
		this.moveBottomClip = moveBottomClip;
		
		return this;
	} else {
	  	return null;
	}
}


//+---------------+\\
//|   Functions   |\\
//|      for      |\\
//| Layer-Objects |\\
//+---------------+\\


//+-----------------------------------------+\\
//|    sets the visibility of the Layer     |\\
//| vis: true = visible | false = invisible |\\
//+-----------------------------------------+\\

function setVisible(vis) {
	this.visible = vis;
	if (vis) {
	   	this.cssStyle.visibility = "visible";
	} else {
	  	this.cssStyle.visibility = "hidden";
	}
}


//+------------------------+\\
//| changes the visibility |\\
//|      of the Layer      |\\
//+------------------------+\\

function changeVisibility() {
	this.setVisible(!this.visible);
}


//+--------------------------------------------------+\\
//| changes the coordinates of the upper left corner |\\
//|                   of the Layer                   |\\
//+--------------------------------------------------+\\

function setLocation(x,y) {
	this.setTop(y);
	this.setLeft(x);
}


//+----------------------------------------------+\\
//| returns the x-value of the upper left corner |\\
//|                 of the Layer                 |\\
//+----------------------------------------------+\\

function getLeft() {
	return this.Browser.ns? this.cssStyle.left : this.cssStyle.pixelLeft;
}


//+-------------------------------------------+\\
//| sets the x-value of the upper left corner |\\
//|               of the Layer                |\\
//+-------------------------------------------+\\

function setLeft(newLeft) {
	this.cssStyle.left = newLeft;
}


//+----------------------------------------------+\\
//| returns the y-value of the upper left corner |\\
//|                 of the Layer                 |\\
//+----------------------------------------------+\\

function getTop() {
	return this.Browser.ns? this.cssStyle.top : this.cssStyle.pixelTop;
}


//+-------------------------------------------+\\
//| sets the y-value of the upper left corner |\\
//|               of the Layer                |\\
//+-------------------------------------------+\\

function setTop(newTop) {
	this.cssStyle.top = newTop;
}

//+-------------------+\\
//| returns the width |\\
//|   of the Layer    |\\
//+-------------------+\\

function getWidth() {
	return this.Browser.ns? this.cssStyle.document.width : this.el.offsetWidth;
}


//+----------------+\\
//| sets the width |\\
//|  of the Layer  |\\		IE ONLY!!!
//+----------------+\\

function setWidth(newWidth) {
	if(this.Browser.ns) {
//		this.cssStyle.clip.width = newWidth;
	} else {
	  	this.cssStyle.width = newWidth;
	}
}


//+--------------------+\\
//| returns the height |\\
//|    of the Layer    |\\
//+--------------------+\\

function getHeight() {
	return this.Browser.ns? this.cssStyle.document.height : this.el.offsetHeight;
}


//+-----------------+\\
//| sets the height |\\
//|  of the Layer   |\\		IE ONLY!!!
//+-----------------+\\

function setHeight(newHeight) {
	if(this.Browser.ns) {
//		this.cssStyle.clip.height = newHeight;
	} else {
	  	this.cssStyle.height = newHeight;
	}
}


//+--------------------+\\
//| returns the zIndex |\\
//|    of the Layer    |\\
//+--------------------+\\

function getZ() { 
	return this.cssStyle.zIndex;
}

//+-----------------+\\
//| sets the zIndex |\\
//|  of the Layer   |\\
//+-----------------+\\

function setZ(index) {
	this.cssStyle.zIndex = index;
}


//+--------------------------------------------+\\
//| sets or returns the ignoreScreen-attribute |\\
//|                of the Layer                |\\
//+--------------------------------------------+\\

function ignoreScreenSize(ignore) {
	if (ignore == null) {
	   	return this.ignoreScreen;
	} else {
	  	this.ignoreScreen = ignore;
	}
}


//+-----------------------------------------+\\
//| takes the coordinates of another Layer  |\\
//| for the upper-left corner of this Layer |\\
//+-----------------------------------------+\\

function takeLocation(divId) {
	this.cssStyle.top = this.Browser.ns? this.Browser.window.document.layers[divId].pageY : this.Browser.window.document.all[divId].offsetTop;
	this.cssStyle.left = this.Browser.ns? this.Browser.window.document.layers[divId].pageX : this.Browser.window.document.all[divId].offsetLeft;
}


//+-----------+\\
//| moves the |\\
//| Layer up  |\\
//+-----------+\\

function moveUp(dY) {
	if (!isNaN(dY)) {
	   	dY = parseInt(dY);
	   	dY = Math.abs(dY);
		newTop = this.getTop() - dY;
		if (this.ignoreScreen) {
		   this.setTop(newTop);
		   return true;
		} else {
		  	if (newTop > this.Browser.getActualTop()) {
			   	this.setTop(newTop);
			   	return true;
			} else {
			  	this.setTop(this.Browser.getActualTop());
				return false;
			}
		}
	} else {
	  	return false;
	}
}


//+------------+\\
//| moves the  |\\
//| Layer down |\\
//+------------+\\

function moveDown(dY) {
	if (!isNaN(dY)) {
	   	dY = parseInt(dY);
	   	dY = Math.abs(dY);
		newTop = this.getTop() + dY;
		if (this.ignoreScreen) {
		   this.setTop(newTop);
		   return true;
		} else {
		  	relevantTop = this.Browser.getActualTop() + this.Browser.getAvailableHeight() - this.getHeight();
		  	if (newTop < relevantTop) {
			   	this.setTop(newTop);
			   	return true;
			} else {
			  	this.setTop(relevantTop);
				return false;
			}
		}
	} else {
	  	return false;
	}
}


//+-----------------+\\
//| moves the Layer |\\
//|   to the left   |\\
//+-----------------+\\

function moveLeft(dX) {
	if (!isNaN(dX)) {
	   	dX = parseInt(dX);
	   	dX = Math.abs(dX);
		newLeft = this.getLeft() - dX;
		if (this.ignoreScreen) {
		   this.setLeft(newLeft);
		   return true;
		} else {
		  	if (newLeft > this.Browser.getActualLeft()) {
			   	this.setLeft(newLeft);
			   	return true;
			} else {
			  	this.setLeft(this.Browser.getActualLeft());
				return false;
			}
		}
	} else {
	  	return false;
	}
}


//+-----------------+\\
//| moves the Layer |\\
//|  to the right   |\\
//+-----------------+\\

function moveRight(dX) {
	if (!isNaN(dX)) {
	   	dX = parseInt(dX);
	   	dX = Math.abs(dX);
		newLeft = this.getLeft() + dX;
		if (this.ignoreScreen) {
		   this.setLeft(newLeft);
		   return true;
		} else {
		  	relevantLeft = this.Browser.getActualLeft() + this.Browser.getAvailableWidth() - this.getWidth(); 
		  	if (newLeft < relevantLeft) {
			   	this.setLeft(newLeft);
			   	return true;
			} else {
			  	this.setLeft(relevantLeft);
				return false;
			}
		}
	} else {
	  	return false;
	}
}


//+-----------------+\\
//| moves the Layer |\\
//+-----------------+\\

function move(dx, dy) {
	if (dx == 0 && dy == 0) {
	   return true;
	}
	oldX = this.getLeft();
	oldY = this.getTop();
	if (dx > 0) {
	   this.moveRight(dx);
	} else if (dx < 0) {
	   this.moveLeft(dx);
	}
	if (dx != 0) {
	   	realdx = this.getLeft() - oldX;
	  	dy = dy * (Math.abs(realdx) / Math.abs(dx));
		dx = realdx;
	}
	if (dy > 0) {
	   this.moveDown(dy);
	} else if (dy < 0) {
	   this.moveUp(dy);
	}
	if (dy != 0) {
	   realdy = this.getTop() - oldY;
	   dx = dx * (Math.abs(realdy) / Math.abs(dy) - 1);
	} else {
	  	dx = 0;
	}
	if (dx > 0) {
	   this.moveRight(dx);
	} else if (dx < 0) {
	   this.moveLeft(dx);
	}
	if (dx == 0 && dy == 0) {
	   	return false
	} else {
	   	return true;
	}
}


function getLeftClip() {
	Returner = this.Browser.ns? this.el.clip.left : String(this.cssStyle.clip);
	alert(Returner);
	return Returner;
}


function getRightClip() {
	return this.cssStyle.clip.right;
}


function getTopClip() {
	return this.cssStyle.clip.top;
}


function getBottomClip() {
	return this.cssStyle.clip.bottom;
}

function clipTo(newTop,newRight,newBottom,newLeft){
//	if (this.Browser.ns) {
	   	this.cssStyle.clip.top=newTop;
	   	this.cssStyle.clip.right=newRight;
	   	this.cssStyle.clip.bottom=newBottom;
	   	this.cssStyle.clip.left=newLeft;
//	} else {
//	  	this.cssStyle.clip="rect("+newTop+","+newRight+","+newBottom+","+newLeft+")";
//	}
}

function moveClip() {
	return false;
}


function moveLeftClip(dx) {
//	if (this.cssStyle.clip.left + dx <= 0) {
//	  	this.clipTo(this.getTopClip(), this.getRightClip(), this.getBottomClip(), this.getLeftClip() + dx);
//	   	return false;
//	} else {
//	  	this.clipTo(this.getTopClip(), this.getRightClip(), this.getBottomClip(), this.getLeftClip() + dx);
	  	this.clipTo(0, 100, 100, this.getLeftClip() + dx);
	  	return true;
//	}
}

function moveRightClip(dx) {
}

function moveTopClip(dx) {
}

function moveBottomClip(dy) {
}
