// milliseconds
var slideshow_fadingspeed = 2000;
var slideshow_pause = 5000;
var general_fading = 150;

$(document).ready(function() {

    if($("#BackgroundImage").length > 0) {
        $(window).resize( function() {
            resize_background();
        });    
        resize_background();
    }
  
    $("li.LO > ul").fadeTo(50,.85);
    
    if( $("#StartMainMenuContainer").length > 0) {
        init_mainmenueffects();
        
        $('#BackgroundImage').bxSlider({
            mode: 'fade',
            speed: slideshow_fadingspeed,
            pause: slideshow_pause,
            auto: true,
            controls: false,
            auto_direction: 'left',
            next_text: 'next image',
            prev_text: 'previous image',
            wrapper_class: 'ssc'
        });        
    }

  if( $(".internal-link-new-window").length > 0 ) {
    $(".internal-link-new-window").fancybox({
      'width'        : 800,
      'height'      : 520,
      'autoScale'      : false,
      'transitionIn'    : 'none',
      'transitionOut'    : 'none',
      'type'        : 'iframe'
    });  
  }

  if( $(".news-latest-gotoarchive a").length > 0 ) {
    $(".news-latest-gotoarchive a").fancybox({
      'width'        : 800,
      'height'      : 520,
      'autoScale'      : false,
      'transitionIn'    : 'none',
      'transitionOut'    : 'none',
      'type'        : 'iframe'
    });  
  }
  
    if( $("#MainMenuContainer").length > 0)
        init_mainmenueffects();

});

var init_mainmenueffects = function() {

    $("#StartMainMenuContainer li.LO").each(function() {
       var MaxHeight = 0;
       $("li.LT", this).each(function(){
           var LinkText = $(this).find("a").html();
           var newLinkText = LinkText.replace(/--/, "<br/>");
           $(this).find("a").html(newLinkText);         
           MaxHeight += $(this).height() + 3;
       })

       $(this).hover(
        function() {
            $(this).find("ul").stop(true,true).animate( { height: MaxHeight+"px" }, 600, "easeOutSine");
            $(this).stop(true,true).animate( { "margin-top": "-50px" }, 600, "easeOutSine")
            $(this).css("border-bottom", "6px solid #aaaaaa");
        },
        function() {
            $(this).find("ul").stop(true,true).animate( { height: 0 }, 200);
            $(this).stop(true,true).animate( { "margin-top": 0 })
            $(this).css("border-bottom", "6px solid #818182");
        }
       )
    });

    $("#MainMenuContainer li.LO").each(function() {
        
       var MaxHeight = 0;
       var MaxTop = 0;
       $("li.LT", this).each(function(){
           var LinkText = $(this).find("a").html();
           var newLinkText = LinkText.replace(/--/, "<br/>");         
           $(this).find("a").html(newLinkText);         
           MaxHeight += $(this).height() + 3;
           MaxTop += ( $(this).height() + 3 ) * (-1);
       })
        
       $(this).hover(
        function() {
            $(this).find("ul").stop(true,true).animate( 
                { 
                    height: MaxHeight+"px",
                    "margin-top": MaxTop+"px"
                }, 600, "easeOutSine");
            $(this).css("border-bottom", "6px solid #aaaaaa");
        },
        function() {
            $(this).find("ul").stop(true,true).animate( { height: 0, "margin-top": 0 }, 200);
            $(this).css("border-bottom", "6px solid #818182");
        }
       )
    });


}



var resize_background = function() {

    var winHeight = $(window).height();
    var winWidth = $(window).width();

    // Scale Background
    containerObj = $("#BackgroundImage");
    containerObj.css("width",  winWidth + "px");
    containerObj.css("height", winHeight + "px");
    $(".ssc").css("width",  winWidth + "px");
    $(".ssc").css("height", winHeight + "px");

    var iw = containerObj.children('img').width();
    var ih = containerObj.children('img').height();
    if (winWidth > winHeight) {      // wenn Fenster breiter als hoch
            if (iw > ih) {      // wenn Bild breiter
                    var fRatio = iw/ih;
                    containerObj.children('img').css("width",winWidth + "px");
                    containerObj.children('img').css("height",Math.round(winWidth * (1/fRatio)));
                    var newIh = Math.round(winWidth * (1/fRatio));
                    if(newIh < winHeight) {      // wenn neue Bildhoehe kleiner als Fensterhoehe
                            var fRatio = ih/iw;
                            containerObj.children('img').css("height",winHeight);
                            containerObj.children('img').css("width",Math.round(winHeight * (1/fRatio)));
                    }
            } else {
                    var fRatio = ih/iw;
                    containerObj.children('img').css("height",winHeight);
                    containerObj.children('img').css("width",Math.round(winHeight * (1/fRatio)));
            }
    } else {
            var fRatio = ih/iw;
            containerObj.children('img').css("height",winHeight);
            containerObj.children('img').css("width",Math.round(winHeight * (1/fRatio)));
    }    
}
