/**
 * @author: Anatolij Rau
 * @copyright: Anatolij Rau
 * @access: 2009-10-06
 * @version: 1.0.1 Beta
 **/

/**
 * Information box / alert
 */
function iBoxClass() {
	this.multiBox = false; // more as one box allowed

	/** --> mouse position * */
	this.use_mouse_pos = true;

	this.placement_x = 'center'; // left | center | right
	this.placement_y = 'middle'; // top | middle | botom

	/** <-- mouse position * */

	this.default_title = 'Message';
	this.close_button = 'X';
	this.maxWidth = '600px';

	this.zIndex = 100;

	/** Requirements **/
	if (typeof dimensions == 'undefined') alert('iBoxClass::init() Demensions object required!');

	/** Private vars **/
	
	// ////////////////////// Functions ////////////////////////////////
	this.display = function(message, title, css_class) {
		if (!document.body) {
			window.setTimeout(function() {
				this.display(message, title, css_class);
			}, 100);
			return false;
		}

		if (!this.multiBox) {
			var to_close = document.getElementById('iBox');
			if (to_close)
				this.close(to_close);
		}

		// Create box
		var iBoxDiv = document.createElement("div");
		iBoxDiv.id = 'iBox';
		iBoxDiv.style.position = 'absolute';
		// iBoxDiv.style.backgroundColor = '#FFF';
		iBoxDiv.style.zIndex = this.zIndex;
		if (css_class)
			iBoxDiv.className = css_class;
		document.body.appendChild(iBoxDiv);

		// Create Title
		var iBoxTitleTable = document.createElement("table");
		iBoxTitleTable.id = 'iBoxTitleTable';
		iBoxTitleTable.cellpadding = 0;
		iBoxTitleTable.cellspacing = 0;
		if (css_class)
			iBoxTitleTable.className = css_class + 'TitleTable';
		iBoxDiv.appendChild(iBoxTitleTable);

		var iBoxTitleBody = document.createElement("tbody");
		iBoxTitleTable.appendChild(iBoxTitleBody);

		var iBoxTitleTr = document.createElement("tr");
		if (css_class)
			iBoxTitleTr.className = css_class + 'TitleTr';
		iBoxTitleBody.appendChild(iBoxTitleTr);

		// title
		var iBoxTitle = document.createElement("td");
		iBoxTitle.id = 'iBoxTitle';
		if (css_class)
			iBoxTitle.className = css_class + 'Title';
		iBoxTitle.innerHTML = title || this.default_title;

		iBoxTitleTr.appendChild(iBoxTitle);

		// close button
		var iBoxClose = document.createElement("td");
		iBoxClose.id = 'iBoxClose';
		if (css_class)
			iBoxClose.className = css_class + 'Close';
		iBoxClose.style.textAlign = 'right';
		iBoxClose.style.cursor = 'pointer';
		iBoxClose.innerHTML = this.close_button;
		iBoxClose.onclick = (function(obj, box) {
			return function() { obj.close(box); };
		})(this, iBoxDiv);
		iBoxTitleTr.appendChild(iBoxClose);

		var iBoxContentOuter = document.createElement("div");
		iBoxContentOuter.id = 'iBoxContentOuter';
		if (css_class)
			iBoxContentOuter.className = css_class + 'ContentOuter';
		iBoxDiv.appendChild(iBoxContentOuter);

		var iBoxContent = document.createElement("div");
		iBoxContent.id = 'iBoxContent';
		if (css_class) iBoxContent.className = css_class + 'Content';
		iBoxContent.innerHTML = message;
		iBoxContentOuter.appendChild(iBoxContent);
		
		if (parseInt(this.maxWidth) && iBoxDiv.offsetWidth > parseInt(this.maxWidth)) 
			iBoxDiv.style.width = this.maxWidth;

		this.setPosition(iBoxDiv, iBoxTitleTable);

		var box_size = dimensions.getSizeOf(iBoxDiv);
		// alert(box_size.width);
		//iBoxTitleTable.width = box_size.width + 'px';
		/**/
		this.zIndex++;
		return true;
	};

	this.setPosition = function(iBoxObject, iBoxTitleTableObj, loop) {
		if (!loop) loop = 0;
		/** --> Mouse position * */
		if (this.use_mouse_pos) {
			var mPos = dimensions.getMousePos();
			var size = dimensions.getSizeOf(iBoxObject);

			var x = 0;
			// set x position
			switch (this.placement_x) {
			case 'left':
				x = Math.round(mPos.x - size.width);
				break;
			case 'center':
				x = Math.round(mPos.x - size.width / 2);
				break;
			case 'right':
			default:
				x = mPos.x;
				break;
			}
			var y = 0;
			
			
			// set y position
			switch (this.placement_y) {
			case 'top':
				y = Math.round(mPos.y - size.height);
				break;
			case 'middle':
				y = Math.round(mPos.y - size.height / 2);
				break;
			case 'bottom':
			default:
				y = mPos.y;
				break;
			}
			
			iBoxObject.style.left = (x > 0 ? x : 0) + 'px';
			iBoxObject.style.top = (y > 0 ? y : 0) + 'px';

		}
		/** <-- Mouse position * */
		/** --> Default Position * */
		/***********************************************************************
		 * else {
		 *  } /** <-- Default Position
		 **********************************************************************/
		
		/** Position loop (Grafic loading...) **/
		if (loop <= 2) {
			loop++;
			iBoxTitleTableObj.width = iBoxObject.offsetWidth-2;
			var tmp = this;
			window.setTimeout(function() { tmp.setPosition(iBoxObject, iBoxTitleTableObj, loop);}, 100);
		}
	};

	/**
	 * Method: close
	 */
	this.close = function(iBoxObject) {
		if (typeof iBoxObject != 'object')
			iBoxObject = document.getElementById('iBox');
		if (typeof iBoxObject == 'object') {
			if (iBoxObject.id == 'iBox') {
				document.body.removeChild(iBoxObject);
			} else if (iBoxObject.parentNode) {
				// alert(easy_popup.id);
				this.close(iBoxObject.parentNode);
				return false;
			}
		}
		return true;
	};
}

var iBoxObj = new iBoxClass();