/**
*	Author: @Thomas Ene
*
*	Audit:	
*	03/06/2008, v0.0.1
*		- First Draft
*	04/13/2008, v0.0.2
*		- Various improvements and bug fixing
*/

function sliderGroup()
{
	// Constructs
		this.objects = new Array();
		
		this.objectToOpen = null;
		this.objectToClose = null;
		
		this.slideTotalSteps = 10;
		this.slideCurrentStep = 0;
		
		this.timerId = null;
		this.timeoutSpeed = 30;
}

sliderGroup.prototype.add = function(object)
{
	object.hideNow();
	object.setSlideTotalSteps(this.slideTotalSteps);
	this.objects.push(object);
}

sliderGroup.prototype.setSlideTotalSteps = function(mySlideTotalSteps)
{
	this.slideTotalSteps = mySlideTotalSteps;
	for (var obj_key in this.objects)
		this.objects[obj_key].setSlideTotalSteps(mySlideTotalSteps);
}

sliderGroup.prototype.hideAllNow = function()
{
	for(var obj_key in this.objects)
		this.objects[obj_key].hideNow();
}

sliderGroup.prototype.closeAllButNow = function(object)
{
	this.hideAllNow();
	
	object.showNow();
	this.objectToOpen = object;
}

sliderGroup.prototype.closeAllBut = function(object)
{
	//if (this.objectToOpen == null)
	//{
	//	alert("VS Group: Please open an object first!");
	//	return false;
	//}
	if (this.objectToOpen == object)
	{
		// alert("VS Group: Object already opened!");
		return false;
	}
	
	if (this.running()) return false;
	
	this.slideCurrentStep = 0;
	this.objectToClose = this.objectToOpen;
	this.objectToOpen = object;
	
	this.start();
}

sliderGroup.prototype.closeAllBut_old = function(object)
{
	var obj_key;
	for (obj_key in this.objects)
	{
		//alert(this.objects[obj]);
		if (this.objects[obj_key].containerId != object.containerId)
		{
			this.objects[obj_key].className = this.defaultCssClassName;
			this.objects[obj_key].hide();
		}
		else
		{
			this.objects[obj_key].className = this.selectedCssClassName;
			this.objects[obj_key].show();
		}
	}
}

sliderGroup.prototype.sliding = function()
{
	if (this.slideCurrentStep < this.slideTotalSteps)
	{

		// Method 1
		// Prepare Open
		//this.objectToOpen.containerCurrentHeight += this.objectToOpen.scrollSpeed;
		//if (this.objectToOpen.containerCurrentHeight > this.objectToOpen.containerHeight)
		//	this.objectToOpen.containerCurrentHeight = this.objectToOpen.containerHeight;
			
		// Prepare Close
		//this.objectToClose.containerCurrentHeight -= this.objectToClose.scrollSpeed;
		//if (this.objectToClose.containerCurrentHeight < this.objectToClose.minHeight)
		//	this.objectToClose.containerCurrentHeight = this.objectToClose.minHeight;
		
		// ------- Alert message
		//var sum = this.objectToClose.containerCurrentHeight
		//			+ this.objectToOpen.containerCurrentHeight;
		//alert("close="+this.objectToClose.containerCurrentHeight+"\r\n"
		//		+"open="+this.objectToOpen.containerCurrentHeight+"\r\n\r\n"
		//		+"sum="+sum);
		
		// Update (as closely as possible)
		//this.objectToOpen.containerObj.style.height = this.objectToOpen.containerCurrentHeight;
		//this.objectToClose.containerObj.style.height = this.objectToClose.containerCurrentHeight;
		
		// Method 2 - elegant but flickering (not anymore)
		// Recommended way to do it
		// Please use stepOpen and stepCLose instead of this
		// Although the code below fixes firefox's flickering problem
		 //this.objectToOpen.stepOpen();
		 //if (this.objectToClose != null)
		//	this.objectToClose.stepClose();
			
		// Method 3 - best
		this.objectToOpen.stepOpen(false);
		if (this.objectToClose != null)
			this.objectToClose.stepClose(false);
		
		this.objectToOpen.repaint();
		if (this.objectToClose !=null)
			this.objectToClose.repaint();
		
		this.slideCurrentStep++;
	}
	else
	{
		this.stop();
	}
}

sliderGroup.prototype.start = function()
{
	if (this.running())
	{
		alert('VS Group already running!');
		return false;
	}
	
	var _this = this;
	this.timerId = window.setInterval(	function(){ _this.sliding(); },
										_this.timeoutSpeed);
}

sliderGroup.prototype.stop = function()
{
	if (this.running())
		window.clearInterval(this.timerId);
	this.timerId = null;
}

sliderGroup.prototype.running = function()
{
	return this.timerId!=null ? true : false;
}