if (!document.Widget.Wrapper)
(function(){

    // shortcuts
    var undef = function(param) { return param; }

    Widget.Wrapper = function( widget )
    {
        this.widget = widget;
        this.widget.wrapper = this;
        this.idPrefix = this._idPrefix + this._idSeparator;
        this.idPostfix = this._idSeparator + widget.id;
        this.init();
    };
    Widget.Wrapper.GetMessage = function(params)
    {
        var w = Widget.Get(params.wauth);
        if (w)
            switch(params.name) {
                case 'Widget::setTitle' :
                {
                    w.setTitle(params.value);
                }
                case 'Widget::setIFrameHeight' :
                {
                    w.setIFrameHeight(params.value);
                }
        }
    };
    Widget.Wrapper.prototype = {
        _idPrefix : "wd",
        _idSeparator : "-",
        $ : function( idPart )
        {
            if (idPart != undef())
                return document.getElementById(this.idPrefix + idPart + this.idPostfix);
            else
                return document.getElementById(this.idPrefix + "wrapper"+this.idPostfix);
        },
        setTitle : function( title )
        {
            this.$("title").innerHTML = title;
        },
        setIFrameHeight : function( newHeight )
        {
            if ((""+newHeight).match('^[0-9]+'))
                newHeight = newHeight + "px";
            this.$("iframe").style.height = newHeight;
        },
        init : function()
        {
            // тут, видимо, код, который будет добавлять элементы управления в заголовок, etc.
        },
		showEditForm: function(f){
		    // TODO: refactor and get rid of mysterious "wg.wa" & "wg.wid"
			var src = '/form.html?wauth=' + wg.wa + '&widget=' + wg.wid;
	        var req;
	
	        if (typeof XMLHttpRequest != 'undefined') {
	            req = new XMLHttpRequest();
	        } else if (typeof ActiveXObject != 'undefined') {
	            req = new ActiveXObject('Microsoft.XMLHTTP');
	        } else {
	            return false;
	        }
	        req.onreadystatechange = function() {
	            try {
					if (req.readyState == 4 && req.status == 200) {
						f(req.responseText);
					}
	            } catch (e) {}
	        };			
	        req.open('GET', src, true);
	        req.send(null);
		}                
    }
	// показывает форму настроек виджита
	// TODO: get rid of this form
	Widget.showEditForm = function(id, f){
		Widget.GetInstance(id).wrapper.showEditForm(f);
	};	
	
    y5.Events.observe('y5:XFrameMessage', Widget.Wrapper.GetMessage, y5, true);
})();
