var myConfirmList = [];
var myConfirmListQueue = [];

function myconfirm(p1, p2, p3){
	if(myConfirmList.length == 1){
		myConfirmListQueue.push([p1,p2,p3]);
		return false;
	}
	var obj = new myConfirm(p1, p2, p3);
	return obj;
}

function myConfirm(data, onConfirm, onCancel){
	var thisI = this;
	this.initialized = 0;
	var uid = "myConfirm_"+uniqueId();
	this.uid = uid;

	if(!data.css=="" || !data.css==undefined){
		loadCSS(data.css);
	}else{
		loadCSS("css/myconfirm.css");
	}

	title = (data.title) ? data.title : "Vahvistus";
	text = (data.text) ? data.text : "Vahvista toiminto";
	//onConfirm = (args.onConfirm) ? args.onConfirm : function(){void(0)};
	//onCancel = (args.onCancel) ? args.onCancel : function(){void(0)};

	var blocker = newElem("div");
	addStyle(blocker, {position:"absolute", top:"0px", left:"0px", width:"100%", backgroundColor:"#000000", zIndex:"998"} );
	blocker.style.height = px(getPageHeight()+50);

	setOpacity(blocker, 50);
	this.blocker = blocker;

	var container = newElem("div", {id:uid, className:"myConfirm_container"});

	var top = px( getScrollTop() + (getInnerHeight() / 2) - 50);

	//var overflow = document.body.style.overflow;
	//this.overflow = overflow;
	//document.body.style.overflow = "hidden";

	addStyle(container, {zIndex:"999", top:top} );
	
	var header = newElem("div", {className:"myConfirm_header"});
	header.innerHTML = '<div style="padding-left:5px;padding-top:2px;">'+title+'</div>';
	var headerContainer = newElem("div");
	addStyle(headerContainer, {padding:"2px"} );
	headerContainer.appendChild(header);

	var textElem = newElem("div", {className:"myConfirm_text"});
	textElem.innerHTML = text;

	var buttonsContainer = newElem("div", {className:"myConfirm_buttons"});
	var button1 = newElem("button", {className:"myConfirm_button"});
	button1.style.marginRight = "30px";
	button1.innerHTML = "Yes";
	var button2 = newElem("button", {className:"myConfirm_button"});
	button2.innerHTML = "No";

	this.button1 = button1;
	this.button2 = button2;

	this.onConfirm_action = onConfirm;
	this.onCancel_action = onCancel;

	buttonsContainer.appendChild(button1);
	buttonsContainer.appendChild(button2);

	container.appendChild(headerContainer);
	container.appendChild(textElem);
	container.appendChild(buttonsContainer);
	this.container = container;

	myConfirmList.push(this);

	if(onConfirm) this.init();
	return this;
}
myConfirm.prototype = {
	onConfirm: function(f){
		var button1 = this.button1;
		addEvent(button1, "mouseup", function(e){f(e)});
	}
	,
	onCancel: function(f){
		var button2 = this.button2;
		addEvent(button2, "mouseup", function(e){f(e)});
	}
	,
	init: function(onCancel){
		if(this.initialized == 0){
			if(this.onConfirm_action) this.onConfirm(this.onConfirm_action);
			if(this.onCancel_action) this.onCancel(this.onCancel_action);
			var thisI = this;
			addEvent(this.button1, "mouseup", function(e){thisI.close()});
			addEvent(this.button2, "mouseup", function(e){thisI.close()});
			document.body.appendChild(this.blocker);
			document.body.appendChild(this.container);
		}
		this.initialized = 1;
		var height = "-"+(this.container.offsetHeight / 2)+"px";
		this.container.style.marginTop = height; 
		setTimeout("window.myConfirmList[0].button2.focus();", 50);
	}
	,
	close: function(){
		myConfirmList = myConfirmList.remove(this);
		//document.body.style.overflow = this.overflow;
		document.body.removeChild(this.container);
		document.body.removeChild(this.blocker);

		var tmp = myConfirmListQueue.pop();
		if(tmp){
			myconfirm(tmp[0], tmp[1], tmp[2]);
		}

	}
}
