function Vote() {}

Vote.prototype.voteUp = function( itemId, userId, voteHash, type ) {
    this.request( itemId, userId, voteHash, 'up', type );
}

Vote.prototype.voteDown = function( itemId, userId, voteHash, type ) {
    this.request( itemId, userId, voteHash, 'down', type );
}

Vote.prototype.request = function( itemId, userId, hash, action, type ) {
    var obj = this;

    $.post( '/votes/dovote', 
            { iid: itemId, 
              uid: userId, 
              hash: hash, 
              action: action, 
              type: type 
            }, 
            function ( msg ) {
                if ( msg.message == 'success' ) {
                    var currentVote = msg.data.currentVote;
                    if ( currentVote == 1 ) {
                        $( '#voteUp' + itemId ).removeClass( 'upVote' );
                        $( '#voteUp' + itemId ).addClass( 'upVoted' );
                        $( '#voteDown' + itemId ).addClass( 'downVote' );
                        $( '#voteDown' + itemId ).removeClass( 'downVoted' );
                    }
                    else if ( currentVote == 0 ) {
                        $( '#voteUp' + itemId ).removeClass( 'upVoted' );
                        $( '#voteUp' + itemId ).addClass( 'upVote' );
                        $( '#voteDown' + itemId ).removeClass( 'downVoted' );
                        $( '#voteDown' + itemId ).addClass( 'downVote' );
                    }
                    else if ( currentVote == -1 ) {
                        $( '#voteUp' + itemId ).removeClass( 'upVoted' );
                        $( '#voteUp' + itemId ).addClass( 'upVote' );
                        $( '#voteDown' + itemId ).removeClass( 'downVote' );
                        $( '#voteDown' + itemId ).addClass( 'downVoted' );
                    }
                    var value = msg.data.totalVotes;
//                    value = ( value < 0 && type == 'image' ) ? 0 : value;
                    $( '#voteScore' + itemId ).text( value );
                }
            },
            'json' );
}

vote = new Vote();