function PortfolioGallery()
{
	this.galleryList = '.portfolio_entry_photogallery';
	this.outerFrame = '.portfolio_entry_outer_gallery_frame';
	
	this.navigationLeft = '.portfolio_navigation_left_button';
	this.navigationRight = '.portfolio_navigation_right_button';
	
	this.currentGallery = null;
	this.currentFrame = null;
	
	this.imageWidth = 79;
	
	this.stickySpeed = 75;
	this.normalSpeed = 200;
	
	var _this = this;
	
	this.init = function()
	{
		_this.setRequiredCSS();
		_this.loadListeners();
		_this.enableStickyWalls();
	}
	
	this.setRequiredCSS = function()
	{
		$(_this.galleryList).each(function(){
			children = $(this).children("li");
			$(this).css({ position : 'relative', width : (_this.imageWidth*children.length)+'px' });
		});
	}
	
	this.loadListeners = function()
	{
		$(_this.navigationRight).click(function(){
			_this.getCurrentGalleryList($(this));
			_this.getCurrentFrame($(this));
			_this.moveLeft();
			return false;
		});
		
		$(_this.navigationLeft).click(function(){
			_this.getCurrentGalleryList($(this));
			_this.getCurrentFrame($(this));
			_this.moveRight();
			return false;
		});
	}
	
	this.moveLeft = function(speed)
	{
		if(speed == null)
			speed = _this.normalSpeed;
		if(!$(_this.galleryList).is(":animated"))
			$(_this.currentGallery).animate({ left : '-='+_this.imageWidth }, speed);
	}
	
	this.moveRight = function(speed)
	{
		if(speed == null)
			speed = _this.normalSpeed;
			
		if(!$(_this.galleryList).is(":animated"))
			$(_this.currentGallery).animate({ left : '+='+_this.imageWidth }, speed);
	}
	
	this.enableStickyWalls = function()
	{
		setInterval(function(){
			if(!$(_this.galleryList).is(":animated"))
			{
				var outerFrame = $(_this.currentFrame).offset();
				var galleryFrame = $(_this.currentGallery).offset();
				
				var outerFrameWidth = $(_this.currentFrame).outerWidth();
				var outerGalleryWidth = $(_this.currentGallery).width();
				
				if(outerFrame != null && galleryFrame != null)
				{
					if((outerFrame.left+10) < galleryFrame.left)
					{
						_this.moveLeft(_this.stickySpeed);
					}
					else if((outerFrame.left+outerFrameWidth) > (galleryFrame.left+outerGalleryWidth) || outerFrameWidth > outerGalleryWidth)
					{
						if(outerFrameWidth < outerGalleryWidth)
							_this.moveRight(_this.stickySpeed);
						
						if(outerFrameWidth > outerGalleryWidth && outerFrame.left > galleryFrame.left)
						{
							_this.moveRight(_this.stickySpeed);
						}
					}
				}
				
			}
		}, 50);
	}
	
	this.getCurrentGalleryList = function(clicked_element)
	{
		var parent = $(clicked_element).parent();
		var outerFrame = $(parent).children(_this.outerFrame);
		gallery = $(outerFrame).children(_this.galleryList);
		_this.currentGallery = gallery;
	}
	
	this.getCurrentFrame = function(clicked_element)
	{
		var parent = $(clicked_element).parent();
		var outerFrame = $(parent).children(_this.outerFrame);
		_this.currentFrame = outerFrame;
	}
}
