(function($) {
    
    $(function() {
        initAnchor();       
        
        $('#topnav a').bind('click.default-scroll', handleNavigation );
        $('#bottomnav a').bind('click.default-scroll', handleNavigation );
        
        $('#toggle-job-description').click( function(event) {  
            $('#jobs-extended').slideToggle(400);
            
            var text = $(this).text();
            $(this).text(text == "weitere Informationen" ? "Informationen ausblenden" : "weitere Informationen");
            
            event.preventDefault();        
        });
        
        handleLogoFocus();
        bindLogoClicks();
        logoPositioning();
        
        
        $('#get-job-now').click(function() {
            scrollToElement( $('#jobs') , true );
            $('#jobs-extended').slideDown(400);
            
        });
        
        $('#jobs-extended').hide();
        $('#toggle-job-description').css({'display' : 'block'});
        
        externalLinks();
        
        
    });

    
    /* Function-Declarations */    
    function logoPositioning() {
        // just for ie6
        if( $.browser.msie && $.browser.version <= 6 ) {
            var $wrapper = $('ul#logo-list li'),
                wrapper_height = $wrapper.outerHeight(),
                wrapper_width = $wrapper.outerWidth();

            $.each($wrapper, function() {
                var $image = $(this).find('img'),
                image_height = $image.height(),
                image_width = $image.width();
                
                if( image_width > wrapper_width - 20 ) {
                    results_width = parseInt(wrapper_width - 20);
                } else {
                    results_width = image_width;
                }
                
                if( image_height > wrapper_height - 10 ) {
                    result_height = parseInt(wrapper_height - 20);
                } else {
                    result_height = image_height;
                }
                
                $image.css({
                    'top' : (wrapper_height - image_height) / 2 + 'px',
                    'width': results_width + 'px',
                    'left' : (wrapper_width-results_width) / 2 + 'px',
                    'height': result_height + 'px'
                });
            });
        }        
    }
    
    function externalLinks() {
        $('a[rel=extern]').attr('target', 'blank').removeAttr('rel');
    }
    
    function initAnchor() {
        var hash = window.location.hash;
        
        if(hash) {
            var rex = new RegExp(/\#(.*)/gi),
            res = rex.exec(hash);
            
            if(res[1]) {
                handleNavigation(false, res[1], true);
            }
        }
    }
        
    function handleNavigation($element, anchor) {
       
        if( !anchor ) {
            var $element = $(this),
            eid = $element.attr('href'),
            instant = false;    
            
            scrollToElement( $(eid) , instant );
            
        } else {
            eid = anchor;
            instant = true;
            
            scrollToElement( $('#'+eid) , instant );
            
        }        
        return false;         
    }
    
    function scrollToElement($element, instant) {        
        var eoffset = $element.offset(),
        duration = 600;        

        if(instant) {duration = 1;}
        
        $('html,body')
        .stop(true, true)
        .animate({
            scrollTop : eoffset.top
        }, duration, function() {
            window.location.hash = $element.attr('id');
        });
        
    }
    
    
    function handleLogoFocus() {
        var $links = $('#logo-list a');
        
        $links.focus(function() {            
            $links.parent().removeClass('active');
            $(this).parent().addClass('active');
        });
        
        
    }
    
    function bindLogoClicks() {
        var $lists = $('#logo-list li');
        
        $lists.click(function(event) {
            if( $(this).attr('id') != 'logo-advice' ) {
                var url = $(this).find('a').attr('href');
                window.location.href=url;
                event.preventDefault();
            }
            
        })
        .hover(function() {$(this).toggleClass('active');});
    }
    
})(jQuery);
