var _orw;
function rWipe(o){
	var sDisplay = o.style.display;
	if (o.style.display == 'none') o.style.display = '';
	this._collapsedHeight = parseInt(o.style.height);
	this._expandedHeight = parseInt(o.scrollHeight);
	this._clipSize = parseInt(o.offsetHeight);
	this._timeFrame = 150;
	this._iteration = 10;
	this._obj = o;
	o.style.overflow = 'hidden';
	o.style.display = sDisplay;
	this.chunkSize = this._calculateChunkSize();
	

	//Event handlers
	this.onexpand = null;
	this.afterexpand = null;
	this.oncollapse = null;
	this.aftercollapse = null;
}

rWipe.prototype._calculateChunkSize = function(){ return this._expandedHeight / ( this._timeFrame / this._iteration ); }

rWipe.prototype.setExpandedHeight = function(i){
	this._expandedHeight = i;
	this.chunkSize = this._calculateChunkSize();
}

rWipe.prototype.setCollapsedHeight = function(i){
	this._collapsedHeight = i;
	this.chunkSize = this._calculateChunkSize();
}

rWipe.prototype.setTimeFrame = function(i){
	this._timeFrame = i;
	this.chunkSize = this._calculateChunkSize();
}

rWipe.prototype.setClipSize = function(i){
	this._clipSize = i;
	this._obj.style.clip = 'rect(auto, auto, ' + i + 'px, auto)';
}

rWipe.prototype.setIteration = function(i){
	this._iteration = 10;
	this.chunkSize = this._calculateChunkSize();
}

rWipe.prototype.expand = function(bClip){
	_orw = this;

	if (bClip)
		rWipe._doClipExpand();
	else
		rWipe._doExpand();

	if (this._obj.onexpand)
		this._obj.onexpand();
	
	if (this.onexpand)
		this.onexpand(); 
}

rWipe.prototype.collapse = function(bClip){
	_orw = this;

	if (bClip)
		rWipe._doClipCollapse();
	else
		rWipe._doCollapse();

	if (this._obj.oncollapse)
		this._obj.oncollapse();
	
	if (this.oncollapse)
		this.oncollapse();
}

rWipe._doExpand = function (){
	var o = _orw._obj;
	o.style.height = (parseInt(o.style.height) + _orw.chunkSize >= _orw._expandedHeight ? _orw._expandedHeight : parseInt(o.style.height) + _orw.chunkSize) + 'px';
	if (parseInt(o.style.height) < _orw._expandedHeight)
		setTimeout("rWipe._doExpand();", _orw._iteration);
	else{
		if (_orw.afterExpand)
			_orw.afterExpand();
	}
}

rWipe._doCollapse = function(){
	var o = _orw._obj;
	o.style.height = (parseInt(o.style.height) - _orw.chunkSize <= _orw._collapsedHeight ? _orw._collapsedHeight : parseInt(o.style.height) - _orw.chunkSize) + 'px';	
	if (parseInt(o.style.height) > _orw._collapsedHeight){
		setTimeout("rWipe._doCollapse();", _orw._iteration);
	}else{
		if (_orw.afterCollapse)
			_orw.afterCollapse();
	}
}

rWipe._doClipExpand = function (){
	var o = _orw._obj;
	
	if (_orw._clipSize + _orw.chunkSize >= _orw._expandedHeight)
		_orw._clipSize = _orw._expandedHeight;
	else
		_orw._clipSize += _orw.chunkSize;

	o.style.clip = 'rect(auto, auto, ' + _orw._clipSize + 'px, auto)';
	
	if (_orw._clipSize < _orw._expandedHeight)
		setTimeout("rWipe._doClipExpand();", _orw._iteration);
	else{
		if (_orw.afterExpand)
			_orw.afterExpand();
	}
}

rWipe._doClipCollapse = function(){
	var o = _orw._obj;
		
	if (_orw._clipSize - _orw.chunkSize <= _orw._collapsedHeight)
		_orw._clipSize = _orw._collapsedHeight;
	else
		_orw._clipSize -= _orw.chunkSize;

	o.style.clip = 'rect(auto, auto, ' + _orw._clipSize + 'px, auto)';
	
	if (_orw._clipSize > _orw._collapsedHeight){
		setTimeout("rWipe._doClipCollapse();", _orw._iteration);
	}else{
		if (_orw.afterCollapse)
			_orw.afterCollapse();
	}
}