var black = (function () {
//------------

// function max (x, y) {
//     if (x < y) {
//         return y;
//     }
//     return x;
// }

function balance_tops (nodes) {
    var local_max = 0;
    nodes.each(function () {
        local_max = Math.max($(this).offset().top, local_max);
    });
    nodes.each(function () {
        var me = $(this);
        var needs = local_max - me.offset().top;
        me.css('margin-top', needs + 20);
    });
};


function blur_self_label () {
    var me = $(this);
    if (!me.val() || me.val() == me.attr('title')) {
        me.addClass('showing-label');
        me.val(me.attr('title'));
        if (me.attr('title') == 'Password') {
            me.attr('type', 'text');
        }
    }
}

function label_with_title () {
    var me = $(this);
    if (!me.val()) {
        me.val(me.attr('title'));
        me.addClass('showing-label');
    }
    me.focus(focus_self_label);
    me.blur(blur_self_label);
};

function clear_self_labels () {
    var me = $(this);
    inps = me.find('.self-label');
    inps.each(function () {
        var input = $(this);
        input.unbind('blur');
        input.focus();
        input.blur();
    });
}


function show_panel_with_content (content, callback) {
    containerPos = $('#container').offset();
    $('#panel-mask').css({
        'height': $(document).height(),
        'margin-left': 0,
        'margin-right': 0,
        'width': '100%',
        'filter': 'alpha(opacity=90)'
    }).fadeIn();
    var panel = $('#panel');
    if (!panel.length) {
        var panel = $('<div id="panel" class="container_12"><a href="#close" class="panel-close-link">Close</a><div id="panel-inner" class="clearfix"></div></div>');
        panel.hide();
        $('body').append(panel);
    }
    var inner = panel.find('#panel-inner');
    inner.empty();
    inner.append(content.html());
    panel.css({
        top: $(window).scrollTop() + 20,
        left: ($('body').width() - panel.width()) / 2
    });
    panel.fadeIn(400, callback);
    $('.panel-close-link').click(close_panel);
}

function close_panel () {
    $('#panel').fadeOut();
    $('#panel-mask').fadeOut();
    return false;
} 

function show_wine () {
    window.setTimeout(function () {
        show_panel_with_content($('#wines-panel'));        
    }, 600);
    return false;
}
function show_food () {
    window.setTimeout(function () {
        show_panel_with_content($('#food-panel'));        
    }, 600);
    return false;
}
function show_beers () {
    window.setTimeout(function () {
        show_panel_with_content($('#beers-panel'));        
    }, 600);
    return false;
}


$(function () {
    $('#nav a').add('a.scroll').click(function () {
        var target = $(this.hash);
        var hash = this.hash;
        // $target = $target.length && $target
        // || $('[name=' + this.hash.slice(1) +']');
        if (target.length) {
            var targetOffset = target.offset().top;
            $('html,body').animate({scrollTop: targetOffset}, 600);
            return false;
		}
	});
	

	$('#panel-mask').click(close_panel);
	$('.wines-link').click(show_wine);
	$('.food-link').click(show_food);
	$('.beers-link').click(show_beers);
balance_tops($('#people .balance'));
	balance_tops($('#the-wines .final'));
	
    // THIS IS REALLY FREAKING BIZARRE 
    // I SHOULD NOT HAVE TO DO THIS BUT IE (6 & 7) ARE PRETTY MESSED UP.
    $('#news').css({'position': 'absolute'});
	var nav = $('#nav');
	nav.css({'position': 'absolute'});
	
	/*
	 * MAGICAL SCROLLING NAV
	 * (not that magical)
	 * 
 	 * This is using lots of vars because it gets called all the time 
 	 * on scroll, so it needs to be fast.
	 */
	var topmost_point = nav.offset().top;
    var left_point = $('#container').offset().left;
	var PADDING_TOP = 63; // MAGIC NUMBER
    var REAL_TOP = topmost_point - PADDING_TOP;
	var the_window = $(window);
	var NAV_IS_FIXED = (nav.css('position') == 'fixed');
	the_window.scroll(function () {
        if (the_window.scrollTop() > REAL_TOP) {
            if ($.browser.msie && $.browser.version == "6.0") {
                nav.css('top', the_window.scrollTop() + PADDING_TOP);
            } else if (!NAV_IS_FIXED) {
        	    nav.css({
                    left: left_point,
        	        top: PADDING_TOP,
        	        position: 'fixed'
        	    });
        	    NAV_IS_FIXED = true;
        	}
        } else {
            if (NAV_IS_FIXED) {
                nav.css({
                    position: 'absolute',
                    top: topmost_point,
                    left: ''
                });
                NAV_IS_FIXED = false;
            }
        }
	});
	
	the_window.resize(function () {
	    left_point = $('#container').offset().left;
	    if (NAV_IS_FIXED) {
    	    nav.css('left', left_point);	        
	    }
	});
});


//---
    return {
        format_price: format_price,
        get_price: get_price
    };
})();

