﻿$(document).ready(function() {
// Variablen entsprechend der Sprachversion
langBild = "Bild ";
langVon = " von ";
    if ( $("html").attr( "lang" ) == "en")    {
        langBild = "Image ";
        langVon = " of ";
    }
// Katalogdownload
$("div.katalog-dl").addClass('katalog-dl-js');
    // call makeacolumnlists
    $('.katalog-dl-list').makeacolumnlists({cols: 3, colWidth: 160, equalHeight: false, startN: 1});
    
if ( $("div.katalog-dl") )
{
    
    var ImgArr = ""
    $("div.katalog-dl a").each(function(index) {
        ImgSrc = ""+this.onmouseover;
        ImgSrc = ImgSrc.replace(/"/,"'");
        ImgSrc = ImgSrc.replace(/"/,"'");
        ImgSrc = ImgSrc.substring(ImgSrc.indexOf("'")+1,ImgSrc.lastIndexOf("'"));
    
        ImgArr = ImgArr + ImgSrc + ",";
    });
    preload(ImgArr);
}    
// Startseite
    $(function() {
        $("#accordion-start").accordion({
            autoHeight: true,
            fillSpace: true
        });
    });
// Unternehmen
    $(function() {
        $("#accordion-laender").accordion({
            autoHeight: false    
        });
    });
    $('#galerie').jcarousel({
        vertical: true,
    //wrap: "circular",
    visible: 3,
        scroll: 3
    });
// Sortimente
    $(function() {
        $("#accordion-sortiment").accordion({
        });
    });

// Startseite und Partner
    $(function() {
        $("#tabs").tabs();
    });
// Partner
    $(".partner a, a.wg").fancybox({
        'overlayColor'    : '#000',
        'overlayOpacity': '0.7',
        'width'        : 453,
        'height'    : 230,
        'autoScale'    : false,
        'transitionIn'    : 'none',
        'transitionOut'    : 'none',
        'type'        : 'iframe'
    });
// Blaetterkatalog
    jQuery('.karussell').jcarousel({
    scroll: 3 
    });
// Fancybox allgemein
    $("a[rel=galerie]").fancybox({
        'overlayColor' : '#000',
        'overlayOpacity' : '0.7',
        'transitionIn'        : 'none',
        'transitionOut'        : 'none',
        'transitionIn'    : 'elastic',
        'transitionOut'    : 'elastic',
        'titleFormat'        : function(title, currentArray, currentIndex, currentOpts) {
                    return '<span id="fancybox-title-over">' + (title.length ? ' &nbsp; ' + title : '') + '<span id="noi">' + langBild +  (currentIndex + 1) + langVon + currentArray.length + '</span></span>';
                }
            });
    $("#google-map").fancybox({
        'overlayColor' : '#000',
        'overlayOpacity' : '0.7',
        'width'                : '75%',
        'height'            : '75%',
        'autoScale'            : false,
        'transitionIn'        : 'none',
        'transitionOut'        : 'none',
        'type'                : 'iframe'
    });
    $("a.iframe").fancybox({
        'overlayColor' : '#000',
        'overlayOpacity' : '0.7',
        'width'                : '75%',
        'height'            : '75%',
        'autoScale'            : false,
        'transitionIn'        : 'none',
        'transitionOut'        : 'none',
        'type'                : 'iframe'
    });
// teaser-boxen in der Hoehe angleichen
$("div.tu-box").each(function(index) {
    teaser = $(this).find("div[class^='tu']").not("div[class^='tu-sp2']");
    teaser.each(function(index2) {
        teaser.eq(index2).find("div[class='links']").css({'position': 'absolute' });
        teaser.eq(index2).find("div[class='button']").css({'position': 'absolute' });
        teaser.eq(index2).find("div[class='min']").css({'minHeight': '0' });
        teaser.eq(index2).find("div[class='min']").css({'height': 'auto' });
        teaser.eq(index2).find("div[class='min']").css({'height': teaser.eq(index2).find("div[class='min']").height() + teaser.eq(index2).find("div[class='links']").height()  + ( teaser.eq(index2).find("div[class='button'] a").innerHeight() / 3 ) + teaser.eq(index2).find("div[class='button']").innerHeight() + 12 });
        if (index2 != 0)
        {
        second = teaser.eq(index2).height() + parseFloat(teaser.eq(index2).css("marginTop")) + teaser.eq(index2).position().top;
        first = teaser.eq(index2-1).height() + parseFloat(teaser.eq(index2-1).css("marginTop")) + teaser.eq(index2-1).position().top;
        if ( second > first  )
        {
            diff = ( second - first );
            teaser.eq(index2-1).find("div[class='stretch']").css({'height': teaser.eq(index2-1).find("div[class='stretch']").height() + diff});
            $(this).find("div[class='stretch']").css({'height': $(this).find("div[class='stretch']").height()});
            teaser.eq(index2-1).find("div[class='stretch'] > div").css({'height': teaser.eq(index2-1).find("div[class='stretch']").height()});
                        
        }

        else if ( second < first )
        {
            diff = ( first - second );
            $(this).find("div[class='stretch']").css({'height': $(this).find("div[class='stretch']").height() + diff});
            teaser.eq(index2-1).find("div[class='stretch']").css({'height': $(this).find("div[class='stretch']").height()});
            $(this).find("div[class='stretch'] > div").css({'height': $(this).find("div[class='stretch']").height()});
            
        }
        }
     });
  });
// websites-boxen in der Hoehe angleichen
$("div.websites-box").each(function(index) {
    wteaser = $(this).find("div[class='teaser']");
    var highest = 0;
    wteaser.each(function(index2) {
        if ( wteaser.eq(index2).height() > highest  )
        {
            highest = wteaser.eq(index2).height();
            // alert(index2 + " :: " + highest + " :: " + wteaser.eq(index2).height());
        }
     });
    wteaser.each(function(index2) {
        diff = ( highest - wteaser.eq(index2).height() );
        wteaser.eq(index2).find("div[class='stretch']").css({'height': wteaser.eq(index2).find("div[class='stretch']").height() + diff});
     });
  });
// letzte Elemente im Accordion START auszeichnen
$("#accordion-start div.item").each(function(index) {
if (index == $("#accordion-start div.item").length -1)        {
    $(this).addClass('acc-item-last');    }
  });
$("#accordion-start div.headline").each(function(index) {
if (index == $("#accordion-start div.headline").length -1)        {
    $(this).addClass('acc-hl-last');    }
  });
$("#accordion-laender div.item").each(function(index) {
if (index == $("#accordion-laender div.item").length -1)        {
    $(this).addClass('acc-item-last');    }
  });
$("#accordion-laender div.headline").each(function(index) {
if (index == $("#accordion-laender div.headline").length -1)        {
    $(this).addClass('acc-hl-last');    }
  });
$("#menu li div").each(function(index) {
    $(this).find("ul").each(function(index) {
    if (index >= $(this).parent().find("ul").length - 1)    {
        $(this).css({"width" : "auto" });
        $(this).css({"marginRight" : 4 });
    }
    })
  });
jQuery.fn.exists = function(){return jQuery(this).length>0;}
if ($("#menu li td div").exists()) {
// IE 6
$("#menu li td div").each(function(index) {
    $(this).wrapInner('<span></span>').wrapInner('<span></span>').wrapInner('<span></span>').wrapInner('<span></span>');
    $(this).wrapInner('<span class="ie6"></span>');
  });
}
else
{
    $("#menu > li div").each(function(index) {
        $(this).wrapInner('<span>').wrapInner('<span>').wrapInner('<span>').wrapInner('<span>').wrapInner('<span>').wrapInner('<span>').wrapInner('<span>');
      });
}
$("#menu > li div").addClass('sub');
/* prim-nav-hover div einfügen */
$("#menu > li div").prepend("<div class='prim-nav-hover'>");
$("body").append("<div id='sub-l'>");
$("body").append("<div id='sub-r'>");
    function megaHoverOver(){
        $(this).addClass('hover');
        sub = $(this).find(".sub");
        sub.css({'top':'auto'});
        //sub.css({'top':'123px'});
        if (! $("#menu li td div").exists() )
        {
        // nicht IE6
        sub.css({'left':'auto'});
        }
        else {
            sub.css({'left': 0});    }
        rowWidth = 0;
        // Breite aller uls in sub berechnen
        $(this).find("ul").each(function() {
            rowWidth += $(this).outerWidth(true); 
                });
        // sub Breite zuweisen
        sub.css({'width' : rowWidth + 22});
        // .sub positionieren
        if ($(this).position().left  - $(this).parent().position().left + $(this).find(".sub").outerWidth() > $(this).parent().width())
        {
            navRechts = $(this).parent().position().left + $(this).parent().width() - $(this).find(".sub").outerWidth();
            korr = $(this).find(".sub").position().left - $(this).find(".sub").offset().left;
            if ($("#menu li td div").exists()) {
            // IE 6
                $(this).find(".sub").css({'left': navRechts + korr});    }
            else {
                $(this).find(".sub").css({'left': navRechts + korr + 4});    }
        }
        if ($(this).parent().attr("id") == "menu")
        {
        if (! $("#menu li td div").exists() )
        {
        // nicht IE6
            if ($(this).find(".sub").position().left == $("#menu").position().left )
            {
                $("#sub-l").css("left", $("#menu").position().left - 3 );
                $("#sub-l").css("top", $(this).position().top + $(this).height() - ($("#sub-l").height() - 3) );
                $("#sub-l").show();
            }
            if ($(this).find(".sub").offset().left + $(this).find(".sub").outerWidth() == $("#menu").position().left  + $("#menu").width() + 4 )
            {
                $("#sub-r").css("left", $("#menu").position().left  + $("#menu").width() - $("#sub-r").width() + 4 );
                $("#sub-r").css("top", $(this).position().top + $(this).height() - ($("#sub-r").height() - 3) );
                $("#sub-r").show();
            }
        }
            // prim-nav-hover ausrichten
            korr = $(this).find(".sub").position().left - $(this).find(".sub").offset().left;
            hoverLeft = ( $(this).position().left - $(this).find(".sub").position().left + ($(this).width() / 2) - ( $(this).find(".prim-nav-hover").width() / 2 ) );
            $(".prim-nav-hover").css("left", hoverLeft + korr );
        }
    }
    
    function megaHoverOut(){
        $(this).removeClass('hover');
        $(this).find(".sub").css({'top':'-2000px'}).css({'left':'-2000px'});
        if ($(this).parent().attr("id") == "menu")
        {
            $("#sub-l").hide();
            $("#sub-r").hide();
        }
    }

    var config = {    
         sensitivity: 25, // number = sensitivity threshold (must be 1 or higher)    
         interval: 50, // number = milliseconds for onMouseOver polling interval    
         over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
         timeout: 25, // number = milliseconds delay before onMouseOut    
         out: megaHoverOut // function = onMouseOut callback (REQUIRED)   
    };
    $("ul#menu li .sub").css({'top':'-2000px'}).css({'left':'-2000px'});
    $("ul#menu li").not("ul#menu li li").hoverIntent(config);
    // Bedienung mit TAB
    $("ul#menu li a").focus(function(event) {
        $("ul#menu li").not("ul#menu li li").hover(megaHoverOver, megaHoverOut);
        $(this).parent("li").trigger('mouseover');
    });
    $("ul#menu li li a").blur(function() {
        $(this).parent("li").trigger('mouseout');
    });
    // Katalogdownload - Bedienung mit TAB
    $("div.katalog-dl a").mouseout( hideCover );
    
    $("div.katalog-dl a").focus(function(event) {
        $(this).trigger('mouseover');
    });
    $("div.katalog-dl a").blur(function() {
        $(this).trigger('mouseout');
    });
});
// ENDE document.ready
    function openKatalog(URL,thisLink)
    {
        thisLink.blur();
        window.open(URL,"katalog","resizable=yes");
        return false;
    }
    
    
// Katalogdownload
    
    function preload(images) {
        if (document.images) {
            var i = 0;
            var imageArray = new Array();
            imageArray = images.split(',');
            var imageObj = new Image();
            for(i=0; i<=imageArray.length-1; i++) {
        // document.write('<img src="' + imageArray[i] + '" />');// Write to page (uncomment to check images)
            imageObj.src=imageArray[i];
            }
        }
    }
    
    function showCover(img) {
        oldCoverHtml = $('#cover').html();
        $('#cover img').attr("src",img);
    }
    function hideCover() {
        $('#cover').html(oldCoverHtml);
    }
