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

var DashannounceAssistant = Class.create({
	initialize: function(windowName, single, icon, itemcount, title, message, iconCallback, messageCallback, timestamp) {
	    QDLogger.log( "DashannounceAssistant::initialize");
	   	this.windowName = windowName;
		this.single = single;
		this.icon = icon;
		this.itemcount = itemcount;
		this.title = title,
		this.message = message;
		this.iconCallback = iconCallback;
		if (messageCallback) {
			this.messageCallback = messageCallback;
		} else {
			this.messageCallback = this.iconCallback;
			this.closeOnMessageTap = true;
		}
		if(windowName == 'misseddash' && timestamp)
			this.missedTimeStamp=timestamp;
 	},
  
  setup: function() {
	   QDLogger.log("DashannounceAssistant::setup");
	   if (this.icon === undefined || this.icon === "") {
	   		//this.icon = "notification-large-ignored.png";
			this.icon = "ignored";
	   }
	   /*
	   if (this.single == true) {
	   		this.controller.get('notification').addClassName("single");
	   }
	   */
		this.controller.get('icon-action').addEventListener(Mojo.Event.tap, this.onIconTap.bindAsEventListener(this));
		this.controller.get('text-action').addEventListener(Mojo.Event.tap, this.onMessageTap.bindAsEventListener(this));
	   // build icon
	  // this.controller.get('icon').innerHTML = "<img src=\"images/" + this.icon +"\">";
	   this.controller.get('icon').addClassName(this.icon);
	   this.updateBadge();
	   this.controller.get('title').innerHTML = this.title;
	   this.controller.get('message').innerHTML = this.message;
	   this.controller.listen(this.controller.document, Mojo.Event.activate, this.onFocus.bindAsEventListener(this));
	   this.controller.stageController.indicateNewContent(true);
  },
  onFocus:function(){
  	if (this.missedTimeStamp && this.windowName == 'misseddash') {
		var d = new Date(parseInt(this.missedTimeStamp));
		var msg = Messages.missedCallLabel 
			+ Mojo.Format.formatRelativeDate(d, {date:'short'})
			+ " " + Mojo.Format.formatDate(d, {time:'short'});
		if (this.message !== msg) {
			this.message = msg;
			this.updateMessage(this.title, this.message, this.itemcount);
		}
	}
  },
  deactivate:function(){
	this.controller.stopListening(this.controller.document, Mojo.Event.activate, this.onFocus);
  	
  },

  // run callback and close this
  onIconTap: function () {
	QDLogger.log( "DashannounceAssistant::onTap", "windowName:" , this.windowName);
	if (this.iconCallback) {
		this.iconCallback();
	} else {
		Mojo.Controller.getAppController().closeStage(this.windowName);
	}
  },
  
  onMessageTap: function () {
	QDLogger.log( "DashannounceAssistant::onTap", "windowName:" , this.windowName);
	if (this.messageCallback) {
		this.messageCallback();
	} else {
		Mojo.Controller.getAppController().closeStage(this.windowName);
	}
  },
  
  updateMessage: function(header, body, itemcount, timestamp) {
  	QDLogger.log("DashannounceAssistant::updateMessage");
	if(this.windowName == 'misseddash' && timestamp)
		this.missedTimeStamp=timestamp;

	this.title = header;
	this.controller.get('title').innerHTML = this.title;
	this.message = body;
	this.controller.get('message').innerHTML = this.message;
	this.itemcount = itemcount;
	this.updateBadge();
	this.controller.stageController.indicateNewContent(true);
  },
  
  updateBadge: function() {
  	   if (this.itemcount > 1) {
	   		QDLogger.log("DashannounceAssistant::updateBadge", this.itemcount);
			this.controller.get('notification').removeClassName("single")
			this.controller.get('itemcount').show();
			this.controller.get('itemcounttext').innerHTML = this.itemcount;
	   } else {
	   		this.controller.get('notification').addClassName("single");
	   		this.controller.get('itemcount').hide();
	   }
  }
  
});

