// JavaScript Document
var currentscroller;
(function($) {
	$.fn.vhascroller = function(options) {
		var opts = $.extend({}, $.fn.vhascroller.defaults, options);
		return this.each(function() {
			var $this = $(this);
			if($this.find('li').length > 0){
				$this.opts 				= $.extend({}, opts);
				currentscroller 		= $this;
				$this.opts.height = $this.find('img:eq(0)').height();
				
				// SLIDESPEED VASTSTELLEN
				$('#scroller').bind('mouseout',function(){
					clearTimeout($this.opts.stoptimeout);
					$this.opts.stoptimeout = setTimeout(function(){ $.fn.vhascroller.mouseout($this)},10)
				}).bind('mousemove',function(e){
					$this.opts.movetotarget = null;
					clearInterval($this.opts.fadeoutinterval);
					clearTimeout($this.opts.stoptimeout);
					eenderde = $(document).width()/3;
					if(e.pageX >= eenderde && e.pageX <= eenderde*2){
						$this.opts.slidespeed = 0;						
					}else if(e.pageX >= eenderde*2){
						mouseposition = (e.pageX-(eenderde*2)) / eenderde; // Math.round
						$this.opts.slidespeed = -(mouseposition*$this.opts.slidesensitivity);
						if($this.opts.slidespeed < -1 * $this.opts.maxspeed) 	{ $this.opts.slidespeed = -1 * $this.opts.maxspeed; }
						if($this.opts.slidespeed > $this.opts.maxspeed) 	{ $this.opts.slidespeed = $this.opts.maxspeed; }				
					}else{
						mouseposition = -((eenderde - e.pageX) / eenderde); // Math.round
						$this.opts.slidespeed = -(mouseposition*$this.opts.slidesensitivity);
						if($this.opts.slidespeed < -1 * $this.opts.maxspeed) 	{ $this.opts.slidespeed = -1 * $this.opts.maxspeed; }
						if($this.opts.slidespeed > $this.opts.maxspeed) 	{ $this.opts.slidespeed = $this.opts.maxspeed; }
					}
					//$("#top #contents").html($this.opts.slidespeed);
				});
				
				$this.opts.scrollwidth = 0;
				$this.opts.sliderwidth = 0;
				
				$this.find('li').each(function(i){
					$(this).addClass('original');
					$(this).attr('leftpos',$this.opts.scrollwidth);	
					$(this).attr('number',i);
					$this.opts.scrollwidth += $(this).width();
					
					thumbwidth = Math.round($(this).width()/$this.opts.thumbscale);
					$(".thumbs ul").append('<li number="'+i+'"></li>')
					thumb = $(".thumbs ul li[number="+i+"]");
					thumb.attr('leftpos',$this.opts.scrollwidth);	
					thumb.attr('number',i);	
					thumb.css({'width' : thumbwidth+'px'})
					$this.opts.sliderwidth += thumbwidth + 2;
					thumb.hover(function(){
						 $this.find('li:not([number='+$(this).attr('number')+'])').stop().fadeTo(200,0.3);
						//clearInterval($this.opts.fadeoutinterval);
//						$this.opts.slidespeed = 0;
//						$("#scroller").stop().animate({left:'-'+$(this).attr('leftpos')+'px'},500);
					},function(){
						$this.find('li').stop().fadeTo(200,1);
					});
					thumb.click(function(){
						 var thumb2 = $(this);
						 clearInterval($this.opts.fadeoutinterval);
						 $this.opts.movetotarget = thumb2.attr('number');
						// alert('En nu? Scrollen naar de thumb? Thumb links houden?');	
					});
				});
				
				$(".thumbs").css({'width':Math.round($this.opts.sliderwidth)+'px'})
				$this.opts.thumbscalereal = $this.opts.scrollwidth / $this.opts.sliderwidth;
				$(".thumbs .slider, .thumbs .slider2").css({'width':Math.round($(document).width()/$this.opts.thumbscalereal)+'px'})
				
				$this.css('width',$this.opts.scrollwidth+'px');
				
				
				// STATS VAN SCROLLER SETTEN EN NA RESIZE POSITIONEREN
				$.fn.vhascroller.setstatusandposition($this);
				
				
				// BEGIN MAAR MET SCROLLEN
				$.fn.vhascroller.startautoslide($this);
			}
		});
	};
	
	$.fn.vhascroller.defaults = {
		height: 			142,
		slidesensitivity: 	15,
		maxspeed: 			8,
		slidespeed: 		0,
		slideinterval:  	null,
		fadeoutinterval:  	null,
		stoptimeout:  		null,
		movetotarget:		null,
		thumbscale:			10,
		scrollwidth:		0,
		openspeed:			500,
		closespeed:			500
	};
	
	$.fn.vhascroller.mouseout = function($this){
		$this.opts.fadeoutinterval = setInterval(function(){ $this.opts.slidespeed = $this.opts.slidespeed / 1.05 },100);
	}
	$.fn.vhascroller.startautoslide = function($this){
		$.fn.vhascroller.stopautoslide($this);
		$this.opts.slideinterval = setInterval(function(){ $.fn.vhascroller.slide($this)},20)
	}
	
	$.fn.vhascroller.stopautoslide = function($this){
		clearInterval($this.opts.slideinterval);
	}
	
	$.fn.vhascroller.unloadplugin = function($this){
		clearInterval($this.opts.slideinterval);
		$.fn.vhascroller.close($this);
		currentscroller = null;
	}
	
	$.fn.vhascroller.slide = function($this){
		if($this.opts.movetotarget != null){
			if(Math.abs($this.find('li[number='+$this.opts.movetotarget+']').offset().left) < 1){
				$this.opts.slidespeed = 0;
			}else{
				$this.opts.slidespeed = ((-$this.find('li[number='+$this.opts.movetotarget+']').offset().left) / 8);
				if($this.opts.slidespeed >= -1 && $this.opts.slidespeed <= 2){
					if($this.opts.slidespeed < 0) 	$this.opts.slidespeed = -1;
					else							$this.opts.slidespeed = 1;
				}
			}
		}
		var oldpos 			= parseInt($this.css('left'))
		var newpos 			= parseInt(oldpos + $this.opts.slidespeed);
		var first 			= $("#scroller ul li:first-child");
		var firstwidth 		= parseInt(first.width());
		var last 			= $("#scroller ul li:last-child");
		var lastwidth 		= parseInt(last.width());
		if(oldpos <= 0 && newpos > 0){
			oldpos -= lastwidth;
			newpos = oldpos + $this.opts.slidespeed;
			$("#scroller ul").css({'left':oldpos+'px'});
			last.prependTo('#scroller ul');
		}else if((newpos) <= -firstwidth && oldpos < 0){
			oldpos += firstwidth;
			newpos = oldpos + $this.opts.slidespeed;
			$("#scroller ul").css({'left':oldpos+'px'});
			first.appendTo('#scroller ul');
		}
		
		var sliderpos = parseInt($("#scroller ul li:first-child").attr('leftpos')) - Math.round(newpos);
		sliderpos = Math.round(sliderpos/$this.opts.thumbscalereal);
		$(".thumbs .slider").css({'left':sliderpos+'px'})
		
		slider2pos = -($(".thumbs").width() - sliderpos);
		$(".thumbs .slider2").css({'left':slider2pos+'px'})
		$this.css('left',Math.round(newpos)+'px');
	};
	
	$.fn.vhascroller.open = function($this){
		$(".thumbs").slideDown($this.opts.openspeed);
		$this.opts.status = 'open';
		$("#top").stop().animate({'height':$("#top").attr('height')-Math.ceil($this.opts.height/2)+'px'},$this.opts.openspeed)
		$("#scroller ul").animate({'top':'0px'},$this.opts.openspeed)
		$("#scroller").animate({height:$this.opts.height+'px'},$this.opts.openspeed);
	};
	
	$.fn.vhascroller.close = function($this){
		$(".thumbs").slideUp($this.opts.closespeed);
		$this.opts.status = 'close';
		$("#top").stop().animate({'height':$("#top").attr('height')+'px'},$this.opts.closespeed)
		$("#scroller ul").stop().animate({top:Math.ceil($this.opts.height/-2)+'px'},$this.opts.closespeed);
		$("#scroller").stop().animate({'height':'0px'},$this.opts.closespeed);
	};
	
	$.fn.vhascroller.setstatusandposition = function($this){
		if($("#scroller").height() > 1 || $this.opts.status == 'open'){
			$this.opts.status = 'open';
//			if($("#top").css('height') != $("#top").attr('height')-Math.ceil($this.opts.height/2)+'px'){
				$("#top").stop().animate({'height':$("#top").attr('height')-Math.ceil($this.opts.height/2)+'px'},400);
				$("#scroller ul").css({'top':'0px'})
				$("#scroller").css({height:$this.opts.height+'px'});
//			}
		}else{
			$this.opts.status = 'close';
//			if($("#top").css('height') != $("#top").attr('height')+'px'){
				$("#top").stop().animate({'height':$("#top").attr('height')+'px'},400)
				$("#scroller ul").css({top:Math.ceil($this.opts.height/-2)+'px'});
				$("#scroller").css({'height':'0px'},$this.opts.closespeed);
//			}
		}
		if(parseInt($this.css('width')) > 0){
			while(parseInt($this.css('width')) < ($(document).width()+400)){
				var scrollul = $this;
				var clones = $this.find('.original').clone();
				$(clones).removeClass('.original').appendTo("#scroller ul");
				$(clones).find('a').removeAttr('keyword').removeAttr('flaginit');
				scrollul.css('width',(parseInt(scrollul.css('width'))+$this.opts.scrollwidth)+'px');
			}
		}
		initLinks();
		initFlags();
		$(".thumbs .slider, .thumbs .slider2").css({'width':Math.round($(document).width()/$this.opts.thumbscalereal)+'px'});
	};
	
	
})(jQuery);
