
var slider;

function mouseMove(e)
{	
	e = new Event(e).stop();
	slider.set( slider.step - (e.wheel * 100) );
}

window.addEvent('load', function() {
	updateScrollWidth();
	checkImages();
});

window.addEvent('resize', function() {
	updateScrollWidth();
});

function checkImages() 
{
	var widnow_width = window.getSize().x + 500;

	var divs = $$('.one_image');
	for(var i=0; i<divs.length; i++) 
	{
		if (divs[i].getChildren().length) 
		{	
			continue;
		}
		
		if ((parseInt($('slideshow_images').getPosition().x) + parseInt(divs[i].get('xpos'))) >= widnow_width) 
		{
			return;
		}

		var img = new Element('img', 
		{
			'src': divs[i].get('source')
		});
	
		divs[i].adopt(img);
	}
}

function updateScrollWidth() 
{
	if ($('slideshow') && $('scrollbar') && $('scrollbar_handle'))
	{
		createScroll( $('slideshow'), $('slideshow_images'), $('scrollbar'), $('scrollbar_handle'));
	}
}

function createScroll(content, checkContent, scrollbar, scroll_handle)
{
	if (checkContent.getScrollSize().x < scrollbar.getSize().x) 
	{
		scroll_handle.setStyle('display','none');
		
		return;
	}
	
	scroll_handle.setStyle('display', 'block');

	var steps = content.getScrollSize().x - content.getSize().x;
	slider = new Slider(scrollbar, scroll_handle, 
	{	
		steps: steps,
		mode: 'horizontal',
		onChange: function(step)
		{
			var x = step;
			var y = 0;
			content.scrollTo(x, y);
			checkImages();
		}
	}).set(0);
	
	$$(content, scrollbar).addEvent('mousewheel', mouseMove);

}


