/**
 * (C) 2008-2009 Marcello Barnaba <vjt@openssl.it> - http://sindro.me/
 * (C) 2008-2009 Fabio Grande <fabio@ndstr.com>    - http://ndstr.com/
 */
(function() {
  var label_offset = parseInt($('.slide_descr').css('left'));
  $.fn.align2center = function() {
    var pano_width = parseInt($(this).css('width')); // This is because of IE. SHIT!
    var map_width = parseInt($('#map_container').css('width'));
    var logo_width = parseInt($('.logo_comune').css('width'));
    var window_width = $(window).width();

    return this.each(function() {

      var slide_shift = (pano_width - window_width) / 2;

      $(this).css({left: -slide_shift});
      $(this).children('.slide_descr').css({left: slide_shift + label_offset});

      var logo_shift = (window_width + map_width) / 2 - logo_width;

      $('.logo_comune').css({left: logo_shift}).show();
    });
  }
})();

$(window).resize(function() {
  $('#slide img').align2center();
});

$(document).ready(function() {
  // Header images cycle
  $('#slide_pano #slide').cycle({
    fx: 'fade',
    speed: 2000,
    timeout: 8000,
    pause: 1 //pause on hover
  });

  $('#slide img').align2center();

  // Load the map
  $(document).trigger('map:shown');

  // Clickable pano && title magic.
  //
  // Used by thekrpano_inplace_link_to helper, that generates links with
  // an href that points to the detail page, and a `pano` class.
  //
  $('a.pano').click(function() {
    embedKrpano(this.rel);

    // Return false so the click isn't handled by the browser. On the cloned link,
    // this handler won't be triggered so it will bring the user on the detail page.
    return false;
  });

});
