if (typeof WebGUI == 'undefined') {
    WebGUI = {};
}

WebGUI.widgetButton = function(config) {
    this.config = config;
    var attachElement = config.attachElement;
    if (!attachElement) {
        attachElement = document.getElementById("wgWidgetButton-" + config.assetId);
    }
    if (!attachElement) {
        attachElement = document.getElementById("staticWidgetButton");
    }
    this.config.attachElement = attachElement;
};
WebGUI.widgetButton.prototype = {
    config: null,
    render: function(el) {
        if (!el) {
            el = document.body;
        }
        var config = this.config;

    var jsCode
        = '&lt;script type="text/javascript" src="' + config.wgWidgetPath + '"&gt; &lt;/script&gt;'
        + '&lt;script type="text/javascript"&gt;'
        + 'document.write(WebGUI.widgetBox.widget("' + config.fullUrl + '","' + config.assetId + '",'
        + config.width + ',' + config.height + ',"' + config.templateId + '","' + config.styleTemplateId
        + '"));&lt;/script&gt;'
        ;

    // Instantiate the Dialog
    var dialogId = YAHOO.util.Dom.generateId('wgWidgetDialog');
    this.codeDialog = new YAHOO.widget.SimpleDialog(dialogId, {
        width               : '300px',
        height              : '200px',
        fixedcenter         : true,
        visible             : false,
        draggable           : true,
        close               : true,
        text                : '<textarea style="border: 1px solid #000; width: 100%; height: 150px" rows="5" cols="50">' + jsCode + '</textarea>',
        icon                : YAHOO.widget.SimpleDialog.ICON_INFO,
        constraintoviewport : true,
        modal               : true,
        zIndex              : 9999,
        buttons             : [
            {
                text        : "Dismiss",
                handler     : function () { this.hide(); },
                isDefault   : true
            }
        ]
    });
    this.codeDialog.setHeader('Widget code');

    // Render the Dialog

    var attachElement = config.attachElement;
    this.codeDialog.render(el);
    YAHOO.util.Event.addListener(
        attachElement,
        'click',
        function (e) {
            YAHOO.util.Event.preventDefault(e);
            this.show();
        }, this.codeDialog, true
    );
}
};


WebGUI.widgetDisplay = function( url, parentId, width, height, templateId, styleTemplateId ) {
    if (url != undefined)
        this.url = url;
    if (parentId != undefined)
        this.parentNodeId = parentId;
    if (width != undefined)
        this.width = width;
    if (height != undefined)
        this.height = height;
    if (templateId != undefined)
        this.templateId = templateId;
    if (styleTemplateId != undefined)
        this.styleTemplateId = styleTemplateId;
};
WebGUI.widgetDisplay.prototype = {
    url             : null,
    parentNodeId    : null,
    width           : 600,
    height          : 800,
    templateId      : null,
    styleTemplateId : null,
    render: function() {
        var url = this.url;
        if (url == "") {
            return '<iframe scrolling="no"><body>No content available from ' + url + '</body></iframe>';
        }
        if (this.templateId == undefined) {
            url += '?func=ajaxInlineView';
        }
        else {
            url += '?func=widgetView&templateId=' + this.templateId
                + ';width=' + this.width
                + ';height=' + this.height
                + ';styleTemplateId=' + this.styleTemplateId;
        }
        
        var markup
            = '<iframe scrolling="no" frameborder="0" id="'
            + this.parentNodeId
            + '" src="' + url
            + '" width="' + this.width
            + '" height="' + this.height
            + '" onload="WebGUI.widgetBox.loadFrame(this)"></iframe>';

        return markup;
    }
};


WebGUI.widgetBox = {
    widget : function( url, parentId, width, height, templateId, styleTemplateId ) {
        var display = new WebGUI.widgetDisplay(url, parentId, width, height, templateId, styleTemplateId);
        return display.render();
    },
    loadFrame: function(el) {
    //alert(this);
    },

    retargetLinksAndForms : function() {

        // get all the <a> elements, change their target appropriately
        var allLinks = document.getElementsByTagName('a');
        for(var i = 0; i < allLinks.length; i++) {
            // skip the gear links for widgets
            if( allLinks[i].href.search(/#/) != -1 && allLinks[i].name.search(/^wgWidgetButton-/) != -1 ) {
                continue;
            }
            else {
                allLinks[i].target = '_blank';
            }
        }

        // same for <form>s
        var allForms = document.getElementsByTagName('form');
        for(var i = 0; i < allForms.length; i++) {
            allForms[i].target = '_blank';
        }
    },

    doTemplate : function(elementId) {
        document.body.innerHTML = TrimPath.processDOMTemplate(elementId, data);
    },

    initButton : function(first, second) {

        // XXX this comment is almost certainly wrong, but I don't feel like fixing it or the code
        // for some unknown reason (God do I love JS), either the first or
        // second argument to initButton may have the params we sent it.
        // hurray. work around this by checking to see which object holds our
        // information and then setting a known name to hold our values.

        var params;
        if(!first.fullUrl) {
            params = second;
        }
        else {
            params = first;
        }
        
        var wButton = new WebGUI.widgetButton(params);
        wButton.render();
    }
}
