/* Copyright 2007 You may not modify, use, reproduce, or distribute this software except in compliance with the terms of the License at:
 http://developer.sun.com/berkeley_license.html
 $Id: component.js,v 1.0 2007/04/15 19:39:59 gmurray71 Exp $
*/
dojo.require("dijit.Tooltip");

// define the namespaces
jmaki.namespace("jmaki.widgets.dojo.dijit.tooltip");

/**
 * @constructor
*/
jmaki.widgets.dojo.dijit.tooltip.Widget = function(wargs) {
    var uuid = wargs.uuid;
    var _widget = this;

    var delay = "400";        
    var startHeight=50;
    var startWidth=90;
    var overflow = "hidden";
    var useIframe = false;
    var autosize = false;
    var overflowY = "hidden";
    var overflowX = "hidden"
    var show = false;
    var ttContents = "this is a tooltip";
    var ttConnectId;
    
    var publish="/dojo/dijit/tooltip";
    var subscribe=["/dojo/dijit/tooltip","/tooltip"];
    
    _widget.container = document.getElementById(uuid);
    jmaki.debug=true;
    jmaki.debugGlue=true;

    /**
     *  Set the content of the tooltip.
     *
     *
     * @param o - The targetId of the item to remove or an object containing a
                  message.targetId property like:
                  { messsage : {value : 'Some new content'}}
     *
     */
    this.setContent = function(o) {
        var content;
        if (o.message) o = o.message;
        if (o.value)content = o.value;
        else content = o;        
        if (content) {
            _widget.dcontainer.setContent(content);
        }
    };

    /**
     * Set the included page of the tooltip
     *
     * @param o - The value (url) to include
                  message.targetId property like:
                  { messsage : {value : 'foo.jsp'}}
     *
     */
    this.setInclude = function(o) {
        var include;
        if (o.message) o = o.message;
        if (o.value)include = o.value;
        else include = o;
        
        if (include) {
            -widget.dcontainer.loadURL(include);
        } 
    };
    
    /**
     *  Show the tooltip.
     *
     */
    this.show = function(){
        _widget.wrapper.open();
    };
    
    /**
     *  Show the tooltip.
     *
     */
    this.hide = function(){
        _widget.wrapper.close();        
    };
        
    /**
     * init  create the widget 
     */
    function init() {
        var _m = _widget.model;      
        var label = _m.label;
        var _content = _m.content;
        var _include = _m.include;

        // take the model or default to args properties

        var _overflow = (_m.overflow) ? _m.overflow : overflow;
        var _overflowY = (_m.overflowY) ? _m.overflowY : overflowY;
        var _overflowX = (_m.overflowX) ? _m.overflowX : overflowX;
        var _startHeight = (_m.startHeight) ?  _m.startHeight : startHeight;
        var _startWidth =(_m.startWidth) ? _m.startWidth : startWidth;
        var _autosize = (_m.autosize) ? _m.autosize : autosize;   

        var _pane = document.getElementById( wargs.uuid + "_content");
       
        _widget.dcontainer = new jmaki.DContainer(
            {target:  wargs.uuid + "_content",
             useIframe : _widget.model.iframe,
             overflow : _overflow,
             overflowY : _overflowY,
             overflowX : _overflowX,
             content : _content,
             startHeight : _startHeight,
             startWidth : _startWidth,
             autosize : _autosize}); 
           
        var _contentPane = document.getElementById(wargs.uuid + "_wrapper");
        
        if (!ttConnectId)ttConnectId=wargs.uuid+"content";
        if (_include)_widget.dcontainer.loadURL(_include);
         _widget.wrapper = new dijit.Tooltip({label:_content, showDelay: delay, connectId:[ttConnectId]} );

// reparent the domNode 
        // this allows us to not have to set style on the body.
        _widget.container.appendChild(_widget.wrapper.domNode);

    }
    
    function doSubscribe(topic, handler) {
        var i = jmaki.subscribe(topic, handler);
        _widget.subs.push(i);
    }
    
    this.destroy = function() {
        for (var i=0; _widget.subs && i < _widget.subs.length; i++) {
            jmaki.unsubscribe(_widget.subs[i]);
        }
    };
    
    
    this.postLoad= function() {
        if (wargs.args){
            if (wargs.args.showDelay){
                showDelay=wargs.args.showDelay;
            }
            if (wargs.args.connectId){
                ttConnectId = wargs.args.connectId;
            } else {
                jmaki.log("Tooltip not connected to an element on the page");
            }
            if (wargs.args.height){
                startHeight = wargs.args.height;
            }
            if (typeof wargs.args.width != 'undefined'){
                startWidth = wargs.args.width;
            }   
            if (wargs.args.overflow){
                overflow = wargs.args.overflow;
            }   
            if (wargs.args.iframe){
                useIframe = (wargs.args.iframe == true);
            }

            if (wargs.args.autosize) {
                autosize = wargs.args.autosize;
            }

            if (wargs.args.overflowX) {
                overflowX = wargs.args.overflowX;
                if (!overflow) overflow = 'auto';
            }
            if (wargs.args.overflowY) {
                overflowY = wargs.args.overflowY;
                if (!overflow) overflow = 'auto';
            }
            if (wargs.args.show) {
                show = wargs.args.show;
            }   
        }   

        if (wargs.publish) publish=wargs.publish;
        
        if (wargs.subscribe){
            if (typeof wargs.subscribe == "string") {
                subscribe = [];
                subscribe.push(wargs.subscribe);
            } else {
                subscribe = wargs.subscribe;
            }
        }        
        _widget.subs = [];
        
        for (var _i=0; _i < subscribe.length; _i++) {
            doSubscribe(subscribe[_i]  + "/setContent", _widget.setContent);
            doSubscribe(subscribe[_i]  + "/setInclude", _widget.setInclude);
            doSubscribe(subscribe[_i]  + "/show", _widget.show);
            doSubscribe(subscribe[_i]  + "/hide", _widget.hide);            
        }

        if (wargs.value){
            _widget.model = wargs.value;
            init();
        } else if (wargs.service) {
            jmaki.doAjax(
                {url: wargs.service, 
                    callback: function(req) {
                        if (req.responseText == "") {
                            container.innerHTML = "No data provided by: " + wargs.service;
                            return;
                        }
                        var _in = eval('(' + req.responseText + ')');
                        //new format if using the data store need to provide followin properties
                        _widget.model = _in;
                        init();
                    },
                    onerror : function (message) {
                        container.innerHTML = "Failed to load data: " + message;
                    }
                }
            );
        }
        
    }

}
