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

var TermsAssistant = Class.create({
	initialize: function(){
		$('acceptTerms').addEventListener(Mojo.Event.tap, this.showTutorial.bindAsEventListener(this))
		$('declineTerms').addEventListener(Mojo.Event.tap, this.declineTerms.bindAsEventListener(this));
    	this.expanded = false;
		FirstUseUtil.addDashboardBig($('target'));
		saveCurrentState("terms");
	},
	
	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-box', {});  
		this.controller.setupWidget('terms-text', {});
		
		FirstUseUtil.initValues();
		
		this.termsExpand = this.controller.get('terms-expand-div');
		this.termsCollapse = this.controller.get('terms-collapse-div');
		this.termsBox = this.controller.get('terms-box');
		this.privacyDiv = this.controller.get('palm-privacy-policy');
			
		this.expandTermsFunction = this.expandTermsFunction.bind(this);
		this.termsBox.addEventListener(Mojo.Event.tap, this.expandTermsFunction);
		this.termsBox.addEventListener(Mojo.Event.flick, this.expandTermsFunction);		
		this.privacyDiv.addEventListener(Mojo.Event.tap, this.showPrivacyPolicy.bind(this));
	},
	
	showPrivacyPolicy: function (event) {
		var dialog = this.controller.showDialog({
			template: 'terms/privacy-dialog',
			assistant: new DialogAssistant(this),			
		});	 
		dialog.observe(Mojo.Event.keydown, FirstUseUtil.handleKeyPressEvent.bind(this)); 
	},	
	
  expandTermsFunction: function (event) {
		if ((event.target.nodeName === "A" && this.expanded)
				|| (event.target.id === "palm-privacy-policy")) {
			return;
		}

		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');
	},
		
	handleCommand: function(event) {
		if (event.type == Mojo.Event.back) {
			event.preventDefault();
			event.stopPropagation();
			if(Account.platformType.toLowerCase() == 'gsm') {
				Mojo.Log.info("------------ Pop to language scene on back -----------");
				Mojo.Controller.stageController.popScenesTo();
				Mojo.Controller.stageController.pushScene("language");		
			}
		}	
	},	
	
	showTutorial: function(event){
		Mojo.Controller.stageController.popScene();
		Mojo.Controller.stageController.pushScene('intro');
	},
	
	declineTerms: function(event){
		var dialog = this.controller.showDialog({
			template: 'terms/decline-dialog',
			assistant: new DeclineDialogAssistant(this),			
		});	  
		
		dialog.observe(Mojo.Event.keydown, FirstUseUtil.handleKeyPressEvent.bind(this));
	}
	
});

var DialogAssistant = Class.create({
	
	initialize: function(sceneAssistant) {
		this.sceneAssistant = sceneAssistant;
		this.controller = sceneAssistant.controller;	
    
	},
	
	setup : function(widget) {
		this.widget = widget;
		this.dismissButton = this.controller.get("dismissButton");
    
    this.dismissButton.addEventListener(Mojo.Event.tap, this.closeDialog.bind(this));
	},	
	
  closeDialog: function(event) {
    this.widget.mojo.close();
  },

});

var DeclineDialogAssistant = Class.create({
  
	initialize: function(sceneAssistant) {
		this.sceneAssistant = sceneAssistant;
		this.controller = sceneAssistant.controller;	
	},
	
	setup: function(widget) {
		this.widget = widget;
		this.acceptButton = this.controller.get("acceptButton");
		this.declineButton = this.controller.get("declineButton");    
	    this.acceptButton.addEventListener(Mojo.Event.tap, this.acceptTerms.bind(this));
	    this.declineButton.addEventListener(Mojo.Event.tap, this.declineTerms.bind(this));    
	},	
	
  acceptTerms: function(event) {
    Mojo.Controller.stageController.pushScene('intro');    
  },

  declineTerms: function(event) {
    Mojo.Controller.stageController.pushScene('language');    
  },

});	