/*
	Small controller class used for the Jumpto dialog .

Copyright 2009 Palm, Inc.  All rights reserved.

*/
var JumptoDialogAssistant = Class.create({
	
	initialize: function(sceneAssistant) {
		this.sceneAssistant = sceneAssistant;
	},
	
	setup : function(widget) {
		this.widget = widget;
		this.dateModel = {
			date: getCurrentDateTime()
		};
   	this.sceneAssistant.controller.setupWidget('jumpto-picker', {label: $L('DATE')}, this.dateModel);
  },
	
	activate: function() {
		this.timePickerChangeHandler = this.timepicker.bindAsEventListener(this);
		this.sceneAssistant.controller.listen('jumpto-picker', Mojo.Event.propertyChange,this.timePickerChangeHandler);			
		this.dateListener = this.handleJumpto.bindAsEventListener(this,'Date'); //do not localize
		this.todayListener= this.handleJumpto.bindAsEventListener(this,'Today'); //do not localize
		this.sceneAssistant.controller.get('Date').addEventListener(Mojo.Event.tap, this.dateListener);
		this.sceneAssistant.controller.get('Today').addEventListener(Mojo.Event.tap, this.todayListener);
		
		this.cancelTapHandler = this.handleCancel.bindAsEventListener(this);
    this.sceneAssistant.controller.get('Cancel').addEventListener(Mojo.Event.tap, this.cancelTapHandler);
	},
	
	deactivate: function() {
		this.sceneAssistant.controller.get('jumpto-picker').stopObserving(Mojo.Event.propertyChange, this.timePickerChangeHandler);
		this.sceneAssistant.controller.get('Date').removeEventListener(Mojo.Event.tap, this.dateListener);
		this.sceneAssistant.controller.get('Today').removeEventListener(Mojo.Event.tap, this.todayListener);
		this.sceneAssistant.controller.get('Cancel').removeEventListener(Mojo.Event.tap, this.cancelTapHandler);
	},
	
	
	handleJumpto:function(event,whereTo){
		// Set up the zoom transition
		var myTransition = this.sceneAssistant.controller.prepareTransition(Mojo.Transition.zoomFade, true); // use the “pop” version of the standard scene transition

		// Close the dialog
		this.widget.mojo.close();
		
		// Set the day to the one chosen by the user
		if(whereTo == 'Today')
			setCurrentDateTime(new Date(), false /*dontNotify*/, true /*scrollToDefault*/);
		else if(whereTo == 'Date')
			setCurrentDateTime(this.changedDate);
			
		// Run the transition
		myTransition.run(function(){Mojo.Log.info("intra-scene transition complete!");});
	},
	
	handleCancel:function(event){
		this.widget.mojo.close();
	},
	
	timepicker: function(event){
		//Mojo.Log.info("picked time"+event.value);
		this.changedDate = event.value;
	},
});