var Popin = new Class({
	initialize: function(){
		var popinLayer = new Element("div", {
			"id": "popinLayer"
		});
		Element.insert(popinLayer, document);
		var popinFrame = new Element("div", {
			"id": "popin"
		});
		Element.insert(popinFrame, document);
		Element.insert(popinFrame, new Element("h1", {
			"id": "popinTitle"
		}));
		Element.insert(new Element("div", {
			"id": "popinMessage"
		}), popinFrame);
		var popinPromptFrame = new Element("div", {
			"id": "popinPrompt"
		});
		Element.insert(popinPromptFrame, popinFrame);
		Element.insert(popinPromptFrame, new Element("label", {
			"for": "popinInput",
			"id": "popinLabel"
		}));
		Element.insert(new Element("input", {
			"type": "text",
			"class": "inputbox",
			"id": "popinInput",
			Events: {
				"keypress": this.checkKeyb.bindWithEvent(this)
			}
		}), popinPromptFrame);
		var popinButtonsFrame = new Element("div", {
			"class": "popinButtons"
		});
		Element.insert(popinButtonsFrame, popinFrame);
		Element.insert(new Element("button", {
			"class": "button",
			"id": "popinButtonCancel",
			"rel": "Cancel",
			Text: "Annuler",
			Events: {
				"click": this.callback.bind(this)
			}
		}), popinButtonsFrame);
		Element.insert(new Element("button", {
			"class": "button",
			"id": "popinButtonOk",
			"rel": "Ok",
			Text: "Ok",
			Events: {
				"click": this.callback.bind(this)
			}
		}), popinButtonsFrame);
		Element.insert(new Element("button", {
			"class": "button",
			"id": "popinButtonNo",
			"rel": "No",
			Text: "Non",
			Events: {
				"click": this.callback.bind(this)
			}
		}), popinButtonsFrame);
		Element.insert(new Element("button", {
			"class": "button",
			"id": "popinButtonYes",
			"rel": "Yes",
			Text: "Oui",
			Events: {
				"click": this.callback.bind(this)
			}
		}), popinButtonsFrame);
		Element.setOpacity("popinLayer", 0);
	},
	
	show: function(parameters){
		var defaultParameters = {
			title: "Message",
			message: "- no message -",
			type: "ALERT",
			onOk: function(){},
			onYes: function(){},
			onNo: function(){},
			onCancel: function(){}
		};
		this.parameters = $extend(parameters || {}, defaultParameters);
		
		Element.update("popinTitle", this.parameters.title);
		Element.update(this.parameters.type == "PROMPT" ? "popinLabel" : "popinMessage", this.parameters.message);
		Element.update("popinInput", "");
		switch(this.parameters.type){
			case "ALERT":
				Element.hide("popinButtonCancel");
				Element.show("popinButtonOk");
				Element.hide("popinButtonNo");
				Element.hide("popinButtonYes");
				Element.hide("popinPrompt");
				Element.show("popinMessage");
			break;
			case "CONFIRM":
				Element.show("popinButtonCancel");
				Element.show("popinButtonOk");
				Element.hide("popinButtonNo");
				Element.hide("popinButtonYes");
				Element.hide("popinPrompt");
				Element.show("popinMessage");
			break;
			case "QUESTION":
				Element.hide("popinButtonCancel");
				Element.hide("popinButtonOk");
				Element.show("popinButtonNo");
				Element.show("popinButtonYes");
				Element.hide("popinPrompt");
				Element.show("popinMessage");
			break;
			case "PROMPT":
				Element.show("popinButtonCancel");
				Element.show("popinButtonOk");
				Element.hide("popinButtonNo");
				Element.hide("popinButtonYes");
				Element.show("popinPrompt");
				Element.hide("popinMessage");
			break;
		}
		this.draw.call(this);
	},
	
	draw: function(){
		Element.show("popinLayer");
		new Animation("popinLayer", {
			"opacity": {to: .5}
		});
		Element.setStyle("popin", {"visibility": "hidden"});
		Element.show("popin");
		Element.setStyle("popin", {
			"marginTop": -(Element.getHeight("popin")/2)+"px",
			"visibility": "visible"
		});
		if(this.parameters.type == "PROMPT")
			$("popinInput").focus();
	},
	
	close: function(){
		Element.hide("popin");
		new Animation("popinLayer", {
			"opacity": {to: 0}
		}, {
			onComplete: function(){
				Element.hide("popinLayer");
			}
		});
	},
	
	prompt: function(){
		this.parameters.onOk.call(this, $("popinInput").value);
		this.close.call(this);
	},
	
	checkKeyb: function(e){
		if(Event.getKey(e) == 13)
			this.prompt();
	},
	
	callback: function(e){
		this.parameters["on"+Event.getElement(e).getAttribute("rel")].call(null, this.parameters.type == "PROMPT" ? $("popinInput").value : null);
		this.close.call(this);
	}
});
