var closeImageUrl = "hadbox/images/close.gif";


/**
 * @author: David Hauger
 * 
 */

// start HadBox
//var hadBoxCounter = 0;
//var HadBox = Class.create();

HadBox = function(title){
	var objBody = document.getElementsByTagName("body").item(0);
	var tmpText;
	var oThis = this; // for function calls
	var zIndex = 8884;

	if (HadBox.hadBoxCounter == undefined) HadBox.hadBoxCounter = 0;
	this.__id = HadBox.hadBoxCounter++;

	var objHadBox = document.createElement("div");
	objHadBox.setAttribute('id','hadbox' + this.__id);
	objHadBox.className = 'hadbox';
	//objHadBox.style.display = 'none';
	objHadBox.style.position = "absolute";
	objHadBox.style.zIndex = zIndex++;
	//objHadBox.innerHTML = "bbbbb<br/>asda";
	objHadBox.style.padding = "2px";
	objHadBox.style.backgroundColor = "#333333";
	objBody.appendChild(objHadBox);

	var objHadBoxResize = document.createElement("div");
	objHadBoxResize.style.position = "absolute";
	objHadBoxResize.style.top = 0;
	objHadBoxResize.style.left = 0;
	objHadBoxResize.style.backgroundColor = "#333333";
	objHadBoxResize.style.cursor = "n-resize";
	objHadBox.style.zIndex = zIndex++;
	objHadBoxResize.onmouseover = objHadBoxResize.onmousemove = function (e) {
		var mouseX = oThis.getMouseX(e);
		var mouseY = oThis.getMouseY(e);
		var moveX = oThis.getBorderX(mouseX);
		var moveY = oThis.getBorderY(mouseY);
		if (moveX * moveY == 1) {
			this.style.cursor = "nw-resize";
		} else if (moveX * moveY == -1) {
			this.style.cursor = "ne-resize";
		} else if (moveY == 0) {
			this.style.cursor = "e-resize";
		} else if (moveX == 0) {
			this.style.cursor = "n-resize";
		} else {
			this.style.cursor = "auto";
		}
		return false;
	};
	objHadBoxResize.onmousedown = function (e) {
		oThis.resizeStart(e);
		return false;
	};
	objHadBox.appendChild(objHadBoxResize);
	this.__hadBoxResize = objHadBoxResize;

	var objHadBoxInner = document.createElement("div");
	objHadBoxInner.style.position = "absolute";
	objHadBoxInner.style.zIndex = zIndex++;
	objHadBoxInner.style.top = "2px";
	objHadBoxInner.style.left = "2px";
	objHadBoxInner.className = 'hadboxInner';
	objHadBoxInner.style.cursor = "auto";
	objHadBox.appendChild(objHadBoxInner);
	this.__hadBoxInner = objHadBoxInner;
	//alert(objHadBox.getHeight());
	//alert(objHadBoxInner.getHeight());
	/*
	objHadBoxInner.style.height = objHadBoxInner.offsetHeight - 12;
	alert(objHadBoxInner.offsetHeight);
	8884
	*/
	
	var objHadBoxTop = document.createElement("div");
	objHadBoxTop.style.width = "100%";
	objHadBoxTop.style.backgroundColor = "#CCCCCC";
	objHadBoxTop.style.border = "1px outset #000000";
	objHadBoxTop.style.borderBottom = "0px none #666666";
	objHadBoxTop.className = 'hadbox_title';
	objHadBoxTop.style.cursor = "move";
	
	objHadBoxTop.onmousedown = function(ereignis){
		oThis.dragStart(ereignis);
	};
	this.__hadBoxTop = objHadBoxTop;
	
	objHadBoxInner.appendChild(objHadBoxTop);
	
	var objHadBoxTitle = document.createElement("div");
	objHadBoxTitle.style.padding = 3;
	objHadBoxTop.appendChild(objHadBoxTitle);

	var objHadBoxCloseImage = document.createElement("img");
	objHadBoxCloseImage.setAttribute('border', 0);
	objHadBoxCloseImage.setAttribute('src', closeImageUrl);
	objHadBoxCloseImage.setAttribute('align', "right");
	objHadBoxCloseImage.style.cursor = "pointer";
	objHadBoxCloseImage.onclick = function() { oThis.hide(); return false; }
	objHadBoxTitle.appendChild(objHadBoxCloseImage);

	tmpText = document.createTextNode(title != null ? title : "Had");
	objHadBoxTitle.appendChild(tmpText);

	
	// create content
	var objHadBoxContent = document.createElement("div");
	objHadBoxContent.style.overflow = "scroll";
	objHadBoxContent.style.border = "1px outset #000000";
	objHadBoxContent.className = 'hadbox_content';
	objHadBoxContent.setAttribute('id','hadboxContent'+this.__id);
	objHadBoxContent.style.backgroundColor = "#FFFFFF";
	objHadBoxInner.appendChild(objHadBoxContent);
	
	this.__contentDiv = objHadBoxContent;				
	this.__hadBox = objHadBox;
	this.clear();
	
	this.setX(100);
	this.setY(50);
	this.setWidth(400);
	this.setHeight(300);
}

HadBox.prototype.setX = function(x){
	this.__x = x;
	this.__hadBox.style.left = x + "px";
}

HadBox.prototype.setY = function(y){
	this.__y = y;
	this.__hadBox.style.top = y + "px";
}

