function addEvent( obj, type, fn ) {
  if ( obj.attachEvent ) {
    obj['e'+type+fn] = fn;
    obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
    obj.attachEvent( 'on'+type, obj[type+fn] );
  } else
    obj.addEventListener( type, fn, false );
}
function removeEvent( obj, type, fn ) {
  if ( obj.detachEvent ) {
    obj.detachEvent( 'on'+type, obj[type+fn] );
    obj[type+fn] = null;
  } else
    obj.removeEventListener( type, fn, false );
}

//************************************************************************************************************
		window.ondom = function(fn){
				window.__ondom_functionArray.push(fn);
		};
		(function(){
				window.__ondom_functionArray = [];
				function _runFunctions(){
						for (var i in window.__ondom_functionArray){
								(window.__ondom_functionArray[i])();
						}
				};
				var _khtml = /(WebKit|khtml)/i.test(navigator.userAgent);
				if(document.addEventListener && !_khtml){
						document.addEventListener("DOMContentLoaded", _runFunctions, false);
				}else if(_khtml){
						var _timer = setInterval(function(){
								if(/loaded|complete/.test(document.readyState)){
										clearInterval(_timer);
										_runFunctions();
								}
						}, 10);
				}else{
						document.write("<script id=__ie_ondom defer src=javascript:void(0)><\/script>");
						var script = document.getElementById("__ie_ondom");
						script.onreadystatechange = function(){
								if(this.readyState == "complete"){
										_runFunctions();
								}
						};
				}
		})();


var initial_w = 0;
function adjustCols()
{
	//alert("Viewport w: "+f_clientWidth()+"; W/H: "+getWindowWidth()+" / "+getWindowHeight());
	var _w = getElementWidth("centerCol");
	if (initial_w == 0) initial_w = _w;
	//var w_w = getWindowWidth();
	var w_w = f_clientWidth();
	var h_h = f_clientHeight();
//alert(w_w);
	if (initial_w*1 > w_w*1)
	{
		side_w_l = 1;
		side_w_r = 1;
		_w = w_w-2;
		getElemStyle("centerCol").width = _w+"px";
	}
	else
	{
		getElemStyle("centerCol").width = initial_w+"px";
		_w = initial_w;
		var side_w_l = (w_w*1 - _w*1) / 2;
		var side_w_r = (Math.round(side_w_l) == side_w_l) ? side_w_l : Math.round(side_w_l)-1;
		side_w_l = Math.round(side_w_l);
		leftover = w_w-side_w_l-side_w_r-_w; // should be equal to 0, if not do some corrections
		if (leftover != 0) side_w_r += leftover;
	}
	//alert("Initial: "+initial_w+"; Window: "+w_w+"; Left: "+side_w_l+"; Center: "+_w+"; Right: "+side_w_r+"; Total: "+(side_w_l+side_w_r+_w)+"; Leftover: "+leftover+";");
//alert("Initial: "+initial_w+"; Window: "+w_w+"; Left: "+side_w_l+"; Center: "+_w+"; Right: "+side_w_r+"; Total: "+(side_w_l+side_w_r+_w));
	getElemStyle("leftCol").width = side_w_l+"px";
	getElemStyle("rightCol").width = side_w_r+"px";
	getElemStyle("centerCol").left = side_w_l+"px";
//alert("Left: "+getElementWidth("leftCol")+"; Center: "+getElementWidth("centerCol")+"; Right: "+getElementWidth("rightCol"));

	bottom_element_height = 120;
	content_from_top = 330;
	h_holder = getElementHeight("centerCol");
	h_content = getElementHeight("content");
	h_content_full = h_content+bottom_element_height+content_from_top;
//alert("Holder: "+h_holder+"; Content: "+h_content);
	if (h_holder < h_content_full) // If holder is smaller than the content, this means text is breaking out of container
	{// so increase the container size
		getElemStyle("leftCol").height = h_content_full+"px";
		getElemStyle("rightCol").height = h_content_full+"px";
		getElemStyle("centerCol").height = h_content_full+"px";
	}
	else // The holder is bigger than the content
	{
		if (h_holder > h_content_full) // If holder is bigger by a lot more than content, reduce it's size to match with content
		{
			//alert(h_holder+" "+h_content_full+" "+h_h);
			if (h_content_full > h_h)
			{
				getElemStyle("leftCol").height = h_content_full+"px";
				getElemStyle("rightCol").height = h_content_full+"px";
				getElemStyle("centerCol").height = h_content_full+"px";
			}
			else if (h_holder < h_h)
			{
				getElemStyle("leftCol").height = h_h+"px";
				getElemStyle("rightCol").height = h_h+"px";
				getElemStyle("centerCol").height = h_h+"px";
				//alert(h_holder+" "+h_content_full+" "+h_h);
			}
		}

		correction = bottom_element_height-(h_holder-(h_content+content_from_top)); // Check if text is not overlaping bottom element
		//alert("Holder: "+h_holder+"; Content: "+h_content+"; Correction: "+correction);
		if (correction > 0) // If it is overlaping, increase page height by the overlap size
		{
			getElemStyle("leftCol").height = (h_holder+correction)+"px";
			getElemStyle("rightCol").height = (h_holder+correction)+"px";
			getElemStyle("centerCol").height = (h_holder+correction)+"px";
		}
		else // If its not overlaping - the page is small and bottom element can be safely positioned at the bottom of it.
		{
			//getElemStyle("content").height = (h_holder-content_from_top-bottom_element_height)+"px";
		}
	}

	bottom_push = (getElementWidth("centerCol")-getElementWidth("bottom"))/2-100;
	getElemStyle("bottom").left = bottom_push+"px";
	// make content tab be as wide as center column, except 200px margins
	getElemStyle("content").width = (getElementWidth("centerCol")-200)+"px";

//h_holder = getElementHeight("centerCol");
//h_content = getElementHeight("content");
//alert("Holder: "+h_holder+"; Content: "+h_content);
//alert("Holder: "+h_holder+"; Content: "+h_content+"; Correction: "+correction);
//alert("Left: "+getElementWidth("leftCol")+"; Center: "+getElementWidth("centerCol")+"; Right: "+getElementWidth("rightCol"));
}
//window.ondom = adjustCols;
window.onload = adjustCols;
window.onresize = adjustCols;