//** Dynamic Drive Equal Columns Height script v1.01 (Nov 2nd, 06)
//** http://www.dynamicdrive.com/style/blog/entry/css-equal-columns-height-script/
//** Modified for BlueCacti by Alain Rousseau alain@daroost.ca

// usage
//Input IDs (id attr) of columns to equalize. Script will check if each corresponding column actually exists:
//var columnswatch=["leftcolumn", "rightcolumn", "contentwrapper"];
//var myEqualColumns=new EqualCols(columnswatch);
//var ieVers = ieVersion;
function EqualCols(_columnswatch, _adjustment) {
	this.columnswatch = _columnswatch;
	this.adjustment  = (_adjustment == undefined) ? this.defaultAdjustment() : _adjustment;
	this.timer = 0;
	this.msg = "";
	var columnsInstance = this;
	this.ieVers = this.getVersion();
	this.doDebug = false;
	this.tallest = 0;
	this.idTallest = 0;
	this.onEqualDone = new YAHOO.util.CustomEvent('EqualCols Done');
		//if (this.ieVers == 0)
			//alert("EqualCols started for "+this.columnswatch[0] +" and "+this.columnswatch[1]);
	//if (document.getElementById(this.columnswatch[0])) {
		// if the user has IE 7 (ieVers >6) and up or any other browser (ieVers = 0)
		if ((this.ieVers > 5)||(this.ieVers == 0)) {
			this.dotask(window, function(){columnsInstance.setHeights()}, "load");
			if ((this.ieVers !=0) && (this.ieVers < 7))
				this.dotask(window, function() {if(columnsInstance.timer != 0) {clearTimeout(columnsInstance.timer);}else{columnsInstance.timer=setTimeout(function(){columnsInstance.resetHeights()}, 100)}},"resize");
			else
				this.dotask(window, function(){if (typeof columnsInstance.timer!=0) clearTimeout(columnsInstance.timer); columnsInstance.timer=setTimeout(function(){columnsInstance.resetHeights()}, 100)}, "resize");
		} else {
			return false;
		}
	//}
	this.loopCount = 0;
	
}

EqualCols.prototype.debug = function() {
	if (this.doDebug)
		alert(this.msg);
	this.msg = "";
}

EqualCols.prototype.defaultAdjustment = function() {
	var defaultAdj = new Array();
	var i = 0;
	var len = this.columnswatch.length;
	for (i = 0; i < len; i++) {
		defaultAdj[i] = 0;	
	}
	return defaultAdj;
}

EqualCols.prototype.addEventListener = function(fn, obj, bOverride){
	this.onEqualDone.subscribe(fn,obj,bOverride);	
}

EqualCols.prototype.itemExists = function() {
	for (var i=0; i < this.columnswatch.length; i++) {
		var ele = document.getElementById(this.columnswatch[i]);
		if (ele) {}
		else {return false;}
	}
	return true;	
}

EqualCols.prototype.setHeights = function(reset) {
	
	if (this.itemExists()) {
		this.loopCount++;
		var tallest=0;
		var diff=0;
		var diffOffset = 0;
		var idTallest = 0;
		var curTallest = 0;
		var resetit=(typeof reset=="string")? true : false;
		var s, i;
	  	var msg = "";
	  	var klass = "#"+this.columnswatch[0];
	  	var element = "marginTop";
	  	var ffAdjust = 10;
		var topOffset;
		
		var extraOffset = 0;
		msg += this.columnswatch.length+" classes to watch \n";
		
		
		var newHeight = 0;
		var msg = "";
		for (var i=0; i<this.columnswatch.length; i++){
			var ele = document.getElementById(this.columnswatch[i]);
			curTallest = this.getEleHeight(ele);
			this.idTallest = (curTallest > this.tallest) ? i : this.idTallest;
			this.tallest = (curTallest > this.tallest) ? curTallest : this.tallest;
			if (resetit)
				ele.style.height="auto";
			this.msg+="ele : "+ele.id+"  height = "+ele.scrollHeight+" \tidTallest = "+this.idTallest+" \t\t curTallest = "+curTallest+"\t\ttallest = "+this.tallest+"\n";
		}
		//this.debug(msg);
		this.msg += "\n\n";
		for (var i=0; i<this.columnswatch.length; i++) {
			
			if (i != this.idTallest) {
				this.tallest+=this.adjustment[i];
				var ele = document.getElementById(this.columnswatch[i]);
				document.getElementById(this.columnswatch[i]).style.height=this.tallest+"px";
				this.msg+="setting height of "+this.columnswatch[i]+" to "+this.tallest+"\ni = "+i+"\nidTallest = "+this.idTallest+"\n\n";
			}
		}
		
		var finalHeight = this.getFinalHeight(this.tallest);
		this.msg += "finalHeight ? "+finalHeight+"\n";
		this.debug(this.msg);
		if ((!finalHeight) && (this.loopCount < 4)) {
			this.setHeights(tallest);	
		} else {
			this.onEqualDone.fire();
			//this.onEqualDone.unsubscribeAll();	
		}
	} else {
		return false;
	}
}

