$.fn.crossfade = function (target) {
  return this.each(function () { 
    // cache the copy of jQuery(this) - the start image
    var $$ = $(this);
    var oldImage = $$;
    // get the target from the backgroundImage + regexp
    //var target = $$.css('backgroundImage').replace(/^url|[()]/g, ''));
    
    // nice long chain: wrap img element in span
    //var newImage = ($$.wrap('<span style="position: relative;"></span>')
      // change selector to parent - i.e. newly created span
      $$.parent()
      // prepend a new image inside the span
      .prepend('<img>')
      // change the selector to the newly created image
      .find(':first-child')
      // set the image to the target
      .attr('src', target);
    
    // position the original image
    /*$$.css({
      'position' : 'absolute', 
      'left' : 0, 
      // this.offsetTop aligns the image correctly inside the span
      'top' : this.offsetTop
    });*/
    
    // note: the above CSS change requires different handling for Opera and Safari,
    // see the full plugin for this.
    
    
      oldImage.animate({
          opacity: 0
      }, 1000,'linear',function(){
          oldImage.remove();
      });
  //  });
  });
};

$(function(){
    var jBig = $('#headerTours .big');
    var jSmall = $('#headerTours .small');
    var jNextImage = $('#headerTours .imagesContainer a').first().next();
    var bigBannerTitle = $('#bigImageHeader');
    var play = true;
    
    function getNextImage(){
        var next = jNextImage.next();
        if (next.length <=0){
            next = $('#headerTours .imagesContainer a').first();
        } 
        
        jNextImage = next;
        return next;
    }
    function changeBanner(jBanner,newA){     
        jBanner.attr('href',newA.attr('href'));;
        jBanner.find('img').crossfade(newA.find('img').attr('src'));
        jBanner.find('img').attr('title',newA.find('img').attr('title'));
        jBanner.find('title').html('changed');
        //jBanner.find('img').attr('src',newA.find('img').attr('src'));
        
    }
    
    jSmall.click(function(e){
        e.preventDefault();
        var nextImage = getNextImage();
        bigBannerTitle.fadeOut(function(){
            changeBanner(jBig,jSmall);    
            changeBanner(jSmall,nextImage);
            bigBannerTitle.html(jBig.find('img').attr('title'));
            bigBannerTitle.fadeIn();
        });

    });   
  
  
    /**pozastaven pri najeti mysi*/
    $('#headerTours').mouseover(function(){
        play = false;
    });
    $('#headerTours').mouseout(function(){
        play = true;
    });
    
    
    //meneni obrazku intervalem
    window.setInterval(function(){
        if (play)
            jSmall.click();
    }, 4 * 1000);
});

