var custom_scroll_settings = {
	next_image_on: "/img/next.png",
	next_image_off: "/img/next_off.png",
	prev_image_on: "/img/prev.png",
	prev_image_off: "/img/prev_off.png"
};

$(document).ready(function()
{
	var currently_scrolling_container = null;
	var arrow_left = null;
	var arrow_right = null;
	var scroll_timer = null;
	var scroll_interval = 500;
	
	$(".custom_scroll_element_container").each(function(i, e)
	{
		var width = 0;
		
		$(this).children('div').each(function(j, ele)
		{
			width += parseInt($(this).css('width'));
		});
		
		$(this).css('width', width+'px');
		var max_move_right = width - $(this).closest('.custom_scroll_viewable_area').width();

		$.data($(this).get(0), 'custom_scroll_settings', {
			max_movement: "-"+max_move_right+'px',
			immovable: max_move_right <= 0
		});
	});
	
	$('.custom_scroll_arrow')
		.mousedown(function()
		{
			currently_scrolling_container = $(this)
				.closest('.custom_scroll_container')
					.find('.custom_scroll_element_container');
					
			arrow_left = currently_scrolling_container
				.closest('.custom_scroll_container')
					.find('.custom_scroll_arrow_left')
						.find('img');
					
			arrow_right = currently_scrolling_container
				.closest('.custom_scroll_container')
					.find('.custom_scroll_arrow_right')
						.find('img');
			
			if ($(this).hasClass('.custom_scroll_arrow_left'))
			{
				scroll_timer = setInterval(scroll_left, 1);
			}
			else
			{
				scroll_timer = setInterval(scroll_right, 1);
			}
		})
		.mouseleave(function()
		{
			cleanup();
		})
		.mouseup(function()
		{
		   cleanup();
		});
		
	//$('.custom_scroll_arrow_left').find('img').attr('src', custom_scroll_settings.prev_image_off);
	
	function scroll_right()
	{
		var settings = $.data(currently_scrolling_container.get(0), 'custom_scroll_settings');
		
		var max_move = parseInt(settings.max_movement);
		var current = parseInt(currently_scrolling_container.css('left'));
		
		set_arrows();
		
		if (settings.immovable || max_move >= current)
		{
			return;
		}
		
		currently_scrolling_container.animate({"left": "-=2px"}, 1);
	}
	
	function scroll_left()
	{
		set_arrows();
		
		if (parseInt(currently_scrolling_container.css('left')) >= 0)
		{
			return;
		}
		
		currently_scrolling_container.animate({"left": "+=2px"}, 1);
	}
	
	function set_arrows()
	{
		var settings = $.data(currently_scrolling_container.get(0), 'custom_scroll_settings');

		if (parseInt(settings.max_movement) >= parseInt(currently_scrolling_container.css('left')) || settings.immovable)
		{
			arrow_right.attr('src', custom_scroll_settings.next_image_off);
		}
		else
		{
			arrow_right.attr('src', custom_scroll_settings.next_image_on);
		}
		
		if (parseInt(currently_scrolling_container.css('left')) >= 0)
		{
			arrow_left.attr('src', custom_scroll_settings.prev_image_off);
		}
		else
		{
			arrow_left.attr('src', custom_scroll_settings.prev_image_on);
		}
	}
	
	function cleanup()
	{
		clearInterval(scroll_timer);
		currently_scrolling_container = null;
	}
});