kbox = Class.create();
kbox.prototype = {
	initialize: function(content){
		this.content = content;
		this.options = Object.extend({
			title: 'Title',
			width: 600,
      		height: 425,
			type: 'window',
			func: null
		}, arguments[1] || {});
		
		this.window();
	},
	window: function(){
		//this.div_overlay = Builder.node('div', {id:'div_overlay'});
		//document.body.appendChild(this.div_overlay);
		//this.div_overlay.onclick = this.destroy;
		
		if (this.options.type == 'window'){
			this.div_wrapper = Builder.node('div', {id:'div_wrapper',style:'height:'+ this.options.height +'px;top:'+ this.get_pos() +'px;width:'+ this.options.width +'px;margin:0px 0px 0px -'+ (this.options.width/2) +'px'})
			document.body.appendChild(this.div_wrapper);
			//this.div_overlay.div_wrapper = this.div_wrapper; this.div_overlay.div_overlay = this.div_overlay;
			
			this.div_header = Builder.node('div', {id:'div_header',style:'width:'+ (this.options.width - 11) +'px;'});
			this.div_close = Builder.node('div', {id:'div_close',style:'float:right;cursor:pointer;'},'lukk vindu');
			this.div_content = Builder.node('div', {id:'div_content',style:'height:'+(this.options.height - 40)+'px;width:'+ (this.options.width - 10) +'px;'});
			this.div_header.appendChild(this.div_close);
			this.div_wrapper.appendChild(this.div_header);
			this.div_wrapper.appendChild(this.div_content);
			this.div_content.innerHTML = this.getfile(this.content);
			this.div_close.onmousedown = this.destroy;
			this.div_close.div_wrapper = this.div_wrapper;
			//this.div_close.div_overlay = this.div_overlay;
			new Draggable(this.div_wrapper,{handle:'div_header',starteffect:false,endeffect:false});
		}
		else if (this.options.type == 'sidebar'){
			this.clear();
			this.div_wrapper = Builder.node('div', {id:'div_wrapper',style:'height:'+ this.options.height +'px;top:15px;left:0px;width:282px;'})
			$('mal_placeholder').appendChild(this.div_wrapper);
			//this.div_overlay.div_wrapper = this.div_wrapper; this.div_overlay.div_overlay = this.div_overlay;
			
			this.div_header = Builder.node('div', {id:'div_header',style:'width:271px;'},[
					Builder.node('div', {id:'div_title',style:'float:left;'},[ Builder.node('span',this.options.title) ])
				]);
			//this.div_close = Builder.node('div', {id:'div_close',style:'float:right;cursor:pointer;'},[ Builder.node('img',{ id:'close_img', src:'/script/kbox/images/kbox_close.jpg' } ) ]);
			this.div_content = Builder.node('div', {id:'div_content',style:'height:'+(this.options.height - 30)+'px;width:262px;'});
			//this.div_header.appendChild(this.div_close);
			this.div_wrapper.appendChild(this.div_header);
			this.div_wrapper.appendChild(this.div_content);
			this.div_content.innerHTML = this.getfile(this.content);
			//this.div_close.onclick = this.destroy;
			//this.div_close.div_wrapper = this.div_wrapper;
			//this.div_close.div_overlay = this.div_overlay;
		}
		else{
			this.div_wrapper = Builder.node('div', {id:'div_wrapper',style:'height:'+ (this.options.height + 20) +'px;top:'+ this.get_pos() +'px;width:'+ this.options.width +'px;margin:0px 0px 0px -'+ (this.options.width/2) +'px'})
			document.body.appendChild(this.div_wrapper);
			//this.div_overlay.div_wrapper = this.div_wrapper; 
			//this.div_overlay.div_overlay = this.div_overlay;
			this.div_header = Builder.node('div', {id:'div_header',style:'width:289px;'},[
					Builder.node('div', {id:'div_title',style:'float:left;'},[ Builder.node('span',this.options.title) ])
				]);
			this.div_close = Builder.node('div', {id:'div_close',style:'float:right;cursor:pointer;'},[ Builder.node('img',{ id:'close_img', src:'/script/kbox/images/kbox_close.jpg' } ) ]);
			this.div_header.appendChild(this.div_close);
			this.div_wrapper.appendChild(this.div_header);	
				
			this.div_content_confirm = Builder.node('div', {id:'div_content_confirm'});
			this.div_content_confirm_text = Builder.node('div', {id:'div_content_confirm_text'},this.content/*[Builder.node('img', {src:'/images/alert.gif',alt:'',style:'vertical-align:middle'}),this.content]*/);
			this.div_content_confirm_yes = Builder.node('input', {id:'div_content_confirm_yes',value:'Ok',type:'button'});
			this.div_content_confirm.appendChild(this.div_content_confirm_text);
			this.div_content_confirm.appendChild(this.div_content_confirm_yes);
			
			switch(this.options.type)
			{
				case 'confirm':
					this.div_content_confirm_no = Builder.node('input', {id:'div_content_confirm_no',value:'Avbryt',type:'button'});
					this.div_content_confirm.appendChild(this.div_content_confirm_no);
					this.div_content_confirm_no.onmousedown = this.destroy;
					this.div_content_confirm_yes.temp = this.options.func;
					this.div_content_confirm_no.div_wrapper = this.div_wrapper;
					//this.div_content_confirm_no.div_overlay = this.div_overlay;
					break;
				case 'alert':
					(this.options.func != null)
					{
				 		this.div_content_confirm_yes.temp = this.options.func;
					}
					break;
			}
			
			this.div_wrapper.appendChild(this.div_content_confirm);
			this.div_content_confirm_yes.onclick = this.destroy;
			this.div_content_confirm_yes.div_wrapper = this.div_wrapper;
			//this.div_content_confirm_yes.div_overlay = this.div_overlay;
		}
	},
	destroy:function(){
		if(this.temp) this.temp();;
		Element.remove(this.div_wrapper);
	},
	clear:function(){
		if($('div_wrapper')) $('div_wrapper').remove();
	},
	getfile:function(url){
		var xmlhttp=false;
		
		try{     
		xmlhttp = new XMLHttpRequest();
		} 
		catch(e){
			try{       
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch(e){
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			}  
		}   
		
		xmlhttp.open("GET", url, false);
		xmlhttp.send(null);
		return xmlhttp.responseText;
	},
	get_pos:function(){
		var getPos = document.viewport.getScrollOffsets().toString();
		var getHeight = document.viewport.getHeight();
		getPos = getPos.split(",");
		curPos = parseInt(getPos[1]);
		newPos = ((getHeight/2) - (this.options.height/2)) + curPos;
		if (newPos < 0) newPos = 30;
		return newPos;
	}
}
