// Tikwerk besparen
function $(e) {
	return document.getElementById(e);
}

function Slider(sliderId, numItems) {
	this.currentImage = 0;
	this.timeout = false;
	this.doubleClickUrl = '';
	
	var steps = 10;
	var stepFrom;
	var stepTo;
	var imageHeight = 116;
	var thumbId = '';
	var mainImg;
	
	this.setImageHeight = function(h) {
		imageHeight = h + 8;
	}
	
	this.setThumbIdStart = function(thumb) {
		thumbId = thumb;
	}
	
	this.setMainImg = function(img) {
		mainImg = img;
	}
	
	this.clickThumb = function(n) {
		if(thumbId) {
			$(thumbId + '' + this.currentImage).className = '';
			$(thumbId + '' + n).className = 'active';
			if(mainImg) {
				$(mainImg).src = $(thumbId + '' + n).childNodes[0].src.replace('/small', '');
			}
		}
		this.currentImage = n;
		stepFrom = $(sliderId).scrollTop;
		stepTo = (n - .5) * imageHeight;
		this.doStep(0);
		this.callStatistics();
	}
	
	this.doStep = function(step) {
		step++;
		$(sliderId).scrollTop = factor(step / steps) * (stepTo - stepFrom) + stepFrom
		//alert(stepTo + ' ' + step + ' ' + steps)
		if (this.timeout) {
			clearTimeout(this.timeout);
		}
		if (step < steps) {
			var _this = this;
			this.timeout = setTimeout(function() { _this.doStep(step); } , 10);
		}
	}
	
	this.scrollUp = function() {
		$(sliderId).scrollTop-=3;
		var _this = this;
		this.timeout = setTimeout(function() { _this.scrollUp(); } , 1);
	}
	
	this.scrollDown = function() {
		$(sliderId).scrollTop+=3;
		var _this = this;
		this.timeout = setTimeout(function() { _this.scrollDown(); } , 1);
	}
	
	this.stop = function() {
		clearTimeout(this.timeout);
	}
	
	this.up = function() {
		var next = this.currentImage - 1;
		if (next < 0) {
			next = 0;
		}
		this.clickThumb(next);
	}
	
	this.down = function() {
		next = this.currentImage + 1
		if (next > numItems - 1) {
			next = numItems - 1;
		}
		this.clickThumb(next);
	}
	
	factor = function(f) {
		return .5 - Math.cos(f * Math.PI) * .5
	}
	
	this.initialize = function() {
		/*
		var txt = '';
		for(img in photos) {
			txt += '<a href="#" onclick="slider.clickThumb(' + img + ');return false;">';
			cls = (img == 0) ? 'on' : 'thumb'
			txt += '<img id="thumb' + img + '" class="' + cls + '" src="' + photos[img] + '"/>'
			txt += '</a>';
		}
		$('thumbnails').innerHTML = txt;
		$('photoXL').src = photos[0].replace('thumbs', 'groot');
		*/
		if(thumbId) {
			$(thumbId + '' + this.currentImage).className = 'active';
		}
		//h = Math.floor($(sliderId).offsetHeight / imageHeight) * imageHeight;
		//$(sliderId).style.height = h + 'px';
	}
	
	this.callStatistics = function() {
		if (!$("stats")) return;
		$("stats").src = doubleClickUrl;
	}
}

