/* 
	confirm
	2 buttons
	event: 'confirmed'	 
*/
mmDialog.Confirm = new Class({	
	
	Extends: mmDialog
	, options: {
		buttons : {
			'ok': '<span class="mmIcon icon-ok"></span> Ok'
			, 'cancel': 'Cancel'
		}
		, autosize: true
		// , closeable: false
		, focus: true
		, classes: {
			btnOk: 'mmBtn btn-l btn-default'
			, btnCancel: 'mmBtn btn-l mr-10'
			, msgContainer: 'mmDialogConfirmMessage'
		}
		, template: '<div class="rel ptb-10"><div class="row"><div class="col c1-7 tC"><span class="mmIcon icon-confirm icon-xxl dB"></span></div><div class="col c6-7 clast"><div class="rel mmDialogConfirmMessage f-l tL pt-5 plr-5 tsb-w"></div></div></div></div>'
	}
	, initialize: function(msg, posFunc, negFunc, options){
		this.parent(options);
		this.setPosFunc(posFunc).setNegFunc(negFunc);
		this.setupDialogElements();
		this.setDefaultContent(msg);
		this.setupDialogEvents();
	}
	, setupDialogElements: function() {
		this.btnOk = new Element('a',{
			'class': this.options.classes.btnOk
			, 'href': '#'
			, events: {
				click: function(e){
					e.stop();					
					this.fireEvent('confirmed');
					this.posFunc();
					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);
		if (msg != '') {
			this.setMessage(msg);
		}
	}	
	, setupDialogEvents:function() {
		if(this.options.focus){
			this.addEvent('show',function(){
				this.btnOk.focus();
			});
		}	
	}
	, 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;
	}	
	, 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) {
// mm.log('confirm keyDown el:'+el);		
		if (this.dialogTabIndex.contains(el) && e.key=='tab') {
			e.preventDefault(); 
			if (e.shift) {
				this.setFocusElement.delay(50, this, [el, true]);
			} else {
				this.setFocusElement.delay(50, this, [el, false]);				
			}
			return false;			
		}
		if (this.dialogTabIndex.contains(el) && (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');
		}
	}	
	, setFocusElement:function(el, back) {
// mm.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;
		}
// mm.log('next el = '+nextEl.get('id'));				
		if (nextEl) { nextEl.focus(); }
	}	
});
