/* Copyright 2009 Palm, Inc.  All rights reserved. */

var MmiAssistant = Class.create({
	initialize: function(sceneAssistant, message, timeout, timeoutMessage, showCloseButton) {
		
		this.sceneAssistant = sceneAssistant;
		this.controller = this.sceneAssistant.controller;
		this.message = message;
		this.showCloseButton = showCloseButton;
		if (timeout && timeout > 0) {
			this.timeoutMessage = timeoutMessage;
			// timeout if we don't hear from the network in a while
			this.mmiTimeout = window.setTimeout(this.displayTimeoutMessage.bind(this), timeout);
		} 
	},
	
	setup: function(parent){
		
		this.parent = parent;
		this.controller.get('mmiMessage').innerHTML = this.message;
		
		if (this.showCloseButton == false) {
			this.controller.get('closeButton').hide();
		} else {
			this.controller.get('closeButton').show();
		}
		
		Mojo.Event.listen(this.controller.get('closeButton'),'mojo-tap', this.handleClose.bindAsEventListener(this));
		
	},
	
	handleClose: function() {
		
		this.parent.mojo.close();
		window.clearTimeout(this.mmiTimeout);
		this.mmiTimeout = undefined;
		this.sceneAssistant.mmiDialogBox = undefined;
		
	},
	
	displayTimeoutMessage: function() {
		if (this.controller.get('closeButton')) {
			this.controller.get('closeButton').show();
			this.controller.get('mmiMessage').innerHTML = this.timeoutMessage;
		}
	},
	
	updateMessage: function(message, timeoutMessage, timeout) {
		this.message = message;
		this.timeoutMessage = timeoutMessage;
		this.controller.get('mmiMessage').innerHTML = this.message;
		
		if (timeout) {
			window.clearTimeout(this.mmiTimeout);
			this.mmiTimeout = undefined;
			this.mmiTimeout = window.setTimeout(this.displayTimeoutMessage.bind(this), timeout);
		}
		
	}
})