jQuery(document).ready(function($) {
    
    $('#cart').miniCart();
    $("#dialog").dialog({autoOpen: false, modal: true});
    
    $('form.quickadd').submit(function() {
        $.ajax({
            url: $(this).attr('action'),
            type: $(this).attr('method'),
            data: $(this).serialize(),
            dataType: 'json',
            success: function(json) {
                if (json.result) {
                    $('#cart').miniCart.updateQty(json.cart.num_items)
                                       .updateAmt(json.cart.total)
                                       .show();
                    $('#cart').effect('bounce', {}, 100);
                }
            }
        });
        
        return false;
    });
    
    $('a.button-cart-minimize').live('click', function() {
        $('#cart').miniCart.minimize();
    });
    
    $(':input[name=payment_method]').click(function() {
        var updateDivs = $('.payment-method-'+$(this).val());
        
        $('.payment-method').not($('.payment-method-'+$(this).val())).slideUp();
        
        updateDivs.each(function() {
            if ($(this).css('display') == 'none') {
                $(this).slideDown(); 
            }
        });
    });
    
    $('dl.expandable dt a').click(function() {
        var mylink = $(this).attr('href').substring(1);
        $(this).parent().parent().children('dd').hide();
        $(this).parent().parent().children('dt').removeClass('expanded');
        $(this).parent().addClass('expanded');
        $('.product-screenshot li').fadeOut();
        $('#detail-'+mylink).show();
        $('#screenshot-'+mylink).fadeIn();
    });
    
    /* copy billing info to shipping info */
    $("#copy_billing").click(function() {
       var copy = $(this).is(":checked");
       
       $("fieldset.billing input[type=text], fieldset.billing select").each(function() {
           var billingId = $(this).attr("id");
           var shippingId = billingId.replace('billing', 'shipping');
           if (copy) {
               $("#"+shippingId).val($(this).val());
           } else {
               $("#"+shippingId).val("");
           }
       });
    });
    
    $('#billing-state').change(function() {
        if ($(this).val() == 'KY') {
            $('#tax_exempt').fadeIn();
        } else {
            $('#tax_exempt').fadeOut();
        }
    }).trigger('change');
    
    
    $("form.shipping_calculator").submit(function() {
       var button = $(this).find('input[type=submit]');
       button.attr("disabled", "disabled").attr("value", "Calculating...");
       $.post($(this).attr("action"), $(this).serialize(), function(json) {
           button.removeAttr("disabled").attr("value", "Calculate Shipping");
           $('#calculated_shipping').val('');
           if (json.errors.length) {
               $("#dialog").html(json.errors.join("<br/>"))
                           .dialog("open");
           } else {
               $('#calculated_shipping').html("<strong>$" + json.cost + "</strong>")
                                        .effect('highlight');
           }
       }, 'json');
       return false;
    });
});

function loginAjax(loginForm)
{
    var username = loginForm.elements['username'].value;
    var password = loginForm.elements['password'].value;
    
    jQuery.post('/login', {
        username: username,
        password: password 
    }, function(json) {
        if (json.result == true) {
            try { tb_close(); } catch (e) { };
            location.reload();
        } else {
            alert(json.message);
        }
    }, 'json');
}

