    
/* widget sellection classes */
wid_sel = new Class.create();
wid_sel.prototype =
{
    initialize: function(id, key, wid_sel_id, data, min_width, max_width, init_width, min_height, max_height, init_height, init_layer, category, icon, title, vsize)
    {
        this.id = id;
        this.key = key;
        this.wid_sel_id = wid_sel_id;
        this.data = data;
        this.min_width=min_width;
        this.max_width=max_width;
        this.min_height=min_height;
        this.max_height=max_height;
        this.vsize = vsize;
        this.edit_btn = null;
        wid_sel.list.push(this);
    },
    get_wid_sel_id: function()
    {
        return(this.wid_sel_id);
    },
    
    get_class: function()
    {
        return("wid_sel");
    }
};


wid_sel.list = Array();
wid_sel.get_by_id = function(id)
{
    for(var i=0; i<wid_sel.list.length; i++)
    {
        if (wid_sel.list[i].id == id)
        {
            return(wid_sel.list[i]);
        }
    }
    return(null);
}
// border class
border = new Class.create();
border.prototype =
{
    initialize: function(top, bottom, left, right, corner, img, id, color, backgroundColor)
    {
        this.top = top;
        this.bottom = bottom;
        this.left = left;
        this.right = right;
        this.corner = corner;
        this.img = img;
        this.id = id;
        this.color = color;
        this.backgroundColor = backgroundColor;
    }
}

/* ie 6 layout work around */

var n = navigator.userAgent.toLowerCase();
var ie = !!(n.indexOf("msie") >= 0 && document.all);


/* widget class - generic object for managing content */
widget  = new Class.create();
widget.prototype =
{
    initialize: function(db_id, wid_sel, cont, data, brdr, w, h, x, y, layer)
    {

        var myw = w*1;
        var myh = h*1;
        this.db_id = db_id;
        this.key = wid_sel.key;
		  this.cont = cont;
        this.data = data;
        this.x = x*1;
		  this.y = y*1;
		  this.min_height = wid_sel.min_height;

	//Check if it's a "build your own widget" which uses iframe.  If so, set the max height to the widget height
        this.max_height = wid_sel.id == 16 ? h : wid_sel.max_height;
        this.min_width = wid_sel.min_width;
        this.max_width = wid_sel.max_width;
		  this.width= w*1;
		  this.height= h*1;
        this.widget_number = widget.counter++;
        this.widget_id = "widget_"+this.widget_number;
        this.wid_sel_id = wid_sel.id;
        this.allow_reset = true;
        this.vsize_reset = wid_sel.vsize == 'variable' ? true : false;
        
        this.border = brdr == null ? new border(0,0,0,0,0,null,0,null): brdr;
        this.div = document.createElement("DIV");
        this.div.className = "widget";
        var cm = cont_mgr.obtain(this.key, false, this);
        this.reset_size(cont, w, h);
        this.div.id=this.widget_id;
        this.cont_mgr = cm;
        this.add_border();
        this.cont_mgr.display(false);
		  this.div.style.top= this.y;
		  this.div.style.left=this.x;
        this.div.style.zIndex = layer;
		  $(cont.cont_id).appendChild(this.div);
        
        widget.list.push(this);
        this.cont_mgr.reset_display();
    },
    
    reset_border: function()
    {
        if (this.border_div == null)
            return;
        var wid = this.total_width();
        var hi = this.total_height();
        this.border_div.style.background="url(/shared/images/border.php?w="+wid+"&h="+hi+"&b="+this.border.corner+"&c="+this.border.color+"&img=/shared/images/"+this.border.img+") no-repeat";
    },
    
    add_border: function()
    {
        if (this.border.img == null)
            return;
        this.border_div = this.div; // document.createElement("DIV");
//        this.border_div.style.position="absolute";
//        this.border_div.style.left="0px";
//        this.border_div.style.top="0px";
        this.reset_border();
//        this.div.appendChild(this.border_div);
    },
    
    get_data: function()
    {
        return(this.data);
    },

    get_obj_height: function(obj, is_top)
    {
        var max = 0;
        if (is_top)
        {
            max = obj.offsetHeight;
        }
	else if (obj.name == "widget_frame")// && window.location.href == "http://pruflorida.blueroof360.com/site/index.php?test=1" && this.db_id == "626733")
	{
		obj.height = this.height;
		max = obj.height;
	}
        else if (obj.offsetHeight != null && obj.offsetTop != null)
        {
            max = obj.offsetHeight+obj.offsetTop;
        }

        if (obj.hasChildNodes())
        {
            for (var i=0; i<obj.childNodes.length; i++)
            {
                var next = this.get_obj_height(obj.childNodes[i], false);
                if (next > max)
                {
                    max = next;
                }
            }
        }
        return (max);
    },
    reset_size: function(cont, w, h)
    {
        var lr_wid =  this.border.left + this.border.right;
        var tb_wid = this.border.top + this.border.bottom;
        this.width = w - lr_wid;
        if (ie)
            this.div.style.width = this.total_width();
        else
            this.div.style.width = this.width;
        this.height = h - tb_wid;
        if (ie)
            this.div.style.height = this.total_height();
        else
            this.div.style.height = this.height;
        this.div.style.paddingLeft = this.border.left;
        this.div.style.paddingTop = this.border.top;
        this.div.style.paddingRight = this.border.right;
        this.div.style.paddingBottom=this.border.bottom;
        if (this.cont_mgr != null)
            this.cont_mgr.reset_display();

        if (this.vsize_reset)
        {
            //new_total_height = this.get_obj_height(this.div);
            new_total_height = this.get_obj_height(this.div, true);

            var new_height = new_total_height - tb_wid;
            if (new_height > this.min_height) 
            {
                this.height = min(max(new_height, this.min_height), this.max_height);
            }
            
            if (ie)
                this.div.style.height = this.total_height()+"px";
            else
                this.div.style.height = this.height+"px";
        }
        this.reset_border();
        return(this.height + tb_wid);
        
    },
    

     // this is called to add this element to the given container. It removes itself from the previous container
    set_cont: function(cont, ins_before)
    {
	    alert("set_cont called");
		return;
        this.prev_cont = this.cont;
        this.sibling = this.nextSibling;
        if (ins_before != null)
        {
            cont.div.insertBefore(this.div, ins_before);
        }
        else
        {
            cont.div.appendChild(this.div);
        }
        if (this.cont != null)
        {
            this.cont.remove_widget(this);
        }
        this.cont = cont;
    },

     // On a failure to add this element to a container, this function is called to place it back in the previous container
    reset_cont: function()
    {
    },
     // returns the container for this widget
    get_cont: function()
    {
        return(this.cont);
    },
    
    get_cont_number: function()
    {
        return(this.cont.get_cont_number());
    },
    
    get_list_order: function()
    {
        return(this.cont.get_widget_order(this));
    },
    
    get_widget_id: function()
    {
        return(this.widget_id);
    },
    
    get_widget_num: function()
    {
        return(this.widget_number);
    },
    
    get_db_id: function()
    {
        return(this.db_id);
    },
    
    get_content_type_id: function()
    {
        return(this.wid_sel_id);
    },
    
    get_div: function()
    {
        return(this.div);
    },
    
    get_class: function()
    {
        return('wid');
    },

    total_width : function()
    {
        return(this.width + this.border.left+ this.border.right);
    },
    total_height: function()
    {
        return(this.height + this.border.top+this.border.bottom);
    }
}

