
var Sdialog = 
{
	/*variables*/
	instances: new Array(),
	visible:0,
	dBox:undefined,
	blackbox:undefined,
	body_overflow:'visible',
	box_pos:'absolute',
	do_open:true,
	
	/*The box class*/
	box: function(content)
	{
	/*construct*/
		this.emptyOnClosure=false;
		this.element=document.createElement('div');
		this.element.className='dialogbox';
		this.index=Sdialog.instances.length;
		this.closeButton="<div class='dbCloseButton' onClick='Sdialog.close();return false;'></div>";
		this.element.innerHTML=this.closeButton + "<div style='padding:20px;'>" + content + "</div>";
		this.element.id='SdialogBox' + this.index;
		this.element.style.display="none";
		this.visible=false;
		
		this.container=document.createElement('div');
		this.container.appendChild(this.element);
		document.body.appendChild(this.container);
		Sdialog.instances.push(this);
		
		if(Sdialog.blackbox==undefined)
		{//If this is the first dialogbox created, initialize the blackbox too
			Sdialog.blackbox=document.createElement('div');
			Sdialog.blackbox.className="wholePageBlackBox";
			Sdialog.blackbox.style.display="none";
			document.body.appendChild(Sdialog.blackbox);
		}//end of if
		
		/**METHODS**/
		this.close = function()
		{
			this.onClose();
			this.element.style.display="none";
			Sdialog.visible--;
			if(Sdialog.visible==0)
			{
				Sdialog.blackbox.style.display="none";
				document.body.style.overflow=Sdialog.body_overflow;
			}
			if(this.emptyOnClosure) this.element.innerHTML="";
			this.visible=false;
			if(typeof pleppo!="undefined")
				document.body.onmousedown=grabNmove.start;
			
		}//end of close
		/*******************************************************************************/
		this.display= function(where)
		{
		/*where can be set to center (center on screen) or to divid
		 When set to a div-id  the box will be appended to the div if found
		 returns false on error
		 */
		if(Sdialog.do_open==false) return false;
			addEvent(document,'Resize',this.fixWH);
			switch(where)
			{
				case "center":
					this.element.style.display="block";
					this.element.style.position=Sdialog.box_pos;
					//this.element.style.opacity='0';
					addEvent(this.element,'load',this.fixWH);
					if(typeof pleppo!="undefined")
					{
						this.element.onmouseover=function(){grabNmove.noScroll(this);}
					}
					if(typeof document.body.style.minWidth == "undefined")//The position fixed proporty won't render right in ie6
					{
						this.element.style.position='absolute';
						this.element.style.width='700px';//Ie6 doesnt't understand min-width :/
					}
					
						/*Makes the background faded:*/
						Sdialog.blackbox.style.width="100%";
						Sdialog.blackbox.style.height="100%";
						Sdialog.blackbox.style.display="block";
						if(typeof pleppo !="undefined")
							Sdialog.blackbox.onmouseover=function(){grabNmove.noScroll(this);}
						
						
						this.fixWH();//Center on screen
				
					if(!this.visible) Sdialog.visible++;
					this.visible=true;
				break;
				default:
				//IF where isn't set to center, check if there are any elements with the id set in where, else quit
					var element=getElement(where);
					if(typeof element == "undefined") return false;
					element.appendChild(this.element);
				break;
				
			}
				
		}//end of display
	
		this.content=function(html)
		{
			this.element.innerHTML=this.closeButton + "<div style='padding:20px;'>" + html + "</div>";
			
		}//end of content
		/***************************************/
		this.destroy = function()
		{
			//FIXME
		}
		/************************************/
		this.fixWH= function(minW,minH)//centers the box on the screen
		{
			
			try
			{//had problems with ie-throwing errors sometimes
				if(typeof minW!= "undefined")
				{
					if(this.element.offsetWidth>=getCanvasWidth()-30) this.element.style.width=getCanvasWidth()-30 + "px";
					if(this.element.offsetHeight>=getCanvasHeight()-30) this.element.style.height=getCanvasHeight()-30 + "px";
					if(this.element.offsetWidth<minW) this.element.style.width=minW + 'px';
					if(this.element.offsetHeight<minH) this.element.style.height=minH  + 'px';
				}
				this.element.style.left="50%";
				this.element.style.top="50%";
				this.element.style.marginLeft=this.element.offsetWidth/-2 + getScrollWidth() +  "px";
				this.element.style.marginTop=this.element.offsetHeight/-2 + getScrollHeight() +  "px";
			}
			catch(e){}
			
			
			
		
		}//end of fixWH()
		/********************/
		this.width=function()
		{
			return this.element.offsetWidth-40;
			
		}
		/***********************/
		this.height=function()
		{
			return this.element.offsetHeight-40;
		}
		/*********************/
		this.onClose = function(){/**DUMMY FUNCTION***/}
	},
	/********************************/
	
	/****************************************************************/
	/************* S I M P L E  I M P L E M E N T A T I O N S *************/
	/****************************************************************/
	/** A few functions that simplify the use of the class, you can i.e. open ****/
	/** dialogboxes with openDialog, close them with closeDialog, and so on  ***/
	/****************************************************************/
	/****************************************************************/
	
	
	open:function(url)//Retrieves url and opens it in a dialog Window created witht the Sdialog class
	{
		var Ajax=ajaxObj();
		Ajax.open('GET',url,false);
		Ajax.send(null);
		var response=Ajax.responseText;
		var error=request_success(response);
		if(error!=false)
		{
			response="<center><h3>" + error.response + "</h3></center>";
		}
		try
		{
			Sdialog.dBox.content(response);
		}
		catch(e)
		{
			Sdialog.dBox= new Sdialog.box(response);
			Sdialog.dBox.emptyOnClosure=true;
		}
		Sdialog.dBox.display("center");
		var script=getElement('onLoadScript');
		if(script!=undefined)
			eval(unescapeHTML(script.innerHTML));
		
		return Sdialog.dBox;
			
	},

	/**********************************************************************************************/
	openContent:function(html)//Opens the given html-string in a dialog-box
	{
		try
		{
			Sdialog.dBox.content(html);
		}
		catch(e)
		{
			Sdialog.dBox= new Sdialog.box(html);
			Sdialog.dBox.emptyOnClosure=true;
		}

		Sdialog.dBox.display("center");

	},
	/*************************************************************************************************/
	close:function()//closes the dialog opened with Sdialog.open
	{
		Sdialog.dBox.close();
	},
	/**********************************************************************************************/
	
	prompt_callback:function(){/*DUMMY*/},
	prompt:function(callback,message,defaultVal,buttontext)
	{
		//Creates and displays a dialog-prompt, calls the callback function whit the returnvalue when the user clicks the button
		if(typeof defaultVal=="undefined") defaultVal="";
		if(typeof buttontext=="undefined") buttontext="OK";
		Sdialog.prompt_callback=callback;
		var id= "sdialogPrompt" + Math.random();

		var html="<center><h3>" + message + "</h3>";
		html +="<input type='text' value=\"" + defaultVal + "\" id='" + id + "' /><br />";
		html +="<button onclick=\"Sdialog.prompt_callback(getElement('" + id + "').value);Sdialog.close();\">";
		html+= buttontext + "</button></center>";
		this.openContent(html);
	},
	waitBox:function()//opens a wait-dialog, close with closeDialog
	{
		this.openContent("<center><h3>Vänta!</h3>Detta kan ta en stund!</center>");
		Sdialog.dBox.element.style.width="300px";
	}

}



