HD.util.Email = function(config) {
	this.model = new HD.util.Email.Model(config);
	this.view = new HD.util.Email.View(this.model);
	this.controller = new HD.util.Email.Controller(this.model, this.view);
	this.model.addObserver(this.view);
};

HD.util.Email.prototype = {
	init : function(config) {
		this.view.init(config);
	}
};

HD.util.Email.Model = function(config) {
	this.config = config;
	this.observers = [];
};

HD.util.Email.Model.prototype = {
	sendEmail : function(email) {
		this.notifyObservers("sendEmail_Start");
		
		var cb = this;
		EmailService.sendEmail(email, function(response){
			var eventName = response.success ? "sendEmail_Finish" : "sendEmail_Error";
			cb.notifyObservers.call(cb, eventName);
		});
	}
};

HD.extend(HD.util.Email.Model, [HD.util.Observable]);

HD.util.Email.View = function(model){
	this.model = model;
	this.config = this.model.config;
	this.observers = [];
	this.loadTemplates(arguments.callee);
	
	this.triggerEl = HD.get(this.config.trigger);
	if(this.triggerEl == null) {
		// alert("ERROR: No trigger element specified for e-mail widget.");
		return;
	}
	
	var cb = this;
	this.triggerEl.onclick = function() {
		cb.render.call(cb);
	};
};

HD.util.Email.View.templates = {
	overlayTitle : "Send via Email",
	successText  : "Your e-mail has been successfully sent!",
	failureText  : "ERROR: Your Email could not be sent at this time, please verify information entered and/or try again later.",
	fromLabel    : "From : ",
	toLabel      : "To : ",
	subjectLabel : "Subject : ",
	bodyLabel    : "Body : ",
	submitLabel  : "Send",
						
	getOverlayTitle : function(){
		return this.overlayTitle;
	},
	
	getSuccessText : function(){
		return this.successText;
	},
	
	getFailureText : function() {
		return this.failureText;
	},
	
	getFromLabel : function() {
		return this.fromLabel;
	},
	
	getToLabel : function() {
		return this.toLabel;
	},
	
	getSubjectLabel : function() {
		return this.subjectLabel;
	},
	
	getBodyLabel : function() {
		return this.bodyLabel;
	},
	
	getSubmitLabel : function() {
		return this.submitLabel;
	},
	
	getHtml : function() {
		return this.html;
	},
	
	html :  '\
		<form onsubmit="return false;" class="${classes.EMAIL_FORM}">\
			<table>\
				<tr><td>${templates.getFromLabel()}</td><td><input name="from" type="text"></input></td></tr>\
				<tr><td>${templates.getToLabel()}</td><td><input name="to" type="text"></input><br/></td></tr>\
				<tr><td>${templates.getSubjectLabel()}</td><td><input name="subject" type="text"></input><br/></td></tr>\
				<tr><td>${templates.getBodyLabel()}</td><td><textarea name="body" cols="30" rows="5"></textarea><br/></td></tr>\
				<tr><td></td><td><input type="Submit" value="${templates.getSubmitLabel()}"></input></td></tr>\
			</table>\
		</form>'
};

HD.util.Email.View.prototype = {
	
	render : function() {
		var html = this.getHtml();
		var overlayEl = HD.util.Common.showOverlay(html, HD.util.Email.View.templates.getOverlayTitle(), '500px', '300px');
		var formEl = overlayEl.getElementsByTagName("form")[0];
		this.parentEl = formEl;
		
		var inputEls = formEl.getElementsByTagName("input");
		var submitEl = inputEls[inputEls.length - 1];
		
		var modelCb = this.model;
		submitEl.onclick = function() {
			var emailPayload = {
				to : formEl.to.value,
				from : formEl.from.value,
				subject : formEl.subject.value,
				body : formEl.body.value
			};
			modelCb.sendEmail.call(modelCb, emailPayload);
		};
	},
	
    getParent : function() {
    	return this.parentEl;
    },
	
    update : function(eventName, eventData) {
    	var util = HD.util.Common, templates = HD.util.Email.View.templates;
    	if(eventName == "sendEmail_Start") {
    		this.loading(true);
    	} else if(eventName == "sendEmail_Finish") {
    		this.loading(false);
    		util.showOverlay(templates.getSuccessText(), templates.getOverlayTitle(), '500px', '300px');
    		setTimeout('HD.util.Common.hideOverlay()', 3000);
    	} else if(eventName == "sendEmail_Error") {
    		this.loading(false);
    		util.showOverlay(templates.getFailureText(), templates.getOverlayTitle(), '500px', '300px');
    		setTimeout('HD.util.Common.hideOverlay()', 5000);
    	}
    },
	
	getHtml : function() {
		return this.processTemplate(this.config.templates.getHtml());
	}
};

HD.extend(HD.util.Email.View, [HD.Widget]);

HD.util.Email.Controller = function(model, view){
	this.model = model;
	this.view = view;
};

HD.util.Email.Controller.prototype = {
	
};

HD.register("hd_email", HD.util.Email, {version: "1.0", build: "1"});