$(document).ready (function () {

	$("#menu li").hover(
		function() {
			$(this).addClass("sfhover");
		},
		function() {
			$(this).removeClass("sfhover");
		}
	);

	if ($("#slider").length > 0) {
		$("#slider").slider({ 
			orientation: "vertical",
			change: handleChange,
			slide: handleSlide,
			min: -100,
			max: 0 
		});
		
		$("#scroller").bind('mousewheel', function(event, delta) {
			$("#slider").slider("value",$("#slider").slider("value")+delta);
            return false;
        });
	}
	
	var a = self.document.location.hash.substring(1);
	var i = 0;
	$('#scroller li a').each (function(){
		if ($(this).attr('id') == a) {
			$(this).css('font-weight','bold');
			$("#slider").slider("value",i / $('#scroller li a').length * -100);
		}
		i++;
	});
	
	$('dd ul li.basket').append('<img src="/img/basket_put.png" style="float:right; margin-right:110px" />');
	
	bind_gallery ();
});

function handleChange(event, ui)
{
	var maxScroll = $("#scroller").attr("scrollHeight") - $("#scroller").height();
	$("#scroller").attr({ scrollTop: -ui.value * (maxScroll / 100) });
	// $("#scroller").animate({ scrollTop: -ui.value *	(maxScroll / 100)}, 1000);
}

function handleSlide(event, ui)
{
	var maxScroll = $("#scroller").attr("scrollHeight") -$("#scroller").height();
	$("#scroller").attr({ scrollTop: -ui.value * (maxScroll / 100) });
}

/**
 * gallery
 **/
function bind_gallery ()
{
	// http://coffeescripter.com/code/ad-gallery/
	var galleries = $('.ad-gallery').adGallery({
		width: 440, // Width of the image, set to false and it will read the CSS width
  		height: 400, // Height of the image, set to false and it will read the CSS height
  		thumb_opacity: 0.7, // Opacity that the thumbs fades to/from, (1 removes fade effect)
		animation_speed: 400, // Which ever effect is used to switch images, how long should it take?
		display_next_and_prev: false, // Can you navigate by clicking on the left/right on the image?
  		display_back_and_forward: true, // Are you allowed to scroll the thumb list?
  		slideshow: {
		    enable: false,
		    autostart: true,
		    speed: 4000,
		    start_label: 'Start',
		    stop_label: 'Stop',
		    stop_on_scroll: true, // Should the slideshow stop if the user scrolls the thumb list?
		    countdown_prefix: '(', // Wrap around the countdown
		    countdown_sufix: ')'
		  },
		  effect: 'slide-hori' // 'slide-hori' or 'slide-vert', 'resize', 'fade', 'none' or false
	});

	if ($('.ad-gallery .ad-thumb-list li').length <= 1) $('.ad-gallery .ad-nav').hide();

	if ($('#product').length > 0) {
        if ($('.ad-gallery').length < 1) $('h2.product-header').css('padding-left', '25px');
    }
}
