﻿$(document).ready(
    function() {
        $('img.thumbs-up').hover(
            function(e) {
                $(this).attr('prevSrc', $(this).attr('src'));
                $(this).attr('src', _vpath + '/images/thumbs_up_on.gif');
            },
            function(e) {
                $(this).attr('src', $(this).attr('prevSrc'));
            }
        );

        $('img.thumbs-down').hover(
            function(e) {
                $(this).attr('prevSrc', $(this).attr('src'));
                $(this).attr('src', _vpath + '/images/thumbs_down_on.gif');
            },
            function(e) {
                $(this).attr('src', $(this).attr('prevSrc'));
            }
        );

        $('img.thumbs-up').click(
            function(e) {
                var params = eval('(' + $(this).attr('rel') + ')');
                params['p'] = 1;
                mojo.submit(params, $(this).parent().find('span.thisMojo'));
                
                $(this).attr('prevSrc', _vpath + '/images/thumbs_up_on.gif');
                $(this).attr('src', _vpath + '/images/thumbs_up_on.gif');
                $(this).unbind('click');
                $(this).parent().find('img.thumbs-down').unbind('mouseover');
                $(this).parent().find('img.thumbs-down').unbind('click');
            }
        );

        $('img.thumbs-down').click(
            function(e) {
                var params = eval('(' + $(this).attr('rel') + ')');
                params['p'] = -1;
                mojo.submit(params, $(this).parent().find('span.thisMojo'));

                $(this).attr('prevSrc', _vpath + '/images/thumbs_down_on.gif');
                $(this).attr('src', _vpath + '/images/thumbs_down_on.gif');
                $(this).unbind('click');
                $(this).parent().find('img.thumbs-up').unbind('mouseover');
                $(this).parent().find('img.thumbs-up').unbind('click');
            }
        );
    }
);

    var mojo = {
        submit: function(data, thisMojo) {
            $.post(_vpath + '/MojoHandler.ashx', data,
            function(data, textStatus) {
                if (textStatus == 'success') {
                    data = eval('(' + data + ')');
                    if (data['times'] == -1) alert('Sorry, but you can only mojo up to 10 times per day.');
                    else if (data['times'] >= 0) {
                        if (thisMojo) thisMojo.text(data['mojo'] > 0 ? '+' + data['mojo'] : data['mojo']);
                        alert('Thanks!  You can mojo ' + (10 - data['times']) + ' more time' + (data['times'] > 1 ? 's' : '') + ' today.');
                    }
                    else alert(data['error']);
                }
                else alert('Unable to process request!');
            },
            'text'
        );
        }
    };