var Store = {
    setup: function() {
        var hide_em = $('dneirf_ot_liame_na_edih');
        
        if(hide_em) {
            hide_em.style.display = 'none';
        }
        
        Store.protect_quantity();
        // Store.email_a_friend();
    },
    
    protect_quantity: function() {
        var quantity = $('id_the_quantity_available');
        var product_form = $('add_to_cart_form');
        
        if(quantity && product_form) {
            qty_avail = parseInt(quantity.innerHTML);
            product_form.observe('submit', function(submit_event) {
               qty_desired = $F('id_quantity');
               
               if(qty_desired > qty_avail) {
                   submit_event.stop();
                   alert("You've tried to purchase more product than is available! Please correct the amount and try adding again.");
               }
            });
        }
    },
    
    email_a_friend: function() {
        // FIXME: Remove this old code and create a couple Ajax handlers that 
        // pull in the page and do the submit over Ajax.
        
        var email_link = $('id_email_a_friend_link');
        var email_wrapper = $('id_email_a_friend_wrapper');
        
        if(email_link && email_wrapper) {
            email_link.observe('click', function(the_event) {
                the_event.stop();
                email_wrapper.toggle();
            });
        }
        
        var email_form = $('id_email_a_friend_form');
        
        if(email_form) {
            email_form.observe('submit', function(submit_event) {
                submit_event.stop();
                email_form.request({
                    onFailure: function() {
                        alert("Sorry, we couldn't send the email for some reason.");
                    },
                    onSuccess: function() {
                        alert('Your friend has been emailed! Thank you very much!');
                    },
                    onComplete: function() {
                        $('id_email_a_friend_wrapper').hide();
                    }
                });
            });
        }
    }
}

Event.observe(document, 'dom:loaded', Store.setup);