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

var ConfirmlanguageAssistant = Class.create({
  	
	initialize: function(selectedLanguage, countryName) {
	  this.selectedLanguage = selectedLanguage;
	  this.resourcePath = "/usr/palm/applications/com.palm.app.firstuse/resources/";
	  this.locale = Account.language + '_' + Account.country;
	  this.countryName = countryName;
	},
	
	setup: function() {
		if(this.locale.length != 5) {
			this.locale = "en_us";
		}
		if(this.stringTable == null){
			this.stringTable = Mojo.Locale.readStringTable("strings.json", this.locale, this.resourcePath);
		}
    
		this.acceptTapHandler = this.acceptTapHandler.bind(this);
		this.declineTapHandler = this.declineTapHandler.bind(this);				
		this.controller.listen("acceptLanguage", Mojo.Event.tap, this.acceptTapHandler);
		this.controller.listen("declineLanguage", Mojo.Event.tap, this.declineTapHandler);
		
    	this.confirmDiv = this.controller.get("confirmContainer");		
		
		// Listen to ## key presses
		this.controller.listen(this.controller.sceneElement, Mojo.Event.keydown, FirstUseUtil.handleKeyPressEvent.bind(this));
	},
	
	activate: function () {
		var confirmText = this.$L("Confirm #{language}").interpolate({
					language: this.selectedLanguage
		});
		this.controller.get("selectedLanguage").update(confirmText);
		this.confirmDiv.show();
		this.firstTap = false;
	},
	
	$L: function(str){
		return Mojo.Locale.localizeString(str, this.stringTable);
	},
	
  	acceptTapHandler: function(event) {
  		Mojo.Log.info("Set Locale =" + Account.language + "_" + Account.country);
		Mojo.Log.info("----------- this.firstTap ------------", this.firstTap);	
		if(this.firstTap == false) {
			this.firstTap = true;
			this.removeLocalizedStylesheets();
			Mojo.Locale.set(Account.language + '_' + Account.country,Account.country);
			this.setDefaultTimeFormat(); 
		}	
		Mojo.Log.info("----------- push customization ------------");
		
		Account.regionCountryCode=Account.country;
		Account.regionCountryName=this.countryName;
		FirstUseUtil.changeLocale();
		this.controller.stageController.swapScene('customization');
		//Mojo.Controller.stageController.swapScene('regionalsettings', this.currentRegion); 	 
  	},
  
  	setDefaultTimeFormat: function() {
		Mojo.Log.info("----------- setDefaultTimeFormat ------------");	
		var format = 'HH12';
		if (Mojo.Format.isAmPmDefault() == false) {
			format = 'HH24';
		}
		FirstUseUtil.setDefaultTimeFormat(format);
	},
	  
  	declineTapHandler: function(event) {
    	Mojo.Controller.stageController.popScene();
  	},

	cleanup: function() {
		this.controller.stopListening("acceptLanguage", Mojo.Event.tap, this.acceptTapHandler);
		this.controller.stopListening("declineLanguage", Mojo.Event.tap, this.declineTapHandler);			  
	},
	
	removeLocalizedStylesheets: function() {
	    var h = Element.select(this.controller.document, 'head')[0];
	    var links = h.querySelectorAll('link[type="text/css"]');
	    var rx = new RegExp(Mojo.Locale.current+'|'+Mojo.Locale.language+'/|'+Mojo.Locale.language+'/'+Mojo.Locale.region);
	    for (var i=0; i < links.length; i++) {
	        var path = links[i].href;
	        if (path.search(rx) !== -1) {
	            h.removeChild(links[i]);
	        }
	    }
	}
	
});
