// confirmbox.js v0.7
//
// Copyright (c) 2009 internet2day GmbH
// Author: Werner G Liedl | http://www.internet2day.de
//
// If you care about the file size ...
//   Be a pro: 
//	    http://www.thinkvitamin.com/features/webapps/serving-javascript-fast
//      http://rakaz.nl/item/make_your_pages_load_faster_by_combining_and_compressing_javascript_and_css_files
//

/*-----------------------------------------------------------------------------------------------*/

if(typeof Prototype == 'undefined')
  throw("confirmbox.js requires including Prototype library!");

var ConfirmboxInstances = new Array();
var Confirmbox = Class.create();	
Confirmbox.prototype = {
	/**
	 * @var int
	 * The size of a scrollbar in px
	 */
	myIndex : 0,
	/**
	 * @var object
	 * The confirm box
	 */
	confirmBox : null,
	/**
	 * @var object
	 * The dimmed background box
	 * Comprizes the whole window
	 */
	bgBox : null,
	
	/**
	 * @var int
	 * The size of a scrollbar in px
	 */
	scrollbarOffset : 20,
	/**
	 * @var string
	 * The title bar text
	 */
	heading: '',
	/**
	 * @var string
	 * The question to be asked
	 */
	question: '',
	/**
	 * @var int
	 * The width of the box in px
	 */
	boxWidth: 100,
	/**
	 * @var int
	 * The height of the box in px
	 */
	boxHeight: 50,
	/**
	 * @var string
	 * The label of the button that returns 1
	 */
	yesLabel: 'YES',
	/**
	 * @var string
	 * The label of the button that returns 0
	 */
	noLabel: 'NO',
	/**
	 * @var string
	 * The label of the button that returns 0
	 */
	cancelLabel: 'Cancel',
	/**
	 * @var function
	 * The call back function to pass the users choice to
	 */
	onChoiceMade: Prototype.emptyFunction,
	/**
	 * @var function
	 * The existing onresize function
	 */
	mem_resize: null,
	/**
	 * @var function
	 * The existing onscroll function
	 */
	mem_scroll: null,
	
	/**
	 * Initializes the class
	 * 
	 * @access public
	 * @param void
	 * @return void
	 */
	initialize : function() {
		this._setupBox();
		this.myIndex = ConfirmboxInstances.length;
		ConfirmboxInstances[this.myIndex] = this;
	},
	
	/**
	 * Use the confirm box
	 * produces 
	 * 
	 * @access public
	 * @param int		the confirm box width
	 * @param int		the confirm box height
	 * @param string	the question to be asked
	 * @param string	the yesButton label
	 * @param string	the noButton label
	 * @param string	the cancelButton label
	 * @param function	the callback function to receive the user's choice (YES=1 or NO=0)
	 * @return void
	 */
	ask : function(msgBoxWidth,msgBoxHeight,msgTitle,msgTxt,msgBtnYes,msgBtnNo,msgBtnCancel,msgCallback) {
		this.boxWidth = msgBoxWidth;
		this.boxHeight = msgBoxHeight;
		this.heading = msgTitle;
		this.question = msgTxt;
		this.yesLabel = msgBtnYes;
		this.noLabel = msgBtnNo;
		this.cancelLabel = msgBtnCancel;
		this.onChoiceMade = msgCallback;
	 
		this._ask();
	},

	_setupBox : function() {
		this.confirmBox = ( document.all ? document.all.cb_box : document.getElementById("cb_box") );
		if ( this.confirmBox == null ) {
			this.confirmBox = document.createElement("div");
			this.confirmBox.id = "cb_box";
	    	document.body.appendChild(this.confirmBox);
		}
		
		this.bgBox = ( document.all ? document.all.cb_box_bg : document.getElementById("cb_box_bg") );
		if ( this.bgBox == null ) {
			this.bgBox = document.createElement("div");
			this.bgBox.id = "cb_box_bg";
	    	document.body.appendChild(this.bgBox);
		}
	},
	
	_winWidth : function() {
		if (window.innerWidth) {
			return window.innerWidth
		} else if (document.documentElement && document.documentElement.clientWidth) {
			return document.documentElement.clientWidth
		} else if (document.body) {
			return document.body.clientWidth
		}
		return 0;
	},
	
	_winHeight : function() {
		if (window.innerHeight) {
			return window.innerHeight
		} else if (document.documentElement && document.documentElement.clientHeight) {
			return document.documentElement.clientHeight
		} else if (document.body) {
			return document.body.clientHeight
		}
		return 0;
	},

	_ask : function() {	
		if ( this.confirmBox != null ) {
			var ww = this._winWidth();
			var wh = this._winHeight();
			var iebody = ( (document.compatMode && document.compatMode != "BackCompat") ? document.documentElement : document.body );
			var dsocleft = ( document.all ? iebody.scrollLeft : window.pageXOffset-this.scrollbarOffset );
			var dsoctop  = ( document.all ? iebody.scrollTop : window.pageYOffset-this.scrollbarOffset );
			
			var xPos = Math.floor( (ww - this.boxWidth) / 2 + 0.5 + dsocleft);
			var yPos = Math.floor( (wh - this.boxHeight) / 2 + 0.5 + dsoctop );
			
			if ( xPos < 0 ) xPos = 0;
			if ( yPos < 0 ) yPos = 0;
					
			this.confirmBox.style.left   = xPos+"px";
			this.confirmBox.style.top    = yPos+"px";
			this.confirmBox.style.width  = this.boxWidth+"px";
			this.confirmBox.style.height = this.boxHeight+"px";
			//this.confirmBox.style.overflow = "none";
			
			var titleLine = ( this.heading.length ? '<p id="title">' + this.heading + '</p>' : '' );
			var questionLine = ( this.question.length ? '<p id="question">' + this.question + '</p>' : '' );
			var yesButton = ( this.yesLabel.length ? '<button type="button" onclick="ConfirmboxInstances['+this.myIndex+']._forwardResult(1);">'+this.yesLabel+'</button>' : '' );
			var noButton = ( this.noLabel.length ? '<button type="button" onclick="ConfirmboxInstances['+this.myIndex+']._forwardResult(0);">'+this.noLabel+'</button>' : '' );
			var cancelButton = ( this.cancelLabel.length ? '<button type="button" onclick="ConfirmboxInstances['+this.myIndex+']._forwardResult(2);">'+this.cancelLabel+'</button>' : '' );
			var buttonLine = '<p id="buttons">' + yesButton + noButton + cancelButton + '</p>';
			this.confirmBox.innerHTML =  titleLine + questionLine + buttonLine;
			
			if ( this.bgBox != null ) {
				this.bgBox.style.left   = dsocleft+"px";
				this.bgBox.style.top    = dsoctop+"px";
				this.bgBox.style.width  = ww+"px";
				this.bgBox.style.height = wh+"px";
				//this.bgBox.style.overflow = "none";
				this.bgBox.style.display  = "block";		
			}
			this.confirmBox.style.display  = "block";
			
			window.onresize = this._ask.bind(this);
			window.onscroll = this._ask.bind(this)
		}
	},
		
	_forwardResult : function(val){
		window.onresize = this.mem_resize;
		window.onscroll = this.mem_scroll;
			
		if ( this.confirmBox != null ) {
			this.confirmBox.style.display  = "none";
			this.confirmBox.style.left   = "-10px";
			this.confirmBox.style.top    = "-10px";
			this.confirmBox.style.width  = "0px";
			this.confirmBox.style.height = "0px";
			//this.confirmBox.style.overflow = "clip";
		}
		
		if ( this.bgBox != null ) {
			this.bgBox.style.display  = "none";		
			this.bgBox.style.left   = "-10px";
			this.bgBox.style.top    = "-10px";
			this.bgBox.style.width  = "0px";
			this.bgBox.style.height = "0px";
			//this.bgBox.style.overflow = "clip";
		}
	
		if ( typeof this.onChoiceMade == 'function' ) {
			this.onChoiceMade(val);
		}
	}
}

/*-----------------------------------------------------------------------------------------------*/

Event.observe(window, 'load', confirmboxInit, false);

//
//	Set up all we need
//
var myConfirmbox = null;
function confirmboxInit() {
	myConfirmbox = new Confirmbox();
}

