/* ie 6 layout work around */

var n = navigator.userAgent.toLowerCase();
var ie = !!(n.indexOf("msie") >= 0 && document.all);


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);
    },
    get_obj_height: function(obj, is_top)
    {
        var max = 0;
        if (is_top)
        {
            max = obj.offsetHeight;
        }
        else if (obj.nodeName == "FORM")
        {
            if (obj.offsetHeight != null && obj.offsetTop != null)
            {
                return(obj.offsetHeight + obj.offsetTop);
            }
            return(0);
        }
        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 (typeof(obj.style) != 'undefined' && (obj.style.overflow == "auto" || obj.style.overflow == "hidden" || obj.style.overflow == "scroll"))
            return(max);

        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);
    },
    
    widget_height: function()
    {
		var widgets = this.div.select(".widget");
		var max_height = this.div.offsetHeight;

//        alert("offset heights- img: "+$('img1_0').offsetHeight+" col: "+$('col_img1_0').offsetHeight);
		widgets.each
		(
			function(name, index)
			{
				var oheight = this.get_obj_height(widgets[index], true)*1;
				var sheight = (widgets[index].style.height.slice(0,-2))*1;
				if (sheight != null && sheight != 0 && oheight - 10 <= sheight) // if the offset height is within 10 pix of the style height, use the style height.
					oheight = sheight;
				var wheight = oheight + widgets[index].offsetTop;
				if (wheight + this.bottom_space > max_height)
				{
//					alert("widget : "+widgets[index].id+" height: "+wheight);
					max_height = wheight+this.bottom_space;
				}
			}
		, this);

        //This doesn't work right with the xhtml doctype
		//this.div.style.height = max_height;
		//So, we're just going to use jquery:
        $j(this.div).height(max_height);

/*        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');
    }
}

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);
}

