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

var FirstAssistant = Class.create({

    initialize: function(docAssistant) {
        //Mojo.Log.info("FirstAssistant: initialize");
		this.appAssistant = Mojo.Controller.appController.assistant;
		this.docAssistant = docAssistant;
		this.controller = docAssistant.controller;

        this.buttonTitle = $L('Browse...');
        this.fileExists = false;

        if (docAssistant.filename !== undefined && docAssistant.filename != 'unspecified file')
        {
            this.fileExists = true;

            var last_index = docAssistant.filename.lastIndexOf('/');

            if (last_index != -1) {
                this.buttonTitle = $L('Open ') + docAssistant.filename.substring(last_index+1);
			}
        }
    },

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

        this.controller.get('browse_button').observe(Mojo.Event.tap,
            this.doBrowse.bindAsEventListener(this));

        this.controller.get('info_button').observe(Mojo.Event.tap,
            this.doInfo.bindAsEventListener(this));

        this.controller.get('buy_button').observe(Mojo.Event.tap,
            this.doBuy.bindAsEventListener(this));

        this.controller.get('trial_button').observe(Mojo.Event.tap,
            this.doTrial.bindAsEventListener(this));

		var appMenuModel = {
			items: [
					 {
					   label: $L('Help...'),
					   command: Mojo.Menu.helpCmd,
					   disabled: true
					 }
				   ]
			};
        this.controller.setupWidget(Mojo.Menu.appMenu,
                                    { omitDefaultItems: true },
                                    this.appMenuModel);
    },

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

        this.controller.get('first_button').innerHTML = this.buttonTitle;
    },

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

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

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

    doBrowse: function(event) {
        Mojo.Log.info("FirstAssistant: doBrowse");

        // Push the recent scene
        if (!this.fileExists) {
            var params = {
                // Only show files
                kind: 'file',

                // Get the following extensions
                extensions: this.appAssistant.fileExtensions,

                onValidate: function(file) {
                    //Mojo.Log.info("onValidate: '%s'", file.fullPath);

                    return true;
                },

                onSelect: function(file) {
                    Mojo.Log.info("onSelect: '%s'", file.fullPath);

                    if (this.appAssistant.checkFileType(file.fullPath)) {
                        this.docAssistant.openFile(file.fullPath);
                    }
                }.bind(this)
            };

            Mojo.FilePicker.pickFile(params, this.controller.stageController);
        }
        else {
            this.docAssistant.openFile(this.docAssistant.filename);
        }
    },

    doInfo: function(event) {
        //Mojo.Log.info("FirstAssistant: doInfo");
    },

    doBuy: function(event) {
        //Mojo.Log.info("FirstAssistant: doBuy");
    },

    doTrial: function(event) {
        //Mojo.Log.info("FirstAssistant: doTrial");
    }
});
