(function($) {
    
    /**
    * Variablen für Link-Detection
    * unter Schatten
    */
    
    var mouseOverShadow = {
        top : false,
        btm : false
    }
    
    var links = {
        top : new Array(),
        btm : new Array()
    }
    
    var shadow = {
        top : { pos : 0, height : 0 },
        btm : { pos : 0, height : 0 }  
    }
    
    var linkTop, linkHeight;
    var shadowLink = null;
    
    
    
    
    $(document).ready(function() {
        
        fixFooter();
        
        /**
        * RESIZE-HANDLER
        */
        
        $(window).resize(function() {
            fixFooter();
        });
    
        /********************************************************************************
         * Hauptnavigation
         ********************************************************************************/
        
        if (!isiPhone()) {
	        $('ul#Menu1 li').mouseenter(function() {
	            if ($(this).hasClass('current')
	             || $(this).hasClass('section'))
	                return;
	            
	            $(this).find('a').animate({
	                    'padding-top' : '43px'    
	                },
	                100,
	                'linear',
	                function () {
	                    $(this).css({
	                        'color' : '#DCDCDC'
	                    })
	                }
	            );
	        });
	        
	        $('ul#Menu1 li').mouseleave(function() {
	            if ($(this).hasClass('current')
	             || $(this).hasClass('section'))
	                return;
	            
	            $(this).find('a').animate({
	                    'padding-top' : '78px'    
	                },
	                100,
	                'linear',
	                function () {
	                    $(this).css({
	                        'color' : '#878787'
	                    })
	                }
	            );  
	        });    
        }
        
        
        /********************************************************************************
         * Footer
         ********************************************************************************/
        
        var footerLock = false;
        
        $('#footerBottom').mouseenter(function() {

            $('#footerTop').animate({
                    'top' : -1 * $('#footerTop').height()
                },
                100,
                'linear'
            );    
        });
        
        $('#footer').mouseleave(function() {
            
            if (!footerLock) { 
                
                $('#footerTop').animate({
                        'top' : 0
                    },
                    200,
                    'linear'
                );
            }
        });
        
        $('#open').click(function() {
            
            if (!footerLock) {
                
                $('#open').css('display', 'none');
                $('#close').css('display', 'block');
                    
                $('#footerTop').animate({
                        'top' : -1 * $('#footerTop').height()
                    },
                    100,
                    'linear'
                ); 
                
                footerLock = true;
            }
        });
        
        $('#close').click(function() {
            if (footerLock) {
                $('#open').css('display', 'block');
                $('#close').css('display', 'none');
                
                $('#footerTop').animate({
                    'top' : 0
                    },
                    200,
                    'linear'
                );
                
                footerLock = false;
            }
        });
        
        
        
        /********************************************************************************
         * Schatten-Links
         ********************************************************************************/
        
        shadow.top.height = $('#topShadow').height();
        shadow.btm.height = $('#Shadow').height();
        
        /**
        * Überlagerte Links finden
        */
        function selectLinks(strShadow) {
            
            links[strShadow] = new Array();
            $('a[href]').each(function() {
                var padd   = $(this).css('padding-top').replace('px', '');       
                
                var padd = parseInt($(this).css('padding-top'));
                
                
                linkTop    = $(this).offset().top;
                linkHeight = $(this).height() + padd;
                
                if (linkTop <= shadow[strShadow].pos + shadow[strShadow].height
                 && linkTop + linkHeight >= shadow[strShadow].pos) {
                    links[strShadow].push($(this));     
                } 
            });
        }
        
        $(document).mousemove(function(e) {
            
            if (!mouseOverShadow.top && !mouseOverShadow.btm)
                return;
            
            var strShadow;
            var shadowId;  
            if (mouseOverShadow.top) {
                strShadow = 'top';
                shadowId  = '#topShadow';    
            } else {
                strShadow = 'btm';
                shadowId  = '#Shadow';
            }
                    
            var len = links[strShadow].length;
            var cur, off, pad;
            if (shadowLink) {
                shadowLink.removeClass('jshover');    
                shadowLink = null;
            }
                    
            for (var i = 0; i < len; i++) {
                cur = links[strShadow][i];
                off = cur.offset();
                pad = cur.css('padding-top').replace('px', '');        
                if (e.pageX > off.left 
                 && e.pageX < off.left + cur.width()
                 && e.pageY > off.top 
                 && e.pageY < off.top + cur.height() + pad) {
                    shadowLink = cur;     
                }            
                
                if (shadowLink)
                    break;
            }
                    
            if (shadowLink) {
                $(shadowId).css({'cursor':'pointer'});
                if (!shadowLink.hasClass('mainNavItem')) {
                    shadowLink.mouseover();
                    shadowLink.mouseenter();
                }
                shadowLink.addClass('jshover');
                $(shadowId).click(function() {
                    shadowLink.click();
                    if (shadowLink.attr('href')) {
                        window.location = shadowLink.attr('href');
                    }
                });
                
            } else {
                $(shadowId).css({'cursor':'default'});
                $(shadowId).click(function() {
                    console.log('-');
                });
            }
        });
                
        $('#topShadow').mouseover(function() {
           mouseOverShadow.top = true;
           shadow.top.pos = $(this).offset().top;
           shadow.top.height = $(this).height();
           selectLinks('top'); 
        });
                
        $('#topShadow').mouseout(function() {
           mouseOverShadow.top = false;
           if (shadowLink) {
                shadowLink.removeClass('jshover');    
                shadowLink = null; 
            }     
        });
        
        $('#Shadow').mouseover(function() {
           mouseOverShadow.btm = true;
           shadow.btm.pos = $(this).offset().top;
           shadow.btm.height = $(this).height();
           selectLinks('btm'); 
        });
                
        $('#Shadow').mouseout(function() {
           mouseOverShadow.btm = false;
           if (shadowLink) {
                shadowLink.removeClass('jshover');    
                shadowLink = null; 
            }     
        });
        
    });
    
})(jQuery);


function isiPhone(){
    return (
        (navigator.platform.indexOf("iPhone") != -1) ||
        (navigator.platform.indexOf("iPod") != -1)
    );
}

/**
* Footer
*/

function fixFooter() {
	
	var height = window.innerHeight ? window.innerHeight : jQuery(window).height(); 
	
	if (jQuery(window).width() < 906) {
        jQuery('#footer').css('display', 'none');
        jQuery('#footer_small').css('display', 'block');
        jQuery('#footer_low').css('display', 'none');
    } else if (height < 740) {
        jQuery('#footer').css('display', 'none');
        jQuery('#footer_small').css('display', 'none');
        jQuery('#footer_low').css('display', 'block');
    } else {
    	jQuery('#footer').css('display', 'block');
        jQuery('#footer_small').css('display', 'none');
        jQuery('#footer_low').css('display', 'none');
    }        
}

/**
* PREV, NEXT
*/

function goToByScroll(id) {
     jQuery('html,body').animate({scrollTop: jQuery("#"+id).offset().top},'slow');
}

