/* Copyright 2009 Palm, Inc.  All rights reserved. */
/*global Mojo, Class, $L */
/*jslint rhino:true, undef:true */

var SlidetextAssistant = Class.create({

	initialize: function(docAssistant) {
		this.docAssistant = docAssistant;
		this.controller = docAssistant.controller;
		this.fileAssistant = this.docAssistant.fileAssistant;
	},

    setup: function() {
        Mojo.Log.info("SlidetextAssistant: setup");

        var slide_number = this.docAssistant.getCurrentPage() + 1;

        this.slide_attributes = {

            title: $L('Jump to slide (#{slideNo} of #{slideCount})').interpolate({slideNo:slide_number, slideCount:this.docAssistant.getPageCount()}),
            fields: [
                      {
                        label: '',
                        property: 'user_input',
                        maxlength: 6
                      }
                    ]
        };

        this.slide_model = { user_input: '' };

        this.controller.setupWidget('slideText',
                                    this.slide_attributes,
                                    this.slide_model);

        this.controller.get('slideText').observe(Mojo.Event.propertyChange,
                               this.propertyChangeHandler.bind(this));

        this.controller.setupWidget(Mojo.Menu.appMenu,
                                    { omitDefaultItems: true },
                                    this.appMenuModel);
    },

    activate: function() {
        //Mojo.Log.info("SlidetextAssistant: activate");
    },

    ready: function() {
        //Mojo.Log.info("SlidetextAssistant: ready");
    },

    deactivate: function() {
        //Mojo.Log.info("SlidetextAssistant: deactivate");
    },

    propertyChangeHandler: function(event) {
        //Mojo.Log.info("SlidetextAssistant: slideChangeHandler");

        var goto_page = event.value;

        if (goto_page == 'NaN') {
            this.controller.stageController.popScene();
        }
        else {
            goto_page--;

            if (goto_page >= this.docAssistant.getPageCount()) {
                goto_page = this.docAssistant.getPageCount() - 1;
			}
            else if (goto_page < 0) {
                goto_page = 0;
			}

            this.controller.stageController.popScene();
            this.fileAssistant.gotoPage(goto_page);
        }
    },

    appMenuModel: {
                    items: [
                             {
                               label: $L('Help...'),
                               command: Mojo.Menu.helpCmd,
                               disabled: true
                             }
                           ]
                  }
});
