var boxie = {
		elements : {},
		hasElement : {},
		isOpen : false,
		
		/**
		 * openBoxie
		 * Open the box
		 * 
		 * @param obj|str htmlNodes The HTML nodes to put into the box
		 */
		openBoxie : function(htmlNodes, div, hidden){
	        div = div == undefined || div == true? true : false;
	        hidden = hidden == undefined || hidden == true? true : false;
		    if (this.isOpen){
		        this.closeBoxie();
		    }
		    this.div = div;
			this.makeBox(htmlNodes);
			this.setElements();
			if (div){
			this.elements.overlay.style.display = 'block';
			}
			
			this.elements.boxie.style.display = hidden? 'none' : 'block';
			/*this.elements.img_x.onclick = function (){
				boxie.closeBoxie();
				return false;
			};*/
			onkeypress = function (e){
				if (e.keyCode == 27){
					boxie.closeBoxie();
				}
			}
			
			this.isOpen = true;
		},
		
		/**
		 * makeBox
		 * Make the nodes of the box and put the html in it
		 * 
		 * @param str|obj htmlNodes The nodes to put into the box
		 */
		makeBox : function (htmlNodes){
			this.elements.body = document.getElementsByTagName('body');
			this.elements.body = this.elements.body[0];
			if (this.div) {
			  var div = document.createElement('div');
			   div.id = 'overlay';
			   this.elements.body.appendChild(div);
			   this.hasElement.overlay = true;
			}
			var div = document.createElement('div');
			div.id = 'boxie';
			/*var a = document.createElement('a');
			a.href = '#';
			a.id = 'img_x';
			a.title = 'x';
			var img = document.createElement('img');
			img.src = 'images/x.gif';
			img.set('alt', 'x');
			a.appendChild(img);
			div.appendChild(a);*/
			if (htmlNodes instanceof Array){
				for (var i = 0; i < htmlNodes.length; i++){
					div.appendChild(htmlNodes[i]);
				}
				this.elements.body.appendChild(div);
			} else if (typeof(htmlNodes) == 'string'){
				this.elements.body.appendChild(div);
				document.getElementById('boxie').innerHTML += htmlNodes;
			} else {
				div.appendChild(htmlNodes);
				this.elements.body.appendChild(div);
			}
		},
		
		/**
		 * addToBox
		 * Add some HTML nodes to the box or another element on the page
		 * 
		 * @param obj htmlNodes The nodes to add
		 * @param str id Optional id of an element where the htmlNodes must put in. If this is not filled
		 * 				in, the html nodes will be put into the main box element
		 */
		addToBox : function (htmlNodes, id){
			var element = typeof(id) == 'string'? document.getElementById(id) : this.elements.boxie;
			if (htmlNodes instanceof Array){
				for (i = 0; i < htmlNodes.length; i++){
					element.appendChild(htmlNodes[i]);
				}
			} else {
				element.appendChild(htmlNodes);
			}
		},
		
		/**
		 * removeFromBox
		 * Remove some HTML nodes from the box or another element on the page
		 * 
		 * @param obj htmlNodes The nodes to remove
		 * @param str id Optional id of an element where the htmlNodes must extracted from. If this is not filled
		 * 				in, the html nodes will be extracted from the main box element
		 */
		removeFromBox : function (htmlNodes, id){
			var element = typeof(id) == 'string'? document.getElementById(id) : this.elements.boxie;
			if (htmlNodes instanceof Array){
				for (i = 0; i < htmlNodes.length; i++){
					element.removeChild(htmlNodes[i]);
				}
			} else {
				element.removeChild(htmlNodes);
			}
		},
		
		/**
		 * setElements
		 * Set some HTML elements of the box
		 */
		setElements : function() {
			this.elements.overlay = document.getElementById('overlay');
			this.elements.boxie = document.getElementById('boxie');
			//this.elements.img_x = document.getElementById('img_x');
		},
		
		hideBoxie : function (){
		    this.elements.boxie.style.display = 'none';
		},
		
		showBoxie : function (){
		    this.elements.boxie.style.display = 'block';
		},
		
		/**
		 * closeBoxie
		 * Close the box element
		 */
		closeBoxie : function (){
		    if (this.div){
		        this.elements.overlay.style.display = 'none';
			    if(this.hasElement.overlay){
			        this.elements.body.removeChild(this.elements.overlay);
			        this.hasElement.overlay = false;
			    }
		    }
			this.elements.boxie.style.display = 'none';
			
			if(document.getElementById('boxie')) {
			    this.elements.body.removeChild(this.elements.boxie);
			}
			
			//$('container').set('style','opacity:1')
			this.isOpen = false;
		}
	};

