$(function() {
	$('img#btnleft').hover(
		function() {$(this).attr('src', '/images/left_rollover.png');},
		function() {$(this).attr('src', '/images/left.png');}
	);

	$('img#btnright').hover(
		function() {$(this).attr('src', '/images/right_rollover.png');},
		function() {$(this).attr('src', '/images/right.png');}
	);

	$('img.accessoriesimage').mouseover(function(){
		var img = $(this).attr('name');
		updateImage(img);
		return false;
	}).mouseout(function(){
		var old = $('img#mainimage').attr('name');
		$('img#mainimage').attr('src', old);
	});

	$("a#fancyvideo").fancybox({'type':'youtube', 'width': 640, 'height':400});
	var aantal = $('div.slider_item').length;
	slide(0, aantal);

	$("div.continent-item").hover(function(){
			$(this).css('background-position','right');
		}, function(){
			$(this).css('background-position','left');
		});

});

var previous = -1;
var i = 0;
var t = 4000;
var timer = null;

function updateImage(image){
	$('img#mainimage').attr('src','/upload/accessories/large_'+image);
}

function slide(nr, aantal){
	$("#btnimg"+previous).attr('src', '/images/btnimg'+previous+'.png');
	$("#btnimg"+previous).hover(
		function() {$(this).attr('src', '/images/'+this.id+'_hover.png');},
		function() {$(this).attr('src', '/images/'+this.id+'.png');}
	);
	clearTimeout(timer);
	$('div[class^=slider_item]').each(function(i){
		$(this).clearQueue();
		if(i>=nr){
			$(this).delay((i-nr)*t).queue(function(){
				$("#scroller-content").animate({ "marginLeft": '-'+(i*880)+'px' }, 300, function(){
					if ((i+1)>=aantal){
						$('img#btnright').attr('src', '/images/right_inactive.png').unbind('mouseenter').unbind('mouseleave');
					} else {
						$('img#btnright').attr('src', '/images/right.png');
						$('img#btnright').hover(
							function() {$(this).attr('src', '/images/right_rollover.png');},
							function() {$(this).attr('src', '/images/right.png');}
						);
					}

					if (i<=0){
						$('img#btnleft').attr('src', '/images/left_inactive.png').unbind('mouseenter').unbind('mouseleave');
					} else {
						$('img#btnleft').attr('src', '/images/left.png');
						$('img#btnleft').hover(
							function() {$(this).attr('src', '/images/left_rollover.png');},
							function() {$(this).attr('src', '/images/left.png');}
						);
					}
				});
				previous = nr;
			});
		}
	});
	var tijd = t*(aantal-nr);
	timer = setTimeout('slide(0, '+aantal+')', tijd);

	/*$("#scroller-content").animate({ "marginLeft": '-'+(nr*880)+'px' }, 300,
		function(){
			previous = nr;
		}
	);*/
}

function slideNext(aantal){
	if (previous<aantal){
		slide(previous + 1, aantal);
	}
}

function slidePrev(aantal){
	if (previous>0){
		slide(previous - 1, aantal);
	}
}
