  function showHideTabs(parentSet){
  
    $(parentSet).each(function(){
      
      if ($(this).hasClass('tab-hidden')){
        $(this).hide();
      }
      if ($(this).hasClass('tab-shown')){
        $(this).show();
      }
      
    });
  
  }
  
  function checkForm(t, timeout){
    var small = $(t).siblings('small');
    var out = true;
    
    if (t.value != t.lastValue) {
    
      if (t.timer) clearTimeout(t.timer);
      
      //small.parent().removeClass('invalid').removeClass('valid');
      //small.text('Trwa sprawdzanie poprawności...');
      
      var action = $('#cf').attr('action');
      var data = 'action=' + $(t).attr('id') + '&' + $(t).attr('id') + '=' + t.value;
      
      t.timer = setTimeout(function () {
        $.ajax({
          url: action,
          data: data,
          dataType: 'json',
          type: 'post',
          success: function (j) {
          
            small.html(j.msg);
            if (j.ok == true) {
              small.parent().removeClass('invalid').addClass('valid');
            } else {
              small.parent().removeClass('valid').addClass('invalid');
              out = false;
            }
            
            if ($('#cf > ul > li.valid').size() == 3) {
              $('#cf_submit').removeAttr('disabled');
            } else {
              $('#cf_submit').attr('disabled','disabled');
            }
            
          }
        });
      }, timeout);
      
      t.lastValue = t.value;
    }
  
    return out;
  }

	$(function(){
  
    // Mark that browser supports javascript
    $('body').addClass('javascript');
    
    // Add target: _blank to external links
    $('a.icon-external').attr({'target':'_blank'});
  
     /* *** Homepage *** */
    if ($('#page-home').size() > 0) {
    
  		$('#slideshow').cycle({ 
        fx: 'fade',
        speed: 1000,
        timeout: 6000
      });
      
    }
    
     /* *** Contact form *** */
    if ($('#cf').size() > 0) {
    
      $('#cf_submit').attr('disabled','disabled');
   
      $('#cf_name, #cf_mail, #cf_msg').each(function(){
      
        if ($(this).siblings('small').size() < 1) {
          $(this).after('<small></small>');
        }
        
        var small = $(this).siblings('small');
        
        $(this).keyup(function() {
          checkForm(this,300);
        });
        $(this).blur(function() {
          checkForm(this,0);
        });
        
      });
    
  		$('#cf').submit(function(){
          
        var action = $(this).attr('action');
        var data = 'action=submit';
                    
        $('#cf_name, #cf_mail, #cf_phone, #cf_msg').each(function(){
          var t = this;
          data += '&' + $(t).attr('id') + '=' + t.value;
        });

        $.ajax({
          url: action,
          data: data,
          dataType: 'json',
          type: 'post',
          success: function (j) {
            if (j.ok == true) {
              var cfParent = $('cf').parent();
              var h6 = $('#cf > h6');
              $('#cf').replaceWith('<p class="sent">' + j.msg + '</p>');
              $('p.sent').before(h6);
            } else {
              $('#cf > h6').after('<p class="error">' + j.msg + '</p>');
            }
          }
        });

        return false;
      });
      
    }
    
     /* *** Oferta *** */
    if ($('#page-oferta').size() > 0) {
    
      // Add links
      $('#oferta-list h6').after('<p class="toggle"><a href="#"><small>Pokaż ofertę</small></a></p>');
      
      // Hide descs
      $('#oferta-list div.oferta-desc').hide();
      $('#oferta-list .toggle a').click(function(){
      
        $(this).parent().siblings('div.oferta-desc').slideToggle();
        $(this).parent().parent().siblings('li.oferta-item').children('div.oferta-desc:visible').slideUp();
        return false;
        
      });
      
    }
    
    /* *** Schmersal page *** */
    if ($('#page-schmersal').size() > 0) {
      
      // Change positions of two main introductory texts and add links
      $('#automatyka-przemyslowa-introduction').insertAfter('#bezpieczenstwo-maszyn-introduction');
      $('#automatyka-przemyslowa-introduction').append('<p class="button"><a href="#automatyka-przemyslowa" title="Więcej...">Więcej...</a></p>');
      $('#bezpieczenstwo-maszyn-introduction').append('<p class="button"><a href="#bezpieczenstwo-maszyn" title="Więcej...">Więcej...</a></p>');
      $('#automatyka-przemyslowa h5').empty();
      
      $('<div id="arrow"></div>').insertAfter('#automatyka-przemyslowa-introduction');
      
      // Initial state
      var activeState = location.hash; // Get the current state from the address
      var inactiveState;
      if (activeState == '#bezpieczenstwo-maszyn' || activeState == '') {
        inactiveState = '#automatyka-przemyslowa';
        activeState = '#bezpieczenstwo-maszyn';
      } else {
        inactiveState = '#bezpieczenstwo-maszyn';
        $('#arrow').toggleClass('right');
      }

      $(inactiveState).toggle();
      $(activeState + '-introduction').toggleClass('active');
      $(activeState + '-introduction a').toggle();
      
      // Imitation of hover state on li elements
      $('#automatyka-przemyslowa-introduction, #bezpieczenstwo-maszyn-introduction').hover(
        function () {
          $(this).toggleClass('hover');
        }, 
        function () {
          $(this).toggleClass('hover');
        }
      );
      
      // Event handlers for two main div's
      $('#automatyka-przemyslowa-introduction a, #bezpieczenstwo-maszyn-introduction a').click(function(){
      
        $('#automatyka-przemyslowa').toggle();
        $('#bezpieczenstwo-maszyn').toggle();
        
        $('#automatyka-przemyslowa-introduction').toggleClass('active');
        $('#bezpieczenstwo-maszyn-introduction').toggleClass('active');
        
        $('#arrow').toggleClass('right');
        
        $('#automatyka-przemyslowa-introduction a').toggle();
        $('#bezpieczenstwo-maszyn-introduction a').toggle();
        return false;
      
      });
      
    
      // Create new menu for #bezpieczenstwo-maszyn
      $('#bezpieczenstwo-maszyn').prepend('<ul class="first clearfix"></ul>');
      $('#bezpieczenstwo-maszyn h5').remove().wrapInner('<li></li>').children(0).clone().appendTo('#bezpieczenstwo-maszyn ul.first');
      
      // Remove text nodes to avoid using text-indent:-9999px css rule
      $('#bezpieczenstwo-maszyn > ul.first li a').empty();
      
      var shbez = $('#bezpieczenstwo-maszyn > ul:not(:first)');
      
      // Make initial state
      $(shbez).addClass('tab-hidden');
      $(shbez).eq(0).addClass('tab-shown');
      showHideTabs(shbez);
      $('#bezpieczenstwo-maszyn > ul.first li:first').addClass('active')
      
      // Imitation of hover state on li elements
      $('#bezpieczenstwo-maszyn > ul.first li').hover(
        function () {
          $(this).toggleClass('hover');
        }, 
        function () {
          $(this).toggleClass('hover');
        }
      );
      
      // Click event handler
      $('#bezpieczenstwo-maszyn ul.first li a').click(function(){
        
        // Mark active <li> element
        $(this).parent().siblings().removeClass('active');
        $(this).parent().addClass('active');
        
        // Toggle specific classes to tabs and update screen
        $(shbez).removeClass('tab-shown').addClass('tab-hidden');
        $('#bezpieczenstwo-maszyn').find('#' + $(this).attr('href').split('#')[1]).removeClass('tab-hidden').addClass('tab-shown');
        showHideTabs(shbez);
        
        return false;
      
      });
      
      // Imitation of hover state on p.small
      $('#bezpieczenstwo-maszyn li p.types, #automatyka-przemyslowa li p.types').hover(
        function () {
          $(this).css('color','#191919');
        }, 
        function () {
          $(this).css('color','#898987');
        }
      );
      
    }
    
	});

