function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return [curleft,curtop];
}

function scroll(fId){
	$(fId).css('overflow','hidden');
	var fNumber = $(fId +' > ul li').css({'width':'136', 'display':'block'}).size();
	var fWidth = (fNumber * 144) / 2;
	
	var hoverLeft = -5;
	var hoverRight = 5;
	var mouseDown = false;
	var move = true;
	
	var fUl = $(fId +' > ul')
		.css('width', ((fNumber) * 144) +'px')
		.bind("mouseenter", function(){hover(0);})
		.bind("mouseleave", hoverExit);
		
	$("#images").mousemove(function(e){
		var x = e.pageX - findPos(this)[0];
		if (x < 200){
			if (x < 150){
				play();
				var calc = (x - 150) / 1.5;
				hover(-(hoverLeft * (calc / 100)));
			} else {
				hold();
			}
		} else {
			if (x > 250){
				play();
				var calc = (x - 250) / 1.5;
				hover(hoverRight * (calc / 100));
			} else {
				hold();
			}
		}
	});
	
	$("#images").mouseleave(function(e){
		mouseDown = false;
		play();
	});
	$("#images").mousedown(function(e){
		mouseDown = true;
		hold();
	});
	$("#images").mouseup(function(e){
		mouseDown = false;
		play();
	});
	
	$('#left').bind("mouseenter", function(){hover(hoverLeft);}).bind("mouseleave", hoverExit);
	$('#right').bind("mouseenter", function(){hover(hoverRight);}).bind("mouseleave", hoverExit);
	
	var fPosition = 0;
	var fDefaultSpeed = fSpeed = 1;
	var loopTimer;
	
	function hold(){
		move = false;
		clearTimeout(loopTimer);
	}

	function play(){
		if (move == false && mouseDown == false){
			move = true;
			loop();
		}
	}
	
	function hover(s){
		fSpeed = s;
	}
	function hoverExit(){
		if (fSpeed > 0){
			fSpeed = fDefaultSpeed;
		} else {
			fSpeed = fDefaultSpeed * -1;
		}
	}
	function loop(){
		if (move){
			fPosition += fSpeed;
			if(fPosition > fWidth)
				fPosition -= fWidth;
			if(fPosition < 0)
				fPosition += fWidth;
				
			fUl.css('left', (fPosition * -1) +'px');
			loopTimer = setTimeout(loop, 30);
		}
	}
	loop();
}

function moreInformation(QueryStringLink){
	$.get("/meer-informatie.asp", {page: "meer-informatie", product: QueryStringLink, ajax: true}, function(html){
		var div_information = $(document.createElement("div"))
			.attr("id", "more_information")
			.html(html)
			.append($(document.createElement("div"))
				.attr("id", "close")
				.bind("click", new Function('hideInformation();'))
				.text("sluit ")
				.append($(document.createElement("img"))
					.attr("src", "/images/close.png")
					.attr("alt", "Sluit") ) );
		if ($("#information").length > 0){
			$("#information #more_information").remove();
			$("#information").append(div_information);
		} else {
			var div_background = $(document.createElement("div"))
				.attr("id", "information_background");
			var br = $(document.createElement("br"));
			var div = $(document.createElement("div"))
				.attr("id", "information");
			div.append(div_background);
			div.append(br);
			div.append(div_information);
			div.appendTo(document.body);
		}
	});
}

function allProducts(QueryStringLink, type){
	$.get("/meer-informatie.asp", {page: "alle-artikelen", type: type, link: QueryStringLink, ajax: true}, function(html){
		var div_information = $(document.createElement("div"))
				.attr("id", "more_information")
				.html(html)
				.append($(document.createElement("div"))
					.attr("id", "close")
					.bind("click", new Function('hideInformation();'))
					.text("sluit ")
					.append($(document.createElement("img"))
						.attr("src", "/images/close.png")
						.attr("alt", "Sluit") ) )
				.append($(document.createElement("br"))
					.attr("style", "clear:both;") );
		if ($("#information").length > 0){
			$("#information #more_information").remove();
			$("#information").append(div_information);
		} else {
			var div = $(document.createElement("div"))
				.attr("id", "information")
				.append($(document.createElement("div"))
					.attr("id", "information_background"))
				.append($(document.createElement("br")))
				.append(div_information)
				.appendTo(document.body);
		}
	});
}

function hideInformation(){
	$("#information").remove();
}
