// jListCarousel v1.1 (28/10/2011)

// v1.1 28/10/2011 removed bad clone system, changed the computations

// TODO Use ns.extend to create element defaults + options
// TODO create clones if necessary
// TODO make the viewport smaller than the wrapport
// TODO split the clone and wrap part of the code
var jListCarousel = function (arr_options)
{
	var _obj_wrapper_selector 			= '#photostrip';
	var _obj_list_selector				= 'ul';
	var _obj_list_items_selector 		= 'li';
	var _obj_button_back_selector 		= '#scroll_left';
	var _obj_button_forward_selector 	= '#scroll_right';
	
	var _int_speed 						= 10;
	var _int_difference 				= 1;
	var _int_list_width					= 1;
	
	var _str_direction					= 'none';

	var _initiateElements = function ()
	{
		$(_obj_wrapper_selector)
			.css( 'position', 'relative' );
		$(_obj_wrapper_selector).find(_obj_list_selector)
			.css('position', 'relative');
		$(_obj_wrapper_selector).find(_obj_list_items_selector)
			.each( function ()
				{
					$(this).css( {
						'top': $(this).parent().height()/2 - $(this).height()/2,
						'left': $(this).offset().left - $(this).parent().offset().left
					} );
					_int_list_width += $(this).outerWidth(true);
				}
			)
			.css('position', 'absolute');
	};
	var _activateControls = function ()
	{
		$(_obj_button_back_selector)
			.css('z-index', '2')
			.bind('mouseover', function ()
				{
					_changeDirection('right');
					return false;
				}
			)
			.bind('mouseout', function ()
				{
					_changeDirection('none');
					return false;
				}
			);
		$(_obj_button_forward_selector)
			.css('z-index', '2')
			.bind('mouseover', function ()
				{
					_changeDirection('left');
					return false;
				}
			)
			.bind('mouseout', function ()
				{
					_changeDirection('none');
					return false;
				}
			);
	};
	var _start = function ()
	{
		// If big enough to scroll, start scroll
		if ( $(_obj_wrapper_selector).width() < _int_list_width)
		{
			setInterval(_startScroll, _int_speed);
		}
		// Else center all
		else
		{
			$(_obj_wrapper_selector).find(_obj_list_items_selector).css({'left': '+=' + ( $(_obj_wrapper_selector).width() /2 - _int_list_width/2) + 'px'});
		}
	}
	var _startScroll = function ()
	{
		if ('left' === _str_direction) {
			$(_obj_wrapper_selector).find(_obj_list_items_selector).css({'left': '-=' + _int_difference + 'px'});
			$(_obj_wrapper_selector).find(_obj_list_items_selector).each( function ()
			{
				// if left side of first image is scrolling out of the box on the left side
				if ( $(this).position().left < 0 ) // image links < container links
				{
					// TODO create clone if necessary
				}
				// if right side of first image is scrolling out of the box on the left side
				if ( $(this).position().left < 0 - $(this).outerWidth(true) ) // image links < container links - image breedte
				{
					$(this).css('left', $(_obj_wrapper_selector).find(_obj_list_items_selector).last().position().left + $(_obj_wrapper_selector).find(_obj_list_items_selector).last().outerWidth(true));
					$(this).appendTo($(_obj_wrapper_selector).find(_obj_list_items_selector).parent());
				}
			} );
		}
		if ('right' === _str_direction) {
			$(_obj_wrapper_selector).find(_obj_list_items_selector).css({'left': '+=' + _int_difference + 'px'});
			$($(_obj_wrapper_selector).find(_obj_list_items_selector).get().reverse()).each( function ()
			{
				// if right side of last image is scrolling out of the box on the right side
				if ( $(this).position().left + $(this).outerWidth(true) > $(_obj_wrapper_selector).width() ) // image rechts > container rechts
				{
					// TODO create clone if necessary
				}
				// if left side of last image is scrolling out of the box on the right side
				if ( $(this).position().left + $(this).outerWidth(true) > $(_obj_wrapper_selector).width() + $(this).outerWidth(true) ) // image rechts > container rechts + image breedte
				{
					$(this).css('left', $(_obj_wrapper_selector).find(_obj_list_items_selector).first().position().left - $(this).outerWidth(true));
					$(this).prependTo($(_obj_wrapper_selector).find(_obj_list_items_selector).parent());	
				}
			} );
		}
	};
	var _changeDirection = function (str_direction)
	{
		if ( str_direction )
		{
			_str_direction = str_direction;
		}
		else
		{
			if ( 'left' === _str_direction ) { _str_direction = 'right'; }
			if ( 'right' === _str_direction ) { _str_direction = 'left'; }
		}
	};
	
	// Construction
	_initiateElements();
	_activateControls();
	_start();
		
	// Return object with public methods
	return {
		change: _changeDirection
	};
};

$(window).bind('load', function ()
{
	obj_carousel = new jListCarousel();
});
