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

var TutorialAssistant = Class.create({
  initialize : function() {
  	saveCurrentState("tutorial");
  },
  
  activate: function() {
	this.blockRequest = AppAssistant.accountService.blockPowerKey();
  },
  
  setup: function() {  
  
	// Listen to ## key presses
	this.controller.listen(this.controller.sceneElement, Mojo.Event.keydown, FirstUseUtil.handleKeyPressEvent.bind(this));
	
	this.displayRequest = AppAssistant.accountService.turnOffScreenTransitions();	
	
	var mediaExtensionLib = MojoLoader.require({
                                                  name: "mediaextension",
                                                  version: "1.0"
                                               })["mediaextension"];
	this.videoPlayer = this.controller.get('player');
	this.mediaExtensions = mediaExtensionLib.MediaExtension.getInstance(this.videoPlayer);
	this.mediaExtensions.audioClass = "media";
	if (this.videoPlayer.palm) {
		this.videoPlayer.palm.audioClass = "media";
		if (this.videoPlayer.palm.connected) {
			this.initPlayer();
		} else {
			this.videoPlayer.addEventListener('x-palm-connect', this.initPlayer.bind(this));
		}
	}
	else {
		this.initPlayer();
	}    
	this.videoPlayer.addEventListener('error', this.doneHandler.bind(this));
	this.pauseEventHandler = this.pauseHandler.bind(this);
	this.videoPlayer.addEventListener('pause', this.pauseEventHandler);        
  },
  
  initPlayer: function (event) {
  	this.videoPlayer.src = "/usr/palm/applications/com.palm.app.firstuse/videos/HaveFun.mp4";
	this.videoPlayer.autoplay = true;
	this.controller.enableFullScreenMode(true);
	this.videoPlayer.addEventListener ('ended', this.doneHandler.bind(this));
	this.videoPlayer.addEventListener('x-palm-disconnect', this.doneHandler.bind(this));
  },
  
  handleCommand : function(event) {
		if (event.type == Mojo.Event.back) {
			event.stop();
            event.preventDefault();
            event.stopPropagation();
		}
  },
  
  pauseHandler: function(event) {
  	Mojo.Log.info("--------------- video paused ------------------");
	this.resumeRequest = setTimeout(this.resumeVideo.bind(this), 500);
  },
  
  resumeVideo: function () {
  	clearTimeout(this.resumeRequest);
	Mojo.Log.info("--------------- video resumed ------------------");
  	this.videoPlayer.play();
  },
  
  doneHandler: function(){
  	Mojo.Log.info("--------------- video ended ------------------");
	this.videoPlayer.removeEventListener('pause', this.pauseEventHandler);
  	Mojo.Controller.stageController.pushScene({name:"end-one",transition:Mojo.Transition.crossFade});										
  },
	
  readNotificationStatus: function (response) {
		if(response.errorCode) {
			Mojo.Log.error("--------- Could not notify provisioning to process NIDC ---------", Object.toJSON(response));
		} else {
			Mojo.Log.info("--------- Notified provisioning to process NIDC ---------", Object.toJSON(response));
		}
		FirstUseUtil.saveDoneState();
	},
		
	deactivate: function () {
		if(this.blockRequest) {
			this.blockRequest.cancel();
		}
	}  
})

