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

var ThemesAssistant = Class.create({
	initialize: function(themes, onThemeChanged) {
		this.themes = themes;
		this.themeList = themes.themeList;
		
		this.onThemeChanged = onThemeChanged;
	},
	
	setup: function() {
		//this.controller.document.body.removeClassName("clockview");
		////this.controller.document.body.addClassName("palm-dark");
		
		this.themesModel = {listTitle: $L('Themes'), items:this.themeList};
		
		this.controller.setupWidget('themes',
		            				{
		                       			itemTemplate: 'themes/example',
		                        		renderLimit: 80,
										formatters: { "nicename" : this.nameLocalizer.bind(this)}
		            				},
		            				this.themesModel);
		this.controller.listen('themes', Mojo.Event.listTap, this.handleListTap.bindAsEventListener(this));
		var appMenuModel = {
			visible: true,
			items: [
				Mojo.Menu.editItem,
				{label:$L('Help'), command:Mojo.Menu.helpCmd, disabled:false}
			]
		};
		this.controller.setupWidget(Mojo.Menu.appMenu, {omitDefaultItems: true}, appMenuModel);
	},
	
	// workaround to localize name
	nameLocalizer: function(nicename, model) {
		if (!nicename)
			return;
		
		model.nicename = this.themes.getNicename(nicename);
	},
	
	cleanup: function() {
		////this.controller.document.body.removeClassName("palm-dark");
		//this.controller.document.body.addClassName("clockview");
	},
	
	handleListTap: function(listTapEvent) {
		var theme = listTapEvent.item;
		var name = theme.name;
		var source = theme.source;
		//console.log(name + " " + source);	
		this.themes.saveDefaultTheme(name);
		this.onThemeChanged(name);
		listTapEvent.stop();
		this.controller.stageController.popScene();
	},

 	 deactivate : function() {
		//$$('body')[0].removeClassName('palm-dark');
	  },
	
	handleCommand: function(event){
		if (event.type == Mojo.Event.command && event.command == Mojo.Menu.helpCmd) {
			this.controller.serviceRequest("palm://com.palm.applicationManager", {
			  method: "open",
			  parameters: {
			      id: 'com.palm.app.help',
			      params: {
			          target: "http://help.palm.com/clock/index.html"
			      }
			  }
			});
		} else if (event.type == Mojo.Event.commandEnable && event.command == Mojo.Menu.helpCmd) {
			event.stopPropagation();
		}
	}
});