//** 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;
	this.timer = 0;
	var columnsInstance = this;
	this.ieVers = this.getVersion();
	// if the user has IE 7 (ieVers >6) and up or any other browser (ieVers = 0)
	if ((this.ieVers > 6)||(this.ieVers == 0)) {
		this.dotask(window, function(){columnsInstance.setHeights()}, "load");
		this.dotask(window, function(){if (typeof columnsInstance.timer!=0) clearTimeout(columnsInstance.timer); this.timer=setTimeout(function(){columnsInstance.resetHeights()}, 100)}, "resize");
	} else {
		return false;
	}
	
}

EqualCols.prototype.setHeights = 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 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){
			if (resetit)
				document.getElementById(this.columnswatch[i]).style.height="auto";
			if (document.getElementById(this.columnswatch[i]).offsetHeight>tallest){
				diff = tallest - document.getElementById(this.columnswatch[i]).offsetHeight;
				tallest=document.getElementById(this.columnswatch[i]).offsetHeight;
				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){
		if (document.getElementById(this.columnswatch[idTallest]).offsetHeight>tallest){
			tallest = document.getElementById(this.columnswatch[idTallest]).offsetHeight;
			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 "+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;
}
