/**
 * Slideshow for banners and such
 */
 
// Start enclosure for compatibility
(function($) {
  // Start plugin
	
  jQuery.current_slide = 1;
  jQuery.number_of_slides = 0;
  timer = 0;

  /**
   * Goes to the calling slide.
   */
  $.fn.goToSlide = function() {  
    return this.each(function() {
      if($(".slide:visible").attr("class") != $(this).attr("class")) {
        this_slide = $(this).attr("class").split('-');
        current_slide = this_slide[1];
        clearTimeout(timer);
        $(".slide:visible").css("z-index", "50").fadeOut(750);
        $(this).css("z-index", "40").show();
      }
      return $(this);
    });  
  };
  
  /**
   * Goes to the next slide
   */
  $.nextSlide = function() {
    goto_slide = jQuery.current_slide + 1;

    $('.slide-' + goto_slide).goToSlide();
    if (goto_slide == 1){
    	$('.pagerbar').html('Parco di Mileto');
    } else if (goto_slide == 2){
    	$('.pagerbar').html('Project Cambore');
    } else if (goto_slide == 3){
    	$('.pagerbar').html('Hotel Parco di Mileto');
    }
    
    clearTimeout(timer);
    timer = setTimeout('jQuery.nextSlide()', 4000);
    if(jQuery.current_slide == jQuery.number_of_slides) {
      jQuery.current_slide = 0;
    }
    else {
      jQuery.current_slide = jQuery.current_slide + 1;
    }
  }

  /**
   * Prepares the slideshows
   * Binds clicks and adds pager elements.
   */
  $.prepareSlideshows = function() {
    // Only run if slideshow is on page
    if($('.slideshow').length > 0) {
      jQuery.number_of_slides = $('.slideshow .slide').length;
      var first = true;   // Current slide is the first
      var slide_nr = 1;   // Current slide we're preparing
      var show_nr = 1;    // Current slideshow we're preparing

      // Append pager
      $('.slideshow').append('<div class="pager"></div>');

      // Prepare all slideshows
      $('.slideshow').each(function() {
        $(this).addClass("slideshow-" + show_nr);
        
        //  Hide images, except the first, and bind image click to next image
        $(".slideshow-" + show_nr + " .slide").each(function(){
          if(!first) {
            $(this).hide().css("z-index", "40").prev().click(function() {
              $(this).next().goToSlide();
           });
          }
          else {
            // Rest of images are not first
            first = false;
          }

          // Add pager selector for current slide
          var element = '<a href="#" class="select select-' + slide_nr + '">' + slide_nr + '</a>';
          $('.slideshow-' + show_nr + ' .pager').append(element);
          $(this).addClass("slide-" + slide_nr);
          
          // Bind pager clicks
          $('.slideshow-' + show_nr + ' .select-' + slide_nr).click(function() {
            $(this).parent().siblings('.slide-' + $(this).html()).goToSlide()
            return false;
          });
          slide_nr = slide_nr + 1;
        }); // End of slide iteration
        show_nr = show_nr + 1;
      }); // End of slideshow iteration
      
      
      // Start slideshow
      timer = setTimeout('jQuery.nextSlide()', 4000);
    }
    $('a[@rel*=lightbox]').lightBox(); // Select all links that contains lightbox in the attribute rel
  }; // End plugin
})(jQuery);  // End enclosure for compatibility