var SE_EXPANDING   = 0;
var SE_CONTRACTING = 1;

function SmoothExpand() {
	this.iSpeed = 2;
	this.iDelay = 15;
	this.aElems = new Array();	
}
SmoothExpand.prototype.reExpandClass   = /\bsmo_expand\b/;
SmoothExpand.prototype.reContractClass = /\bsmo_contract\b/;
SmoothExpand.prototype.reTarget = /Target: ([^\.]+)/;
	
	SmoothExpand.prototype.doit = function(oEvent) {
		var oButton = fGEOBJ(oEvent);
		var sArgs   = oButton.title;
		aTarget = sArgs.match(this.reTarget);
		if(aTarget.length < 2)
			return(true); //Make browser open default link
		sTarget = aTarget[1];
		return(this.work(sTarget, oButton));
	}
	
	SmoothExpand.prototype.work = function(sID, oButton) {
		if(!isset(oElem = document.getElementById(sID)))
			return(false);
		
		var iOH   = oElem.offsetHeight;
		var oSelf = this;
		
		if(!isset(this.aElems[sID]))
			this.aElems[sID] = new SmoothExpandElement(iOH, oElem.style.position, oElem.style.display);
		
		if(checkClass(oButton, this.reContractClass)) {
			if(!this.aElems[sID].bWorking) {
				oElem.style.overflow = "hidden";
				this.aElems[sID].bWorking = true;
				setTimeout(function() {oSelf.contract(oElem, oSelf.aElems[sID].iOH, oSelf.aElems[sID].iOH, oButton);},this.iDelay);
			};
		} else {
			if(!this.aElems[sID].bWorking) {
				removeClass(oElem, "hidden");
				oElem.style.overflow = "hidden";
				this.aElems[sID].bWorking = true;
				oElem.style.height = "1px";
				setTimeout(function() {oSelf.expand(oElem, oSelf.aElems[sID].iOH, 1, oButton);},this.iDelay);
			};
		}
		return(false);
	}
	
	SmoothExpand.prototype.expand = function(oElem, iOH, iH, oButton) {
		iH += iH * this.iSpeed;
		var oSelf = this;
		if (iH > iOH) {
			oElem.style.height = "auto";
			sID = oElem.id;
			changeClass(oButton,'smo_contract', 'smo_expand' );
			this.aElems[sID].iOH = oElem.offsetHeight;
			this.aElems[sID].bWorking = false;
		} else {
			oElem.style.height = iH+"px";
			setTimeout(function() {oSelf.expand(oElem, iOH, iH, oButton);},this.iDelay);
		}
	}
	
	SmoothExpand.prototype.contract = function(oElem, iOH, iH, oButton) {
		iH = iH * (1/this.iSpeed);
		var oSelf = this;
		if (iH <= 1) {
			oElem.style.height = iH+"px";
			addClass(oElem, "hidden");
			sID = oElem.id;
			changeClass(oButton, 'smo_expand', 'smo_contract')
			this.aElems[sID].bWorking = false;
		}
		else {
			oElem.style.height = iH+"px";
			setTimeout(function() {oSelf.contract(oElem, iOH, iH, oButton);},this.iDelay);
		}
	}
	
function SmoothExpandElement(iOH, sPos, sDis) {
	this.iOH = iOH;
	this.sPosition = sPos;
	this.sDisplay  = sDis
	this.bWorking  = false;
}
	
function SmoothExpander(oEvent) {
	return(oSmoothExpand.doit(oEvent));	
}

function SmoothExpandInit() {
	aElems = fGEOBJLI('a', 'smooooth');
	for(i in aElems) {
		if(isnumeric(i))
    		SmoothExpandAddEvents(aElems[i]);
	};
}

function SmoothExpandAddEvents(oElem) {
	eventMouseClick(oElem, SmoothExpander);
}

var oSmoothExpand = new SmoothExpand();
WindowOnload(SmoothExpandInit);
