// JavaScript Document
function GalleryControl (btPrev, btNext, total, index, callback, dispatchOutBounds)
{
	var _that = this;
	var _btPrev = btPrev;
	var _btNext = btNext;
	var _total = total;
	var _index = index;
	var _callback = callback;
	var _dispatchOutBounds = dispatchOutBounds == null ? false : dispatchOutBounds;
	
	var _disabledPrev = false;
	var _disabledNext = false;
	
	// Public Methods
	this.getIndex = function ()
	{
		return _index;
	}
	
	this.setIndex = function (value, callbackBool)
	{
		callbackBool = callbackBool == undefined ? true : callbackBool;
		_index = value;
		checkButtons ();
		if(callbackBool) _callback(value);
	}
	
	this.changeGallery = function (total, index)
	{
		_total = total;
		_index = index;
		checkButtons();
	}
	
	this.backImage = function ()
	{
		if(_disabledPrev) {
			_index = _total-1;
			_callback(_index);
			_disabledPrev = false;
			checkButtons ();
			if(_dispatchOutBounds) _callback("-");
			return false; 
		}
		_index--;
		checkButtons ();
		_callback(_index);
	}
	
	this.forwardImage = function()
	{
		if(_disabledNext) {
			_index = 0;
			_callback(_index);
			_disabledNext = false;
			checkButtons ();
			if(_dispatchOutBounds) _callback("+");
			return false; 
		}
		_index++;
		checkButtons ();
		_callback(_index);
	}
	
	function setupControls ()
	{
		_btPrev.unbind("click");
		_btPrev.click(function () {	_that.backImage() });	
		
		_btNext.unbind("click");
		_btNext.click(function () {	_that.forwardImage() });
	}
	
	function checkButtons ()
	{
		if(_index >= _total - 1) {
			//_btNext.addClass("disable");
			_disabledNext = true;
		} else {
			//_btNext.removeClass("disable");
			_disabledNext = false;
		}
		
		if(_index <= 0) {
			//_btPrev.addClass("disable");
			_disabledPrev = true;
		} else {
			//_btPrev.removeClass("disable");
			_disabledPrev = false;
		}
	}	
	
	// INIT
	setupControls ();
	checkButtons ();
}
