// po załadowaniu dokumentu
$(document).ready(function() {
	$('.doc_top .contact > div:gt(0)').hide(); // ukrywa drugą informację kontaktową
	// robi pauzę po najechaniu na kontakt
	$('.doc_top .contact').mouseover(function() {
		clearTimeout(contactTimer);
	}).mouseout(function() {
		contactTimer = setTimeout(contactToggle, 5000); // zmienia kontakt po 5000 milisekund
	});
	contactTimer = setTimeout(contactToggle, 5000); // zmienia kontakt po 5000 milisekund
	// robi pauzę po najechaniu na partnerów
	$('.doc_top .partners').mouseover(function() {
		clearTimeout(partnersTimer);
	}).mouseout(function() {
		partnersTimer = setTimeout(animePartners, 50); // przesunięcie co 50 milisekund
	});
	var i, len = $('.doc_top .partners a').length;
	for (i = 0; i < len; i++)
		$('.doc_top .partners .bar').append($('.doc_top .partners a:first'));
	partnersTimer = setTimeout(animePartners, 50); // przesunięcie co 50 milisekund
	// przypisuje zdarzenie najazdu kursorem na kropki
	$('.doc_dots div').each(function(index, el) {
		$(el).mouseover(function() {
			animeTop(index);
		});
	});
	// pauza po najechaniu na obraz
	$('.doc_imgs .master').mouseover(function() {
		clearTimeout(topTimer);
	}).mouseout(function() {
		clearTimeout(topTimer);
		topTimer = setTimeout('animeTop(-1)', 5000); // zmiana obrazu po 5000 sekund
	}).children('img').css('display', 'block');
	animeTop(0);
});
// zmieniarka do kontaktu
var contactTimer, contactCurrent = 1;
var contactToggle = function() {
	var contactPrev = contactCurrent;
	contactCurrent++;
	if (contactCurrent > $('.doc_top .contact > div').length)
		contactCurrent = 1;
	$('.doc_top .contact' + contactPrev + ', .doc_top .contact' + contactCurrent).toggle(200);
	contactTimer = setTimeout(contactToggle, 5000); // zmienia kontakt po 5000 milisekund
};
// animacja partnerów
var partnersTimer, partnersMargin = 0;
var animePartners = function() {
	var width = $('.doc_top .partners a:first').width();
	if (-partnersMargin > width) {
		partnersMargin += width;
		$('.doc_top .partners .bar').append($('.doc_top .partners a:first'));
	} else
		partnersMargin -= 1; // przesunięcie o piksel
	$('.doc_top .partners .bar').css('margin-left', partnersMargin);
	partnersTimer = setTimeout(animePartners, 50); // przesunięcie co 50 milisekund
};
// animacja topów
var topTimer, topImage = -1;
var animeTop = function(id, stop) {
	if (topImage == id)
		return;
	clearTimeout(topTimer);
	$('.doc_dots div:nth(' + topImage + ')').removeClass('active');
	if (id != -1)
		topImage = id;
	else {
		topImage++;
		if (topImage > 7)
			topImage = 0;
	}
	var animate = function(id, stop) {
		$('.doc_imgs .master').css('background', 'url(' + $('.doc_imgs a:nth(' + (topImage + 1) + ') img').attr('src') + ')').children('img').fadeTo(1000, 0, function() { // ukrycie
			$('.doc_imgs .master').attr('href', $('.doc_imgs a:nth(' + (topImage + 1) + ')').attr('href')); // adres
			$(this).attr('src', $('.doc_imgs a:nth(' + (topImage + 1) + ') img').attr('src')).fadeTo(0, 1); // pokazanie
			clearTimeout(topTimer);
			if (!stop) {
				clearTimeout(topTimer);
				topTimer = setTimeout('animeTop(-1)', 5000); // zmiana obrazu po 5000 sekund
			}
		});
	};
	if ($('.doc_imgs .master img').is(':animated'))
		$('.doc_imgs .master img').stop().fadeTo(400, 1, function() {
			animate(id, stop);
		});
	else
		animate(id, stop);
	$('.doc_dots div:nth(' + topImage + ')').addClass('active');
};
// paginacja
var pagination = function($found, $link, $from, $limit) {
	var $n = Math.ceil($found / $limit), $str = '', $min, $max, $num;
	if ($n <= 1)
		return '<strong>1</strong>';
	if ($n > 9) {
		$max = ($from > 5) ? 1 : $from + 1;
		for ($num = 1; $num < $max + 2; $num++) {
			if ($num == $from)
				$str += '<strong>' + $num + '</strong> ';
			else
				$str += '<a href="' + $link + '#' + $num + '">' + $num + '</a> ';
		}
		if ($from > 5 && $from <= $n) {
			$str += '... ';
			$min = ($from > 3) ? $from : 3;
			$max = ($from < $n - 3) ? $from : $n - 4;
			for ($num = $min - 2; $num < $max + 3; $num++) {
				if ($num == $from)
					$str += '<strong>' + $num + '</strong> ';
				else
					$str += '<a href="' + $link + '#' + $num + '">' + $num + '</a> ';
			}
			if ($from < $n - 4)
				$str += '... ';
		} else
			$str += '... ';
		for ($num = $n - 1; $num <= $n; $num++) {
			if ($num == $from)
				$str += '<strong>' + $num + '</strong> ';
			else
				$str += '<a href="' + $link + '#' + $num + '">' + $num + '</a> ';
		}
	} else {
		for ($num = 1; $num < $n + 1; $num++) {
			if ($num == $from)
				$str += '<strong>' + $num + '</strong> ';
			else
				$str += '<a href="' + $link + '#' + $num + '">' + $num + '</a> ';
		}
	}
	return $str;
};