HadBox.prototype.setWidth = function(width){
	if (width < 80) return false;
	this.__width = width;
	this.__hadBox.style.width = width + "px";
	this.__hadBoxResize.style.width = (width + 6) + "px";
	this.__hadBoxInner.style.width = width + "px";
	this.__contentDiv.style.width = width + "px";
	return true;
}

HadBox.prototype.setHeight = function(height){
	if (height < 50) return false;
	var titleBarHeight = 20;
	this.__height = height;
	this.__hadBoxTop.style.height = titleBarHeight + "px";
	this.__hadBoxResize.style.height = (height + 6) + "px";
	this.__hadBoxInner.style.height = (height - titleBarHeight) + "px";
	this.__contentDiv.style.height = (height - titleBarHeight) + "px";
	return true;
}

HadBox.prototype.show = function(){
	document.getElementById('hadbox' + this.__id).style.display = '';
}

HadBox.prototype.hide = function(){
	document.getElementById('hadbox' + this.__id).style.display = 'none';
}

HadBox.prototype.getMouseX = function(e){
	return document.all ? window.event.clientX : e.pageX;
}

HadBox.prototype.getMouseY = function(e){
	return document.all ? window.event.clientY : e.pageY;
}

HadBox.prototype.getBorderX = function(mouseX){
	mouseX -= this.__x;
	if (Math.abs(mouseX) < 10) {
		return -1;
	} else if (Math.abs(mouseX - this.__width) < 10) {
		return 1;
	}
	return 0;
}

HadBox.prototype.getBorderY = function(mouseY){
	mouseY -= this.__y;
	if (Math.abs(mouseY) < 10) {
		return -1;
	} else if (Math.abs(mouseY - this.__height) < 10) {
		return 1;
	}
	return 0;
}
//}

/************************************************
 * administrate content
 ***********************************************/

HadBox.prototype.addText = function(newText){
	var tmpText = document.createTextNode(newText);
	this.__contentDiv.appendChild(tmpText);
	var tmpNode = document.createElement("br");
	this.__contentDiv.appendChild(tmpNode);
}

HadBox.prototype.setText = function(newText){
	this.clear();
	this.addText(newText);
}

HadBox.prototype.setHTML = function(newText){
	this.__contentDiv.innerHTML = newText;
}

HadBox.prototype.clear = function(){
	this.__contentDiv.innerHTML = "";
}

HadBox.prototype.addHTML = function(newText){
	var contentHTML = this.__contentDiv.innerHTML;
	contentHTML += newText;
	this.__contentDiv.innerHTML = contentHTML;
}

HadBox.prototype.addStyledText = function(newText, cssClass, tagName){
	var newTag;
	if (tagName != undefined) {
		newTag = document.createElement(tagName);
	}
	else 
		if (cssClass != undefined) {
			newTag = document.createElement("div");
		}
	if (cssClass != undefined && cssClass != "") {
		newTag.className = cssClass;
	}
	var tmpText = document.createTextNode(newText);
	if (newTag == undefined) {
		this.__contentDiv.appendChild(tmpText);
	}
	else {
		newTag.appendChild(tmpText);
		this.__contentDiv.appendChild(newTag);
	}
}

/************************************************
 * drag
 ***********************************************/

HadBox.prototype.dragStart = function(e){
	this._offsetX = this.getMouseX(e);
	this._offsetY = this.getMouseY(e);
	var oThis = this;
	document.onmousemove = function(ereignis){
		oThis.drag(ereignis);
		return false;
	};
	document.onmouseup = function(ereignis){
		oThis.dragStop();
	};
	this._posX = this.__hadBox.offsetLeft;
	this._posY = this.__hadBox.offsetTop;
}

HadBox.prototype.dragStop = function(){
	document.onmousemove = null;//undefined;
	document.onmouseup = null;//undefined;
}

HadBox.prototype.drag = function(e){
	var posx = this.getMouseX(e);
	var posy = this.getMouseY(e);
	this.setX(this._posX + posx - this._offsetX);
	this.setY(this._posY + posy - this._offsetY);
}

/************************************************
 * resize
 ***********************************************/
HadBox.prototype.resizeStart = function(e){
	var mouseX = this.getMouseX(e);
	var mouseY = this.getMouseY(e);
	var resizeX = this.getBorderX(mouseX);
	var resizeY = this.getBorderY(mouseY);
	var oThis = this;
	document.onmousemove = function(ereignis){
		oThis.resize(ereignis, resizeX, resizeY);
		return false;
	};
	document.onmouseup = function(ereignis){
		oThis.resizeStop();
	};
}

HadBox.prototype.resizeStop = function(){
	document.onmousemove = null;//undefined;
	document.onmouseup = null;//undefined;
}

HadBox.prototype.resize = function(e, resizeX, resizeY){
	var posx = this.getMouseX(e);
	var posy = this.getMouseY(e);
	if (resizeX == 1) {
		this.setWidth(posx - this.__x - 5);
	} else if (resizeX == -1) {
		if (this.setWidth(this.__x + this.__width - posx))
			this.setX(posx);
	}
	if (resizeY == 1) {
		this.setHeight(posy - this.__y - 5);
	} else if (resizeY == -1) {
		if (this.setHeight(this.__y + this.__height - posy))
			this.setY(posy);
	}
}

