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

var TimezoneAssistant = Class.create({
	
	initialize: function(dtconfigassistant) {
		this.dtconfigassistant = dtconfigassistant;
		this.getTimeZoneList();		
	},
	
	setup: function() {
		this.tzListModel = {
            items: this.tzList		  
        };
		       
        this.template = {
			listTemplate: 'timezone/listcontainer',
            itemTemplate: 'timezone/listitem', 
			filterFunction: this.filterTimezones.bind(this),           		          
        };		

        this.controller.setupWidget('tzlist', this.template, this.tzListModel);
		this.timezoneListWidget = $('tzlist');
		
		//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);	
	
	},
	
	activate: function() {		
		$('tzlist').observe(Mojo.Event.listTap, this.handleTap.bindAsEventListener(this));		
	},
	
	cleanup: function() {
		this.tzList.clear();
		this.timezoneListWidget.mojo.noticeRemovedItems(0,this.tzList.length);
	},
	
	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/date_time/index.html'}}});
			}
		}
	},
	
	tzList: [],
	
	getTimeZoneList: function() {		
		this.timeZoneQuery = this.timeZoneQuery.bind(this);
		this.getTimeZoneValuesReq = SystemService.getTimeZoneValues(this.timeZoneQuery);		
	},
	
	timeZoneQuery: function(payload) {
		if(!payload.timeZone)
			return;		
		
		for (var i = 0; i < payload.timeZone.length; i++) {
			var obj = {};
			obj.Country = $L(payload.timeZone[i].Country);
			obj.City = $L(payload.timeZone[i].City);
			obj.CountryAndCity = $L(payload.timeZone[i].Country) +' '+ $L(payload.timeZone[i].City);
			obj.Description = $L(payload.timeZone[i].Description);
			obj.originalItem = payload.timeZone[i];
			this.tzList.push(obj);
		}
		this.tzList.sort(this.compareCountry.bind(this));
		this.tzListModel.items = this.tzList;
		this.controller.modelChanged(this.tzListModel);
		this.timezoneListWidget.mojo.setLength(this.tzList.length);
		this.timezoneListWidget.mojo.setCount(this.tzList.length);		
	},
	
	compareCountry: function(a,b) {
		var nameA = a.CountryAndCity.toLowerCase();
		var nameB = b.CountryAndCity.toLowerCase();
		if (nameA < nameB) {return -1}
		if (nameA > nameB) {return 1}
		return 0;
	},
	
	filterTimezones: function(filterString, listWidget, offset, count) {
		
		var matching, lowerFilter;
		if (filterString) {
			lowerFilter = filterString.toLocaleLowerCase();
			function matchesName (tzone) {
				return tzone.Country.toLocaleLowerCase().startsWith(lowerFilter) || tzone.City.toLocaleLowerCase().startsWith(lowerFilter);
			}
			function matchesCityName(tzone) {
				return tzone.City.toLocaleLowerCase().startsWith(lowerFilter);
			}
			matching = this.tzList.findAll(matchesName);
		} else {
			matching = this.tzList;
		}
		this.tzListModel.items = matching;	
		listWidget.mojo.noticeUpdatedItems(0, matching);		
		listWidget.mojo.setLength(matching.length);
		listWidget.mojo.setCount(matching.length);
	},
	
	handleTap: function(event) {
		if(!event.item)
			return;		
		this.setTimeZoneReq = SystemService.setTimeZone(event.item.originalItem);
		this.dtconfigassistant.delayedSelectedTimeZone(event.item.originalItem,true);
		Mojo.Controller.stageController.popScene('timezone');
	},
	
});
