var AttachEventListeners = {
    getRecipe: function(clickEl, fillEl) {
        $(clickEl).click(function(e) {
             e.preventDefault();
             RobotChef.requestRecipe(fillEl);
         });
    },
    saveRecipe: function(clickEl, formEl) {
        $(formEl).hide();
        $(clickEl).click(function() {
            RobotChef.showForm(formEl);
        })
        $(formEl).submit(function(e) {
            e.preventDefault();
            RobotChef.submitRecipeReturnURL();
        });
    }
}

var RobotChef = {
    urls: {
        newRecipe:  '/newrecipe',
        saveRecipe: '/addsubmission',
        twitter:    'http://twitter.com/home?status='
    },
    callbacks: {
        populateRecipeCard: function(html, fillEl) {
            $('a.gimme-another').removeClass('loading');
            $(fillEl).empty().append(html);
            AttachEventListeners.saveRecipe('h2:first', 'form');
        },
        addRecipeLink: function(url) {
            var twitterMsg = $.URLEncode('Check out my recipe ' + $('form input[name="title"]').val() + ' that I made using RobotChef: ' + url);
            var twitterLink = '<a class="twitter" href="' + RobotChef.urls.twitter + twitterMsg + '">Tweet this!</a>';
            $(twitterLink).insertAfter( $('h2:first'));
            RobotChef.hideForm('form');
        }
    },
    requestRecipe: function(fillEl) {
        $('a.gimme-another').addClass('loading');
        $.get(RobotChef.urls.newRecipe, function(html) {
            RobotChef.callbacks.populateRecipeCard(html, fillEl);
        });
    }, 
    addCursorAfterRecipeTitle: function(el) {
        $(el).toggleClass('highlight');
    },
    showForm: function(formEl) {
        $('a.twitter').remove();
        $(formEl).show();
        $('h2:first').hide();
        $('form').click(function(e) {
            $('body').click(function(e) {
                $('a.gimme-another').click(function(e) { e.stopPropagation(); });
                RobotChef.hideForm(formEl);
            });
          e.stopPropagation();
        });
    },
    submitRecipeReturnURL: function() {
        $.post(RobotChef.urls.saveRecipe, { 
            instructions: $('form input[name="instructions"]').val(),
            ingredients: $('form input[name="ingredients"]').val(),
            email: $('form input[name="email"]').val(),
            name: $('form input[name="name"]').val(), 
            title: $('form input[name="title"]').val() 
        }, function(url) {
            RobotChef.callbacks.addRecipeLink(url);
        });
    },
    hideForm: function(formEl) {
        $(formEl).slideUp('fast');
        $('h2:first').show();
        $('body').unbind('click');
    }
};

$(document).ready(function() {
    AttachEventListeners.getRecipe('a.gimme-another', '#recipe-card .contents');
    AttachEventListeners.saveRecipe('h2:first', 'form');
    setInterval(function(){
        RobotChef.addCursorAfterRecipeTitle('h2:first span:first');
    }, 500);
    var _gaq = _gaq || []; 
    _gaq.push(['_setAccount', 'UA-4169743-2']); 
    _gaq.push(['_trackPageview']); 
     
    (function() { 
        var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; 
        ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; 
        var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); 
    })();
});
