/*	CLASSE da GALERIA DE FOTOS
-------------------------------------------*/

function GaleriaWidget( linkContainer )
{
	var that = this;
	this.container = $( linkContainer );
	
	this._x;
	this._width;
	this._heigth;
	this.itemsArray = new Array();
	this.itemsContainer = $( "<div class='itemsContainer' ></div>" );
	this.itemsContainer.css( {"overflow":"hidden"} );
	this.itemsContainer.css( {"position":"relative"} );

	this.container.css( {"overflow":"hidden"} );
	this.itemMargin = 0;
	
	this.currentIndex = 0;
	this.currentItem;
}

GaleriaWidget.prototype.getX = function()
{
	return this._x;	
}

GaleriaWidget.prototype.setX = function( value )
{
	this._x = value;
}


GaleriaWidget.prototype.getWidth = function()
{
	return this._width;
}

GaleriaWidget.prototype.setWidth = function( value )
{
	this._width = value;	
	this.itemsContainer.css( {"width":value + "px"} );
}


GaleriaWidget.prototype.getHeight = function()
{
	return this._height;	
}

GaleriaWidget.prototype.setHeight = function( value )
{
	this._heigth = value;
	this.itemsContainer.css( {"height":value + "px"} );
}

GaleriaWidget.prototype.setBtnLeft = function( btn )
{
	if ( !btn )
	{
		return;
	}
	
	var that = this;
	$(btn).bind( "click", function(){
		that.scrollLeft();
	} )
}

GaleriaWidget.prototype.setBtnRight = function( btn )
{
	if ( !btn )
	{
		return;
	}
	
	var that = this;
	$(btn).bind( "click", function(){
		that.scrollRight();
	} )
}

													 


GaleriaWidget.prototype.parseLinks = function()
{
	var that = this;
	
	$( this.container ).children().each( function(){
		this.style.display = "inline-block";
		this.style.cssFloat = "left";
		that.itemsArray.push( this );
	});
	
	this.updateLayout();
}



GaleriaWidget.prototype.init = function()
{
	var that = this;
	this.parseLinks();
	this.container.empty();
	this.container.append( this.itemsContainer );
	
	$( this.itemsArray ).each(function() {
		that.itemsContainer.append( this );
	});
}

GaleriaWidget.prototype.scrollLeft = function()
{
	if ( this.itemsArray[this.currentIndex - 1] )
	{
		this.currentIndex--;
		
		this.currentItem = $( this.itemsArray[this.currentIndex] );
		var lastItem = this.itemsArray[this.itemsArray.length - 1];
		lastItem = $( lastItem );
		
		var limitMargin = lastItem.position().left + lastItem.width() - this.container.width();
		var marginX = this.currentItem.position().left;
		
		if ( marginX > limitMargin )
		{
			marginX = limitMargin;
			this.currentIndex = this.itemsArray.length - 2;
		}		
		
		marginX = -marginX;
		
		this.itemsContainer.animate( { marginLeft:marginX }, { queue:false, duration:500 } );
		$( this.container ).trigger( "CHANGE" );
	}
}

GaleriaWidget.prototype.scrollRight = function()
{
	if ( this.itemsArray[this.currentIndex + 1] )
	{
		this.currentIndex++;
		
		this.currentItem = $( this.itemsArray[this.currentIndex] );
		var lastItem = this.itemsArray[this.itemsArray.length - 1];
		lastItem = $( lastItem );
		
		var limitMargin = lastItem.position().left + lastItem.width() - this.container.width();
		var marginX = this.currentItem.position().left;
		
		
		if ( marginX > limitMargin )
		{
			marginX = limitMargin;
			this.currentIndex = this.itemsArray.length - 2;
		}
		
		
		marginX = -marginX;
		
		this.itemsContainer.animate( { marginLeft:marginX }, { queue:false, duration:500 } );
		$( this.container ).trigger( "CHANGE" );
	}
}	

GaleriaWidget.prototype.updateLayout = function()
{
	var tempWidth = 0;
	for( var i = 0; i < this.itemsArray.length; i++ )
	{
		var item = $(this.itemsArray[i]);

		item.css( {"margin-right" : this.itemMargin + "px"} );
		tempWidth += item.width() + this.itemMargin;
	}
	
	this.setWidth( 100000 );
	//this.setWidth( tempWidth );
 }
