function refresh_item_count() {
    $('#item_count').load('/cart/ajax/cart-item-count/', function() {
        var val = $('#item_count').text();
        $('#header_item_count_singular').toggle(val == '1');
        $('#header_item_count_plural').toggle(val != '1');
    });
}

$(document).ready(function () {
    hs.registerOverlay({
        html: '<div class="closebutton" onclick="return hs.close(this);" title="Close"></div>',
        position: 'top right',
        useOnHtml: true,
        fade: 2 // fading the semi-transparent overlay looks bad in IE
    });
    hs.registerOverlay({
        thumbnailId: 'open_newsletter_signup_errors',
        html: '<div class="closebutton" onclick="return hs.close(this);" title="Close"></div>',
        position: 'top right',
        slideshowGroup: 'open_newsletter_signup_errors',
        useOnHtml: true,
        fade: 2 // fading the semi-transparent overlay looks bad in IE
    });
    hs.registerOverlay({
        thumbnailId: 'open_newsletter_signup_success',
        html: '<div class="closebutton" onclick="return hs.close(this);" title="Close"></div>',
        position: 'top right',
        slideshowGroup: 'open_newsletter_signup_success',
        useOnHtml: true,
        fade: 2 // fading the semi-transparent overlay looks bad in IE
    });
    hs.graphicsDir = '/media/highslide/graphics/';
    hs.dimming=true;
    hs.dimmingOpacity = 0.6;
    hs.dimmingGeckoFix = true;
    hs.showCredits = false;
    hs.loadingText = 'Loading...';
    $('#cart_link a').click(function() {
        if($('#item_count').text() == '0'){
            return false;
        } 
    });
    $('input.quantity').numeric();
    $('.quantity').focus( function(){
        $(this).select();
    });
	$('#newsletter_signup_form').submit(function() {
		$.post($(this).attr('action'), $(this).serialize(), function(data) {
	        // XXX FIXME : This is where we check returning html to see if it was
		    // a success or not. No time to JSON it right now.
		    if (data.search('internal_success_marker') != -1) {
                // check to see if we're on the basket page.
                if ($('#secure_checkout').length > 0) {
                    // and we make sure that we don't ask the user again
                    $('#secure_checkout').unbind('click')
                    $('#secure_checkout').click(function() {
                    	location.href= '/payment/checkout/';
                    });
                }
                $('#newsletter_signup_form').html(data);
                window.setTimeout(function () {
                    if (window.hs.getExpander("open_newsletter_signup_success")) {
                        window.hs.getExpander("open_newsletter_signup_success").close();
                    }
                }, 5000)
                $('#open_newsletter_signup_success').click();
                //$('#join_the_club_div').remove()
                //$('#newsletter_signup_form').parents('.highslide-body:first').load('/no-coupon/thanks/');
                    //.html("Welcome to the KnockKnock Crue! Please check your email for your coupon.");
            } else {
                //alert($("#email_btn").attr('src','/media/images/common/nocoupon_signup.gif'))
                $('#newsletter_signup_form').html(data);
                window.setTimeout(function () { 
                    if (window.hs.getExpander("open_newsletter_signup_errors")){
                        window.hs.getExpander("open_newsletter_signup_errors").close();
                    }
                }, 5000)
                $('#open_newsletter_signup_errors').click();
            }
        });
/*	    
		    $('#newsletter_signup_form').html(data);
		});
*/
		return false;
	});
	
        $('#email_address').focus(function() {
            if($('#email_address').val() == 'EMAIL') {
                $('#email_address').val('');
            }
        });
        $('#email_address').blur(function() {
            if($('#email_address').val() == '') {
                $('#email_address').val('EMAIL');
            }
        });
        if ($('#item_count').text() == "") {
            refresh_item_count();
        }
    });

$(document).ready(function(){
    // free shipping home cta hack
    if(!$("#home_top").size()){ $('#disclaimer').hide(); }
});

function submitSimpleAHAHForm(form, successURL, selectorToReplace) {
    /*
    Intended to be called as a form onsubmit handler.
    On successful reply from server, replace the given jQuery selector with
    data loaded from successURL, if given.
    Otherwise replace form with the plain AHAH server reply, which may list
    validation errors.
    Success is determined by having "internal_success_marker" in the text,
    which is cheesy but has the least risk of breaking existing code that
    already worked similarly.
    */
    $.post($(form).attr('action'), $(form).serialize(), function(data) {
        if (selectorToReplace && data.search('internal_success_marker') != -1) {
            selectorToReplace.load(successURL);
        }
        else {
            $(form).html(data);
        }
    });
    return false;
}
