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

var Themes = Class.create({
	themeList: {},
	//load up the list of themes
	initialize: function() {
		this.defaultThemeCookie = new Mojo.Model.Cookie(Themes.kDefaultThemeCookie);
		
		this.loadThemeList();
		
		if (this.defaultThemeCookie.get()) {
			 var theme = this.defaultThemeCookie.get();
			 if (theme) {
				this.theme = theme;
				var themeObject = this.getCurrentTheme();
				if (!themeObject) {
					Mojo.Log.error("Clock: invalid theme in cookie; resetting to default");
					this.resetToDefaultTheme();
				}
			 }
		}else{
			this.resetToDefaultTheme();
		}
	},
	
	resetToDefaultTheme: function() {
		this.theme = Themes.kDefaultTheme;
		this.saveDefaultTheme(this.theme);
	},
	
	loadThemeList: function() {
		//load the list of themes from the json file
		var sourcesText = palmGetResource(Mojo.appPath + Themes.kThemesJSON, true);
		if (sourcesText) {
			this.themeList = Mojo.parseJSON(sourcesText);
		} else {
			Mojo.Log.error("Clock: failed to load theme list.  Probably dying now.");
		}
	},
	
	saveDefaultTheme: function(theme){
		if (this.defaultThemeCookie) {
			//console.log("saved "+theme+" as default theme.")
			this.defaultThemeCookie.put(theme);
		}
		
		this.theme = theme;
	},
	//return a list of all the themes
	getThemes: function() {
		return this.themeList;
	},
	getCurrentTheme : function(){
		return this.getTheme(this.theme);
	},
	//get a theme (returns a package of functions, css and scene)
	getTheme: function(themeName) {
		//get the theme from the list of all themes
		function rightTheme(themeName, theme){
			if(theme.name == themeName){
				//console.log("matching on "+theme.name+" = "+themeName);
				return theme;
			}else{
				return false;
			}
		}
		var theme = this.themeList.find(rightTheme.curry(themeName));
		//set up the theme to pass it back to the scene assistant
		if(theme){
			theme.css = theme.source + theme.name + "-theme.css";
			
			// Actually load the code & eval() it.
			filename = Mojo.appPath+theme.source+theme.name+"-clock-functions.js";
			sourcesText = palmGetResource(filename, true);
			var themeFuncs;
			if(sourcesText) {
				cf = eval(sourcesText);
				themeFuncs = new cf();
				//console.log("script load successful for " + filename);
			} else {
				//console.log("warning, script load failed for " + filename + ", either remove the script or fix the src path.");				
			}

			if(cf){
				if(themeFuncs.updateTime && themeFuncs.alarm && themeFuncs.refresh){
					theme.updateTime = themeFuncs.updateTime;
					theme.alarm = themeFuncs.alarm;
					theme.refresh = themeFuncs.refresh;
					theme.setup = themeFuncs.setup;
					//console.log(themeFuncs.getInterval());
					if(themeFuncs.getInterval()){
						theme.interval = themeFuncs.getInterval();
					}else{
						theme.interval = 1000;
						//console.log("no theme interval set, or theme interval outside acceptable range");
					}
				}else{
					return false;
				}
			}else{
				//console.log("theme functions not defined "+themeName);
				return false;
			}
			
			//theme.scene = Mojo.View.render({template: "../../../"+theme.source+"clock"});
			theme.scene = palmGetResource(Mojo.appPath+theme.source+theme.name +"-clock.html", true);
			//save this as the default theme for next time
			this.saveDefaultTheme(themeName);
			return theme;
		}else{
			//console.log("theme not found \""+themeName+"\"");
			return false;
		}
	},
	
	getNicename: function(nicename) {
		var origNiceName = nicename;
		nicename = nicename.toLowerCase();
		return Themes.localizedStrings[nicename] || origNiceName;
	}
	
});

Themes.kThemesJSON = "themes/themes.json";
Themes.kDefaultThemeCookie = "defaultclocktheme";
Themes.kDefaultTheme = "digital";

// workaround for localization
Themes.localizedStrings = { 
	"digital": $L("Digital"), 
	"analog": $L("Analog")
}
