/**
 * Controller for Campur.de
 * (c) 2009, Benjamin Zaiser, info@benjamin-zaiser.de
 * Dient dazu, sobald zuviel Inhalt im Content ist, automatisch eine "zweite Seite"
 * anzulegen, siehe Bildergalerie
 */ 
$(document).ready(function(){
  var height = 0;
  var maxheight = 330;
  var tabs = new Array();
  var html = '';
  
  //Check, ob maxheight überschritten wird
  $('.slideIt div.liste, .slideIt div.spacing, .slideIt div.bodytext p').each(function(){
    height += $(this).height();
  });
  
  if(height > maxheight){
    // Aufteilen des gesamten Inhalts in div.content; wenn maximale Höhe
    // überschritten -> neuen Tab anlegen (neues Array-Element)
    height = 0;
    html = '';
    tabs = new Array();
    $('.slideIt div.liste, .slideIt div.spacing').each(function(){
      height += $(this).height();
      if(height < maxheight){
        html += $(this).outerHTML();
      }
      else{
        tabs.push('<div>'+html+'</div>');
        height = $(this).height();
        html = $(this).outerHTML();
      }
    });
    if(html != ''){
      tabs.push('<div>'+html+'</div>');
    }
  
    // Beim bodytext, bissl anders
    height = 0;
    html = '';
    $('.slideIt div.bodytext p, .slideIt div.bodytext ul, .slideIt div.bodytext ol').each(function(){
      height += $(this).height();
      if(height < maxheight){
        html += $(this).outerHTML();
      }
      else{
        tabs.push('<div class="bodytext">'+html+'</div>');
        height = $(this).height();
        html = $(this).outerHTML();
      }
    });
    if(html != ''){
      tabs.push('<div class="bodytext">'+html+'</div>');
    }
    
    // Wenn mehrere Tabs gefunden wurden -> diese jeweils in ein Div packen
    // und die Gallery außen rum
    if(tabs.length > 1){
      var result = '<div class="content"><div class="slideIt">';
      for(var i=0; i<tabs.length; i++){
        result += tabs[i];
      }
      result += '</div></div>';
      $('.content').replaceWith(result);
      
      var navi = '<ul class=\"pagebrowser\">';
      navi += '<li><a href=\"#\" id=\"back\"><img src="files/sys/images/arrow_back.png" alt="zur&uuml;ck" \/><\/a><\/li>';
      navi += '<li><a href=\"#\" id=\"next\"><img src="files/sys/images/arrow_next.png" alt="weiter" \/><\/a><\/li>';
      navi += '<\/ul>'; 
      $('div.content').after(navi);
      
      $('.slideIt').cycle({ 
          fx:     'scrollHorz',
          speed: 1000,
          timeout: 0, 
          next:   '#next', 
          prev:   '#back',
          prevNextClick: function(isNext, zeroBasedSlideIndex, slideElement){
            if(zeroBasedSlideIndex == 0){
              $('#back').hide();
              $('#next').show();
            }
            else{
              $('#back').show();
              $('#next').show();
            }
            if(zeroBasedSlideIndex == $('.slideIt > div').size()-1){
              $('#back').show();
              $('#next').hide();
            }
          }
      });
      $('#back').hide();
    }
  }
});

jQuery.fn.outerHTML = function() {
    return $('<div>').append( this.eq(0).clone() ).html();
};
