syntops.header = (function($) {
    function init() {
        // Display Submenu under active link
        var $hasSubmenu = $('.childActive.hasSubmenu');
        if ($hasSubmenu.length > 0) {
            create_submenu($hasSubmenu);
        }
        
        // Create cool parallax window
        var $window = $('#parallax, #cloud-vorne, #cloud-hinten');
        var $layers = $window.children('.layer:not(.background)');
        $layers.parallax({
            xparallax: -0.25,
            yparallax: false,
            frameDuration: 30,
            mouseport: $window
        });
        
        // Display night, midday, morning and evening images
        var $bgItem = $('#parallax').children('.background');
        if ($bgItem.length > 0) {
            getTime($bgItem);
        }
        
        var $citySwitch = $('.switch-city a.button');
        $citySwitch.bind('click', function(e) {
            e.stopPropagation();
            $('.switch-city-box').toggle('slow');
        });
        $('html').bind('click', function() {
            $('.switch-city-box').hide('slow');
        });
    }
    
    function create_submenu(subMenu) {
        var $parent = subMenu;
        var $children = $('.submenu');
        var position = $parent.position();
        var left = parseFloat(position.left);
        var margin_left = parseFloat($parent.css('marginLeft'));
        var pos = left + margin_left;
        $children.css("left", pos + 'px');
        
        var width = 0;
        $children.children('ul').children('li').each(function() {
            width += $(this).outerWidth(true);
        });
        if ($.browser.msie) {
            if (parseInt($.browser.version, 10) == 6)
                width += 20;
            if (parseInt($.browser.version, 10) == 9)
                width += 20;
        }
        $children.css("width", width + 'px');
    }
    
    function getTime(background) {
        var d = new Date();
        var curr_hour = d.getHours();
        var $bgItem = background;
        
        if (curr_hour >= 6 && curr_hour <= 8) {
            $bgItem.addClass('up');
        } else if (curr_hour > 8 && curr_hour <= 16) {
            $bgItem.addClass('day');
        } else if (curr_hour >= 17 && curr_hour <= 19) {
            $bgItem.addClass('down');
        } else if (curr_hour > 17 && curr_hour < 6) {
            $bgItem.addClass('night');
        } else { /* Nichts zu tun */ }
    }
    
    return {
        init: init,
        create_submenu: create_submenu,
        getTime: getTime
    }
    
})(jQuery);

jQuery(document).ready(function () {
    syntops.header.init();
});

