//Javascript for janvandenbon.be // By 9000 Studios // Using jquery.

$(document).ready(function(){
	//SET UP ROTATION FOR SIDEBAR BUTTONS
	$("#rightBar a img").rotate({maxAngle:0,minAngle:-15,bind:
	[{"mouseover":function(){$(this).rotateAnimation(-15);}},{"mouseout":function(){$(this).rotateAnimation(0);}}]
	});
	
	//SET UP CONTROLS FOR SLIDER
	var posit = 0;
	var total = $("#rightControls UL LI").length*55-(12*55);
	var globalTimer;
	var speed = 4000;
	var currentSlide = 0;
	var totalSlides = $("#rightControls UL LI").length;
	if(totalSlides>0){
		$("#controls").show();
		if(total<=12){
			$("#forward").remove();
		}
	} else {
		$("#imageSlider").hide();
	}
	setActive($("#slideThumbs li:eq(0) a"),currentSlide,totalSlides,posit,total)
	globalTimer=setTimeout(nextSlide,speed);
	var firstId = $("#slideThumbs li:eq(0) a").attr("title");
	$("#back").css("cursor","default");
	$("#back").fadeTo(0,0.001);
	$("#back").click(function(){
		if (posit < 0) {
			$("#rightControls UL").animate({marginLeft: "+=165"}, 1000);
			posit += 165;
			$("#forward").fadeTo(1000,1);
			$("#forward").css("cursor","pointer");
			if (posit == 0) {
				$("#back").css("cursor","default");
				$("#back").fadeTo(1000,0.001);
			}
		}
		return false;
	});
	$("#forward").click(function(){
		if(posit > -total){
			$("#rightControls UL").animate({marginLeft: "-=165"},1000);
			posit -= 165;
			$("#back").fadeTo(1000,1);
			$("#back").css("cursor","pointer");
			if(posit <= -total){
				$("#forward").css("cursor","default");
				$("#forward").fadeTo(1000,0.001);
			}
		}
		
		return false;
	});
	
	//SET UP CLICK HANDLERS FOR SLIDER
	$("#slideThumbs li a").click(function(){
		clearTimeout(globalTimer);
		globalTimer=setTimeout(nextSlide,speed);
		var parent = $(this).parent();
		currentSlide = $("#slideThumbs li").index(parent);
		posit = setActive($(this),currentSlide,totalSlides,posit,total);
		loadImg($(this).attr("href"));
		return false;
	});
	
	//SET UP TIMER FOR SLIDESHOW
	function nextSlide(){
		clearTimeout(globalTimer);
		if(currentSlide>(totalSlides-2))
		{
			currentSlide = 0;	
		} else {
			currentSlide++
		}
		goTo(currentSlide);
		globalTimer=setTimeout(nextSlide,speed);	
	}
	
	function goTo(current){
		loadImg($("#slideThumbs li:eq("+current+") a").attr("href"));
		posit = setActive($("#slideThumbs li:eq("+current+") a"),currentSlide,totalSlides,posit,total);
	}
	
	function loadImg(src){
		if($("#slideWrapper img").length>1){
			$("#slideWrapper img:eq(0)").remove();
		}
		var img = new Image();
		$(img).load(function(){
			$(this).hide();
			$("#slideWrapper").append(this);
			$(this).fadeIn();
		}).attr('src',src)
		return false;
	}
});
function setActive(id,currentSlide,totalSlides,posit,total){
	$("#slideThumbs li").each(function(){
		$(this).removeClass("active"); 
	});
	$(id).parent().addClass("active");
	if(-((currentSlide*55)-11*55)<posit){
		if(currentSlide>(totalSlides-2))
		{
			$("#rightControls UL").animate({marginLeft: "0"},1000);
			$("#back").css("cursor","default");
			$("#back").fadeTo(1000,0.001);
			return 0;
		} else {
			$("#rightControls UL").animate({marginLeft: -((currentSlide*55)-11*55)},1000);
			$("#back").fadeTo(1000,1);
			$("#back").css("cursor","pointer");
			return posit-55;
		}
	}
	return posit;
}

function rPosition(elementID, mouseX, mouseY) {
  var offset = $(elementID).offset();
  var x = mouseX - offset.left;
  var y = mouseY - offset.top;
  return {'x': x, 'y': y};
}
