﻿/* Author: 
  Robert Weber
*/

if(!window.KK) { var KK = {}};

KK.fullscreenr = (function(){
  if(!$('figure.intro img:first').length) return;
  
  var browserwidth = $('html').width();
  var browserheight = $('html').height();
  var $img = $('figure.intro img:first');
  var $anchor = $('figure.intro figcaption a:first');
  var ratio = $img.height() / $img.width();
  var options = {
    height : $img.data('height'),
    width : $img.data('width')
  }
  
  $('body').append($img);
  $img.addClass('fullscreenry');
  $('body').append($anchor);

  $('html').css({'height':'100%', 'overflow':'hidden'});

  $anchor.css({
    "position": "absolute",
    "top": "0",
    "left": "0",
    "width": "100%",
    "z-index": "101"
  });
  
  $anchor.find('img').css({'width':'100%'});

  $img.css({
    "position":"absolute",
    "top": "0",
    "left": "0",
    "z-index": "0"
  });

  // Scale the image
  if ((browserheight/browserwidth) > ratio){
      $img.height(browserheight);
      $img.width(browserheight / ratio);
  } else {
      $img.width(browserwidth);
      $img.height(browserwidth * ratio);
  }
  
  
  var fullscreenr_resizer = function(options) {
    var $img =  $('body > img.fullscreenry:last');
    // Set bg size
    var ratio = options.height / options.width; 
    // Get browser window size
    var browserwidth = $('html').width();
    var browserheight = $('html').height();

    // Scale the image
    if ((browserheight/browserwidth) > ratio){
      $img.height(browserheight);
      $img.width(browserheight / ratio);
    } else {
      $img.width(browserwidth);
      $img.height(browserwidth * ratio);
    }
    return this;    
  };
  
  fullscreenr_resizer(options);
  
  $(window).bind("resize", function() { fullscreenr_resizer(options); });
  
})();


KK.subnavi = (function() {
  jQuery(document).ready(function($) {
    if(!$('nav[role=navigation] ol ol ol').length) return false;

    $('#main hgroup').after('<div class="subnavi" />');
    $('nav[role=navigation] ol ol ol').appendTo($('#main .subnavi'));

    var $first = ($('#main .subnavi ol li.active').length) ? $('#main .subnavi ol li.active') : $('#main .subnavi ol li:first');
    $first.clone().prependTo($('#main .subnavi ol'));

    $('#main .subnavi ol li:first').get(0).className = 'first';
    
    if(KK.ie7) {
      
      var bgc = $('#main .subnavi li').css('background-color');
      
      $('#main .subnavi li').hover(function(){
        $(this).parent().find('li').show();
        $(this).css({'background-color': bgc});
      }, function(){
        if( !$(this).hasClass('first') && !$(this).hasClass('active') ) $(this).css({'background-color': 'transparent'});
      });

      $('#main .subnavi ol').hover(function(){
        $(this).css({'height':'auto'});
        $(this).find('li').show();
      }, function(){
        $(this).css({'height':'2.666666em'});
        $(this).find('li').not('.first').hide();
      });

    }
    
  });
})();

KK.overlay = function(){
  var sizes = get_page_size();
  if(!$('#shadow').length) {
    $('#kzk').append('<div id="shadow" />');
  }
	
	$('#shadow').css({
    'width': sizes[0]+'px',
    'height': sizes[1]+'px'
  });
  
	$(window).bind("resize", KK.overlay);

}

KK.galery = function(){
  window.scrollTo(0, 0);
  KK.overlay();
  $('#kzk').append('<div id="overlay" class="loader galery"></div>');
	$(this).addClass('active');
  
  $(this).clone().appendTo('#overlay');
  $('#overlay').append('<a href="#" class="prev">Vorheriger</a><a href="#" class="next">Nächster</a>')
  $('#overlay').append('<a href="#" class="close">Close</a>');
  $('#overlay .close').bind('click', function(){
    $('#shadow').remove();
    $('#overlay').remove();
		$(window).unbind("resize", KK.overlay);
    return false;
  });
  
  $('#overlay .prev, #overlay .next').bind('click', prevnext);
  
  
  if(KK.ie7 || KK.ie8) {
    $('#overlay .prev, #overlay .next').hover(function(){
      if($(this).hasClass('next')) {
        $(this).addClass('next_hover');
      } else {
        $(this).addClass('prev_hover');
      }
    },function(){
      $(this).removeClass('next_hover');
      $(this).removeClass('prev_hover');
    });
  };
  
  function set_buttons() {
    var $img = $('#overlay figure img:first'),
        offset_left = ((690 - $img.get(0).clientWidth) / 2) + 27;
    
    $('#overlay .prev, #overlay .next').css({'height':$img.get(0).clientHeight+'px'});
    $('#overlay .prev').css({'left':offset_left+'px'});
    $('#overlay .next').css({'right':offset_left+'px'});
  }
  
  set_buttons();
  
  function prevnext(){
    var $next,
        $active = $('.article-images figure.active'),
        $img = $('#overlay figure img:first'),
        $caption = $('#overlay figcaption');
    $('#overlay .prev, #overlay .next').hide();
    
    if($(this).hasClass('next')) {
      $next = ($active.next('figure').length) ? $active.next('figure') : $('.article-images figure:first');
    } else {
      $next = ($active.prev('figure').length) ? $active.prev('figure') : $('.article-images figure:last');
    }
    
    $img.attr('src', $next.find('img:first').attr('src'));
    $caption.text($next.find('figcaption').text());
    window.setTimeout(function(){
      set_buttons();
    }, 200);
    
    $('.article-images figure').removeClass('active');
    $next.addClass('active');
    $('#overlay .prev, #overlay .next').show();
  }
  
};

(function() {

  var correct_images = function() {
    $('#main img').each(function(){
      var h = parseInt($(this).height(), 10), padding, margin = parseInt($(this).css('margin-top').replace('px',''), 10);
      if(h % 40 !== 0) {
        padding =  (40 - (h % 40) - margin) / 32;
        $(this).parent('.figure').css({'margin-bottom':padding+'em'});
      }
    });  
  }

  KK.ie7 = $('html').hasClass('ie7');
  KK.ie8 = $('html').hasClass('ie8');
  
  if(KK.ie7 || KK.ie8) {
    // correct_images();
    // $(window).bind('resize', correct_images);
    
    $('.article-images figure, .figure').hover(function(){
      $(this).find('.figcaption, figcaption').show();
    },function(){
      $(this).find('.figcaption, figcaption').hide();      
    });
  };
  
  $('.topublication').bind('click', function(){
		window.scrollTo(0, 0);
    KK.overlay();
    $('#kzk').append('<div id="overlay" class="loader"></div>');
    $('#overlay').addClass('publication');
    $('#overlay').load(this.href + ' .group', function(){
      $('#overlay').removeClass('loader');
      $('#overlay').append('<a href="#" class="close">Close</a>');
      $('#overlay .close').bind('click', function(){
        $('#shadow').remove();
        $('#overlay').remove();
				$(window).unbind("resize", KK.overlay);
        return false;
      })
    });
    return false;
  });
  
  if($('.article-images figure').length > 1) {
    $('.article-images figure').css({'cursor':'pointer'});
    $('.article-images figure').bind('click', KK.galery);
  }
})();

