var _SliceTimeout 	= null;
var _SliceStack		= [];

/** 
 * @requires jQuery >= 1.2
 * @requires jQuery.easing 
 * @version 0.1
 */
function Slice (sParent, cActive, bNoInit)
{
	this._container 			= null;
	this._headElement 			= 'div.header';
	this._contentElement 		= 'div.content';
	this._singleContainer 		= 'div.single';
	this._cActive 				= null;
	this._headHeight 			= 0;
	this._containerHeight 		= 0;
	this._easeTypeUp			= 'easeInBack';
	this._easeLengthUp			= 600;
	this._easeTypeDown			= 'easeOutBack';
	this._easeLengthDown		= 600;
	this._easeTypeContainer		= 'easeOutQuad';
	this._easeLengthContainer	= 100;
	this._timeout				= 500;
	this._queuedActive			= -1;
	this._stackId				= _SliceStack.length;
	this._firstCall				= false;
	
	if (!bNoInit)
		this.init(sParent, cActive);
}

Slice.prototype = {
	
	init : function (sParent, cActive) {
		this._container = $(sParent);
 		this.oElements = $(sParent + ' ' + this._singleContainer);
 		
 		this.oHeads = this.oElements.find(this._headElement);
 		
 		this.oBodies = this.oElements.find(this._contentElement);
 		
 		if (this.oHeads.length != this.oBodies.length || !this.oHeads.length) 
 			return false;
 		this.oHeads.css('cursor', 'pointer');
 		this.oBodies.css('overflow','hidden');
 			
 		this.aHeadOffsets = [];
 		this.aBodyOffsets = [];
 		var cHead = null;

 		for (var i = 0, cHeadHeight = 0, cBodyHeight = 0; i < this.oElements.length; i++)
 		{
 			this.aHeadOffsets[i] = cHeadHeight;
 			cBodyHeight = this.oBodies.eq(i).height();
 			this.aBodyOffsets[i] = cBodyHeight;
 			cHead = this.oHeads.eq(i);
 			cHeadHeight += cHead.height();
 			cHead.bind('mouseover', {oSlice: this, iActive: i}, function (e) {
 				e.data.oSlice.attachActive(e.data.oSlice, e.data.iActive);
 				_SliceTimeout = window.setTimeout('_SliceStopper(' + e.data.oSlice._stackId + ')', e.data.oSlice._timeout);
 			});
 			cHead.bind('mouseout', {oSlice: this}, function (e) { e.data.oSlice.detachActive(); });
 		}
 		this._headHeight = cHeadHeight;
 		if (!isNaN(cActive))
 		{
	 		this.onInit(cActive);
 		}
 		_SliceStack[this._stackId] = this;
   },
   
   	onInit : function (cActive) {
   		var others = this.oBodies.filter(':not(:eq(' + cActive + '))');
   		if (others.length) {
   			others.height('1px'); 
   		}
 		this.setActive(cActive);
   	},
		   
	setActive : function (i) {
		if (_SliceTimeout)
			return false;
			
		if (i< 0 || i >= this.oElements.length)
			return false;
			
		if (i == this._cActive)
			return true;
			
		this.setContainerHeight(i);
		this.oBodies.filter(':not(:eq(' + i + '))').animate({height: '1px'}, this._easeLengthUp, this._easeTypeUp);
		this.oBodies.eq(i).animate({height: this.aBodyOffsets[i]}, this._easeLengthDown, this._easeTypeDown);
		this.onEachActive(i);
		if (!this._firstCall)
		{
			this.onFirstActive(i);
			this._firstCall = true;
		}
	},
	
	attachActive : function (oObj, i)
	{
		if (isNaN(i) || i < 0 || i > oObj.oElements.length)
			return false;
			
		oObj._queuedActive = i;
	},
	
	detachActive : function ()
	{
		this._queuedActive = -1;
	},
	
	setContainerHeight : function (i) {
		if (i < 0 || i >= this.oElements.length)
			return false;
			
		this._containerHeight = this._headHeight + this.aBodyOffsets[i] + 5;	
		this._container.animate({height: this._containerHeight}, this._easeLengthContainer, this._easeTypeContainer);
		return true;
	},
	
	stop : function () {
		window.clearTimeout(_SliceTimeout);
		_SliceTimeout = null;
		if (this._queuedActive != -1)
		{
			this.setActive(this._queuedActive);
		}
		
		
	},
	
	onFirstActive : function (i) {},
	
	onEachActive : function (i) {  },
	
	restore : function () {}
}
/**
 * global function for trigger Slice.stop()
 */
function _SliceStopper(id)
{
	if (_SliceStack[id])
		_SliceStack[id].stop();
} 