/**
* User Messaging System
* Version: 0.2 (25.05.2005)
*/

msg = new Object();

msg.DEFAULT = { lifetime: 3};

// deprecated
msg.messages = new Hash();

msg.getMessage = function(text, properties){
	
	var message = new Object();
	
	if(!properties || !(message.id = properties.id))
		message.id = 'message'+getRandom();
	
	var lifetime;
	if (!properties || !(lifetime = properties.lifetime))
		lifetime = msg.DEFAULT.lifetime;
	
	var className;
	if(!properties || !(className = properties.className))
		className = 'message';
	
	if(!properties || !(message.parent = properties.parent))
		message.parent = BODY();
	
	message.container = cDOM({
			tag: 'div',
			id: message.id,
			className: className,
			fixedId: true});
			
	message.message = message.container; // for compability, deprecated
	
	message.content = cDOM({
			tag: 'div',
			className: 'messageContent',
			innerHTML: text});
			
	message.container.appendChild(message.content);
	
	message.parent.appendChild(message.container);
	msg.messages.add(message.id, message);
	
	
	if (lifetime > 0)
		setTimeout("msg.clear({id: ['"+message.id+"']})", lifetime*1000);
}

msg.Message = function(content, id, lifetime, properties){
	
	var parent;
	if(!properties || !(parent = properties.parent))
		parent = null;
	
	return msg.getMessage(content, {'id': id, 'lifetime': lifetime, 'parent': parent});
	
}

msg.ConfirmBox = Class.create();
msg.ConfirmBox.prototype = {
	initialize: function(text, id, properties){
		
		if (id == null){
		ts = new Date();
		id = ts.getMilliseconds()+"_"+ts.getSeconds();
		}
		
			
		this.parent = BODY();
		this.id = id;
		msg.messages[this.id] = this;
		
		this.message = document.createElement('div');
		this.message.id = id;
		
		this.message.appendChild(new cDOM({tag: 'p', innerHTML: text}));
		
		if(properties && properties.target){
			this.message.target = properties.target;
		}
		
		//TODO BUTTONS, notify
		if(properties && properties.notify && properties.command){
			this.okButton = cBUTTON({
				notify: properties.notify,
				command:properties.command,
				innerHTML: 'ok',
				pars: {target: this.id}});
			this.message.appendChild(this.okButton);
		}
		
		this.cancelButton = cBUTTON({notify: this.onCancel, innerHTML: 'cancel', pars: {target: this.id}});
		this.cancelButton.target = id;
		this.message.appendChild(this.cancelButton);
		
		if(!properties || !properties.position){
			var position = getWindowSize();
			var height = 100;
			var width = 150;
			this.message.style.height = height+'px';
			this.message.style.width = width+'px';
			this.message.style.top = (position[1]/2-height/2)+'px';
			this.message.style.left = (position[0]/2-width/2)+'px';
		}
		
		else{
			
			var x = properties.position[0];
			var y = properties.position[1];
			if(!isString(x))
				x+='px';
			if(!isString(y))
				y+='px';
			
			this.message.style.top = y;
			this.message.style.left = x;
		}
		
		if(properties && properties.dimension){
			var x = properties.dimension[0];
			var y = properties.dimension[1];
			if(!isString(x))
				x+='px';
			if(!isString(y))
				y+='px';
			
			this.message.style.height = y;
			this.message.style.width = x;
		}
		
		if(properties && properties.className)
		this.message.className = properties.className;
		else
		this.message.className = 'confirmBox';
		
		this.parent.appendChild(msg.messages[this.id].message);
		
		this.isVisible = null;
		
		if(properties && properties.isVisible != null){
			if(properties.isVisible == false)
				this.hide();
			else
				this.show();
		}
		
		else
			this.isVisible = true;
		
	},
	
	onCancel: function(e, target, command, pars){
		msg.clear({id: pars.target});
	},
	
	show: function(){
		if (this.isVisible == false)
			Element.toggle(this.message);

		this.isVisible = true;
	},
	
	hide: function(){
		if(this.isVisible == true || this.isVisible == null)
			Element.toggle(this.message);
		
		this.isVisible = false;
	}
};

msg.clear = function(properties){
	var message = msg.messages.has(properties.id);
	
	if(message){ //deprecated
		message.parent.removeChild(message.message);
		
		if(message.infoBox)
			msg.clear({id: message.infoBox.id});
		
		msg.messages.del(properties.id);
	}
}

