var bif = bif || {};

if (!bif.css) {bif.css = {};}

var content_container;                   // jQuery-Objekt mit dem Scrollbar-Containe
var scrollpane_set  = false;    // Prüfvariable, ob graf. Scrollbars initialisiert werden sollen oder nicht
var scrollpane_lock = false;    // Lockvariable, soll 2-maliges Initialisieren verhindern
var running_on_ie6  = false;    // Trigger-Variable, wenn IE6 verwendet wird

if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 7) { running_on_ie6 = true; }

bif.messageSearchToShort = new Array();
bif.messageSearchToShort['en'] = 'Please enter a longer search term.';
bif.messageSearchToShort['de'] = 'Bitte geben Sie für die Suche mehr Buchstaben ein.';

/**
* @fn void bif.repaint()
* Allgemeine Repaint-Funktionalität zum manuellen Auslösen des Re-Rendering (=> IE)
* @author Alexander Rothe (c&p IIZ)
* @date 2010-07-30
*/
bif.redirect = function(id) {
  document.location.href = '/' + id + '.html';
  /*if (id == 'about-us') {
  }
  else if (id == 'fellowships-grants') {
  }
  else if (id == 'news-network') {
  }
  else if (id == 'titisee-conferences') {
  }*/
};

/**
* @fn void bif.repaint()
* Allgemeine Repaint-Funktionalität zum manuellen Auslösen des Re-Rendering (=> IE)
* @author Alexander Rothe (c&p IIZ)
* @date 2010-07-30
*/
bif.repaint = function() {
  jQuery('#wrapper').addClass('repaint');
  jQuery('#wrapper').removeClass('repaint');
};

/**
* @fn void bif.moveFooter()
* Setzt die Höhe des Wrapper-Containers manuell auf die Höhe des Viewports, bzw. die Minimalhöhe, sofern diese unterschritten wird.
* Sorgt so dafür, dass der Footer immer unten am Fenster "hängt".
* Sollte nur von Browsern eingesetzt werden, welche mit CSS-"min-height" nicht klar kommen, also vornehmlich IE6.
* @author Alexander Rothe
* @date 2010-08-05
*/
bif.moveFooter = function() {
  var wrapper_height = jQuery(window).height();
  if (wrapper_height < bif.css.viewport_min) {
    wrapper_height = bif.css.viewport_min;
  }
  jQuery('#wrapper').height(wrapper_height);
};

/**
* @fn void bif.getCSSValues()
* Liest (einmalig) benötigte Daten aus der CSS-Datei aus und schreibt diese in global verfügbare Variablen
* Diese Werte dienen späteren Funktionen als Größenvorgaben
* @author Alexander Rothe
* @date 2010-07-30
*/
bif.getCSSValues = function() {
  bif.css.header   = jQuery('#header').innerHeight();                                    //Höhe des Header
  bif.css.footer   = jQuery('#footer').innerHeight();                                    //Höhe des Footer
  
  bif.css.main_min = parseInt(jQuery('#main').css('min-height').replace(/px/, ''), 10);  //Minimal-Höhe des Main-Container
  
  bif.css.main_fix = parseInt(jQuery('#main').css('margin-top').replace(/px/, ''), 10) 
                   + parseInt(jQuery('#main').css('margin-bottom').replace(/px/, ''), 10)
                   + parseInt(jQuery('#main').css('padding-top').replace(/px/, ''), 10) 
                   + parseInt(jQuery('#main').css('padding-bottom').replace(/px/, ''), 10)
                   ; //Fixgrößen für Main-Bereich, welche immer zur tatsächlichen Inhaltshöhe hinzu kommen
                   
  if (content_container[0]) {
    bif.css.content_fix  = parseInt(content_container.css('margin-top').replace(/px/, ''), 10) 
                         + parseInt(content_container.css('margin-bottom').replace(/px/, ''), 10)
                         + parseInt(content_container.css('padding-top').replace(/px/, ''), 10)
                         + parseInt(content_container.css('padding-bottom').replace(/px/, ''), 10)
                         ; //Fixgrößen für Content-Bereich, welche immer zur tatsächlichen Inhaltshöhe hinzu kommen
                         
    bif.css.content = content_container.innerHeight();                                         //benötigte Höhe des Content-Bereichs
    
    bif.css.viewport_min = bif.css.header
                         + bif.css.footer
                         + bif.css.main_min
                         + bif.css.main_fix
                         ;
  }
}

/**
* @fn void bif.setContentHeight()
* Berechnet anhand CSS-Vorgaben den für Content verfügbaren (vertikalen) Raum und setzt entsprechende Flags für graf. Scrollbar-Initialisierung
* @author Alexander Rothe
* @date 2010-07-30
*/
bif.setContentHeight = function() {
  if (content_container[0]) {
    content_container.css('height', 'auto');
    var usable_space = jQuery(window).height()
                     - bif.css.header
                     - bif.css.footer
                     - bif.css.main_fix
                     - bif.css.content_fix
                     ;
    if (usable_space < bif.css.main_min) {
      usable_space = bif.css.main_min;
    }
    if (bif.css.content >= usable_space) {
      content_container.css('height', usable_space);
      scrollpane_set = true;
    }
    else {
      content_container.css('height', 'auto');
      scrollpane_set = false;
    }
  }
}

