function marginToNumber(margin)
{
	if(margin == 'auto') return 0;
	else return parseInt(margin);
}
jQuery(document).ready(function(){
	//init - make #slide-galleryBox the correct width
	var showItems = 5;
	var containerWidth = 0;
	var innerWidth = 0;
	var counter = 0;
	var slideHeight = 0;
	var perPage = 0;
	var addToPage = true;
	jQuery('#slide-galleryBox > .row1:first, #slide-galleryBox > .row2:first').each(function(){ slideHeight += jQuery(this).height() + marginToNumber(jQuery(this).css('margin-top')) + marginToNumber(jQuery(this).css('margin-bottom')); });
	jQuery('#slide-galleryBox > .galleryImgBox').each(function(){
		counter++;
		if(counter <= counterMax)
		{
			var w = $(this).width() + marginToNumber(jQuery(this).css('margin-left')) + marginToNumber(jQuery(this).css('margin-right'));
			innerWidth += w;
			if(counter <= showItems)
			{
				perPage += w;
				containerWidth += (w - (counter == showItems?marginToNumber(jQuery(this).css('margin-right')):0));
			}
			else if(addToPage)
			{
				addToPage = false;
				perPage += marginToNumber(jQuery(this).css('margin-left'));
			}
			return true;
		}
		else
		{
			return false;
		}
	});
	jQuery('#slide-galleryBoxContainer').css({'width':containerWidth+'px', 'height':slideHeight+'px', 'position':'relative', 'overflow':'hidden', 'float':'left'}).
		before('<a href="#" id="leftButton" class="disabled" style="margin-left:15px;">&laquo;</a>').
		after('<a href="#" id="rightButton">&raquo;</a><div id="test" style="clear:both;"></div>');

	jQuery('#slide-galleryBox').css({'width' : innerWidth+'px', 'height': slideHeight+'px', 'position':'absolute', 'left':'0px','top':'0px'});

	var nextLeft = perPage * -1;
	var nextRight = 0;

	jQuery('#rightButton').click(
		function()
		{
			var galBox = jQuery('#slide-galleryBox');
			var leftbutton = jQuery(this);
			var rightbutton = jQuery('#rightButton');
			var curLeftDist = parseInt(galBox.css('left')) * -1;
			var galBoxWidth = parseInt(galBox.css('width'));
			if((nextLeft*-1) < galBoxWidth)
			{
				galBox.stop().animate({'left':nextLeft+'px'}, 2000, 'easein',
					function()
					{
						nextLeft -= perPage;
						nextRight = nextLeft + (perPage*2);
						curLeftDist = parseInt(galBox.css('left')) * -1;
						if(curLeftDist == 0)
						{
							leftbutton.addClass('disabled');
						}
						else if(leftbutton.hasClass('disabled'))
						{
							leftbutton.removeClass('disabled');
						}
						if((nextLeft*-1) >= galBoxWidth)
						{
							rightbutton.addClass('disabled');
						}
						else if(rightbutton.hasClass('disabled'))
						{
							rightbutton.removeClass('disabled');
						}
					});
			}

			return false;
		}
	);
	jQuery('#leftButton').click(
		function()
		{
			var leftbutton = jQuery('#leftButton');
			var rightbutton = jQuery(this);
			var galBox = jQuery('#slide-galleryBox');
			var curLeftDist = parseInt(galBox.css('left')) * -1;
			var galBoxWidth = parseInt(galBox.css('width'));
			if(curLeftDist > 0)
			{
				galBox.stop().animate({'left':nextRight+'px'}, 2000, 'easein',
					function()
					{
						nextLeft += perPage;
						nextRight = nextLeft + (perPage*2);
						curLeftDist = parseInt(galBox.css('left')) * -1;
						if(curLeftDist == 0)
						{
							leftbutton.addClass('disabled');
						}
						else if(leftbutton.hasClass('disabled'))
						{
							leftbutton.removeClass('disabled');
						}
						if((nextLeft*-1) >= galBoxWidth)
						{
							rightbutton.addClass('disabled');
						}
						else if(rightbutton.hasClass('disabled'))
						{
							rightbutton.removeClass('disabled');
						}
					});
			}

			return false;
		}
	);
});