/**
 * @author: Anatolij Rau, MiB
 * @copyright: Anatolij Rau & RAGBIT GmbH
 * @access: 2010-10-01
 * @version: 1.2.1
 **/

/**
 * Information box / alert
 */
function iBoxClass() {
	/** --> default settings **/
	this.default_title = 'Message';
	this.close_button = 'X';
	this.maxWidth = '955px';

	this.zIndex = 750;
	this.multiBox = false; // more as one box allowed
	/** --> default settings **/
	
	/** --> mouse position * */
	this.use_mouse_pos = false;
	this.fixpos_x = false;
	this.fixpos_y = 50;

	this.placement_x = 'center'; // left | center | right
	this.placement_y = 'middle'; // top | middle | botom
	/** <-- mouse position * */

	/** --> cover body **/
	this.use_cover = true;
	this.cover_color = '#ffffff';// HEX or RGB
	this.cover_transparenz = 70;// in %
	this.multiCoverEnabled = false;
	/** <-- cover body **/
	
	
	/** --> system (running system!) **/
	this.selectBoxes = new Array();
	this.selectBoxesDummy = new Array();

	this.scriptDir = null;
	var nodes = document.getElementsByTagName("SCRIPT"), script_dir = null;
	for (var i = 0; i < nodes.length; i++) 
		if (nodes[i].src.indexOf("iBox") != -1) this.scriptDir = nodes[i].src.replace(/\\/g,'/').replace(/\/[^\/]*\/?$/, '')+'/';


	/** Requirements **/
	if (typeof dimensions == 'undefined') {
		if (this.scriptDir) {
			script_element = document.createElement("script");
			script_element.type = "text/javascript";
			script_element.src = this.scriptDir+'dimensions.class.js';
			head = document.getElementsByTagName("head")[0];
			head.appendChild(script_element);
		}
  }

	// Move
	this.mouseStartPos = null;
	this.boxStartPos = null;
	this.moveTimeout = null;
	/** <-- system (running system!) **/
	
	//////////////////////// Functions ///////////////////////////
	/**
	 * Alias of display function
	 * @param {String} message
	 * @param {String} title
	 * @param {String} css_class
	 * @param {String|Function} on_close
	 */
	this.open = function(message, title, css_class, on_close) {
		return this.display(message, title, css_class, on_close);
	};

	/**
	 * Display internal popup
	 * @param {String} message
	 * @param {String} title
	 * @param {String} css_class
	 * @param {String|Function} on_close
	 */
	this.display = function(message, title, css_class, on_close) {
		var thmlTag = document.getElementsByTagName("html")[0];
		if (!thmlTag || (thmlTag.innerHTML.indexOf('</body>') == -1 && !thmlTag.innerHTML.indexOf('</BODY>') == -1)) {
			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);
		}

		/** IE 6 Bug **/
		//this.ie6Fix();

		/** Cover site **/
		var cover_div = this.cover(css_class);

		// 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;
		iBoxTitle.style.cursor = 'move';
		iBoxTitle.style.MozUserSelect = 'none';
		iBoxTitle.style.KhtmlUserSelect = 'none';
		iBoxTitle.style.userSelect = 'none';
		iBoxTitle.unselectable="on";
		// Bewegung starten
		iBoxTitle.onmousedown = (function(obj, box) {
			return function() { 
			obj.move(iBoxDiv,'start');
			};
		})(this, iBoxDiv);
		// Bewegung stoppen
		iBoxTitle.onmouseup = document.body.onmouseup = (function(obj, box) {
			return function() { 
			obj.move(iBoxDiv,'stop');
			};
		})(this, iBoxDiv);
		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);

		if (cover_div) cover_div.onclick = (function(obj, box) {
			return function() {obj.close(box);};
		})(this, iBoxDiv);


		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++;

		// onClose action
		iBoxDiv.onclose = on_close;

		return iBoxDiv;
	};
	
	/**
	 * Set box position
	 * @param {Object} iBoxObject
	 * @param {Object} iBoxTitleTableObj
	 * @param {Integer} loop
	 */
	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 {
			var win_size = dimensions.getWindowSize();
			var posXY = dimensions.getScrollPos();
			var size = dimensions.getSizeOf(iBoxObject);

			//debugObj.print_r(size.width+'/('+win_size.width+'/100))/2');
			var x = this.fixpos_x ? this.fixpos_x + posXY.x : Math.round(posXY.x+((win_size.width-size.width)/2)); // in px
			var y = this.fixpos_y ? this.fixpos_y + posXY.y : Math.round(posXY.y+((win_size.height-size.height)/2)); // in px
						
			iBoxObject.style.left = (x > 0 ? x : 0) + 'px';
			iBoxObject.style.top = (y > 0 ? y : 0) + 'px';
		} 
		/** <-- Default Position **/
		 
		iBoxTitleTableObj.width = iBoxObject.offsetWidth-2;
		
		/** 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);
//		}
	};

	/**
	 * Create div to cover site BODY
	 * @param {Object} css_class
	 * @param {Boolean} forceCover (alternative cat be used with iBoxObj.multiCoverEnabled = true)
	 * @return {Object}
	 */
	this.cover = function(css_class, forceCover) {
		if (!this.use_cover) return null;
		if (this.multiCoverEnabled) forceCover = this.multiCoverEnabled;
		else {
			if ((forceCover == 'undefined' || forceCover === null)) forceCover = false;
			if (!forceCover) if (this.multiBox && document.getElementById('iBoxCover')) return null;
		}

		var bodySize = dimensions.getSizeOf(document.body);
		var winSize = dimensions.getWindowSize();
				
		// get optimal height
		var ch1 = bodySize.height;
		var ch2 = winSize.height;
		var coverHeight = ch1 > ch2 ? ch1 : ch2;
		
		var cw1 = bodySize.width;
		var cw2 = winSize.width;
		var coverWidth = cw1 > cw2 ? cw1 : cw2;
		
		// Create cover object
		var iBoxCover = document.createElement("div");
		iBoxCover.id = 'iBoxCover';
		iBoxCover.style.position = 'absolute';
		iBoxCover.style.left = '0px';
		iBoxCover.style.top = '0px';
		iBoxCover.style.zIndex = this.zIndex-1;
		iBoxCover.style.width = coverWidth+'px';
		iBoxCover.style.height = coverHeight+'px';
		iBoxCover.style.backgroundColor = this.cover_color;
		iBoxCover.style.opacity = (this.cover_transparenz/100);
		iBoxCover.style.filter = 'alpha(opacity='+this.cover_transparenz+');';
		//iBoxCover.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(Opacity='+this.cover_transparenz+')';
		//progid:DXImageTransform.Microsoft.Light(attribute=value1, attribute2=value2)

		if (css_class) css_class.className = css_class;
		document.body.appendChild(iBoxCover);
		return iBoxCover;
	};

	/**
	 * Fehler im IE 6 umgehen
	 **/
	this.ie6Fix = function() {
		if (navigator.userAgent.indexOf('MSIE 6') != -1) {
			var selects = document.getElementsByTagName('select');
			for (var i=0; i < selects.length; i++) {
				if (selects[i].style.display != 'none') {
					this.selectBoxes.push(selects[i]);
					selects[i].style.display = 'none';
					var dummy = document.createElement("div");
					dummy.style.width = selects[i].offsetWidth+'px';
					dummy.style.width = selects[i].offsetHeight+'px';
					dummy.id = selects[i].name+'_dummy_';
					selects[i].parentNode.appendChild(dummy);
					this.selectBoxesDummy.push(dummy);
				}
			}
		}
	};

	/**
	 * Fehler im IE 6 Rückgänig machen
	 **/
	this.ie6Unfix = function() {
		for (var i = 0; i<this.selectBoxes.length; i++) {
			this.selectBoxes[i].style.display = '';
			this.selectBoxesDummy[i].parentNode.removeChild(this.selectBoxesDummy[i]);
		}
	};


	/**
	 * Close iBox
	 * @param {Object} iBoxObject
	 */
	this.close = function(iBoxObject) {
		if (typeof iBoxObject != 'object')
			iBoxObject = document.getElementById('iBox');
			
		if (iBoxObject && typeof iBoxObject == 'object') {
			if (iBoxObject && iBoxObject.id && iBoxObject.id == 'iBox') {
				if (iBoxObject.previousSibling.id == 'iBoxCover') 
					document.body.removeChild(iBoxObject.previousSibling);
				on_close_action = iBoxObject.onclose;
				document.body.removeChild(iBoxObject);
				this.ie6Unfix();
				
				if (typeof on_close_action == 'string') {
					if (on_close_action.indexOf("(") == -1) on_close_action += "()";
					eval(on_close_action);
				}
				else if (typeof on_close_action == 'function')
					on_close_action();
				else if (typeof on_close_action == 'object' &&
						typeof on_close_action.onclose == 'function')
					on_close_action.onClose();
			} else if (iBoxObject.parentNode) {
				// alert(easy_popup.id);
				this.close(iBoxObject.parentNode);
				return false;
			}
		}
		return true;
	};

	/**
	 * Start|Stop move action
	 * @version 0.1.0
	 * @param {Object} iBoxObject
	 * @param {String} action (start|stop)
	 */
	this.move = function(iBoxObject, action) {
		if (!action) action = stop;
		switch(action) {
			case 'start':
				//iBoxObject.focus();
				if (this.mouseStartPos == null) {
					window.clearTimeout(this.moveTimeout);
					this.mouseStartPos = new Object();
					//debugObj.print_r(dimensions.getMousePos());
					this.mouseStartPos.x = dimensions.getMousePos().x;
					this.mouseStartPos.y = dimensions.getMousePos().y;
					this.boxStartPos = new Object();
					this.boxStartPos.x = parseInt(iBoxObject.style.left);
					this.boxStartPos.y = parseInt(iBoxObject.style.top);
				}

				var newMousePos = dimensions.getMousePos();
				// move x
				if (newMousePos.x != this.mouseStartPos.x && typeof iBoxObject == 'object') {
					//debugObj.print_r(this.mouseStartPos);
					iBoxObject.style.left = (this.boxStartPos.x+newMousePos.x-this.mouseStartPos.x)+'px';
				}

				// move y
				if (newMousePos.y != this.mouseStartPos.x) {
					iBoxObject.style.top = (this.boxStartPos.y+(newMousePos.y-this.mouseStartPos.y))+'px';
				}
				
				// restart move function
				this.moveTimeout = window.setTimeout((function(obj, iBoxDiv) {
					return function() {
					obj.move(iBoxDiv,'start');
					};
				})(this, iBoxObject), 1);
			break;
			case 'stop':
				window.clearTimeout(this.moveTimeout);
				this.mouseStartPos = null;
				this.boxStartPos = null;
			break;
		}
		return false;
	};
}


var iBoxObj = new iBoxClass();
