Dvintage.Message = Class.create({
	initialize: function(containingNode){
		this.containingNode = containingNode;
		this.messages = {
			success:'Your changes have been saved',
			error:'One or more fields are not filled in properly',
			send:'Your message has been sent'
		}
		this.messageNode = new Element('div').addClassName('message').update(this.messages.success).hide();
		this.showDuration = 6000;
	},
	setMessage: function(message){
		this.messageNode.update(this.messages[message]);
	},
	show: function(){
		this.containingNode.insert(this.messageNode);
		var top = Math.round(this.containingNode.offsetHeight / 2) + 'px';
		var left = '200px';
		this.messageNode.setStyle({top:top,left:left})
		new Effect.Appear(this.messageNode, {
			afterFinish: function() {
				setTimeout(function(){
					new Effect.Fade(this.messageNode, {
						afterFinish: function () {
							this.containingNode.removeChild(this.messageNode);
						}.bind(this)
					})
				}.bind(this), this.showDuration);
			}.bind(this)
		});	
	}
})


Dvintage.Dialog = Class.create({
	initialize: function(containingNode){		
		this.options = Object.extend({
      		cancelButton: false,
			button: false
		}, arguments[1] || {});
		
		this.containingNode = containingNode;
		this.template = new Template('<div class="dialog modal"></div><img src="images/dialog/dropshadow.png" class="dialog" /><div class="dialog window"><h2>#{header}</h2><p>#{message}</p></div>');
	},
	
	_isKeyReturnDoCallBack: function (e) {
		if (e.keyCode == Event.KEY_RETURN) {
			e.stop();
			if (this.button.getStyle('opacity')==1) {
				this.doCallBack();
			}			
		}
	},
	
	setButtonActive: function(){
		if (this.options.button) {
			this.button.setOpacity(1);
		}
	},
	setButtonUnActive: function(){
		if (this.options.button) {
			this.button.setOpacity(.3);
		}
	},
	setMessage: function(header, message){
		this.message = {header:header, message:message};
	},
	doCallBack: function () {
		if (this.options.button.callBack) {
			this.options.button.callBack();
		}
		this.hide();		
	},
	show: function(){
		this.hide();		// to be on the save side.
		var html = this.template.evaluate(this.message);
		this.containingNode.insert(html);
		this.containingNode.down('.dialog p').update(this.message.message);
		if (this.options.cancelButton) {
			this.cancel = new Element('button').addClassName('smallbutton').update('Cancel');
			this.containingNode.down('.dialog.window').insert(this.cancel);
			this.cancel.observe('click', this.hide.bind(this));
		}
		if (this.options.button) {
			this.button = new Element('button').addClassName('smallbutton').setOpacity(.3).update(this.options.button.value);
			this.containingNode.down('.dialog.window').insert(this.button);
			// attach Return key to button
			
			this.testing = this._isKeyReturnDoCallBack.bind(this);
			document.observe('keydown', this.testing)
			this.button.observe('click', function(){
				if (this.button.getStyle('opacity')==1) {
					this.doCallBack();
				}
			}.bind(this));
			
		}		
	},
	hide: function() {
//					this.testing = this._isKeyReturnDoCallBack.bind(this);
		document.stopObserving('keydown', this.testing);
		$$('.dialog').invoke('remove');
	}
})




