/* 
  jQuery script pro www.anifilm.cz
  Author: Martin Michalek, Studio Shortcat, michalek@shortcat.cz
*/

/* -----------------------------------------------------------------------
   Jakmile se nacte dokument
*/

$(document).ready(function() {
  
  // Nahrada titulkoveho pisma 
  Cufon.replace('h1')('h2')('h3')
    ('.news .date')
    ('.news #paging');

  // Akce v programu:
  // Roztahovani programove tabulky, sjednoceni vysky akci
  // a Fancybox na odkazech na mapy mist
  if (!!$('#program_table').length) {
    handle_program_table();
    unify_height_in_row();
    fancybox_on_venues_links();
    handle_back_link();
  }

  // Prohlizec vicenasobnych obrazku
  if (!!$('.event .images').length)
    handle_event_images();
      
}); 



/* -----------------------------------------------------------------------
   Jednotlive funkce
*/

// Maximalni a minimalni hodnota v poli
// http://javascript.about.com/library/blarmaxmin.htm
Array.prototype.max = function() {
  var max = this[0];
  var len = this.length;
  for (var i = 1; i < len; i++) if (this[i] > max) max = this[i];
  return max;
}

Array.prototype.min = function() {
  var min = this[0];
  var len = this.length;
  for (var i = 1; i < len; i++) if (this[i] < min) min = this[i];
  return min;
}


// Akce na programove tabulce
function handle_program_table() {
  $('#program_table td .block').easyTooltip({
    xOffset: 25,
    yOffset: 5
  });    
  handle_program_table_width();
}


// Programovou tabulku roztahneme do sirky
function handle_program_table_width() {
  var table_program_original_width = 926;
  var remainder_width = 
    $(window).width() 
    - parseInt($('#content').css('paddingLeft')) 
    - parseInt($('#container').css('marginLeft')) 
    - table_program_original_width;
  var program_table_new_width = 
    $(window).width() 
    - parseInt($('#content').css('paddingLeft')) 
    - parseInt($('#container').css('marginLeft'));
  if (remainder_width > 0) {
    $('#program_table table .remainder')
      .css('width', remainder_width+'px');  
    $('#program_table table')
      .css('width', program_table_new_width+'px');
  }
}

// Akce na programove tabulce
function handle_event_images() {
  $('.event .images').each(function() {
    var p = this.parentNode;
    $(this).after('<div class="images_controls">').cycle({ 
        fx:     'fade', 
        speed:  'fast', 
        timeout: 0, 
        pager:  $('.images_controls', p)
    });
  });
}


// Sjednoti vysku prvku akce v ramci bloku
function unify_height_in_row() {

  // Vrat maximalni vysku z prvku predavaneho pole
  function elements_max_height(elements) {
    var heights_array = new Array;
    jQuery(elements).each(function() {
      heights_array.push(jQuery(this).height());
    });
    return heights_array.max();
  }
  
  // Nastav vsem prvkum vysku toho maximalniho 
  function unify_elements_height(elements) {
    jQuery(elements).each(function() {
      jQuery(elements).height( elements_max_height(elements) );
    });
  }

  // Projdi vsechny radky a v ramci nich sjednot vysku vybranych prvku podle
  // toho nejvyssiho
  jQuery('.events').each(function(el) {
    unify_elements_height(jQuery(this).find('.event'));
  });

}

// Na odkazech na mapy mist spoustime fancybox
function fancybox_on_venues_links() {
  $("a.venue_link, a.trailer_link").fancybox({
    'padding'		 :	0,
    'width'      : 800,
    'height'      : 600,
    'overlayOpacity' :  0.9,
    'overlayColor'   : '#383731',
    'hideOnOverlayClick' : false,
    'centerOnScroll' : true,
    'type'				: 'iframe',
    'onStart': function() { $("body").addClass('fancybox-active') },
    'onClosed': function() { $("body").removeClass('fancybox-active') }
  });
}

// Zobrazujeme/schovavame odkaz na navrat na programovou tabulku
// podle hashe nebo podle skrolovaci pozice v dokumentu
// Poustime jen mimo IE, protoze tam velmi zpomaluje dlouhe programove stranky
// jako sobotu. On totiz IE nejak moc casto pousti ten scroll event.
function handle_back_link() {
  if (!$.browser.msie) {
    var height_without_back_link = 
      $('#head').height() 
      + parseInt($('#content').css('paddingTop')) 
      + $('#content h1:first').height()
      + parseInt($('#content h1:first').css('marginBottom')) 
      + $('#content #program_table').height()
      + parseInt($('#content .program_block:first').css('marginTop'));
    $(window).scroll(function() {  
      if ($(window).scrollTop() > 590) {
        $('body').addClass('show_back_link');
      } else {
        $('body').removeClass('show_back_link');
      }  
    });
    if (!!(window.location.hash) & (window.location.hash != '#content')) {
      $('body').addClass('show_back_link');
    } else {
      $('body').removeClass('show_back_link');
    }
  }
}


