function SiteHeader(el) {
    this.minMenuTop = 80;
    this.header = el;
    this.header_title = $('h1', this.header)[0];
    this.menubar = $('ul.menu', this.header);
    this.menubar_title = $('h2', this.menubar);
    this.sticky_menuitem = $('li.sticky', this.menubar);
    this.bindEvents();
    this.updateMenubar();
}

SiteHeader.prototype.bindEvents = function() {
    var dis = this;
    $(window).scroll(function() {
        dis.updateMenubar();
    });
    $(window).resize(function() {
        dis.updateMenubar();
    });
};

SiteHeader.prototype.updateMenubar = function() {
    if (this.menuShouldFloat()) {
        if (!this.menubar.hasClass('fixed')) {
            this.menubar.addClass('fixed');
            this.sticky_menuitem.removeClass('sticky');
        }

        if (this.menubar_title.hasClass('hidden')) {
            this.menubar_title.removeClass('hidden');
            this.menubar_title.fadeIn();
        }

    } else {
        if (this.menubar.hasClass('fixed')) {
            this.menubar.removeClass('fixed');
            this.sticky_menuitem.addClass('sticky');
        }

        if (!this.menubar_title.hasClass('hidden')) {
            this.menubar_title.addClass('hidden');
            this.menubar_title.fadeOut();
        }
    }
};

SiteHeader.prototype.menuShouldFloat = function() {
    if ($(window).scrollTop() > this.minMenuTop)
        return true;
    else
        return false;
};

$(function() {
    window.site_header = new SiteHeader($('#header'));
});
