var scrollPane;
var scrollContent;
var percentage;
var scrollbar;
var handleHelper;

$(document).ready(function(){
			
	$("#faqlist h2:first").addClass("active");

	$(".notarimmo").click(function(){
		$("#notarimmo_slider").slideToggle("slow");
		$("#faqlist h2:first").toggleClass("active"); return false;
		$(this).siblings("h2").removeClass("active");
	});
	
	/*
	$('ul#subhome-slideshow').innerfade({
		speed: 3000,
		timeout: 10000,
		type: 'sequence',
		containerheight: '150px'
	});
	*/
	
	// initialize scrollable  
	$("div.scrollable").scrollable({ 
		items:'div.tekoop-lijst', 
		vertical:true,
		size: 2
	}).mousewheel();
			
	var vertical = $("div.scrollable");
	vertical.eq(0).scrollable().focus();
			
	var api = $("div.scrollable").scrollable({api:true});	
	api.onSeek(function (){
		resetValue();
		var mTop = String(($("div.scrollable").height() - $('.ui-slider-handle').height()) * (scrollbar.slider("value") / 100)) + 'px';
		scrollbar.css('margin-top', mTop);
	});
			
	/**
	 * Scrollbar
	 */
	scrollPane = $('.scrollable');
	scrollContent = $('.scroll-content');
	percentage = 0;
	
	if(scrollContent.height() <= scrollPane.height()){
		$('li.next-li, li.previous-li').hide();
		$('#scroll_bar_wrapper').hide();
	}else{
		setSlider()
	}

});

function setSlider(){
	//build slider
	scrollbar = $(".scroll-bar").slider({
		orientation: "vertical",
		slide:function(e, ui){
			if( scrollContent.height() > scrollPane.height() ){ 
				scrollContent.css('top', Math.round( (100- ui.value) / 100 * ( scrollPane.height() - scrollContent.height() )) + 'px');
				scrollbar.css('margin-top', Math.round((100- ui.value) / 100 * $('.ui-slider').height()));
			}else { 
				scrollContent.css('top', 0); 
			}
		}
	});
	
		
	//append icon to handle
	handleHelper = scrollbar.find('.ui-slider-handle')
	.mousedown(function(){
		scrollbar.height( handleHelper.height() );
	})
	.mouseup(function(){
		scrollbar.height( '100%' );
	}).append('<span class="ui-icon ui-icon-grip-dotted-vertical"></span>')
	.wrap('<div class="ui-handle-helper-parent"></div>').parent();

		
	//change handle position on window resize
	$(window)
	.resize(function(){
			resetValue();
			sizeScrollbar();
			reflowContent();
	});
	
	//init scrollbar size
	setTimeout(sizeScrollbar,10);//safari wants a timeout
}

//size scrollbar and handle proportionally to scroll distance
function sizeScrollbar(){
	var remainder = scrollContent.height() - scrollPane.height();
	var proportion = remainder / scrollContent.height();
	var handleSize = scrollPane.height() - (proportion * scrollPane.height());

	scrollbar.find('.ui-slider-handle').css({
		height: handleSize,
		'margin-top': 0
	});
	handleHelper.height('').height( scrollbar.height() - handleSize);
}
	
//reset slider value based on scroll content position
function resetValue(){
	var remainder = scrollPane.height() - scrollContent.height();
	var topVal = scrollContent.css('top') == 'auto' ? 0 : parseInt(scrollContent.css('top'));
		var percentage = Math.round(topVal / remainder * 100);
		
		if(percentage > 100) percentage = 100;

		scrollbar.slider("value", percentage);
}
//if the slider is 100% and window gets larger, reveal content
function reflowContent(){
		var showing = scrollContent.height() + parseInt( scrollContent.css('top') );
		var gap = scrollPane.height() - showing;
		if(gap > 0){
			scrollContent.css('top', parseInt( scrollContent.css('top') ) + gap);
		}
}
