var custom_scrolling = false;
	var timer_handler = null;
	var box_produkty_width;
	var scroller_width;
	var scroller_left;
	var mouse_position;
	var speed = 0;
	var speed_factor = 15;
	var timeout_val = 50;
	var images_width = 0;
        
function setSpeed() {
        var half = Math.round(box_produkty_width/2);
        var local_pos = mouse_position;

        if (local_pos > half) {
                local_pos -= half;

                speed = Math.sin(((local_pos/half)*90)*(Math.PI/180));
        } else {
                speed = -Math.sin((((half-local_pos)/half)*90)*(Math.PI/180));
        }

        speed = speed*speed_factor;
}

function customScroll() {
        if (custom_scrolling) {				
                if (mouse_position > (box_produkty_width/2)) {
                        scroller_left += speed;

//                        if (scroller_left > (images_width-box_produkty_width)) {
//                            alert("scroller left > box!");
//                                scroller_left = (images_width-box_produkty_width);
//
//                                images_width = 0;
//                                $("#scroller img").each(function() { 
//                                        images_width += $(this).outerWidth(true);
//                                });
//                        }
                    if (scroller_left > images_width-479) {
//                            alert("scroller left > box!");
		
//
//                                images_width = 0;
//                                $("#scroller img").each(function() { 
//                                        images_width += $(this).outerWidth(true);
//                                });
//                                scroller_left=0;
                            scroller_left -= speed;
                        }

                        $('.simply-scroll-clip').scrollLeft(scroller_left);
                } else {
                        scroller_left += speed;

                        if (scroller_left < 0) scroller_left = 0;

                        $('.simply-scroll-clip').scrollLeft(scroller_left);
                }

                timer_handler = setTimeout(customScroll, timeout_val);
        }
}
        
function menu_pos() {
//    $('.navigation').css('bottom', 50);
//    $('.navigation').fadeIn();
    var n_w = $('#navigation').width()-20;
    var n_s = $('#menu-main_menu_gabriella > li.menu-item-type-post_type').size();
    var li_w = 0;
    $('#menu-main_menu_gabriella > li.menu-item-type-post_type').each(function() {
        li_w += $(this).width();
    });
    var margin = parseInt( ( (n_w-li_w)/(n_s+1) )  );
    $('#menu-main_menu_gabriella > li.menu-item-type-post_type').each(function() {
    $(this).css('margin-right', margin);
    });
    $('#menu-main_menu_gabriella > li.menu-item-type-post_type:first-child').css('margin-left', margin);
//    $('#navigation ').css('padding-left', margin);
//    $('.navigation').css('padding-right', margin);
   
}

$(window).load(function() {
menu_pos();
});
window.onresize = function() {
menu_pos();
};

$(window).load(function(){
    
    
    var box_produkty=$("#box-produkty #scroller");
    var scroller = $("#scroller");
    var scroller_img = $("#scroller img");
    if ($.browser.msie  && parseInt($.browser.version) == 8) {
box_produkty.simplyScroll({
			autoMode : 'loop',
			pauseOnHover: true,
			frameRate : 23,
			speed : 2
		});
} else {
box_produkty.simplyScroll({
			autoMode : 'loop',
			pauseOnHover: true,
			frameRate : 30,
			speed : 2
		});
}

				
		scroller_width = scroller.outerWidth(true);
		
		scroller_img.each(function() {
			images_width += $(this).outerWidth(true);
		});	
	
//        $("#scroller img").each(function() {
//			images_width += $(this).outerWidth(true);
//		});
		
});

jQuery(document).ready(function($) {	

        $("a.post_podglad").hover(
  function (e) {
    $("div#img_prv").append('<img style="position:absolute;left:0px;top:0px;z-index:9999;display:none;" />');
          $("div#img_prv").children("img").attr('src',$(this).attr('img_src') ).fadeIn(500);
  }, 
  function (e) {
    $("div#img_prv").children("img").remove();
  }
);//end hover
 

$("div#lista_produktow").mousemove(function(e){
    
    div_height=$("div#lista_produktow").height();
      p_ul=$("ul.lista_produktow").position();
      ul_height=p_ul.top+$("ul.lista_produktow").height();
    
    mouse_position = e.pageY -$("div#lista_produktow").offset().top;
    polowa=Math.round($("div#lista_produktow").height()/2);
    
    if($("ul.lista_produktow").attr('kierunek')=='empty' || $("ul.lista_produktow").attr('kierunek')=='up')
    if(mouse_position > polowa+100)
    {
        if(ul_height > div_height)
        {
            przesun=ul_height-div_height+10;
             predkosc=przesun*20;
            $("ul.lista_produktow").stop().animate({top: "-="+przesun},predkosc,"linear");
            $("ul.lista_produktow").attr('kierunek','down');
      }
      
        
    }
    if(mouse_position < polowa+100 && mouse_position > polowa-100 )
        {
            $("ul.lista_produktow").stop();
            $("ul.lista_produktow").attr('kierunek','empty');
        }
    
    
    if($("ul.lista_produktow").attr('kierunek')=='empty' || $("ul.lista_produktow").attr('kierunek')=='down')
    if(mouse_position < polowa-100)
    {
         ul_top=Math.abs(p_ul.top);
      if(p_ul.top < 0){
          przesun=ul_top+10;
          predkosc=przesun*20;
          $("ul.lista_produktow").stop().animate({top: "+="+przesun},predkosc,"linear");
          $("ul.lista_produktow").attr('kierunek','up');
      }
     
    }
    
});//end mousemove
$("div#lista_produktow").mouseleave(function(){
    $("ul.lista_produktow").stop();
    $("ul.lista_produktow").attr('kierunek','empty');
});//end mouseout
//-------- poniżej kod do slideshow-u dla obrazkow od produktu -------------------//


		box_produkty_width = $("#box-produkty").width();
		scroller_width = $("#scroller").width();
		scroller_left = 0;
		mouse_position = Math.round(scroller_width / 2);
		
		$("#scroller").mouseover(function(event) {
			scroller_left = $('.simply-scroll-clip').scrollLeft();
			mouse_position = event.pageX-$("#box-produkty").offset().left;
			
			scroller_width = $("#scroller").outerWidth(true);
			setSpeed();
			
			if (custom_scrolling == false) {
				custom_scrolling = true;
				timer_handler = setTimeout("customScroll()", timeout_val);
			}
		});
		
		$("#scroller").mouseout(function(event) {		
			custom_scrolling = false;			
			clearTimeout(timer_handler);			
		});
		
		$("#scroller").mousemove(function(event) {
			setSpeed();
			
			scroller_left = $('.simply-scroll-clip').scrollLeft();
			mouse_position = event.pageX-$("#box-produkty").offset().left;
			
			if (custom_scrolling == false) {
				custom_scrolling = true;
				timer_handler = setTimeout("customScroll()", timeout_val);
			}
		});
	

});
