(function($) {
    $.fn.extend({
        isChildOf: function( filter_string ) {
          
          var parents = $(this).parents().get();
         
          for ( j = 0; j < parents.length; j++ ) {
           if ( $(parents[j]).is(filter_string) ) {
                return true;
           }
          }
          
          return false;
        }
    });
})(jQuery); 

$(function() {
        Cufon.replace('h1', {fontFamily: 'Tahoma'});
        Cufon.replace('h2', {fontFamily: 'Tahoma'});

        $('input[class="reset"]').each(function() {
          var button = $(this);
          tag=$('<a class="buttonfront"><span>'+button.val()+'</span></a>').click(function() {
              // add button params to form
              form = $(this).parents('form').first();
              form.append('<input type="hidden" name="' + button.attr('name') + '" value="' + button.attr('value') + '" />');
              form.submit();
              form.find('input[name="' + button.attr('name') + '"]');

              return false;
          });
          button.parent().append(tag);
          //$(this).css('display','none');
          $(this).remove();
        });

        $('input[type="submit"]').each(function() {
          $(this).parent().append('<a class="buttonfront form-link"><span>'+$(this).attr('value')+'</span></a>').click(
            function() {$(this).parents('form').submit();}
          );
          $(this).css('display','none');    
        });  
        
        $('input').keypress(function(e) {
            c = e.which ? e.which : e.keyCode;
            if(c == 13) {
                
                $(this).parents('form').submit();
            }
        });        
        
        $('#search').focus(
        function(){
            if($(this).val()=='Wpisz szukany tekst') $(this).val('');
        }
        );     

        $('#search').blur(
        function(){
            if($(this).val()=='') $(this).val('Wpisz szukany tekst');
        }
        );  
            
        $('#search_submit').click(
        function(){ 
           if($(this).parent().find('input').eq(0).val()!='Wpisz szukany tekst') $(this).parents('form').submit();
        }
        );              
        
        $('#navigation ul').superfish({
            delay:       200,                            // one second delay on mouseout
            animation:   {opacity:'show',height:'show'},  // fade-in and slide-down animation
            speed:       'fast',                          // faster animation speed
            autoArrows:  false,                           // disable generation of arrow mark-up
            dropShadows: true,                            // disable drop shadows
            pathLevels:    4
        });       
        
        $(".tipsy-north[title]").tipsy({fade: true, trigger: 'hover', gravity: 'n', html: true, title: 'title'});
        $(".tipsy-south[title]").tipsy({fade: true, trigger: 'hover', gravity: 's', html: true, title: 'title'});
                        
        $('#side-navigation a').hover(
        function(){
            str=$(this).find('img').attr('src');
            $(this).find('img').attr('src',str.replace('.png','-h.png'));
            $(this).animate({paddingTop: '5px'}, 300);
        },
        function(){
            str=$(this).find('img').attr('src');
            $(this).find('img').attr('src',str.replace('-h.png','.png'));  
            $(this).stop().animate({paddingTop: '0px'}, 300);
            
        }
        );
            
        $(".fancy a").fancybox({
                'titleShow'	: true,
                'titlePosition'	: 'inside',
                'transitionIn'	: 'elastic',
                'transitionOut'	: 'elastic',
                'onComplete'        : function() {

//                            $('#fancybox-img').parent().addClass('zoom-small-image');
//                            var html=$('#fancybox-img').parent().html();
//                            var src=$('#fancybox-img').attr('src');
//
//                            $('#fancybox-img').parent().html('<a href="'+src+'" class="cloud-zoom" rel="softFocus: true, position:\'inside\', smoothMove:2">'+html+'</a>');
//                            $('.cloud-zoom, .cloud-zoom-gallery').CloudZoom();

                }
        });   
        
    $('.zoom-img').each(function(){    
        $(this).parent().addClass('zoom-small-image');
        var html=$(this).parent().html();
        var src=$(this).parent().attr('big');

        $(this).parent().html('<a href="'+src+'" class="cloud-zoom" rel="softFocus: true, position:\'inside\', smoothMove:2">'+html+'</a>');
        $('.cloud-zoom').CloudZoom();       
    });    
             
   
   var headline_count;
    var headline_interval;
    var old_headline = 0;
    var current_headline = 0;
    var delay=6800;
    var stop=0;
    var speed_out=900;
    var speed_in=1200;
    
    if($.browser.msie){
        speed_out=0;
        speed_in=0;              
    }
    
    co=-1;

    $(".rotator li div").css('cursor','pointer').click(function (){
            if(stop==0){
      
                clearInterval(headline_interval);
                headline_interval = setInterval(headline_rotate,delay);
                
                next=$('.rotator li div').index(this);
                co=next;
                
                if(next<current_headline) headline_rotate();
                if(next>current_headline)  headline_rotate();
                if(next==current_headline) return false; // headline_interval = setInterval(headline_rotate,delay);
            }
    });
    
    
    $(".rotator li div").css('cursor','pointer').click(function (){
            if(stop==0){
      
                clearInterval(headline_interval);
                headline_interval = setInterval(headline_rotate,delay);
                
                next=$('.rotator li div').index(this);
                co=next;
                
                if(next<current_headline) headline_rotate();
                if(next>current_headline)  headline_rotate();
                if(next==current_headline) return false; // headline_interval = setInterval(headline_rotate,delay);
            }
    });  
    
    //$('.rotator-bg').css('cursor','pointer').click(function (){
    //    window.location = $(".rotator .rotator-element a").eq(current_headline).attr('href');
    //});
    
   

    headline_count = $('.rotator .rotator-element').size();
    headline_interval = setInterval(headline_rotate,delay);

    function headline_rotate() {
      current_headline = old_headline + 1;
      if(current_headline==headline_count) current_headline=0;
      
      if(co!=-1) {current_headline=co;co=-1;}

      stop=1;
 
      $(".rotator .rotator-element").eq(old_headline).fadeOut(speed_out, function(){});
      $(".rotator .rotator-element").eq(current_headline).fadeIn(speed_in, function(){stop=0;});
      
      $(".rotator li div").eq(old_headline).addClass('active');
      $(".rotator li div").eq(current_headline).removeClass('active');
      
      old_headline = current_headline;
    }
    
    
    
    
               
    var com_headline_count;
    var com_headline_interval;
    var com_old_headline = 0;
    var com_current_headline = 0;
    var com_delay=4800;
    var com_stop=0;
    var com_speed_out=900;
    var com_speed_in=900;
    var kierunek=1;
    
    if($.browser.msie){
        com_speed_out=0;
        com_speed_in=0;              
    }
    
    com_co=-1;
    

    com_headline_count = $('.company-rotator .company-rotator-element').size();
    com_headline_interval = setInterval(com_headline_rotate,com_delay);



    $(".rotator-prev").css('cursor','pointer').click(function (){
            if(com_stop==0){
      
                clearInterval(com_headline_interval);
                kierunek=-1;
                com_headline_interval = setInterval(com_headline_rotate,com_delay);
                com_headline_rotate();
            }
    });  

    $(".rotator-next").css('cursor','pointer').click(function (){
            if(com_stop==0){
      
                clearInterval(com_headline_interval);
                kierunek=1;
                com_headline_interval = setInterval(com_headline_rotate,com_delay);
                com_headline_rotate();
            }
    });  


    function com_headline_rotate() {
      com_current_headline = com_old_headline + (1*kierunek);
      if(com_current_headline==com_headline_count && kierunek==1) com_current_headline=0;
      if(com_current_headline<0 && kierunek==-1) com_current_headline=com_headline_count-1;
      //alert(com_current_headline);
      if(com_co!=-1) {com_current_headline=com_co; com_co=-1;}

      com_stop=1;
      
      var zmienna=$(".company-rotator .company-rotator-element").eq(com_old_headline).width()*kierunek; 
      $(".company-rotator .company-rotator-element").eq(com_old_headline).css('left','0px');
      $(".company-rotator .company-rotator-element").eq(com_current_headline).css('left',zmienna+'px');
      zmienna=-1*$(".company-rotator .company-rotator-element").eq(com_old_headline).width()*kierunek;

      $(".company-rotator .company-rotator-element").eq(com_old_headline).animate({left:zmienna},com_speed_out, function(){});
      $(".company-rotator .company-rotator-element").eq(com_current_headline).animate({left:0},com_speed_out,  function(){com_stop=0;}); 
 

      
      com_old_headline = com_current_headline;
    }    
    
    
    
    
    
    
    
    $('#product_filters_category_id option:selected').each(function (){
        
      $(".filter li .page input[value="+$(this).attr('value')+"]").parent().addClass('active'); 
       
    });    
    
    $('#product_filters_category_id').change(function(){
      $(this).parents('form').submit();
    });
});


