var btnOK = 2
var btnCancel = 4
var btnYes = 8
var btnNo = 16

var buttonCaptions = {
	2		: "OK",
	4		: "Cancel",
	8		: "Yes",
	16		: "No"
}

var alertObjectsCount = 0;

function Alert( message, title, buttons, callbackFunction, show ) {
	this.name = name;
	
	if( buttons != undefined ) {
		this.buttons = buttons;
	}
	else {
		this.buttons = btnOK;
	}
	
	if( message != undefined ) {
		this.message = message;
	}
	else {
		this.message = "";
	}
	
	if( title ) {
		this.title = title;
	}
	else {
		this.title = "";
	}
	
	this.callbackFunction = callbackFunction;
	
	this.width = 250;
	
	this._body = document.getElementsByTagName("body").item( 0 );
	
	if( show ) {
		this.show();
	}
	
	return this;
}

Alert.prototype.show = function() {
	var left, top, windowSize = getWindowSize();
	
	this._window = document.createElement( "div" );
	this._title = document.createElement( "div" );
	this._content = document.createElement( "div" );
	this._buttons = document.createElement( "div" );
	
	left = ( windowSize["w"] - this.width ) / 2 + alertObjectsCount * 10;
	
	this._window.id = this.name;
	this._window.className = "Alert";
	this._window.style.position = "absolute";
	this._window.style.left = left + "px";
	this._window.style.top = ( 50 + alertObjectsCount * 10 ) + "px";
	
	//this._title.id = this.name + "Title";
	this._title.className = "Title";
	this._title.appendChild( document.createTextNode( this.title ) );
	
	//this._content.id = this.name + "Content";
	this._content.className = "Content";
	this._content.appendChild( document.createTextNode( this.message ) );
	
	//this._buttons.id = this.name + "Buttons";
	this._buttons.className = "Buttons";
	
	this._window.appendChild( this._title );
	this._window.appendChild( this._content );
	this._window.appendChild( this._buttons );
	
	var buttonCounter;
	
	for( buttonCounter = 2; buttonCounter < 128; buttonCounter = buttonCounter * 2 ) {
		if( parseInt( this.buttons & buttonCounter ) == parseInt( buttonCounter ) ) {
			var button = document.createElement( "input" );
			
			button.type = "button";
			button.value = buttonCaptions[buttonCounter];
			button.alertObject = this;
			button.returnValue = buttonCounter;
			
			button.onclick = function() {
				this.alertObject.returnValue( this.returnValue );
			}
		
			this._buttons.appendChild( button );
		}
	}
	
	this._body.appendChild( this._window );
	
	alertObjectsCount++;
}

Alert.prototype.returnValue = function( value ) {
	if( this.callbackFunction ) {
		this.callbackFunction( value );
	}
	
	this.destroy();
}

Alert.prototype.destroy = function() {
	if( this._window ) {
		this._body.removeChild( this._window );
		
		this._buttons = null;
		this._title = null;
		this._content = null;
		this._window = null;
		
		alertObjectsCount--;
	}
}

/*
function getWindowSize() {
	var w = 0, h = 0;
	
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		w = window.innerWidth;
		h = window.innerHeight;
	}
	else {
		if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
			//IE 6+ in 'standards compliant mode'
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		}
		else {
			if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
				//IE 4 compatible
				w = document.body.clientWidth;
				h = document.body.clientHeight;
			}
		}
	}
	
	return { "w" : w, "h" : h }
}
*/