$(window).load(function() {

// slider
	$('#banner-container').nivoSlider();

});

$(document).ready(function() {

// plywajace menu
	$('a#menu-scroller-lewo').each( function(e) { 
		$('#menu-items').css('left', 0);
		$(this).click(function() {
			if (parseInt($('#menu-items').css('left')) < 0)
				$('#menu-items').animate( {left: '+=151'}, 400, 'swing', function() { if (parseInt($('#menu-items').css('left')) <=0) $('#menu-items').stop(true); } );
			return false;
		});
	});
	$('a#menu-scroller-prawo').each( function(e) { 
		$(this).click(function() {
			if (parseInt($('#menu-items').css('left')) > ($('#menu-items').width()-$('#menu-items table').width()+1))
				$('#menu-items').animate( {left: '-=151'}, 400, 'swing', function() {  if (parseInt($('#menu-items').css('left')) >= ($('#menu-items').width()-$('#menu-items table').width()+1)) $('#menu-items').stop(true); } );
			return false;
		});
	});
// strzalki przewiajania na menu
	$('table.menu').each( function(e) {
		$(this).mouseover( function() { if ($('#menu-items').width()+1 < $('#menu-items table').width()) {$('a#menu-scroller-lewo').show(); $('a#menu-scroller-prawo').show();} } );
		$(this).mouseout( function() { $('a#menu-scroller-lewo').hide(); $('a#menu-scroller-prawo').hide(); } );
	});
// powiekszenia obrazkow
	$('a[rel=grupa]').fancybox({
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'titlePosition' 	: 'over'
	});

// formularz newsletter
	$('#newsletter-email').each( function(e) {
		var newsletter = null;
		$(this).focusin( function() {
			if (!newsletter) newsletter = $(this).val();
			if (newsletter == $(this).val())
				$(this).val('');
		});
		$(this).focusout( function() {
			if ($(this).val() == '') $(this).val(newsletter);
		});
	});

// logo przewijane
/*
	$.ajax({
		type: 'POST',
		url: '/xml/partnerzy.xml',
		dataType: 'xml',
		success: function(xml) {
			var img_count = 0;
			var img_width = 0;
			var img_html = "";
			$(xml).find('partner').each(function () {
				img_count++;
				img_html += '<a target="_blank" href="'+$('url',this).text()+'"><img class="logo" src="'+$('logo',this).text()+'" alt="'+$('nazwa',this).text()+'" /></a>';
				var img = new Image();
				img.onload = function() {
					img_width += this.width+30;
					img_count--;
					if (img_count == 0 && img_width > 0) {
						var img_width_all = img_width;
						var img_html_all = img_html;
						while ($('#partnerzy').width() > img_width_all) {
							img_width_all += img_width;
							img_html_all += img_html;
						}
						img_width_all += img_width;
						img_html_all += img_html;
						$('#partnerzy').html('<div id="partnerzy-animacja" class="partnerzy-animacja">'+img_html_all+'</div>');
						$('#partnerzy-animacja').css('width', img_width_all);
						function anim() { $('#partnerzy-animacja').animate( {left: '-='+img_width}, (img_width)*30, 'linear', function() { $('#partnerzy-animacja').css('left', 0); anim(); } ); }
						$('#partnerzy').mouseover( function() { $('#partnerzy-animacja').stop(); } );
						$('#partnerzy').mouseout( function () { anim(); } );
						anim();
					}
				}
				img.src = $('logo',this).text();
			});
		}
	});
*/
// przelacza produkt-wlasciwosci
	$('a.produkt-wlasciwosci-grupa').each( function(e) { 
		$(this).click(function() {
			$('#produkt-wlasciwosci-img-'+e).toggleClass('produkt-wlasciwosci-img-otwarta');
			$('#produkt-wlasciwosci-img-'+e).toggleClass('produkt-wlasciwosci-img');
			$('#produkt-wlasciwosci-'+e).slideToggle(400);
			return false;
		});
	});

// przelacza pobieranie grupy-pliki
	$('a.pobieranie-grupa').each( function(e) { 
		$(this).click(function() {
			$('#pobieranie-pliki-'+e).slideToggle(400);
			return false;
		});
	});

// partnerzy
	if (jQuery().jCarouselLite)
	{
		$("#partnerzy").jCarouselLite({
			auto: 800,
			scroll: 1,
			visible: 3,
			speed: 1000
		});
	}
});

function textarea_change(id_pola, max_dlugosc, id_pola_opisu)
{
	var tf=document.getElementById(id_pola);
	var chars_count = tf.value.length;
	if (tf.value.length > max_dlugosc)
	{
		if (tf.selectionStart)
		{
			var left_part = tf.selectionStart;
			var right_part = chars_count - tf.selectionStart;
			if (right_part >= max_dlugosc)
			{
				right_part = max_dlugosc;
				left_part = 0;
			}
			else
			{
				left_part = max_dlugosc - right_part;
			}
			tf.value = tf.value.substr(0, left_part)+tf.value.substr(chars_count-right_part, right_part);
			tf.selectionStart = left_part;
			tf.selectionEnd = left_part;
		}
		else
		{
			tf.value=tf.value.substr(0, max_dlugosc);
		}
	}
	chars_count = max_dlugosc - tf.value.length;
	document.getElementById(id_pola_opisu).innerHTML=chars_count;
}

