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

var HomeAssistant = Class.create(HelpAssistant, {
    initialize: function($super) 
    {
        $super();
        this.topic = "home";
        this.onSearchTermsChanged = this.onSearchTermsChanged.bind(this);
        this.searchHandler = this.searchHandler.bind(this);
        this.helpListTapHandler = this.helpListTapHandler.bind(this);
    },

    setup : function() 
    {
        try 
        {
            this.base_setup( true );

            this.setupSearchBox('');

            AppAssistant.Network.addNetworkCheckedMethods({
                target: this,
                onNetworkDown: this.base_connectionAlert,
                methods: ["launchTopic", "launchSearch"]
            });

            var listOptions = {
                itemTemplate:'home/item',
                listTemplate:'home/list'
            };
            this.helpTopics = {items:[
                {icon: "topics", title:$L("Tips"), desc:$L("Read short how-tos"), scene:"json", url: UrlManager.getTipsUrl()},
                {icon: "flicks", title:$L("Clips"), desc:$L("Watch short animations"), scene:"json", url:UrlManager.getClipsUrl()},
                {icon: "featured", title:$L("Featured"), desc:$L("Browse featured articles"), scene:"json", url:UrlManager.getFeaturedUrl()}
            ]};

            this.controller.setupWidget("help-list", listOptions, this.helpTopics);

        }
        catch (e) 
        {
            Mojo.Log.logException(e, "home-setup");
        }
    },

    activate: function(event) 
    {
        Mojo.Log.info("Help App.HomeAssistant#activate");
        this.base_activate();
        this.controller.listen('search-terms', Mojo.Event.propertyChange, this.onSearchTermsChanged);
        this.controller.listen('search-button', Mojo.Event.tap, this.searchHandler);

        this.searchBoxModel.value = '';
        this.controller.modelChanged(this.searchBoxModel);
        this.controller.get('search-button').style.visibility = 'hidden';

        this.controller.listen(this.controller.get('help-list'), Mojo.Event.listTap, this.helpListTapHandler);

    },

    ready: function() 
    {
    },

    helpListTapHandler: function(event) 
    {
        var topic = this.helpTopics.items [event.index];
        this.launchTopic( topic );
    },

    launchTopic: function(topic)
    {
        if (topic.scene) 
        {
            this.controller.stageController.pushScene({name: topic.scene},  topic.url, topic.icon);
        }
    },

    deactivate : function(event) 
    {
        this.controller.stopListening(this.controller.get('help-list'), Mojo.Event.listTap, this.helpListTapHandler);
        this.controller.stopListening('search-button', Mojo.Event.tap, this.searchHandler);
        this.controller.stopListening('search-terms', Mojo.Event.propertyChanged, this.onSearchTermsChanged);
    },

    onSearchTermsChanged : function(event) 
    {
        if (event.originalEvent.type == 'keyup' && event.originalEvent.keyCode == Mojo.Char.enter) 
        {
            this.searchHandler(event);
        }
        else if (event.value === '') 
        {
            this.controller.get('search-button').style.visibility = 'hidden';
        }
        else 
        {
            this.controller.get('search-button').style.visibility = 'visible';
        }
    },

    searchHandler: function(event) 
    {
        var terms = this.controller.get('search-terms').mojo.getValue();
        this.launchSearch(terms);
    },

    launchSearch: function(terms)
    {
        this.controller.stageController.pushScene("search", terms, UrlManager.buildSearchQuery(terms), false);
    }


});
