/*
Written by Jonathan Nicol 
Based on xpander from emusic.com
*/
/*
xpanderInit loops through all elements with the class names stretcher & toggler, and
assigns them an id and onclick event to trigger the xpander. The idea is to avoid having to
assign ids in the html markup. 
*/
function xpanderInit(){
	// create an array of stretchers and togglers (requires custom getElementsByClassName function)
	var stretchers = getElementsByClassName(document,'*','stretcher'); //div that stretches
	var togglers = getElementsByClassName(document,'*','toggler'); //links that trigger xpansion
	
	for (n=0;n<stretchers.length;n++){
	 	// give each stretcher & toggler a custom id
		stretchers[n].id = "stretcher"+n;
	    togglers[n].id = "toggler"+n;
		// create an xpander instance for this stretcher/toggler pair
		this["stretcher"+n] = new Xpander(stretchers[n].id, togglers[n].id, "stretcher"+n, "0", 250, 10, "reviewMore");
		// to begin with, hide the stretcher
	    this["stretcher"+n].collapse();
	    // the image link:
	    // attach custom variable to the toggler element to avoid scope issues
	    togglers[n].getElementsByTagName("a")[0].tempID = n;
		togglers[n].getElementsByTagName("a")[0].onclick = function(){
			eval("stretcher"+this.tempID).toggle();
			return false;
		}
		// the textlink:
		// attach custom variable to the toggler element to avoid scope issues
		togglers[n].getElementsByTagName("a")[1].tempID = n;
		togglers[n].getElementsByTagName("a")[1].onclick = function(){
			eval("stretcher"+this.tempID).toggle();
			return false;
		}
	}
}


/*
Xpander class
divId: id of the block to be expanded
name: global javascript name of this object (This is a super-lame hack, please fix)
height: collapsed height
time: time (in milliseconds) that the expansion should take
part: the number of partial expansions which add up to the complete expansion
*/
function Xpander(stretcherID, togglerID, name, height, time, part)
{
	this.Id = stretcherID;
	this.togId = togglerID;
	this.name = name;
	this.defaultHeight = height;
	this.defaultTime = time;
	this.partitions = part;
	this.elem = document.getElementById(this.Id);
	this.togelem = document.getElementById(this.togId);
	this.inprogress = false;
}

Xr = Xpander.prototype;

Xr.expandRecursion = function() {
	diff = this.elem.scrollHeight - this.elem.offsetHeight;
	if (diff > 0){
		this.inprogress = true;
		//alert( "recurse, diff :"+diff+", this.partSize: "+Math.floor(this.partSize)+", height: "+this.elem.style.height+", off: "+this.elem.offsetHeight+", scroll: "+this.elem.scrollHeight);
		if (diff >= this.partSize){
			this.elem.style.height = this.elem.offsetHeight + Math.floor(this.partSize) + "px";
		} else {
			this.elem.style.height = this.elem.scrollHeight + "px";
		}
		this.expandTimeout = setTimeout( this.name+".expandRecursion()", Math.floor(this.defaultTime/(this.partitions + 1)));
	} else {
		this.inprogress = false;
	}
}

Xr.expand = function(){
	if (this.inprogress == false){
		origDiff = this.elem.scrollHeight - this.elem.offsetHeight;
		this.partSize = Math.floor( origDiff/this.partitions);
		this.expandRecursion();
	}
}

Xr.collapse = function(){
	elem = this.elem;
	if (elem.offsetHeight >= elem.scrollHeight){
		elem.style.height = this.defaultHeight;
		elem.style.overflow = "hidden";    
	}
}

Xr.toggle = function(){
	if (this.elem.offsetHeight >= this.elem.scrollHeight){
		this.collapse();
		this.setImageCollapse();
	} else {
		this.expand();
		this.setImageExpanded();
	}  
}

Xr.setImageCollapse = function()
{
  this.togelem.getElementsByTagName("img")[0].src = imgCollapsed;
}

Xr.setImageExpanded = function()
{
	this.togelem.getElementsByTagName("img")[0].src = imgExpanded;
}

