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

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



function focus_self_label () {
    var me = $j(this);
    if (me.hasClass('showing-label')) {
        me.removeClass('showing-label');
        if (me.val() == me.attr('title')) {
            me.val('');
        }
        if (me.attr('title') == 'Password') {
            me.attr('type', 'password');
        }
    }
}

function blur_self_label () {
    var me = $j(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 = $j(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 = $j(this);
    inps = me.find('.self-label');
    inps.each(function () {
        var input = $j(this);
        input.unbind('blur');
        input.focus();
        input.blur();
    });
}


function show_panel_with_content (content, callback) {
    containerPos = $j('#container').offset();
    $j('#panel-mask').css({
        'height': $j(document).height(),
        'margin-left': 0,
        'margin-right': 0,
        'width': '100%',
        'filter': 'alpha(opacity=90)'
    }).fadeIn();
    var panel = $j('#panel');
    if (!panel.length) {
        var panel = $j('<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();
        $j('body').append(panel);
    }
    var inner = panel.find('#panel-inner');
    inner.empty();
    inner.append(content.html());
    panel.css({
        top: $j(window).scrollTop() + 20,
        left: ($j('body').width() - panel.width()) / 2
    });
    panel.fadeIn(400, callback);
    $j('.panel-close-link').click(close_panel);
}

function close_panel () {
    $j('#panel').fadeOut();
    $j('#panel-mask').fadeOut();
    return false;
}
*/
/*
function get_price (num) {
    return Number(num.replace(/[^-0-9.]/g, ''));
}

function format_price (num) {
    num_str = '' + num;
    if (!num_str.match(/\./)) {
        return '$j' + num_str + '.00';
    } else if (num_str.match(/\..$j/)) {
        return '$j' + num_str + '0';
    } else {
        return '$j' + num_str.replace(/\.([0-9]{2}).+/, '.$j1');
    }
}

function total_for (row) {
    var quant = 0 + row.find('td input.quantity').eq(0).val();
    var price = 0 + get_price(row.find('td.price').text());
    row.find('td.total').text(format_price(price * quant));
    return price * quant;
} 

function update_totals () {
    var grand_quantity = 0;
    var grand_total = 0;
    $j('.order-table tbody tr').each(function () {
        var row = $j(this);
        var new_quant = Number(row.find('td input.quantity').val());
        if (isNaN(new_quant)) {
            row.find('td input.quantity').val(0);
            alert('Please enter a valid quantity');
            row.find('td input.quantity').focus();            
        } else {
            grand_quantity += new_quant;         
        }
        grand_total += total_for(row);
    });
    
    
    $j('.total-quantity').text(grand_quantity);
    $j('.total-price').text(format_price(grand_total));
}

function show_order_form () {
    show_panel_with_content($j('#order-panel'), function () {
        $j('.order-form').submit(submit_order);
        $j('.order-form button').click(function () {
            $j(this).parents('form').submit();
            return false;
        });
        $j('.order-form input').blur(update_totals);
    });
    return false;
}

function show_stockists () {
    window.setTimeout(function () {
        show_panel_with_content($j('#stockists-panel'));        
    }, 600);
    return false;
}


function submit_async (form, callback) {
    var form_data = form.serialize();
    $j.post(form.attr('action'), form_data, callback, "json");
}

function submit_order () {
    // about to submit...
    var me = $j(this);
    
    // check total...
    var quantity = Number($j('.total-quantity').text());
    if (quantity % 3 != 0 || quantity == 0) {
        alert("Please ensure that your total number of bottles can break into 3s. We ship 3-, 6-, and 12-packs.");
        return false;
    }
    
    me.css({
        // opacity: 0.5,
        // filter: "alpha(50)"
    });
    me.find('button').attr('readonly', 'yes');
    $j('.order-status-message').html('<em>Sending…</em>');
    // then we do it
    submit_async(me, function (data, textStatus) {
        if (data['success']) {
            // success
            $j('.order-status-message').html('<p>Order Sent Successfully</p>');
            window.setTimeout(close_panel, 2000);
        } else if (data['errors']) {
            // errors
            $j('.order-status-message').html("<p>Please review the following errors:</p>" + 
                data['errors']);
            me.find('input,button').removeAttr('readonly');
        } else {
            // failure
            $j('.order-status-message').html('<p>An error was encountered while sending</p>');
        }
    });
    me.find('input').attr('readonly', 'yes');
    return false;
}

function submit_maillist () {
    $j("#maillist-error").html("<em>Submitting…</em>");
    submit_async($j(this), function (data, textStatus) {
        if (data['success']) {
            // success
            $j("#maillist-error").text("Thanks, we'll be in touch.");
            $j("#maillist-form input").val("");
        } else {
            // failure
            $j("#maillist-error").text("Problem subscribing: " + data['errors']);            
        }
    });
    return false;
}
*/



	
	//$j('.order-link').click(show_order_form);
	//$j('#panel-mask').click(close_panel);
	
	//$j('.stockists-link').click(show_stockists);
	
	//$j('.self-label').each(label_with_title);
    //$j('form').submit(clear_self_labels);

   //$j('.order-form').submit(submit_order);
   // $j('.maillist-form').submit(submit_maillist);
    
//	balance_tops($j('#people .balance'));
//	balance_tops($j('#the-wines .final'));
	

	
//});


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

$j(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($j(this).offset().top, local_max);
    });
    nodes.each(function () {
        var me = $j(this);
        var needs = local_max - me.offset().top;
        me.css('margin-top', needs + 20);
    });
}; */
    $j('#news a').add('a.scroll').click(function () {
        var target = $j(this.hash);
        var hash = this.hash;
        // $jtarget = $jtarget.length && $jtarget
        // || $j('[name=' + this.hash.slice(1) +']');
        if (target.length) {
            var targetOffset = target.offset().top -50;
            $j('html,body').animate({scrollTop: targetOffset}, 600);
            return false;

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

		}
	});

    $j('#news').css({'position': 'absolute'});
	var partners = $j('#partnersright');
	partners.css({'position': 'absolute'});


    // THIS IS REALLY FREAKING BIZARRE 
    // I SHOULD NOT HAVE TO DO THIS BUT IE (6 & 7) ARE PRETTY MESSED UP.
    $j('#news').css({'position': 'absolute'});
 //   $j('#news2').css({'position': 'absolute'});
	var navi = $j('#navi');
	navi.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 = navi.offset().top;
    var left_point = $j('#container').offset().left;
	var PADDING_TOP = 0; // MAGIC NUMBER
    var REAL_TOP = topmost_point;
	var the_window = $j(window);
	var navi_IS_FIXED = (navi.css('position') == 'fixed');

	the_window.scroll(function () {
        if (the_window.scrollTop() > REAL_TOP) {
            //if ($j.browser.msie && $j.browser.version == "6.0") {
            if ($j.browser.msie ) {
                navi.css('top', the_window.scrollTop());
                partners.css('top', the_window.scrollTop());
            } else if (!navi_IS_FIXED) {
        	    navi.css({
        	        top: PADDING_TOP,
        	        position: 'fixed'
        	    });
        	    partners.css({
        	        top: PADDING_TOP,
        	        position: 'fixed'
        	    });
        	    navi_IS_FIXED = true;
        	}
        } else {
             if ($j.browser.msie ) {
                navi.css('top', 0);
                partners.css('top', 0);
            } else if (navi_IS_FIXED) {
                navi.css({
                    position: 'absolute',
                    top: topmost_point
                });
                partners.css({
                    position: 'fixed',
                    top: topmost_point
                });
                navi_IS_FIXED = false;
            }
        }
	});
	
	/*
	the_window.resize(function () {
	    left_point = $j('#container').offset().left;
	    alert(left_point);
	    if (navi_IS_FIXED) {
    	    navi.css('left', left_point);	        
	    }
	});
	*/
});
