//BLUR LINKS ==============================================================

function BlurLinks(){

// Text- u. Grafik-Links
lnks=document.getElementsByTagName('a');
for(i=0;i<lnks.length;i++){
lnks[i].onfocus=new Function("this.blur()");
}

// ImageMaps
lnks=document.getElementsByTagName('area');
for(i=0;i<lnks.length;i++){
lnks[i].onfocus=new Function("this.blur()");
}
}

onload=BlurLinks;

// This function calls makeMap() if function is present.
// Function comes from rsgooglemap extension.
checkAndMakeMap = function() {
    if( typeof( window.makeMap ) == "function" ) {
        makeMap();
    }
}

//JQUERY SECTION ==============================================================

/* outer html function */
jQuery.fn.outerHTML = function(s) {
    return (s)
        ? this.before(s).remove()
        : jQuery("<p>").append(this.eq(0).clone()).html();
}


$(document).ready(function(){

    // MAIN MENU HIDE

    if( $("#loggedInHeader").length > 0 ) {
        $("#hoverNav").css({
            "top": parseInt( $("#hoverNav").css( "top" ) ) + parseInt( $("#loggedInHeader").height() ) + "px"
        });
    }
    
    $("#hoverNav").hide();

    
    // LAYER TABS - CHANGE TAB  

    $("#pagelayerWrapper ul.tabBar").idTabs(function(id,list){
        for( i in list ) {
            $(list[i]).hide();
        }
        
        $(id).fadeIn(1500);
        
        return false;
    });  



    // SLIDE IN FX - MAIN MENU

    var t;
    $("#navigationFirstlevel").hover(function () {
    t = setTimeout(function () {
    if ($("#hoverNav").is(":hidden")) {
        $("#hoverNav").show('slide', { direction: "up" });
      } else {
        $("#hoverNav").show();
      }
    }, 300)
    }, function () {clearTimeout(t)});
        

    // SLIDE OUT FX - MAIN MENU
    $("#hoverNav").mouseleave(
          function () {
              $("#hoverNav").slideUp();
          }
    ); 
    
    // HORIZONTAL ACCORDION
    $('.sections').hAccordion( { selected: '.sectionRight' } );
    
    
    // FAQ simple accordion

    $('.accordion').accordion({
        active: false,
        header: 'h2',
        autoHeight: false,
        collapsible: true,
        animated: 'easeslide'
    });
    
    $('.accordion h2').focus( function() {
        this.blur();
        return false;
    });
    
    $('.accordion h2').wrapInner( '<span class="accordion-arrow"></span>' );

    
    // MODAL / LIGHTBOX
    $('.modalLightboxBox').hide();
    
    $('a.lightboxLink').click( function ( ) {
        
        if( $(this).attr('href').match('#') ) {
            
            if( $('a#'+$(this).attr('href').split('#')[1]).next('div.modalLightboxBox').length == 0 ) {
                
                // check next element because referenced inserted records in typo3 insert two anchor links
                if( $('a#'+$(this).attr('href').split('#')[1]).next().next('div.modalLightboxBox').length > 0 ) {
                    var modalContentElement = $('a#'+$(this).attr('href').split('#')[1]).next().next('div.modalLightboxBox');
                }
                
            } else {
                var modalContentElement = $('a#'+$(this).attr('href').split('#')[1]).next('div.modalLightboxBox');
            }
            
            if( !modalContentElement ) {
                return false;
            }
            
            if( modalContentElement.hasClass('modalLightboxFrameless') ) {
                var modalWidth = 780;
                var baseHeight = ($(window).height()-50);
                var baseHeightInner = ($(window).height()-100);
            } else {
                var modalWidth = 870;
                var baseHeight = ($(window).height()-120);
                var baseHeightInner = ($(window).height()-250);
            }
            
            modalContentElement.find('script').remove();
            
            $.modal( modalContentElement, {
                minWidth: modalWidth,
                maxWidth: modalWidth,
                minHeight: baseHeight,
                maxHeight: baseHeight,
                position: [100,],
                overlay: 50,
                opacity: 80,
                overlayCss: { backgroundColor: "#3a2e5b" },
                overlayClose: true,
                persist: true,
                onOpen: function( dialog ) {
                    dialog.overlay.slideDown( 'fast', function() {
                        dialog.data.hide();
                        dialog.container.fadeIn( 'fast', function() {
                            dialog.data.fadeIn( 'fast' );
                        });
                    });
                },
                onClose: function( dialog ) {
                    dialog.data.fadeOut( 'fast', function() {
                        dialog.container.fadeOut( 'fast', function() {
                            dialog.overlay.slideUp( 'fast', function() {
                                $.modal.close();
                            });
                        });
                    });
                }
            });
            
            $('#simplemodal-container .modalLightboxContent').css({
                "height":  baseHeightInner + "px"
            });
          
        }
        
        return false;
    } );
    
    // Search field default text and inactive color
    if( $('#navigationMeta ul li input#searchField').val() == '' ) {
        $('#navigationMeta ul li input#searchField').val('Search');
        $('#navigationMeta ul li input#searchField').css('color', '#BBBBBB');
    }
    $('#navigationMeta ul li input#searchField').focus( function(e) {
        if( $(this).val() == 'Search' ) {
            $(this).val('').css('color', '');
        }
    } ).blur( function(e) {
        if( $(this).val() == '' ) {
            $(this).val('Search').css('color', '#BBBBBB');
        }
    } );
    
    // Search field enlargement in meta navigation
    $('#navigationMeta ul li form#indexedsearch').hover(
        function( e ) { // over
            $(this).closest('li').stop(true, true).addClass('widePrepared').addClass('wide', 300);
        },
        function( e ) { // out
            $(this).closest('li').stop(true, true).removeClass('wide', 300).removeClass('widePrepared');
        }
    );
});