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

var StkAssistant = Class.create({
	initialize: function() {
		QDLogger.log( "StkAssistant::initialize");
		// TODO: put escapeHTML()s throughout the app as appropriate
		this.decodedText = document.baseURI.toQueryParams().decodedText.escapeHTML();
		this.messageId = document.baseURI.toQueryParams().messageId;
		this.delayClear = document.baseURI.toQueryParams().delayClear;
		this.highPriority = document.baseURI.toQueryParams().highPriority;
	},
  
  setup: function() {
  	QDLogger.log("StkAssistant::setup");
		var content = Mojo.View.render({
			object: this,
			template: "stk/stkmessage"
		});
	$('stk_message').update(content);
    $('stkok_button').observe(Mojo.Event.tap, this.stkOk.bindAsEventListener(this));
    $('stkback_button').observe(Mojo.Event.tap, this.stkBack.bindAsEventListener(this));
	if (this.delayClear == "true") {
		if (this.highPriority == "true"){
			QDLogger.log("StkAssistant::setup", "STK Timeout set to 60 seconds");
			this.windowTimeout = setTimeout(this.stkTimeout.bind(this), 60000);							
		} else {
			QDLogger.log("StkAssistant::setup", "STK Timeout set to 15 seconds");
			this.windowTimeout = setTimeout(this.stkTimeout.bind(this), 15000);				
		}

	}	
	
//    $('stktimeout_button').observe(Mojo.Event.tap, this.stkTimeout.bindAsEventListener(this));
  },
  
  stkOk: function() {
	TelephonyCommands.sendStkCommand("ok" );  	
	this.closeWindow();
  },
  
  stkTimeout: function() {
	TelephonyCommands.sendStkCommand("timeout");  	
	this.closeWindow();
  },

  stkBack: function() {
	TelephonyCommands.sendStkCommand("abort");  	
	this.closeWindow();
  },
  
  closeWindow : function() {
  	if (this.windowTimeout != undefined) {
		clearTimeout(this.windowTimeout);
	}
	QDLogger.log("StkAssistant::closeWindow");
	// TODO: fixme
   	Mojo.Controller.getAppController().assistant.telephonyEventListener.stkWindow.close();  	
  }
  
});

