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

var LocationTermsAssistant = Class.create({
	
	initialize: function(){
		FirstUseUtil.addDashboardSmall($('target'));
		saveCurrentState("location-terms");
		this.dataPreference = true;
		this.expanded = false;
	},
	
	setup: function(){
		$('target').addEventListener(Mojo.Event.tap, FirstUseUtil.tapHandler.bindAsEventListener(this));
		
		// Listen to ## key presses
		this.controller.listen(this.controller.sceneElement, Mojo.Event.keydown, FirstUseUtil.handleKeyPressEvent.bind(this));
		
		this.controller.setupWidget('terms-text', {});
		
		this.dataAttr = {
			property: "setDataCollection",
			trueValue: "ON",
			falseValue: "OFF",
			fieldName: 'checkboxstuff'
		};
		this.dataModel = {
			value: "ON",
			disabled: false
		};

		this.controller.setupWidget('setBackgroundDataCollection', this.dataAttr, this.dataModel);
		$('setBackgroundDataCollection').observe(Mojo.Event.propertyChange, this.setBackgroundDataCollectionPrefs.bind(this));
		
	    this.termsExpand = this.controller.get('terms-expand-div');
	    this.termsCollapse = this.controller.get('terms-collapse-div');
	    this.termsBox = this.controller.get('terms-box');
	     
	    this.expandTermsFunction = this.expandTermsFunction.bind(this);
	    this.termsBox.addEventListener(Mojo.Event.tap, this.expandTermsFunction);
	    this.termsBox.addEventListener(Mojo.Event.flick, this.expandTermsFunction);	    
		
		this.buttonAttr = {
			disabledProperty: 'disabled'
		};
		
		this.buttonModel = {
			disabled: false,
			buttonLabel : $L('Agree'),
			buttonClass: 'palm-button'
		};
		
		this.controller.setupWidget('acceptTerms', this.buttonAttr, this.buttonModel);
		Mojo.listen(this.controller.get('acceptTerms'), Mojo.Event.tap, this.acceptTerms.bindAsEventListener(this));
		
		this.rejectButtonAttr = {
			disabledProperty: 'disabled'
		};
		
		this.rejectButtonModel = {
			disabled: false,
			buttonLabel : $L("Don't agree"),
			buttonClass: 'palm-button'
		};
		
		this.controller.setupWidget('declineTerms', this.rejectButtonAttr, this.rejectButtonModel);
		Mojo.listen(this.controller.get('declineTerms'), Mojo.Event.tap, this.declineTerms.bindAsEventListener(this));
		
	},
	
	expandTermsFunction: function (event) {    
    if (this.expanded) {
      this.toggleTerms();
	  this.controller.sceneScroller.mojo.scrollTo(undefined, 0, false);
      this.termsBox.addEventListener(Mojo.Event.flick, this.expandTermsFunction);
    } else {
      this.termsBox.removeEventListener(Mojo.Event.flick, this.expandTermsFunction);
      this.toggleTerms();
    }
  },	
	
	toggleTerms: function() {
    	this.expanded = !this.expanded;
		this.termsExpand.toggle();
    	this.termsCollapse.toggle();
    	this.termsBox.parentNode.toggleClassName('expanded');
	},
	
	setBackgroundDataCollectionPrefs: function(event) {
		if ("ON" == event.value) {
			this.dataPreference = true;
		} else {
			this.dataPreference = false;
		}	
		Mojo.Log.info("Set background data collection ==== ", this.dataPreference);
	},
				
	acceptTerms: function(event){
		this.buttonModel.disabled = true;
		this.controller.modelChanged(this.buttonModel);
		this.locationPreference = true;
		AppAssistant.locationService.acceptTermsOfUse(
							this.dataPreference,
							this.successAcceptTerms.bind(this), 
							this.errorAcceptTerms.bind(this), this.controller);
		
	},
	
	successAcceptTerms: function (response) {
		Mojo.Controller.stageController.popScene();
		Mojo.Controller.stageController.pushScene('location-auto-enable');
	},
	
	errorAcceptTerms: function(response) {
		Mojo.Log.error("Error in setting terms acceptance %o", $H(response));
		Mojo.Controller.stageController.popScene();
		Mojo.Controller.stageController.pushScene('location-auto-enable');
	},
	
	declineTerms: function(event){
		var message = $L("Your phone will not be able to use Google's location service, which can provide location information more quickly than GPS alone.");
		
		var dialog = this.controller.showAlertDialog({
			onChoose: function(value){
				if(value == "back") {
					
				} else if (value == "decline") {
					AppAssistant.locationService.rejectTermsOfUse(this.controller);
					Mojo.Controller.stageController.popScene();
					Mojo.Controller.stageController.pushScene('location-auto-enable');
				}
			},
			title: $L("Google Location Services Off"),
			message: message,
			choices: [
				{label:$L('Back'), value:"back"},  
        		{label:$L("Next"), value:"decline"}
			]
		});
		
		dialog.observe(Mojo.Event.keydown, FirstUseUtil.handleKeyPressEvent.bind(this));
	}
	
});	