jQuery(document).ready(function($) {
    
    $('#cart').miniCart();
    
    $('form.quickadd').submit(function() {
        $.getJSON($(this).attr('action'), $(this).serialize(), 
            function (json) {
                if (json.result) {
                    $('.cart-num-items').miniCart.updateQty(json.number_of_items);
                    $('#cart').miniCart.showAdded('<p>The following items have been added to your cart:</p>'
                        +'<p class="cartnumber">'+json.last_added+'</p>'
                        +'<p>You may continue shopping or proceed to review your order and begin the secure checkout process.</p>'
                        +'<p class="nopadding"><a class="button button-cart-checkout" href="/store/cart"><span>Secure Checkout</span></a>'
                        +'<a class="button button-cart-minimize" href="javascript:;"></p>');
                }
                
            }
        );
        
        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();
    });
    
});

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');
}
