$(document).ready(function(){

     $('#limiter_textarea').blur(function() {
          var tekst = $(this).val(); 
          var brakuje = 20 - tekst.length;
          if (brakuje > 4)
               {
               $('#limiter_alert').html('brakuje ' + brakuje + ' znaków!');
               $('#limiter_alert').css('padding', '2px 3px');
               }
               else if (brakuje > 1 && brakuje <= 4) 
               {
               $('#limiter_alert').html('brakuje ' + brakuje + ' znaki!');
               $('#limiter_alert').css('padding', '2px 3px');
               }
               else if (brakuje == 1) 
               {
               $('#limiter_alert').html('brakuje jednego znaku!');
               $('#limiter_alert').css('padding', '2px 3px');
               }
               else
               {
               $('#limiter_alert').html('');
               $('#limiter_alert').css('padding', '0');
               }
     });

     $("a.wiecej_opinii").livequery("click", function(){
          $(this).next('span.wiecej_opinii_span').css("display", "inline");
          $(this).remove();
          return false;
     });

     $("p.ceny3 input").livequery("keyup", function(event) {
          var wartosc = $(this).val();
          wartosc = clearPrice(wartosc);
          $(this).val(wartosc); 
          if (wartosc < 0) $(this).val(0);
     });
      
     $("a[rel=fancy]").each(function(){
          adres = $(this).attr('href');
          adres = '/sub' + adres.replace(".htm", ".php");
          $(this).attr('href', adres);
     });

     $("a[rel=fancy]").fancybox({
		'titleShow': false,
		'width': '50%',
		'height': '90%',
		'autoScale': false,
		'type': 'iframe',
		'showNavArrows': false
	});
     
     $("span#closeThis").livequery("click", function(){
          $("div#popup_logowanie").hide('fast');
     });
     
     $("a#zalogujSie").livequery("click", function(){
          $("div#popup_logowanie").show('fast');
          return false;
     });
     
     $("form.acordion div:not(:first)").each(function(){
          $(this).hide();
     });

     $("form.acordion h4:not(:first)").each(function(){
          $(this).addClass('plus');
     });

     $("form.acordion h4:first").each(function(){
          $(this).addClass('minus');
     });

     $("form.acordion h4.plus").livequery("click", function(){
          $(this).removeClass('plus').addClass('minus');
          $(this).next('div').show('fast');
     });

     $("form.acordion h4.minus").livequery("click", function(){
          $(this).removeClass('minus').addClass('plus');
          $(this).next('div').hide();
     });

     $("input[name='konto']:not(:checked)").livequery("click", function(){
          $("#konto_add").show('fast');
          $(this).attr('checked', true);
     });
     
     $("input[name='konto']:checked").livequery("click", function(){
          $("#konto_add").hide('fast');
          $(this).attr('checked', false);
     });
     
     if ($("input[name='konto']").is(':checked')) {
          $("#konto_add").show();
     };

     if ($("input[name='z_lokalne']").is(':checked')) {
          $("#miacho").show();
     } else { $("#miacho").hide();}

     

     $("input[name='z_lokalne']:not(:checked)").livequery("click", function(){
          $("#miacho").show('fast');
          $(this).attr('checked', true);
     });
     
     $("input[name='z_lokalne']:checked").livequery("click", function(){
          $("#miacho").hide('fast');
          $(this).attr('checked', false);
     });

     $("input[name='waga']").livequery("keyup", function(event) {
          var waga = $(this).val();
          waga = clearPrice(waga);
          $(this).val(waga);
          if (waga > 100) $(this).val(100); 
          if (waga < 0) $(this).val(0);
     });


	$("#tabs").tabs({ fxShow: { height: 'show', opacity: 'show' } });
	$("#tabs2").tabs({ fxShow: { height: 'show', opacity: 'show' }, fxAutoHeight: true  });
	$("#tabs3").tabs({ fxShow: { height: 'show', opacity: 'show' }, fxAutoHeight: true  });
     $(".tabs").show();

     var t = 0;
     var tabsStart = false;
     var tabs = $('li', '#tabs2').size();
     var rotation = setInterval(function() {
         t = ++t <= tabs ? t : 1;
         if (tabsStart == false)
               {
               $('#tabs2').triggerTab(2);
               tabsStart = true;
               }
               else
               {
               $('#tabs2').triggerTab(t);
               }
     }, 4000);
     // stop rotation on tab click
     $('#tabs2>ul:eq(0)>li>a').click(function(e) {
         if (e.clientX && rotation) { // true mouse click ocurred
             clearInterval(rotation);
             rotation = null;
         }
     });

     $('a[rel="external"]').click(function(){
          this.target = "_blank";
          });
          
     $("#form input, #form textarea, #form select").focus(function(event){
          $(this).addClass('foc');
     });
     $("#form input, #form textarea, #form select").blur(function(event){
          $(this).removeClass('foc');
     });


     $("input[name='pytanie']").val('nazwa kuriera...');
     $("input[name='pytanie']").focus(function(event){
          if ($(this).val() == 'nazwa kuriera...') $(this).val('');
     });

     $("input[name='pytanie']").blur(function(event){
          if ($(this).val() == '') $(this).val('nazwa kuriera...');
     });
     
     $("#top form").submit(function(event){
          if ($("#top form input[name='pytanie']").val() == '' || $("#top form input[name='pytanie']").val() == 'nazwa kuriera...') return false;
     });
     
	$("input[name='miejscowosc']").autocomplete("/ajax/miejscowosc.php", {
          minChars:2
     });  
        
	$("input[name='ulica']").autocomplete("/ajax/ulica.php", {
          minChars:2
     });

     // funkcja do dodawania zakladek
     $("#doUlubionych").click(function(){
     	var tytul = document.title;
     	var adres = document.location;
     	if (window.sidebar) {
     		window.sidebar.addPanel(tytul, adres, "");
     	}
     	else if (window.external) {
     		window.external.AddFavorite(adres, tytul);
     	}
     	else if (window.opera && window.print) {
     		var a = document.createElement('a');
     		a.setAttribute('href', adres);
     		a.setAttribute('title', tytul);
     		a.setAttribute('rel','sidebar');
     		a.click();
     	}
     	return false;
     });

     // funkcja do czyszczenia wartości liczbowych
     function clearPrice(price) {
          price = price.replace(',','.');
          price = price.replace('..','.');
          price = price.replace(/[^0-9.\-]/g, "");
          var count = price.split(".");
          if (count.length > 2)
               {
               price = price.replace(/(\s+)?.$/, "");
               }
          return price;
          }

});
