﻿/*
	ConfirmPopup was abstracted from ImageReporter object literal to be used with Comments as well.
	TODO: further refactoring for true reusability.
*/
var ConfirmPopup = Class.create({
	initialize: function(container, elTrigger, elMsg, elConfirm, elSuccess){
		// element refs
		this.container = $(container);
		var containerID = this.container.identify();
		
		this.eKeys = {
			before: 'xhrReport-' + containerID + ':before',
			after:	'xhrReport-' + containerID + ':after',
			error:	'xhrReport-' + containerID + ':error'
		};
		
		this.elMsg = $(elMsg);
		this.elConfirm = $(elConfirm);
		this.elSuccess = $(elSuccess);
		
		// event handlers
		$(elTrigger).observe('click', this.__reportClick.bindAsEventListener(this));
		this.elConfirm.down('a.cancel').observe('click', this.__cancelClick.bindAsEventListener(this));
		this.elConfirm.down('a.confirm').observe('click', this.__confirmClick.bindAsEventListener(this)); // this element should be an ajax link button (e.g. $$('a[id$=btnReportPhoto]');)
		this.elSuccess.down('a.close').observe('click', this.__closeClick.bindAsEventListener(this));
		
		document.observe(this.eKeys.before, this.__xhrBefore.bindAsEventListener(this));
		document.observe(this.eKeys.after, this.__xhrAfter.bindAsEventListener(this));
		document.observe(this.eKeys.error, this.__xhrError.bindAsEventListener(this));
	},
	__reportClick: function(e){
		e.stop();
		this.elConfirm.appear({duration: 0.25});
	},
	__cancelClick: function(e){
		e.stop();
		this.elConfirm.fade({duration: 0.25});
	},
	__confirmClick: function(e){
		//e.stop();
		// Send off a xhr request from popajax link
		var keys = this.eKeys;
		Page.post(e, {
			before:	keys.before,
			after:	keys.after,
			error:	keys.error
		});
	},
	__xhrBefore: function(ce){
		this.elConfirm.fade({duration: 0.1});
		document.fire(ce.memo.eKey, ce.memo);
	},
	__xhrAfter: function(transport){
		this.elConfirm.hide();
		this.elSuccess.appear({duration:0.25});
	},					
	__xhrError: function(transport){
		throw new Error(transport);
	},
	__closeClick: function(e){
		e.stop();
		this.container.hide();
	}	
	
});
