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

var PasswordAssistant = Class.create({

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

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

        // Set up the view buttons (header)
        this.viewMenuModel = { visible: true, items: this.viewMenu };
        this.controller.setupWidget(Mojo.Menu.viewMenu,
                                    undefined,
                                    this.viewMenuModel);
        this.controller.setupWidget(Mojo.Menu.appMenu,
                                    { omitDefaultItems: true },
                                    this.appMenuModel);

        this.attributes = {
                            focus: true,
                            multiline: false,
                            requiresEnterKey: true
                          };
        this.model = { value : '' };
        this.controller.setupWidget('pass_entry', this.attributes, this.model);

        //Mojo.Event.listen( this.controller.get('ok_button'), Mojo.Event.tap, this.passwordSubmit.bind(this));
        Mojo.Event.listen( this.controller.get('pass_entry'), Mojo.Event.propertyChange, this.passwordSubmit.bind(this));

    },

    activate: function() {
        //Mojo.Log.info("PasswordAssistant: activate");

        this.controller.get('documentname').innerHTML = this.documentAssistant.document_noext;

        if (this.documentAssistant.wrongpasscount !== 0) {
            this.controller.get('pass_info').innerHTML = $L('Password incorrect!')+'<br /><br />'+$L('Document is password protected...');
        }

        this.password = '';
    },

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

    passwordSubmit: function() {
        //Mojo.Log.info("PasswordAssistant: passwordSubmit");
        //Mojo.Log.info("value = '%s'", this.model.value);
        
        if (this.model.value.length > 0) {

            this.password = this.model.value;

            // Save this to the document assistant
            this.documentAssistant.documentPassword = this.password;

            this.controller.stageController.popScene();

            this.controller.serviceRequest(
                    'palm://com.palm.pdf/events/setPassword',
                    {
                        parameters: {
                                      docid: this.documentAssistant.document_id,
                                      password: this.password
                                    },

                        onSuccess: function(payload) {
                            //Mojo.Log.info("payload: '%s'", payload);
                        },

                        onFailure: function(payload) {
                            Mojo.Log.error("setPassword failed!");
                        }
                    });
        }
    },

    cleanup: function() {
        //Mojo.Log.info("PasswordAssistant: cleanup");
        this.controller.get('pass_entry').stopObserving(Mojo.Event.propertyChange);
    },

    deactivate: function() {
        //Mojo.Log.info("PasswordAssistant: deactivate");

    },

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

    viewMenu: [ { template: 'document/header_bar' } ]
});
