////////////////////////////////////////////////////////////
////////////////////IMAGE SLIDE ////////////////////////////
////////////////////////////////////////////////////////////

function ImageSlide(box_selector, item_selector, switch_interval, switch_duration)
{
	if(switch_interval <= 0) switch_interval = 10000;
	if(switch_duration <= 0) switch_duration = 1000;

	var swap =
	{
		item_count : 0,
		box_selector : box_selector,
		item_selector : item_selector,
		switch_interval : switch_interval,
		switch_duration : switch_duration,
		direction : 'left',
		position : 0,

		init : function()
		{
			this.item_count = $(this.item_selector).length;
		},

		switch_direction : function()
		{
			this.direction = (this.direction == 'left') ? 'right' : 'left';
		},

		make_step : function(index, item)
		{
			var box = $(item);
			var items = $(item_selector);

			var box_width = box.width();
			var parent_width = box.parent().width();

			this.position += (this.direction == 'left') ? +1 : -1;
			var itemOffset = $(items[this.position]).position();

			if(this.direction == 'right' && this.position <= 0)
			{
				this.switch_direction();
			}
			if(this.direction == 'left' && (this.position > items.length - 1 || parent_width + itemOffset.left >= box_width))
			{
				this.switch_direction();
				this.position += (this.direction == 'left') ? +1 : -1;
				itemOffset = $(items[this.position]).position();
			}

			box.animate({ left: (-1 * itemOffset.left) }, this.switch_duration);
		},

		timer_callback : function()
		{
			$(this.box_selector).each($.proxy(this.make_step, this));
		}
	};

	swap.init();
	setInterval($.proxy(swap.timer_callback, swap), swap.switch_interval);
	return swap;
}
