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

var RegionalsettingsAssistant = Class.create( {
	
	initialize: function() {
		this.getCurrentLocaleAndRegion();
		
	},
	
	onOffToggleOpt: {
        modelProperty: 'value',
        enableProp: 'enabled',        
    },
    
    autoReplaceToggleModel: {
        value: false,
    },
	
	setup: function() {
		$('currLang').observe(Mojo.Event.tap, this.showLangauges.bindAsEventListener(this));
		$('currRegion').observe(Mojo.Event.tap, this.showRegions.bindAsEventListener(this));
		
		//this.controller.setupWidget('autoReplaceToggle', this.onOffToggleOpt, this.autoReplaceToggleModel);
		//Mojo.Event.listen($('autoReplaceToggle'),'mojo-property-change', this.toggleAutoReplace.bindAsEventListener(this));
		
		this.getCurrentFormatValues();
		
		//App Menu - Remove default items and add only Edit & Help items.
		var appMenuModel = {
	        visible: true,
	        items: [Mojo.Menu.editItem,{label:$L('Help'), command:Mojo.Menu.helpCmd}
	            ]
	        };
		this.controller.setupWidget(Mojo.Menu.appMenu, {omitDefaultItems:true}, appMenuModel);
	},
	
	handleCommand: function(event) {
		if (event.type == Mojo.Event.command) {
			if(event.command == Mojo.Menu.helpCmd) {
				new Mojo.Service.Request('palm://com.palm.applicationManager', {
					method: 'open',
					parameters: {'id': 'com.palm.app.help',params: {'target': 'http://help.palm.com/language/index.html'}}});
			}
		}
	},
	
	/*
	 * Make a mojo service call to get the current locale settings.
	 */	
	getCurrentLocaleAndRegion: function() {
		//Make a mojo service call to get the current locale
		this.currlocaleReq = new Mojo.Service.Request("palm://com.palm.systemservice/", {
		method: 'getPreferences', 
		parameters: {"keys":["locale","region"]},
		onSuccess: function(payload) {
			if(payload.locale) {
				this.currentLocale = payload.locale;
			}
			if(payload.region) {
				this.currentRegion = payload.region;
			}
			/*if(payload.x_palm_smartkey) {
				this.autoReplaceToggleModel.value = payload.x_palm_smartkey;
				this.controller.modelChanged(this.autoReplaceToggleModel);
			}
			
			if(Mojo.Locale.current == "en_us") {
				this.controller.get("autoReplaceGroup").show();
				this.controller.get("autoReplaceInfo").show();
			}*/		
        }.bind(this),
		});
		
		this.getLanguagesList();
		this.getCountryList();		
	},
	
	getLanguagesList: function() {
		this.langRequest = new Mojo.Service.Request("palm://com.palm.systemservice", {
			method: 'getPreferenceValues',
			parameters: {"key":"locale"},
			onSuccess: function(payload) {
					if(!payload.locale)
						return;
					
					for(var i = 0; i<payload.locale.length; i++) {
						if(this.currentLocale && this.currentLocale.languageCode === payload.locale[i].languageCode) {
							this.controller.get('langName').innerHTML = payload.locale[i].languageName;
							for(var j = 0; j< payload.locale[i].countries.length; j++) {
								if(this.currentLocale.countryCode == payload.locale[i].countries[j].countryCode) {
									this.controller.get('countryName').innerHTML = payload.locale[i].countries[j].countryName;
									break;
								}
							}
							break;	
						}
					}					
			}.bind(this),
			onFailure: function(payload) {
							
			}
		});
	},
	
	getCountryList: function() {
		this.regionRequest = new Mojo.Service.Request("palm://com.palm.systemservice", {
			method: 'getPreferenceValues',
			parameters: {"key":"region"},
			onSuccess: function(payload) {
					if(!payload.region)
						return;
					
					for(var i = 0; i<payload.region.length; i++) {
						if(this.currentRegion && this.currentRegion.countryCode === payload.region[i].countryCode) {
							this.controller.get('regionalName').innerHTML = $L(payload.region[i].countryName);
							break;											
						}
						
					}					
			}.bind(this),
			onFailure: function(payload) {
							
			}
		});
	},
	
	
	getCurrentFormatValues: function() {
		var date = new Date();
		this.controller.get('shortDate').innerHTML = Mojo.Format.formatDate(date, {date:'short'});
		this.controller.get('longDate').innerHTML = Mojo.Format.formatDate(date, {date:'long'});
		this.controller.get('shortTime').innerHTML = Mojo.Format.formatDate(date, {time:'short'});
		this.controller.get('formatNumber').innerHTML = Mojo.Format.formatNumber(123456789,2);
		this.controller.get('formatCurrency').innerHTML = Mojo.Format.formatCurrency(123456789,2);
	},
	
	showLangauges: function() {
		this.controller.stageController.pushScene('languagepicker',this.currentRegion,this.currentLocale);
	},
	
	showRegions: function() {
		this.controller.stageController.pushScene('regionpicker',this.currentRegion,this.currentLocale);
	},
	
	toggleAutoReplace: function(event) {
		if(!event)
			return;		
		this.controller.serviceRequest("palm://com.palm.systemservice", {
			method: 'setPreferences',
			parameters: {x_palm_smartkey:event.value}			
		});
	},
	
	cleanup: function() {
		
	},
	
});
