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

var PagetextAssistant = Class.create({

    initialize: function( documentAssistant ) {
        this.documentAssistant = documentAssistant;
    },

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

        var page_number = this.documentAssistant.currentpage;

        this.page_attributes = {
            title: $L('Jump To Page (')+page_number+$L(' of ')+this.documentAssistant.pagecount+$L(')'),
            fields: [
                      {
                        label: '',
                        property: 'user_input',
                        maxlength: 6
                      }
                    ]
        };

        this.pageModel = { user_input: '' };

        this.controller.setupWidget('pageText',
                                    this.page_attributes,
                                    this.pageModel);

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

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

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

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

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

        // Pages are 0 based
        var jump_pages = event.value;

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

            if (jump_pages >= this.documentAssistant.pagecount) {
                jump_pages = this.documentAssistant.pagecount;
			}
            else if (jump_pages < 0) {
                jump_pages = 0;
			}

            this.controller.stageController.popScene();
			this.documentAssistant.JumpToPage(jump_pages);
        }
    },

    cleanup: function() {
        //Mojo.Log.info("PagetextAssistant: cleanup");
    },

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

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