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

var OutlineAssistant = Class.create({

	initialize: function(docAssistant, outlineArray) {
		this.docAssistant = docAssistant;
		this.controller = docAssistant.controller;
		this.listArray = outlineArray;
		this.outline_timeout = undefined;
		this.typetext = undefined;
		this.buildOutline = this.buildOutline.bind(this);
	},

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

        if (this.docAssistant.document_type == 'xls') {
            this.listHeader = $L('Sheets');
		}
        else if (this.docAssistant.document_type == 'ppt') {
            this.listHeader = $L('Outline');
		}
        else {
            this.listHeader = $L('Outline not supported');
		}

        // Setting up scroll widget for notes dialog
        this.scrollModel = { scrollbars: false, mode: 'vertical' };

        this.controller.setupWidget('outlineScroll', {}, this.scrollModel);

        // Setting up the list widget for the outline
        this.outlineModel = { items:this.listArray };

        this.controller.setupWidget('OutlineList',
                                    {
                                      itemTemplate:'outline/listitem',
                                      listTemplate:'outline/listcontainer'
                                    },
                                    this.outlineModel);

        this.controller.get('OutlineList').observe(Mojo.Event.tap,
				this.listTapHandler.bindAsEventListener(this));

        this.currentModel = this.outlineModel;

        // Setup the outline filter field
        var attr = { filterFieldName: 'name' };

        this.controller.setupWidget('filterField', attr, null);

		var filterField = this.controller.get('filterField');
		filterField.addEventListener(Mojo.Event.filter, this.filterHandler.bind(this), false);

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

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

        this.docAssistant.hideElement('topHead');
        this.docAssistant.hideElement('PlugIn');
        this.docAssistant.hideElement('botFoot');
    },

    ready: function() {
        //Mojo.Log.info("OutlineAssistant: ready");

        this.controller.get('outline-title').innerHTML = this.listHeader;

        /*
         * Seems to behave really bad if the list is large so we are going to
         * remove this feature if there is more than 100 items in the list
         */

        if (this.docAssistant.getPageCount() < 100) {
            this.controller.get('OutlineList').mojo.revealItem(this.docAssistant.getCurrentPage(), true);
		}
    },

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

        this.docAssistant.showElement('botFoot');
        this.docAssistant.showElement('PlugIn');
        this.docAssistant.showElement('topHead');
    },

    listTapHandler: function(event) {
        //Mojo.Log.info("OutlineAssistant: listTapHandler");

        // Figure out which page they want to jump to
        var new_page = this.controller.get(event.target).readAttribute('x-app-page') - 1;

		this.docAssistant.fileAssistant.gotoPage(new_page);

        this.controller.stageController.popScene();
    },

    filterHandler: function(event) {
        Mojo.Log.info("OutlineAssistant: filterHandler, text:'%s'", event.filterString);

        this.typetext = event.filterString;

        // Only re-build the outline if we are not the first time into here
        this.outline_timeout = this.controller.window.setTimeout(this.buildOutline, 400);
    },


    buildOutline: function() {
        Mojo.Log.info("OutlineAssistant: buildOutline");

        var outlineArray = [];

        this.controller.window.clearTimeout(this.outline_timeout);

        var user_text = this.typetext.toUpperCase();

        for (var i = 0; i < this.listArray.length; i++) {
            if (this.listArray[i].data.toUpperCase().indexOf(user_text) !== -1) {
                outlineArray.push(this.listArray[i]);
			}
        }

		var matches = outlineArray.length;
        // If we are empty then show something special
        if (outlineArray.length === 0) {
            outlineArray.push({
                                list: undefined,
                                data: $L('( No results found )'),
                                listitem: 1,
                                status: '&nbsp;'
                              });
        }

        this.outlineModel.items = outlineArray;

        this.controller.modelChanged(this.outlineModel);
		this.controller.get('filterField').mojo.setCount(matches);
    },

    printOutline: function(pArray)
    {
        //Mojo.Log.info("OutlineAssistant: printOutline");

        for (var i = 0; i < pArray.length; i++)
        {
            //Mojo.Log.info("pArray[%d].list = '%s'", i, pArray[i].list);
            //Mojo.Log.info("pArray[%d].data = '%s'", i, pArray[i].data);
            //Mojo.Log.info("pArray[%d].listitem = '%s'", i, pArray[i].listitem);
            //Mojo.Log.info("pArray[%d].status = '%s'", i, pArray[i].status);
        }
    },

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