var win;

// JavaScript Document
function makeScrollbar(content,scrollbar,handle,horizontal,ignoreMouse){
	var steps = (horizontal?(content.getScrollSize().x - content.getSize().x):(content.getScrollSize().y - content.getSize().y))
	var timer;
	var scrolling;
	var scrollLeftTimer = function () {
		if (scrolling == true) {
			var step = slider.step - 50;	
			slider.set(step);
			var timer = scrollLeftTimer.delay(100);
		}
	};
	var scrollRightTimer = function () {
		if (scrolling == true) {
			var step = slider.step + 50;	
			slider.set(step);
			var timer = scrollRightTimer.delay(100);
		}
	};
	var slider = new Slider(scrollbar, handle, {	
		steps: steps,
		mode: (horizontal?'horizontal':'vertical'),
		onChange: function(step){
			// Scrolls the content element in x or y direction.
			var x = (horizontal?step:0);
			var y = (horizontal?0:step);
			content.scrollTo(x,y);
		}
	}).set(0);
	if( !(ignoreMouse) ){
		// Scroll the content element when the mousewheel is used within the 
		// content or the scrollbar element.
		$$(content, scrollbar).addEvent('mousewheel', function(e){	
			e = new Event(e).stop();
			var step = slider.step - e.wheel * 30;	
			slider.set(step);					
		});
		
		$('scrollLeft').addEvent('mousedown', function(e) {
			scrolling = true;
			e = new Event(e).stop();
			var timer = scrollLeftTimer.delay(100);
			
		});
		$('scrollLeft').addEvent('mouseup', function(e) { e = new Event(e).stop(); timer = $clear(timer); scrolling = false; });
		$('scrollLeft').addEvent('mouseout', function(e) { e = new Event(e).stop(); timer = $clear(timer); scrolling = false;});
		$('scrollRight').addEvent('mousedown', function(e) {
			scrolling = true;
			e = new Event(e).stop();
			var timer = scrollRightTimer.delay(100);
		});
		$('scrollRight').addEvent('mouseup', function(e) { e = new Event(e).stop(); timer = $clear(timer); scrolling = false; });
		$('scrollRight').addEvent('mouseout', function(e) { e = new Event(e).stop(); timer = $clear(timer); scrolling = false;});
	}
	// Stops the handle dragging process when the mouse leaves the document body.
	$(document.body).addEvent('mouseleave',function(){slider.drag.stop()});
}
	
window.addEvent('domready', function(){				
	// -- first example, vertical scrollbar --
	makeScrollbar( $('photos'), $('photoScrollBar'), $('photoScrollHandle'), true );

	$('photoClose').addEvent('click', function(e) { closePhoto(); });
	$('photoImages').addEvent('click', function(e) {
		$('photoList').style.display = "block";
		$('photoLeft').style.zIndex = -1;
		$('photoRight').style.zIndex = -2;
	});
	
	$('photoFullscreen').addEvent('click', function(e) {
		if(win == undefined || win.closed == true) {
			win = window.open(photoSection + "/fullscreen/",null,"fullscreen=yes");
			win.file = photos[currentPhoto];
		} else {
			win.file = photos[currentPhoto];
			win.setBackgroundImage();
		}
	});
	
	$('photoLeft').addEvent('click', function(e) {
		if (currentPhoto <= 0) { currentPhoto = photos.length; }
		photoNum = currentPhoto - 1;
		changePhoto(photoNum);
	});
	$('photoRight').addEvent('click', function(e) {
		if (currentPhoto >= photos.length -1) { currentPhoto = -1; }
		photoNum = currentPhoto + 1;
		changePhoto(photoNum);
	});
	closePhoto();
	
});
var closePhoto = function() {
	$('photoList').style.display = "none";
	$('photoLeft').style.zIndex = 1;
	$('photoRight').style.zIndex = 2;
}
var changePhoto = function(file) {
	document.getElementById('photo').style.background = "url('" + photoSection + "/photos/" + photos[file] + "') top no-repeat";
	currentPhoto = file;
}
