/**
 * Same Height for all Image Containers
 */
function imageResize() {
	var height = $(window).height();
	height = height - $('div#SERVICE-NAVIGATION').height();
	height = height - $('div#LOGO').height();
	height = height - $('div#PROJEKTE-PREVIEW').height();
	height = height - 250;
	// Height auf alle DIVs setzen
	$('div#HG div').height(height);
	//
	$('div#HG div img').each( function() {
		// Aspect Ratio anhand der Attribute
		var iheight = $(this).attr('height');
		var iwidth  = $(this).attr('width');
		var ratio =  iwidth / iheight;
		var width = height * ratio;
		// Breiter als Viewport?
		var viewport =  $(window).width() * 0.9;
		if( width > viewport ) {
			$(this).height(viewport/ratio);
			$(this).width(viewport);
		} else {
			$(this).height(height);
			$(this).width(width);
		}
		// Min-Height
		if( $(this).height() < 370 ) {
			$(this).height(370);
			$(this).width(370*ratio);
		}
	});
}

$(window).load(function() {
   $("#HG img").hide();
   img = new Image();
   img.src =  $("#HG img").attr("src"); 
   if(img.width < 1800) {
   $("#HG").addClass("eingerueckt");
   }
   $("#HG img").show();
   
   var orgDisplay = $(".dragger_container").css("display");
   //alert(orgDisplay);

   imageResize();
	$(window).resize( function() {
		imageResize();
	});
	// Bugfix IE8
	$('div#HG div img').onShow( function() {
		imageResize();
	});
   
});

(function($){
  $.fn.extend({
    onShow: function(callback, unbind){
      return this.each(function(){
        var obj = this;
        var bindopt = (unbind==undefined)?true:unbind;
        if($.isFunction(callback)){
          if($(this).is(':hidden')){
            var checkVis = function(){
              if($(obj).is(':visible')){
                callback.call();
                if(bindopt){
                  $('body').unbind('click keyup keydown', checkVis);
                }
              }
            }
            $('body').bind('click keyup keydown', checkVis);
          }
          else{
            callback.call();
          }
        }
      });
    }
  });
})(jQuery);
