/* 
 * This script handles all on page requests and ties AHAH, JQuery, and the DOM
 * together.
 */

//Get the host
var host = 'http://' + window.location.hostname + ':' + window.location.port;

$(document).ready(function(){
    //Fetch Wallet
    wallet('GET');

    //Fetch Cart
    ahah(host + '/cart/', 'cart', 'GET');

    //Attach click event to incoming wallet button
    $('#wallet button').live("click", function(){wallet('POST')});

    //Attach click event to check out button
    $('#cart button').live('click', function(){
        ahah(host + '/cart/?checkout=1', 'cart', 'POST');
        setTimeout("wallet('GET')", 400);
    })

    //Add item to cart
    $('#shop button').click(function(){
        ahah(host + '/cart/?id=' + this.id, 'cart', 'POST');
    });

    //Attach remove links
    $('#cart .cart-item a.remove').live('click', function(){
        ahah(host + '/cart/?id=' + $(this).parent().parent().attr('id'), 'cart', 'DELETE');
    })
})

function wallet(method){
    ahah(host + '/wallet/', 'wallet',  method);
}