EqualCols.prototype.getFinalHeight = function(curHeight) {
	var tallest = 0;
	var curTallest = 0;
	var msg = "getFinalHeight :: curHeight is "+curHeight+"\n";
	for (var i=0; i<this.columnswatch.length; i++){
		var ele = document.getElementById(this.columnswatch[i]);
		curTallest = this.getEleHeight(ele);
		tallest = (curTallest > tallest) ? curTallest : tallest;
		this.msg+="\t\ti = "+i+"\tcurTallest = "+curTallest+"\ttallest = "+tallest+"\n";
	}
	this.msg+="\t\tgoing to return : "+(curHeight <= tallest)+"\n\n";
	//alert(msg);
	return (curHeight <= tallest);
	//	return true;
	//else
	//	return false;
}

EqualCols.prototype.getEleHeight = function(ele) {
	var newHeight = 0;
	var returnHeight = 0;
	//var px = ((showPX == undefined) || (showPX)) ? "px" : 0;
	 if (ele.scrollHeight) {
		//this.msg += " id ("+ele.id+")  class("+ele.className+") .scrollHeight = "+ ele.scrollHeight +" :: newHeight = "+ newHeight+", tag : "+ ele.tagName +", nodeName : "+ ele.nodeName +"\n\n";
		this.msg += "newHeight Final = "+ele.id+" scrollHeight  = "+ ele.scrollHeight+"\n\n------------------------------------------------------------------------------------\n";
		newHeight += ele.scrollHeight;
		
		return newHeight;
	} else	if (ele.childNodes.length > 0) {
		//this.msg += " we have "+ele.childNodes.length+" childNodes\n";
		for (var i=0; i < ele.childNodes.length; i++) {
			//if (checkNode(ele.childNodes[i]))
			//	newHeight += this.getEleHeight(ele.childNodes[i],false);
			if(ele.childNodes[i].scrollHeight)
				newHeight += ele.childNodes[i].scrollHeight;
			if (ele.childNodes[i].style) {
				if(ele.childNodes[i].style.marginTop)
					newHeight += Number(ele.childNodes[i].style.marginTop.replace("px",""))
				if(ele.childNodes[i].style.marginBottom)
					newHeight += Number(ele.childNodes[i].style.marginBottom.replace("px",""))
			}
			//msg += "id : "+curEle.id+",  class : "+curEle.className+", height : "+ curEle.scrollHeight +", tag : "+ curEle.tagName +", nodeName : "+ curEle.nodeName +"\n";
			//this.msg += "ele.childNodes["+i+"] :: id ("+ele.childNodes[i].id+")  class("+ele.childNodes[i].className+") .scrollHeight = "+ ele.childNodes[i].scrollHeight +" :: newHeight = "+ newHeight+", tag : "+ ele.childNodes[i].tagName +", nodeName : "+ ele.childNodes[i].nodeName +"\n\n";
		}
		
		//newHeight += px;
		this.msg+= ele.id+" newHeight Final = "+newHeight+" \n\n------------------------------------------------------------------------------------\n";

		return newHeight;
	} else {
		return returnHeight;	
	}
}

EqualCols.prototype.getEleHeightOld = function(ele) {
	var newHeight = 0;
	var msg = "";
	if (ele) {
	
		if (ele.childNodes.length > 1) {
			for (var i=0; i < ele.childNodes.length; i++) {
				var curEle = ele.childNodes[i];
				if (curEle.scrollHeight)
					newHeight += curEle.scrollHeight;
				if (curEle.nodeName != "#text") 
					msg += "id : "+curEle.id+",  class : "+curEle.className+", height : "+ curEle.offsetHeight +", tag : "+ curEle.tagName +", nodeName : "+ curEle.nodeName +"\n";
				//} 
			}
			//alert(ele.id+" returnHeight = "+returnHeight+"\n\n"+msg);
		}
	}
		return newHeight;
}

