$.fn.removeZeros = function() {  
    var this_text = $(this).html();
    var temp = new Array();

    temp = this_text.split(".0000");
    this_text = "";
    for (var i=0;i<temp.length;i++){
	       this_text = this_text + temp[i];  
    }

    $(this).html(this_text);
    return $(this);  
}

$(document).ready(function(){

  var oldaction = $('form#form_email_1891').attr('action');
  $('form#form_email_1891').attr('action',oldaction+'#form_email_1891');

  $('#form_email_1891_submit').click(function(event){
    if ($('input#q1891_q2').val() == 0 || $('input#q1891_q3').val() == 0) {
      event.preventDefault();
      alert('Proszę wypełnić pola oznaczone gwiazdką.');
    }
  });

  $('td:contains(".0")').each(function(){
    $(this).removeZeros();
  });

  $("#more td").each(function(row) { 
    if(row%2==0) {
      $(this).addClass("lewa_col");  
    } 
  });

  $("#more tr:odd").addClass("format_rows");

  $('span.liczba').each(function() {

    var liczba = $(this).text();
    var dlugosc = liczba.length;
    var nowa_liczba = "";

    if (dlugosc >= 7) {
      nowa_liczba = liczba.substring(0,liczba.length-6) + " " + liczba.substring(liczba.length-6,liczba.length-3) + " " + liczba.substring(liczba.length-3,liczba.length);
    }

    if (dlugosc > 3 && dlugosc < 7) {
      nowa_liczba = liczba.substring(0, liczba.length-3) + " " + liczba.substring(liczba.length-3,liczba.length);
    }

    if (dlugosc <= 3) {
      nowa_liczba = liczba;
    } 

    $(this).text(nowa_liczba);

  });

  $('table#more tr:gt(11)').fadeOut();
  $('.rozwin').click(function(){
    $('table#more tr:gt(11)').fadeIn("slow");
    $('.rozwin').css('display','none');
    $('.zwin').css('display','block');
  });
  $('.zwin').click(function(){
    $('table#more tr:gt(11)').fadeOut("slow");
    $('.zwin').css('display','none');
    $('.rozwin').css('display','block');
  });

  $('table#more tr:last a').css('letter-spacing','2px');

  var current_offer = $('input#biezaca_oferta').val();

  $('input.podobna_oferta').each(function(){
    if ($(this).val() == current_offer) $(this).parent('li').hide();
  });

  if ( $('.podobne_oferty ul li').length == '1'){
    $('.podobne_oferty ul li:last').after('<li><i>Nie znaleziono podobnych ofert.</i></li>');
    $('.podobne_oferty ul h4').remove();
    $('.podobne_oferty h4').remove();
    $('.podobne_oferty h1 a').removeAttr('href').addClass('no_underline');
  }

});