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

var IntroAssistant = Class.create({
	
	initialize: function(){
		$('tellMeMore').observe(Mojo.Event.tap, this.showAccountInfo.bindAsEventListener(this));
		$('newAccount').observe(Mojo.Event.tap, this.createAccount.bindAsEventListener(this));
		$('signin').observe(Mojo.Event.tap, this.login.bindAsEventListener(this));
		saveCurrentState("intro");
	},
	
	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));
		FirstUseUtil.addDashboardBig($('target'));

    	this.animateToSmallDashboard.bind(this).delay(1);
	},
	
	handleCommand: function(event) {
		if (event.type == Mojo.Event.back) {
			event.preventDefault();
			event.stopPropagation();
		}	
	},
	
	showAccountInfo: function(event){
		var dialog = this.controller.showDialog({
			template: 'accountinfo/accountinfo',
			assistant: new AccountinfoAssistant(this)
		});
		dialog.observe(Mojo.Event.keydown, FirstUseUtil.handleKeyPressEvent.bind(this));
	},
	
	createAccount: function(event){
			
		//Clear all values only if user has chosen a new path
		if (Account.action == "login") {
			this.clearAccountValues();
		} else {
			Account.password = "";
		}	
		Account.action = "create";
		// Check if device is roaming
		if(RoamUtil.enableRoaming() === true) {
			RoamGuardDialog.turnOffRoamguard(
			{
				"sceneController": this.controller,
				"method": this.pushName.bind(this)
			});
			return;
		}
		this.pushName();
	},
	
	pushName: function() {
		Mojo.Controller.stageController.pushScene('name');
	},
	
	login: function(event){
		//Clear all values only if user has chosen a new path
		if(Account.action == "create") {
			this.clearAccountValues();
		} else {
			Account.password = "";
		}
		Account.action="login";
		
		// Check if device is roaming
		if(RoamUtil.enableRoaming() === true) {
			RoamGuardDialog.turnOffRoamguard({
				"sceneController": this.controller,
				"method": this.login.bind(this)
			});
			return;
		}
		Mojo.Controller.stageController.pushScene('login');
	},
		
	clearAccountValues: function() {
		console.log("--------------- clear account values --------------");
		Account.email = "";
		Account.password = "";
	
		Account.firstName = "";
		Account.lastName = "";
	
		Account.questionId = "-1";
		Account.questionText = "";
	
		Account.response = "";
		Account.loginAttemptCount = 0;
	}, 
	
	animateToSmallDashboard: function() {
    	this.controller.get("dashboardContent").hide();
	  
    	var bottomCorners = this.controller.get("bottomCorners");
    	Mojo.Animation.animateStyle(bottomCorners, 'bottom', 'bezier', {
      									  from: 48,
      									  to: 24,
									      duration: 0.3,
									      curve: "ease-in-out",
									      onComplete: this.showPhoneBanner.bind(this, "bottomCorners")
    	});
    
    	var dashboardDiv = this.controller.get("dashboardBig");
	  	Mojo.Animation.animateStyle(dashboardDiv, 'bottom', 'bezier', {
	    	from: 0,
	   		to: -24,
	    	duration: 0.3,
	    	curve: "ease-in-out",
      		onComplete: this.showPhoneBanner.bind(this, "dashboardBig")
	  	});    

	},
	
	showPhoneBanner: function(fromWhere) {
	    if (this.fromWhere && this.fromWhere !== fromWhere) {
			this.controller.get("phoneBannerIcon").show();      
	      	delete this.fromWhere;
	    } else {
	      	this.fromWhere = fromWhere;
	    }
    
	}
		
}); 

