$(document).ready(function(){
	
	/*
	current:			current item showing in slider
	max:				maximum number of items (calculated using content)
	timer:				variable for defining interval
	speed:				speed of interval
	transitionspeed:	speed of fade transition
	*/
	var current = 0;
	var max = 0;
	var timer;
	var speed = 6000;
	var transitionspeed = 1000;
	var url = "http://verzekeren.meeus.com/";
	
	$.getJSON(url + "slider/data.php?callback=?",  function(rtndata) {
		
		// set html data and some variables
		$("#mgnds_slider").html(rtndata);
		max = $('.insblock').length - 1;
		timer = setInterval(nextSlide, speed);
		
		// some settings
		$('.visual').hide();
		$('#visual0').delay(250).fadeIn();
		$('#block0').addClass('active');
		$('.selectionArrow').css({
			'left': 0
		})
		$('.selectionArrow').delay(300).animate({
			'left': 0
		})
		$('.insblock, .selectionArrow').css({
			"width": Math.floor( $("#mgnds_slider").width() / $('.insblock').length ) + "px!important"
		});
		
		// some alterations if visitor is using ie6
		if(typeof document.body.style.maxHeight === "undefined") {
			$('#mgnds_slider .selectionArrow').css({
				'background': 'url('+ url +'slider/arrow.gif) no-repeat center top'
			});
			$('#mgnds_slider .wrap').css({
				'background': 'url("'+ url +'getfile.php?id=107") no-repeat scroll left top transparent !important; filter:alpha(opacity=80)'
			});
		}
		
		// creating bindings
		$(".visuals").bind('mouseover', function(){
			clearInterval(timer);
		}).bind('mouseout', function(){
			timer = setInterval(nextSlide, speed);
		});
		
		$('.insblock').bind('mouseover', function(){
		    
		    clearInterval(timer);
		    
		    if(current != this.id.replace('block','')){
		    
		    	// reset class and visuals
				$('.insblock').removeClass('active');
				$('.visual').fadeOut(transitionspeed);
		    	
			    // fade in block
				$(this).addClass('active');
				$('#visual'+this.id.replace('block','')).fadeIn(transitionspeed);
				$('.selectionArrow').animate({'left': $(this).width() * this.id.replace('block','') + ($(this).width() * .5) - ($('.selectionArrow').width() * .5)});
		    }
		    
		    current = this.id.replace('block','');
		    return false;
		}).bind('mouseout', function(){
			timer = setInterval(nextSlide, speed);
		});
		
	});
	
	function nextSlide(){
		if(current < max){
			current++;
		} else {
			current = 0;
		}
		$('.insblock').removeClass('active');
		$('.visual').fadeOut(transitionspeed);
		$('#block'+current).addClass('active');
		$('#visual'+current).fadeIn(transitionspeed);
		$('.selectionArrow').animate({'left': $('#block'+current).width() * current + ($('#block'+current).width() * .5) - ($('.selectionArrow').width() * .5)});
		
	}

});
