/* 
	prompt
	2 buttons, input
	event: 'prompted' 
*/
mmDialog.Prompt = new Class({	
	
	Extends: mmDialog
	, options: {
		buttons : {
			'ok': '<span class="mmIcon icon-ok"></span> Ok'
			, 'cancel': 'Cancel'
		}
		, autosize: true
		// , closeable: false
		, focus: true
		, promptValue: ''
		, classes: {
			btnOk: 'mmBtn btn-l btn-default'
			, btnCancel: 'mmBtn btn-l mr-10'
			, msgContainer: 'mmDialogPromptMessage'
			, input: 'mmDialogPromptInput'
		}
		, template: '<div class="rel ptb-10"><div class="row"><div class="col c1-7 tC"><span class="mmIcon icon-prompt icon-xxl"></span></div><div class="col c6-7 clast"><div class="rel mmDialogPromptMessage f-l tL pt-5 plr-5 tsb-w"></div><div class="rel pt-5 plr-5"><input type="text" class="mmDialogPromptInput rnd-5 f-m wp-95" /></div></div></div></div>'
	}
	, initialize: function(msg, posFunc, negFunc, options){
		this.parent(options);
		this.setPosFunc(posFunc).setNegFunc(negFunc);
		this.setupDialogElements();
		this.promptValue = this.options.promptValue;
		this.setDefaultContent(msg);
		this.setupDialogEvents();
	}
	, setupDialogElements: function() {
		this.btnOk = new Element('a',{
			'class': this.options.classes.btnOk
			, 'href': '#'
			, events: {
				click: function(e){
					e.stop();				
					var promptResult = this.promptInput.get('value');	
					this.fireEvent('prompted', promptResult);
					this.posFunc(promptResult);
					this.close();
				}.bind(this)
				// , 'keydown': this.keyDown.bindWithEvent(this, this.btnOk)				
			},
			html: this.options.buttons.ok
		});	
		this.btnCancel = new Element('a',{
			'class': this.options.classes.btnCancel
			, 'href': '#'
			, events: {
				click: function(e){
					e.stop();
					this.fireEvent('denied');					
					this.negFunc();					
					this.close();
				}.bind(this)
			},
			html: this.options.buttons.cancel
		});	
		this.btnCancel.addEvent('keydown', this.keyDown.bindWithEvent(this, this.btnCancel));
		this.btnOk.addEvent('keydown', this.keyDown.bindWithEvent(this, this.btnOk));				
		this.buttons = new Element('div', {'class':'rel'}).adopt([this.btnCancel,this.btnOk]);
		this.dialogTabIndex	= [this.btnOk, this.btnCancel];
		this.setFooter(this.buttons);
	}
	, setDefaultContent:function(msg) {
		var el = this.options.template.toHTML();
		this.setContent(el);
		this.msgContainer = this.dialogBody.getElement('div.'+this.options.classes.msgContainer);
		this.promptInput = this.dialogBody.getElement('input.'+this.options.classes.input);
		if (!this.promptInput) {
			this.promptInput = new Element('input', {'class':this.options.classes.input,'type':'text'}).inject(this.dialogBody);
		}		
		this.dialogTabIndex.push(this.promptInput);	
		this.promptInput.set('value','');	
		if (msg != '') {
			this.setMessage(msg);
		}
	}	
	, setupDialogEvents:function() {
		if(this.options.focus){
			this.addEvent('show',function(){
				this.promptInput.focus();
			});
		}	
		this.addEvent('open',function(){
			this.promptInput.set('value',this.promptValue).select();
			this.btnOk.removeClasses(['btn-active', 'btn-focus']);
			this.btnCancel.removeClasses(['btn-active', 'btn-focus']);			
		});		
		this.promptInput.addEvent('keydown', this.keyDown.bindWithEvent(this, this.promptInput));
	}
	, setPosFunc:function(posFunc) {
		this.posFunc = (posFunc != undefined && (typeOf(posFunc) == 'function')) ? posFunc : function() {} ;		
		return this;
	}
	, setNegFunc:function(negFunc) {
		this.negFunc = (negFunc != undefined && (typeOf(negFunc) == 'function')) ? negFunc : function() {} ;		
		return this;
	}	
	, setPromptValue:function(value) {
		this.promptValue = value;
		return this;
	}
	, setMessage:function(msg) {
		var setDefaultButtonTexts = false;
		if (msg.contains('::')) {
			setDefaultButtonTexts = true;
			var msg_array = msg.split('::');
			var msg = msg_array[0];
			if (msg_array[1] != undefined && msg_array[1] != '') {
				var buttontexts = msg_array[1];
				if (buttontexts.contains('|')) {
					var buttontexts_array = buttontexts.split('|');
					if (typeOf(buttontexts_array) == 'array'){ 
						setDefaultButtonTexts = false;
						if (buttontexts_array[0] != undefined && buttontexts_array[0] != '') {
							this.btnOk.set('html', buttontexts_array[0]);
						}
						if (buttontexts_array[1] != undefined && buttontexts_array[1] != '') {
							this.btnCancel.set('html', buttontexts_array[1]);
						}										
					} 
				} 
			} 
		} else {
			setDefaultButtonTexts = true;			
		}
		if (setDefaultButtonTexts) {
			this.setDefaultButtonTexts();
		}
		this.msgContainer.set('html', msg);
		this.reSize();
		this.parseContent();
		return this;
	}
	, setDefaultButtonTexts:function() {
		this.btnCancel.set('html', this.options.buttons.cancel);
		this.btnOk.set('html', this.options.buttons.ok);		
	}
	, parseContent:function() {
		if (mm.objects) {
			mm.objects.scan(this.dialogBody);
		}
	}
	, keyDown:function(e, el) {
		if (this.dialogTabIndex.contains(el) && e.key=='tab') {
// dbug.log('tab');			
			e.preventDefault(); 
			if (e.shift) {
				this.setFocusElement.delay(50, this, [el, true]);
			} else {
				this.setFocusElement.delay(50, this, [el, false]);				
			}
			return false;			
		}
		if ((el==this.btnOk || el==this.btnCancel) && (e.key =='space' || e.key == 'enter')) {
			e.stop();
			el.addClass('btn-active');
			el.fireEvent('click', e);
			el.blur();
			el.removeClass.delay(500, el, 'btn-active');
		}
		if (el==this.promptInput && e.key == 'enter') {
			e.stop();
			this.btnOk.addClass('btn-active');
			this.btnOk.fireEvent('click', e);
			this.btnOk.blur();
			this.btnOk.removeClass(500, this.btnOk, 'btn-active');
		}
	}	
	, setFocusElement:function(el, back) {
// dbug.log('setNextFocusElement el.id:'+el.get('id'));		
		var curPos = 0;
		var elList = [];
		elList = this.dialogTabIndex;
		curPos = elList.indexOf(el);
		if (back) {
			nextEl = elList[curPos-1] || elList.getLast() || false;			
		} else {
			nextEl = elList[curPos+1] || elList[0] || false;
		}
// dbug.log('next el = '+nextEl.get('id'));				
		if (nextEl) { nextEl.focus(); }
	}	
});
