﻿function getElementsByClass(searchClass,node,tag) {
            var classElements = new Array();
            if ( node == null )
                        node = document;
            if ( tag == null )
                        tag = '*';
            var els = node.getElementsByTagName(tag);
            var elsLen = els.length;
            var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
            for (i = 0, j = 0; i < elsLen; i++) {
                        if ( pattern.test(els[i].className) ) {
                                    classElements[j] = els[i];
                                    j++;
                        }
            }
            return classElements;
}

var originalheight = 0;
function getMaxHeight(nodes){
	var maxHeight = 0;
    for (var i = 0; i < nodes.length; i++) {
    	if (nodes[i].offsetHeight > maxHeight) maxHeight = nodes[i].offsetHeight;
    }
    if(!originalheight){
    	originalheight = maxHeight;                        	
    }
    return maxHeight;
}
function mambojambo(aha) {
	alert(aha);
}

function compensateForDynamicHeight(obj){

	var pos = findPos(obj)
	var top = pos[1];
	var offsetHeight = obj.offsetHeight;
	
	var mc = document.getElementById('maincontainer');
	var mcTop = findPos(mc)[1];	
	var topRelativeToMainContainer = top-mcTop;

	//retrieve obj's equal-height-container.
	//either #rightContent or #centerContent in the ww-solution
	var container = null;
	pnode = obj.parentNode
	while(pnode){	
		//pnid = pnode.id;
		//alert(pnid)
		var classname = pnode.className
		//if(pnid == 'rightContent' || pnid == 'centerContent'){
		if(classname == 'equalheight'){
			container =  pnode;
			break;
		}
		pnode = pnode.parentNode;
	}
	
	var cn;
	if (container != null)
	{
		cn = container.childNodes;
	}
	else
	{
		cn = obj.childNodes;
	}
	
		var h = 0;
		//loop through childnodes and sum up their heigh
		for(var i=0,ilength=cn.length; i<ilength-1; i++){
			var n = cn[i];
			if(n.offsetHeight){
				h = h+n.offsetHeight		
			}
		}
		//if new height exceeds original, update height	
		if(h > originalheight ){
			setTall('equalheight', document, '*', h);
		}	
		//reset height to original height
		else{
			setTall('equalheight', document, '*', originalheight );
		}
	
	return
	
}


function setTall(searchClass,node,tag, newheight) {	

	// the divs array contains references to each column's div element.  
	// Replace 'center' 'right' and 'left' with your own.  
	// Or remove the last one entirely if you've got 2 columns.  Or add another if you've got 4!
	var divs = new getElementsByClass(searchClass,node,tag);                        
	// Let's determine the maximum height out of all columns specified
	
	var maxHeight = 0;
	if(newheight){
		maxHeight = newheight;
	}
	else{
		maxHeight = getMaxHeight(divs);	
	}
   // Let's set all columns to that maximum height
    for (var i = 0; i < divs.length; i++) {
       divs[i].style.height = maxHeight + 'px';
	   // Now, if the browser's in standards-compliant mode, the height property
	   // sets the height excluding padding, so we figure the padding out by subtracting the
	   // old maxHeight from the new offsetHeight, and compensate!  So it works in Safari AND in IE 5.x
	   if (divs[i].offsetHeight > maxHeight) {
	   		var newHeight = (maxHeight - (divs[i].offsetHeight - maxHeight));
	   		if (newHeight < 0) {
	   			newHeight = 0;
	   		}

	       	divs[i].style.height = newHeight + 'px';
	    }            
	}

}


function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}


window.onload = function() {
	setTall('equalheight', document, '*');
}

 

window.onresize = function() {			
	setTall('equalheight', document, '*');
}