jQuery.fn.imageScrollInit = function()
{
	var nextButton = jQuery(".nextButton", this);
	var prevButton = jQuery(".prevButton", this);
	var listBox = jQuery(".imageList", this);
	var contentArray = jQuery(".image", listBox);

	if(contentArray.length <= 1)
	{
		nextButton.css("display","none");
		prevButton.css("display","none");
		jQuery("#galleryNote").css("display","none");
	}

	var left = 0;
	contentArray.each(function(){
		this.style.left = left + "px";
		left += 500;
	});

	disableButton(prevButton);
	disableButton(nextButton);


	return this;
}

jQuery.fn.imageScroll = function()
{
	var nextButton = jQuery(".nextButton", this);
	var prevButton = jQuery(".prevButton", this);
	var listBox = jQuery(".imageList", this);
	var contentArray = jQuery(".image", listBox);

	nextButton.add(prevButton).hover(function()
	{
		$(this).css("background-image", "url('img/imageFrame/button_" + $(this).attr("name") + "_over.jpg')")
	},
	function()
	{
		if($(this).attr("rel") == "enabled")
		{
			$(this).css("background-image", "url('img/imageFrame/button_" + $(this).attr("name") + ".jpg')")
		}
	});


	nextButton.click(function()
	{
		if(listBox.attr("rel") == "animated") return;

		var listLeft = parseInt(listBox.css("left"));
		var lastLeft = parseInt(contentArray.filter(":last").css("left"));
		if(listLeft*-1 == lastLeft)
		{
//			listBox.animate({
//				left : "-=80px"
//			},
//			"normal",
//			"swing").animate({
//				left:"0px"
//			},
//			"normal");
		}
		else
		{

			setButtonsStyle(listLeft, lastLeft, prevButton, nextButton, 'next');

			listBox.attr("rel", "animated");

			listBox.animate({
				left : listLeft-500+"px"
			},
			"normal",
			"swing", function(){$(this).attr("rel","");});
		}
	});


	prevButton.click(function()
	{
		if(listBox.attr("rel") == "animated") return;

		var listLeft = parseInt(listBox.css("left"));
		var lastLeft = parseInt(contentArray.filter(":last").css("left"));
		if(listLeft == 0)
		{
//			listBox.animate({
//				left : "+=80px"
//			},
//			"normal",
//			"swing").animate({
//				left: "-"+contentArray.filter(":last").css("left")
//			},
//			"normal");

		}
		else
		{
			setButtonsStyle(listLeft, lastLeft, prevButton, nextButton, 'prev');

			listBox.attr("rel", "animated");

			listBox.animate({
				left : listLeft+500+"px"
			},
			"normal",
			"swing", function(){$(this).attr("rel","");});
		}
	});

	//enableButton(prevButton);
	enableButton(nextButton);

	return this;
}





function setButtonsStyle(listLeft, lastLeft, prevButton, nextButton, opt)
{
	if(opt == 'prev')
	{
		enableButton(nextButton);
		if(listLeft + 500  == 0)
		{
			disableButton(prevButton);
		}
		else
		{
			enableButton(prevButton);
		}
	}

	if(opt == 'next')
	{
		enableButton(prevButton);
		if(-listLeft + 500 == lastLeft)
		{
			disableButton(nextButton);
		}
		else
		{
			enableButton(nextButton);
		}
	}
}

function enableButton(button)
{
	button.css("background-image", "url('img/imageFrame/button_" + button.attr("name") + ".jpg')");
	button.css("cursor", "pointer");
	button.attr("rel","enabled");
}

function disableButton(button, type)
{
	button.css("background-image", "url('img/imageFrame/button_" + button.attr("name") + "_over.jpg')");
	button.css("cursor", "default");
	button.attr("rel","disabled");
}
