$(window).load(function() {
						
	itemCount=$("div.scroll-content-item").length;
	itemWidth=parseInt($("div.scroll-content-item").css("width"));
	$("div.scroll-content").css("width", itemCount * itemWidth + 'px');
	var scrollPane = $('.scroll-pane');
	var scrollContent = $('.scroll-content');
	var stillSliding = false;
	//speed settings.
	var changeSpeedDemo=4400;
	if (IsSlideDemo == '1') changeSpeedDemo = 0;
	var slideSpeed = 500;
	$(".scroll-bar").data("slideSpeed",slideSpeed); // while sliding scrollbar left or right with mouse
	$(".scroll-bar").data("changeSpeed",0); //while clicking on place on scrollbar, redefined later
	
	
	

	var scrollbar = $(".scroll-bar").slider({
		value:100,
		animate: slideSpeed,
		start : function(event, ui) {
			start = ui.value;
		},
		slide : function(e, ui) {
			if (scrollContent.width() > scrollPane.width()) {
				end = ui.value;
				if (stillSliding) {
					$(this).data("slideSpeed",3);
				} else {
					if (Math.abs(start - end) > 1) {$(this).data("slideSpeed",slideSpeed);} else {$(this).data("slideSpeed",0);}
				}
				scrollContent.animate( {
					"margin-left" : Math.round(ui.value/100 * (scrollPane.width() - scrollContent.width())) + 'px'}, $(this).data("slideSpeed"));
					stillSliding = true;
			} else {
				scrollContent.css('margin-left',  marginLeft + 'px');
			}
		},
		change: function(e, ui) {
			
			// now disabled due to no neccesary of anchor-labels (with animation after clicking on label)
															  
			if (scrollContent.width() > scrollPane.width()) {
				scrollContent.stop().animate({"margin-left" : Math.round(ui.value/100	* (scrollPane.width() - scrollContent.width())) + 'px'}, $(this).data("changeSpeed"));
			}
			else {
				scrollContent.css('margin-left',  marginLeft + 'px');
			}
		},
		stop : function(e, ui) {
			stillSliding = false;
		}

		
	});
	
	if (itemCount<=5){
		marginLeft = Math.round((scrollPane.width() - itemCount * itemWidth)/2);
		scrollContent.css("display", "inline"); //needs for ie double margin bug
		scrollContent.css("margin-left", marginLeft + 'px');
		$(".ui-slider-handle").css("display", "none");
	}
	var handleHelper = scrollbar.find('.ui-slider-handle').mousedown(
			function() {
				scrollbar.width(handleHelper.width());
			}).mouseup(function() {
		scrollbar.width('100%');
	})
	.wrap('<div class="ui-handle-helper-parent"></div>').parent();
	// change overflow to hidden now that slider handles the scrolling
	scrollPane.css('overflow', 'hidden');
	// size scrollbar and handle proportionally to scroll distance
	function sizeScrollbar() {
		var remainder = scrollContent.width() - scrollPane.width();
		var proportion = remainder / scrollContent.width();
		var handleSize = 184;
		scrollbar.find('.ui-slider-handle').css( {
			width : handleSize,
			'margin-left' : -handleSize / 2 //left padding, right padding - in css
		});
		handleHelper.width('').width(scrollbar.width() - handleSize);
	}
	function resetValue() {
		var remainder = scrollPane.width() - scrollContent.width();
		var leftVal = scrollContent.css('margin-left') == 'auto' ? 0 : parseInt(scrollContent.css('margin-left'));
		var percentage = Math.round(leftVal / remainder * 100);
		scrollbar.slider("value", percentage);
	}
	function reflowContent() {
		var showing = scrollContent.width()
				+ parseInt(scrollContent.css('margin-left'));
		var gap = scrollPane.width() - showing;
		if (gap > 0) {
			scrollContent.css('margin-left', parseInt(scrollContent
					.css('margin-left'))
					+ gap);
		}
	}
	setTimeout(sizeScrollbar, 10);// safari wants a timeout
	if (itemCount<=5){
		var stepLeftPassive=$('<a id="stepLeftPassive"></a>');
		var stepRightPassive=$('<a id="stepRightPassive"></a>');
		$(".scroll-bar-wrap").append(stepLeftPassive);
		$(".scroll-bar-wrap").append(stepRightPassive);
	}else{
		var stepLeft=$('<a id="stepLeft"></a>')
			.click( function() {
				$(".scroll-bar").slider("value",$(".scroll-bar").slider("value") - 21);		 
			});
		var stepRight=$('<a id="stepRight"></a>')
			.click( function() {
				$(".scroll-bar").slider("value",$(".scroll-bar").slider("value") + 21);	 
			});	
		$(".scroll-bar-wrap").append(stepLeft);
		$(".scroll-bar-wrap").append(stepRight);
	}
	
	//$(".scroll-bar").slider({ animate: false });
	scrollPane.bind('mousewheel',
					function (event, delta) {
						$(".scroll-bar").slider("value", $(".scroll-bar").slider("value")-delta*2);
						return false;
					}
	);
	//setTimeout(function(){ $(".scroll-bar").slider("value",10 ); }, 5000);
	
	$(".scroll-bar").slider("value", 100);
	$(".scroll-bar").data("changeSpeed",changeSpeedDemo);
	$(".scroll-bar").slider("option", "animate", changeSpeedDemo);
	$(".scroll-bar").slider("value", 0);
	$(".scroll-bar").slider("option", "animate", 250);
	$(".scroll-bar").data("changeSpeed", 500);
	
	//$("#cat1").onselectstart(function
	$("#cat1").click(function(e){$(".scroll-bar").slider("value", 0);});
	$("#cat2").click(function(e){$(".scroll-bar").slider("value", 37);});
	$("#cat3").click(function(e){$(".scroll-bar").slider("value", 78);});
	$("#cat4").click(function(e){$(".scroll-bar").slider("value", 98);});
	$("#cat5").click(function(e){$(".scroll-bar").slider("value", 64);});
	$("#cat6").click(function(e){$(".scroll-bar").slider("value", 100);});	
	$("#slider span, #slider div, #slider a").disableSelection();
});

jQuery.fn.extend({ 
        disableSelection : function() { 
                return this.each(function() {
					//console.log(this.tagName.toLowerCase());
					this.onselectstart = function() { return false; }; 
					this.unselectable = "on"; 
					jQuery(this).css('-moz-user-select', 'none'); 
                }); 
        } 
}); 
