/*
 *    Copyright 2004-2009, Boostvision Corporation, All rights reserved.
 */

var is_ie = !!(window.attachEvent && !window.opera);

(function(){
	var PARENTDIV = Class.create();
	PARENTDIV.prototype = {
		initialize : function(){
		},
		is_collision : function(sq1,sq2){
			var l1=sq1.l,r1=sq1.r,t1=sq1.t,b1=sq1.b;
			var l2=sq2.l,r2=sq2.r,t2=sq2.t,b2=sq2.b;
			if (r1 <= l2 || r2 <= l1 || b1 <= t2 || b2 <= t1){
				return false;
			}else{
				return true;
			}
		}
	}
	p_div = new PARENTDIV;
})();

function set_divposition(){
	if (parentDIV.length > 0){
		var t = new Array(),l = new Array(),b = new Array(),r = new Array(),w = new Array(),h = new Array();
		var c,d,pt,pb,pdiv = new Array();
		var top,pd,divs = new Array();
		Object.extend(divs,parentDIV);
		divs.shift();

		var data = "";
		divs.each(function(id){
			if (id > 0){
				c = "box"+id+"_container";
				if (!is_ie) Element.setStyle(c,{height:"auto"});
				d = Element.getDimensions(c);
				pt = eval(Element.getStyle(c,"padding-top").replace(/[^0-9]/g,""));
				pb = eval(Element.getStyle(c,"padding-bottom").replace(/[^0-9]/g,""));
				top = eval(Element.getStyle(c,"top").replace(/[^0-9]/g,""));
				t[id] = top - 1;
				l[id] = eval(Element.getStyle(c,"left").replace(/[^0-9]/g,""));
				if (is_ie && $(c).currentStyle.height.match(/[0-9]+px/)){
					h[id] = eval($(c).currentStyle.height.replace(/[^0-9]/g,""));
				}else{
					h[id] = eval(d.height);
				}
				if (is_ie){
//					h[id] += pt + pb - 0;
				}else{
					h[id] -= (pt + pb - 0);
				}
				Element.setStyle(c,{height:h[id]+"px"});

				if (is_ie && $(c).currentStyle.width.match(/[0-9]+px/)){
					w[id] = eval($(c).currentStyle.width.replace(/[^0-9]/g,""));
				}
				else
				if(is_ie && $(c).currentStyle.width.match(/[0-9]+\%/)) {
					window_width = document.body.clientWidth;
					body_width = 0;
					
					if (Element.getStyle(document.getElementById('body'),"width") != null) {
						if (Element.getStyle(document.getElementById('body'),"width").match(/[0-9]+\%/)) {
							body_width = window_width * (eval(Element.getStyle("body","width").replace(/[^0-9]/g,"")) / 100);
						}
						else {
							body_width = Element.getStyle(document.getElementById('body'),"width").replace(/[^0-9]/g,"");
						}
					}
					if (body_width == 0) {
						body_width = window_width;
					}
					w[id] = eval($(c).currentStyle.width.replace(/[^0-9]/g,"")) * (body_width / 100);
				}
				else {
					w[id] = eval(d.width);
				}
				b[id] = t[id]+h[id];
				r[id] = l[id]+w[id];
//				Element.hide(c);
				data += "["+id+"]"+
						"top="+t[id]+"("+typeof(t[id])+")/"+
						"left="+l[id]+"("+typeof(l[id])+")/"+
						"right="+r[id]+"("+typeof(r[id])+")/"+
						"bottom="+b[id]+"("+typeof(b[id])+")/"+
						"height="+h[id]+"("+typeof(h[id])+")/"+
						"width="+w[id]+"("+typeof(w[id])+")/"+
						"padding-top="+pt+"/"+
						"padding-left="+eval(Element.getStyle(c,"padding-left").replace(/[^0-9]/g,""))+"/"+
						"padding-right="+eval(Element.getStyle(c,"padding-right").replace(/[^0-9]/g,""))+"/"+
						"padding-bottom="+pb+"/"+
						"margin-top="+eval(Element.getStyle(c,"margin-top").replace(/[^0-9]/g,""))+"/"+
						"margin-left="+eval(Element.getStyle(c,"margin-left").replace(/[^0-9]/g,""))+"/"+
						"margin-right="+eval(Element.getStyle(c,"margin-right").replace(/[^0-9]/g,""))+"/"+
						"margin-bottom="+eval(Element.getStyle(c,"margin-bottom").replace(/[^0-9]/g,""))+"/"+
						"\n";
			}else{
				data += "?ID="+id+"\n";
			}
		});
//		alert(data);

		var _h,_id;
		while(pdiv.length < divs.length){
			_h = null;
			_id = null;
			for(i in t){
				if (typeof(t[i]) == "number" && pdiv.indexOf(i) < 0){
					if (_h == null || t[i] >= _h){
						_h = t[i];
						_id = i;
					}
				}
			}
			pdiv.unshift(_id);
		}
		pdiv.each(function(id1){
			var c1,c2,sq1,sq2,bg1,bg2;
			c1 = "box"+id1+"_container";
			pdiv.each(function(id2){
				if (id1 != id2){
					c2 = "box"+id2+"_container";
					sq1 = {r:r[id1],b:b[id1],t:t[id1],l:l[id1]};
					sq2 = {r:r[id2],b:b[id2],t:t[id2],l:l[id2]};
					if (p_div.is_collision({r:r[id1],b:b[id1],t:t[id1],l:l[id1]},{r:r[id2],b:b[id2],t:t[id2],l:l[id2]})){
						Element.setStyle(c2,{top:b[id1]+"px"});
						t[id2] = b[id1];
						b[id2] = t[id2]+h[id2]-0;
					}
//					Element.setStyle(c2,{backgroundColor:bg2});
				}
			});
		});
//		pdiv.each(function(id){
//			if (typeof(id) != "object"){
//				Element.show("box"+id+"_container");
//			}
//		});
	}
}

function load_set_divposition(){
	set_divposition();
}

if (parentDIV && typeof(parentDIV) == "object"){
	Event.observe(window,"load",load_set_divposition);
}

