/**
 * 
 */
var scroll_handle_click = false;
var scroll_handle_click_position = 0;
var scroll_handle_click_slider_position = 0;
var scroll_showed = false;
var scroll_arrow_dir = false;
var scroll_timer;

function scrollTimerHandle() {
	scrollUpDown(scroll_arrow_dir, 0.01);
	scroll_timer = setTimeout(scrollTimerHandle, 25);
}

function scrollPosition() {
	if (jQuery("#pageShow").outerHeight() < jQuery("#pageShowContent").outerHeight()) {
		scroll_handle_click = false;
		jQuery("#scroll").show();
		jQuery("#scroll").css("left", (jQuery("#pageShow").position().left + jQuery("#pageShow").outerWidth()) + "px").css("top", jQuery("#pageShow").position().top + "px").css("height", jQuery("#pageShow").outerHeight() + "px");
		scroll_showed = true;
	} else {
		scroll_handle_click = false;
		jQuery("#scroll").hide();
		scroll_showed = false;
	}
}

function scrollUpDown(up, delta) {
	if (typeof delta == "undefined") delta = 0.1;
	var current = jQuery("#scroll_slider").position().top - jQuery("#scroll_top").position().top - jQuery("#scroll_top").outerHeight();
	var max_top = jQuery("#scroll_bottom").position().top - jQuery("#scroll_top").position().top - jQuery("#scroll_slider").outerHeight() - jQuery("#scroll_top").outerHeight();
	var percent = current/max_top;
	if (up) {
		percent -= delta;
		if (percent < 0)
			percent = 0;
	} else {
		percent += delta;
		if (percent > 1)
			percent = 1;
	}
	var top = Math.round(percent * max_top);
	jQuery("#scroll_slider").css("top", (top  + jQuery("#scroll_top").outerHeight()) + "px");
	var scroll_top = -Math.round(percent * (jQuery("#pageShowContent").outerHeight() - jQuery("#pageShow").outerHeight()));
	jQuery("#pageShowContent").css("top", scroll_top + "px");
}

jQuery(function(){
	scrollPosition();
	setTimeout("scrollPosition();", 100);
	setTimeout("scrollPosition();", 1000);
});
jQuery(document).ready(function(){
	scrollPosition();
	setTimeout("scrollPosition();", 100);
	setTimeout("scrollPosition();", 1000);
});
jQuery(window).resize(function(){ scrollPosition(); });
jQuery("img").load(function(){ scrollPosition(); });
jQuery(function(){
	jQuery("#scroll_slider").mousedown(function(data){
		data.preventDefault();
		scroll_handle_click_position = data.pageY;
		scroll_handle_click_slider_position = jQuery("#scroll_slider").position().top - jQuery("#scroll_top").position().top - jQuery("#scroll_top").outerHeight();
		scroll_handle_click = true;
	});
	jQuery("body").mouseup(function(){
		scroll_handle_click = false;
		clearTimeout(scroll_timer);
	});
	jQuery("body").mousemove(function(data){
		if (scroll_handle_click) {
			data.preventDefault();
			var top = data.pageY - scroll_handle_click_position + scroll_handle_click_slider_position;
			var max_top = jQuery("#scroll_bottom").position().top - jQuery("#scroll_top").position().top - jQuery("#scroll_slider").outerHeight() - jQuery("#scroll_top").outerHeight();
			if (top < 0) {
				top = 0;
			} else if (top > max_top) {
				top = max_top;
			}
			jQuery("#scroll_slider").css("top", (top  + jQuery("#scroll_top").outerHeight()) + "px");

			var percent = top/max_top;
			var scroll_top = -Math.round(percent * (jQuery("#pageShowContent").outerHeight() - jQuery("#pageShow").outerHeight()));
			jQuery("#pageShowContent").css("top", scroll_top + "px");
		}
	});
	jQuery("#scroll_top").mousedown(function(){
		scroll_arrow_dir = true;
		//scrollUpDown(true);
		scrollTimerHandle();
		//scroll_timer = setTimeout(scrollTimerHandle, 250);
	});
	jQuery("#scroll_bottom").mousedown(function(){
		scroll_arrow_dir = false;
		//scrollUpDown(false);
		scrollTimerHandle();
		//scroll_timer = setTimeout(scrollTimerHandle, 250);
	});
	jQuery("#content").wheel(function(event, delta){
		if (scroll_showed)
			scrollUpDown(delta > 0);
	});
});

$(function(){
	$(".form_contact").click(function(){
		scrollUpDown(false, 1);
	});
});

$(function(){
	$('a[rel*=lightbox]').lightBox({
		imageLoading: "images/loading.gif",
		imageBtnClose: "images/closelabel.gif",
		imageBtnPrev: "images/prevlabel.gif",
		imageBtnNext: "images/nextlabel.gif",
		txtImage: "Zdjęcie",
		txtOf: "z"
	});
});