/**
* @fn void bif.initScrollbars()
* Aktualisiert die Höhe des Contentbereichs (abhängig vom verfügbaren Platz) und initialisiert danach ggfls. die graf. Scrollbars
* @author Alexander Rothe
* @date 2010-07-30
*/
bif.initScrollbars = function() {
  if (content_container[0]) {
    content_container.jScrollPaneRemove();
    content_container.removeAttr('style');
    bif.setContentHeight();
    // Setzt graf. Scrollbars
    if (scrollpane_set) {
      content_container.jScrollPane({
        showArrows: true, 
        dragMinHeight: 127,
        dragMaxHeight: 127,
        scrollbarWidth: 25,
        animateTo: true
      });
    }
  }
  bif.repaint();
}

/**
* @fn void bif.initLightbox()
* Lightbox initialisieren
* @author Peter Adelmann
* @date 2010-08-31
*/
bif.initLightbox = function() {
  jQuery('a.lightbox, a[rel="lightbox"]').fancybox({
    'enableEscapeButton': true,
    'type': 'iframe',
    'width': 600,
    'height': 625,
    'padding': 15,
    'titleShow': false
  });
}

/**
* @fn void bif.initExternalLinks()
* Externe Links im neuen Fenster
* @author Peter Adelmann
* @date 2010-08-31
*/
bif.initLinks = function() {
  jQuery('#main a').each(function() {
    jQuery(this).attr('title', jQuery(this).attr('href'));
  });

  // externe Links
  jQuery("a[href^='http://'], a[href^='https://']").click(function() {
    window.open(jQuery(this).attr('href'));
    return false;
  });
  jQuery("a[href^='tl_files/bif/downloads']").click(function() {
    var href = '/' + jQuery(this).attr('href');
    //alert(href);
    window.open(href);
    return false;
  });
  
  
  // Seitenanker
  jQuery("a[href*='#']").click(function() {
    if (content_container[0] && scrollpane_set) {
      jQuery('a[name]').each(function() {
        jQuery(this).attr('id', 'bif_' + jQuery(this).attr('name'));
      })
      
      var href = jQuery(this).attr('href');
      var anchor = href.match(/^(.+)#(.+)$/);
      
      //alert(RegExp.$2);
      content_container[0].scrollTo('#bif_' + RegExp.$2);

      return false;
    }
  });
}

/**
* @fn void bif.initSpecialFont()
* ...
* @author Peter Adelmann
* @date 2010-09-01
*/
bif.initSpecialFont = function() {
  //jQuery('#site_control, .mod_cataloglist .label, .mod_cataloglist .link, .mod_customnav .level_1, .mod_navigation .level_1, .mod_navigation .level_2, .copyright').css('font-family', 'Zurich Cn BT');
}

/**
* @fn void bif.initSerachBar()
* Suchfeld soll mindestens drei Buchstaben haben
* @author Peter Adelmann
* @date 2010-09-01
*/
/*
bif.initSerachBar = function() {
  jQuery('form').submit(function() {
    if (jQuery('#ctrl_keywords').val().length < 2) {
      alert('Please enter a longer search term.');
      return false;
    }
  });
  jQuery(".searchteaser input[type='submit'], .mod_search input[type='submit']").click(function() {
    if (jQuery('#ctrl_keywords').val().length < 2) {
      alert('Please enter a longer search term.');
      return false;
    }
  });
}
*/
bif.initSerachBar = function(lang) {
  var min_chars = 2;
  
  if (!lang || lang != 'en'|'de') {
    lang = 'de';
  }
  
  jQuery('form').submit(function() {
    if (jQuery('#ctrl_keywords').val().length < min_chars) {
      alert(bif.messageSearchToShort[lang]);
      return false;
    }
  });
  
  jQuery(".searchteaser input[type='submit'], .mod_search input[type='submit']").click(function() {
    if (jQuery('#ctrl_keywords').val().length < min_chars) {
      alert(bif.messageSearchToShort[lang]);
      return false;
    }
  });
}


jQuery(document).ready(function() {
  bif.initLightbox();
  bif.initLinks();
  bif.initSerachBar(bif.language);
  //bif.initSpecialFont();
});

jQuery(window).load(function() {
  // Scrollbar-Container, Doppelinitialisierung ist notwendig wg. IE (sicherheitshalber)
  /*
  content_container = jQuery('#main .main_scrollpane');
  if (content_container[0]) {
    bif.getCSSValues();
    scrollpane_lock = true;
    bif.initScrollbars();
  }
  if (running_on_ie6) { bif.moveFooter(); }
  */
});

jQuery(window).resize(function() {
  /*
  if (scrollpane_lock) {
    scrollpane_lock = false;  
  }
  else {
    bif.initScrollbars();
  }
  if (running_on_ie6) { bif.moveFooter(); }
  */
});