widget.counter = 0;

widget.list = Array();
widget.get_widget = function(widget_num)
{
    for (var i=0; i<widget.list.length; i++)
    {
        if (widget.list[i].get_widget_num() == widget_num)
        {
            return widget.list[i];
        }
    }
    return null;
}

dcont = new Class.create();
dcont.prototype = 
{
    initialize: function(id, cont_id, col_num, width, height, max_widget, bottom_space, page_id)
    {
        this.id = id;
        this.cont_id = cont_id;
        this.col_num = col_num;
        this.width = width;
        this.height = height;
        this.org_height = height;
        this.widget_count = 0;
        this.max_widget = max_widget;
        this.bottom_space = bottom_space;
        this.page_id = page_id;
        dcont.list.push(this);
        this.cont_widgets = Array();
        this.div=$(cont_id);
    },
    
    add_widget: function(widget)
    {
        if (this.widget_count < this.max_widget || widget.get_cont() == this)
        {
//            widget.set_cont(this, null);
            this.cont_widgets.push(widget);
            this.widget_count++;
//            this.widget_height();
            return(true);
        }
        else
        {
            return(false);
        }
    },
            
    widget_height: function()
    {
        var max_height = this.org_height;
        for (var i=0; i<this.cont_widgets.length; i++)
        {
            var widget = this.cont_widgets[i];
            widget.reset_size(this, widget.total_width(), widget.total_height());
            var widget_div = widget.get_div();
            var height = widget_div.offsetTop + widget_div.offsetHeight - this.div.offsetTop + this.bottom_space;
            if (height > max_height)
            {
                max_height = height;
            }
        }
        this.height = max_height;
        this.div.style.height = this.height;
    },

    get_cont_id: function()
    {
        return(this.cont_id);
    },
    
    get_cont_number: function()
    {
        return(this.id);
    },
    
    get_class: function()
    {
        return('cont');
    },
    
    get_widget_order: function(widget)
    {
        for (var i=0; i<this.cont_widgets.length; i++)
        {
            if (this.cont_widgets[i] == widget)
            {
                return(i);
            }
        }
        return(10);
    }
}

dcont.list = Array();
dcont.get_by_id = function(id)
{
    for (var i=0; i<dcont.list.length; i++)
    {
        if (dcont.list[i].id == id)
            return(dcont.list[i]);
    }
    return(null);
}
dcont.reset_body= function()
{
    Event.observe(window, 'load', dcont.reset_body_event);
    setTimeout('dcont.reset_body_event();', 1000);
}

dcont.reset_body_event = function()
{
    for (var i=0; i<dcont.list.length; i++)
    {
        dcont.list[i].widget_height();
    }
}
        
function max(a, b)
{
    return(a>b?a:b);
}

function min(a, b)
{
    return(a<b?a:b);
}
