/**
 * Action Message
 */
var ActionMessage = Class.create();
ActionMessage.prototype = {

	/**
	 * type should be one of {Success, Warning, Error}
	 */
	initialize: function(type, message) {
	    this.type = type.strip().capitalize();
	    this.message = message;
	    
	    this.messagesId = "actionMessages";

		this.setImagePaths();
		this.setClassName();
    
	    this.id = "message" + Math.floor(Math.random()*9999);

		this.clearAllOtherMessages();
		this.createElement();
		this.display();
	},

	setImagePaths: function() {
		this.closeButtonPath = localPath + "/image/main/message-close.png";
		switch(this.type) {
			case "Success":
				this.messageIconPath = localPath + "/image/main/message-success.png";
				break;
			case "Error":
				this.messageIconPath = localPath + "/image/main/message-error.png";
				break;
			case "Warning":
				this.messageIconPath = localPath + "/image/main/message-warning.png";
				break;
		}
	},
	
	setClassName: function() {
		switch(this.type) {
			case "Success":
				this.className = "actionMessageSuccess";
				break;
			case "Error":
				this.className = "actionMessageError";
				break;
			case "Warning":
				this.className = "actionMessageWarning";
				break;
		}
	},

	createElement: function() {
		this.displayElement = document.createElement("div");
		Element.extend(this.displayElement);
		this.displayElement.id = this.id;
		this.displayElement.addClassName("actionMessage");
		this.displayElement.addClassName(this.className);

		this.messageIcon = document.createElement("img");
		Element.extend(this.messageIcon);
		this.messageIcon.src = this.messageIconPath;
		this.messageIcon.id = this.id + "Icon";
		this.messageIcon.addClassName("actionMessageIcon");

		this.messageText = document.createElement("span");
		Element.extend(this.messageText);
		this.messageText.innerHTML = this.message;
		this.messageText.id = this.id + "Text";
		this.messageText.addClassName("actionMessageText");
		
		this.closeButton = document.createElement("img");
		Element.extend(this.closeButton);
		this.closeButton.src = this.closeButtonPath;
		this.closeButton.id = this.id + "CloseButton";
		this.closeButton.addClassName("actionMessageCloseButton");
		this.closeButton.onclick = function() {
			this.remove();
		}.bind(this);

		this.displayElement.appendChild(this.messageIcon);		
		this.displayElement.appendChild(this.messageText);
		this.displayElement.appendChild(this.closeButton);
		this.displayElement.appendChild(Element.extend(document.createElement("div")).addClassName("clear"));
	},
	
	display: function() {		
		$(this.messagesId).appendChild(this.displayElement);
		this.displayElement.show();
	},
	
	remove: function() {
		new Effect.Fade(this.displayElement, {delay: 0, duration: 3}); 
	},
	
	clearAllOtherMessages: function() {
		$(this.messagesId).innerHTML = "";
	}
};