/*
 * Copyright 2009 Palm, Inc. All rights reserved.
 */
/*global Mojo, Class, $L, HelpAssistant, LazyLoadImage, UrlManager, WebViewHelper, XMLHttpRequest, escape */
/*jslint rhino:true, undef:true */

var ArticleAssistant = Class.create(HelpAssistant, {
    initialize: function($super, url) 
    {
        $super();
        this.url = url; 

        var params =
        {
            "url": url,
            "assistant": this
        };

        this.webViewHelper = new WebViewHelper(params);

        // Callback function that is called when the title of the webpage is loaded
        /*
        this.webViewHelper.onTitleSet = 
            function( title ) 
            {
                Mojo.Log.info("Got title notification: " + title );
                this.setTitle( title );
            }.bind(this);
            */

        Mojo.Log.info("ArticleAssistant initialized");
    },
    setup:function() 
    {
        this.base_setup( );

        this.requestUrl();
        
        Mojo.Log.info("ArticleAssistant setup");
    },
    cleanup: function(event) 
    {
        this.base_cleanup();
    },
    activate: function(event) 
    {
        this.base_activate();
        Mojo.Log.info("ArticleAssistant activated");
    },
    ready: function() 
    {
        this.base_ready();
        Mojo.Log.info("ArticleAssistant ready");
    },
    deactivate: function(event) 
    {
        this.base_deactivate();
    },

    requestUrl: function()
    {
        this.xmlHttp = new XMLHttpRequest();
        this.xmlHttp.onreadystatechange = this.httpRequestResponse.bind(this);
        this.xmlHttp.open("HEAD", this.url, true);

        this.xmlHttp.setRequestHeader("X-Palm-Locale", UrlManager.locale);
        //this.xmlHttp.setRequestHeader("X-Palm-Carrier", UrlManager.carrier);
        this.xmlHttp.setRequestHeader("X-Palm-Carrier-Name", Mojo.Environment.DeviceInfo.carrierName);
        this.xmlHttp.setRequestHeader("X-Palm-Device", UrlManager.device);
        this.xmlHttp.setRequestHeader("X-Palm-Device-Name", escape(Mojo.Environment.DeviceInfo.modelName));
        this.xmlHttp.setRequestHeader("X-Palm-Device-Model", UrlManager.deviceModel);

        try
        {
            this.xmlHttp.send();
        }
        catch(e)
        {
            Mojo.Log.info("Failed to send xmLHttpRequest");
        }

    },

    httpRequestResponse: function() 
    {
        if (this.xmlHttp.readyState == 4) 
        {
            try
            {
                if (this.xmlHttp.status==404)
                {
                    Mojo.Log.error("URL does not exist: " + this.url);
                    return;
                }

                /*
                var headers = this.xmlHttp.getAllResponseHeaders();
                Mojo.Log.info("Headers: " + headers );
                     Sample header data:
                     title: TEST - Article Link To Chat
                     app_name: com.palm.app.help
                     icon: file:///usr/palm/applications/com.palm.app.help/chat.png
                     chat: yes
                     chat_entryid:1985920840
                     chat_problem:Other
                 */

                var icon = this.xmlHttp.getResponseHeader( "icon" );
                var title = this.xmlHttp.getResponseHeader( "title" );

                if (!icon && !title)
                {
                    // Hide the header area since we didn't get anything
                    this.controller.get("article-viewer").hide();
                    return;
                }

                this.setIcon( icon );
                this.setTitle( title );

                // Show the header area since we got something
                this.controller.get("article-viewer").show();
            }
            catch(e)
            {
                Mojo.Log.info("Failed to getAllResponseHeaders: " + e);
            }
        }

    },

    setIcon: function( icon )
    {
        // Set the title icon specified in the json object
        if (icon && icon.length)
        {
            Mojo.Log.info("Setting the icon at the top to: " + icon );
            this.titleicon = new LazyLoadImage( icon, this.controller.get("article-title-icon"));
        }
    },

    setTitle: function( title )
    {
        // Set the title specified in the json object
        if (title) 
        {
            Mojo.Log.info("Setting the title at the top to: " + title );
            this.controller.get("article-title").update( title );
        }
    }


});