EqualCols.prototype.setHeightsOld = function(reset) {
	var tallest=0;
	var diff=0;
	var diffOffset = 0;
	var idTallest = 0;
	var curTallest = 0;
	var resetit=(typeof reset=="string")? true : false;
	var s, i;
  	var msg = "";
  	var klass = "#"+this.columnswatch[0];
  	var element = "marginTop";
  	var ffAdjust = 10;
	var topOffset;
	
	var extraOffset = 0;
	msg += this.columnswatch.length+" classes to watch \n";
	for (var i=0; i<this.columnswatch.length; i++){
			msg +="Looking for "+this.columnswatch[i]+" :  exists ? "+(document.getElementById(this.columnswatch[i])!=null)+"\n";
		if (document.getElementById(this.columnswatch[i])!=null){
			var curColumn = document.getElementById(this.columnswatch[i]);
			if (resetit)
				curColumn.style.height="auto";
			
			
			if (curColumn.scrollHeight>tallest){
				diff = tallest - curColumn.scrollHeight;
				tallest= curColumn.scrollHeight;
				if (this.ieVers == 0) {
					tallest += ffAdjust;
				}
				idTallest = i;
				if (this.columnswatch[i] == "mainContent") {
					var ref = (i == 0) ? 25 : 24;
	  				var klass = "#"+this.columnswatch[i];
	  				var baseRule = (document.styleSheets[0].cssRules) ? document.styleSheets[0].cssRules: document.styleSheets[0].rules;
					topOffset = baseRule.item(ref).style["marginTop"];
					topOffset = Number(topOffset.substring(0, topOffset.length - 2));
					if (document.getElementById("productPresentationImage") != null) {
						 for (s = 0; s < document.styleSheets.length; s++){
							 var baseRules = (document.styleSheets[s].cssRules) ? document.styleSheets[s].cssRules : document.styleSheets.rules;
						    for (i = 0; i < baseRules.length; i++){
						      if ( baseRules.item(i).selectorText == "#productPresentationImage" ){
						      extraOffset = baseRules.item(i).style.top;
						      extraOffset = Number(extraOffset.substring(0, extraOffset.length - 2));
						      }
						    }
						  }
					}
				}
			}
		}
	}
	if (topOffset = "") topOffset = 0;
	topOffset = topOffset + extraOffset;
	tallest -= Math.abs(topOffset);
	//
	//Apply Adjustment to shortest
	//
	if (tallest > 0){
		for (var i=0; i<this.columnswatch.length; i++){
			if (document.getElementById(this.columnswatch[i])!=null) {
				//diffOffset = Math.abs(diff/tallest);
				//tallest = tallest + diffOffset;
				
				if (i != idTallest) {
					//alert("adjusting "+this.columnswatch[i]+" by "+this.adjustment[i]);
					document.getElementById(this.columnswatch[i]).style.height=tallest+this.adjustment[i]+"px";
				}
			}
		}
	}
	//
	//Final Adjustment to take in account text wrap
	//
	/*
	if (document.getElementById(this.columnswatch[idTallest]) != null){
		//alert("Final Adjustement : "+ ((this.ieVers > 6)||(this.ieVers == 0)));
		if ((this.ieVers > 6)||(this.ieVers == 0)) {
			if (this.ieVers == 0) {
				//alert("current height : "+ 	document.getElementById(this.columnswatch[idTallest]).scrollHeight +"\ntallest : "+tallest);
			}
			while (document.getElementById(this.columnswatch[idTallest]).scrollHeight>tallest){
				tallest = document.getElementById(this.columnswatch[idTallest]).scrollHeight;
				for (var i=0; i<this.columnswatch.length; i++){
					if (document.getElementById(this.columnswatch[i])!=null) {
						if (i != idTallest) {
							//alert("adjusting "+this.columnswatch[i]+"  from "+document.getElementById(this.columnswatch[i]).style.height+"  to "+(tallest+this.adjustment[i]));
							document.getElementById(this.columnswatch[i]).style.height=tallest+this.adjustment[i]+"px";
						}
					}
				}
			}
		} else {
			if (document.getElementById(this.columnswatch[idTallest]).scrollHeight>tallest){
				tallest = document.getElementById(this.columnswatch[idTallest]).scrollHeight;
				for (var i=0; i<this.columnswatch.length; i++){
					if (document.getElementById(this.columnswatch[i])!=null) {
						if (i != idTallest) {
							//alert("adjusting "+this.columnswatch[i]+"  by "+(tallest+this.adjustment[i]));
							document.getElementById(this.columnswatch[i]).style.height=tallest+this.adjustment[i]+"px";
						}
					}
				}
			}
		}
	}
	*/
}

EqualCols.prototype.resetHeights = function() {
	this.setHeights("reset");
}

EqualCols.prototype.dotask = function(target, functionref, tasktype) { //assign a function to execute to an event handler (ie: onunload)
	var nTaskType=(window.addEventListener)? tasktype : "on"+tasktype;
	if (target.addEventListener)
		target.addEventListener(nTaskType, functionref, false);
	else if (target.attachEvent)
		target.attachEvent(nTaskType, functionref);
}


EqualCols.prototype.getVersion = function() {
	var browser=navigator.appName;
	var b_version=navigator.appVersion;
	var version=parseInt(b_version);
	var arrV = b_version.split("(");
	var vArr = arrV[1].split(";");
	var ieVersion = 0;
	var msg = "";
	for (var i = 0; i < vArr.length; i++) {
		var tempStr = vArr[i].substring(0,5);
		tempStr = tempStr.split(" ").join("");
		if (tempStr == "MSIE") {
     		ieVersion = parseFloat(vArr[i].substring(vArr[i].length - 3, vArr[i.length]));
		}
		msg += "tempStr (i:"+i+") = "+tempStr +"\n";
	}
	//alert(msg);
	return ieVersion;
}