// Denotes if an Ajax request is underway
var requestInAction = false;

var responseMessages = {
    'voteInAction': 'Edellinen äänestys kesken. Ole hyvä, ja odota.',
    'alreadyVoted': 'Olet jo äänestänyt tätä viestiä.'
};

$(document).ready(function() {
    
    jQuery.fn.extend(fieldSelection);
    
    changeMessageVotes = function (id, positive, negative, thumb) {
        // tähän kohtaan ylös-alas merkintöjä
        if (thumb == 'negative') {
            
        } else if (thumb == 'positive') {
        
        } else if (thumb == 'changed') {
            
        }
        if (positive != '' && negative != '') {
            $('#rat-' + id + ' span.pos, #raq-' + id + ' span.pos').text(positive);
            $('#rat-' + id + ' span.neg, #raq-' + id + ' span.neg').text(negative);
        } 
        $('#rat-' + id + ', #raq-' + id).addClass('disabled');
    }

    isEligibleToVote = function (msgId, votesCast) {
        var canVote = false;
    
        if (votesCast == null || votesCast == undefined) {
            votesCast = [];
        } else {
            votesCast = votesCast.replace('|', ',');
            votesCast = votesCast.split(',');
        }
        
        if ($.inArray(msgId, votesCast) != -1) {
            canVote = false;
        } else {
            canVote = true;
        }
        return canVote;
    }

    var registeredVotes = $.cookie('ts_votes');
    
    if (registeredVotes != null && isset("votablesOnPage")) { // votablesOnPage is defined inline by the page
        if (registeredVotes != null) {
            var tempVotes = registeredVotes.replace('|', ',');
            tempVotes = tempVotes.split(',');
            
            votablesOnPage = votablesOnPage.split(',');
        
            $.each(votablesOnPage, function(i, val) {
                if ($.inArray(val, tempVotes) != -1) {
                    changeMessageVotes(val, '', '', 'changed');
                }
            });
        }
    } 
    

    vote = function (msgId, thumb) {
        if (requestInAction == true) { // requestInAction blocks simultaneous ajax calls
            alert(responseMessages.voteInAction);
        } else {
            requestInAction = true;
            
            if (thumb != 'negative') {
                thumb = 'positive';
            }
            
            var registeredVotes = $.cookie('ts_votes');

            var canVote = isEligibleToVote(msgId, registeredVotes);

            if ($('.ratingTools.'+msgId).hasClass('disabled')) { // user has already voted
                requestInAction = false; // make other messages votable again
                canVote = false;
                alert(responseMessages.alreadyVoted);
            }

            if (canVote) {
                var positive = $('.ratingTools.'+msgId+' span.pos').text();
                var negative = $('.ratingTools.'+msgId+' span.pos').text();
                $.getJSON('/cinc/recommend.php', { msgID: msgId, vote_thumb: thumb }, 
                    function(json) {
                        requestInAction = true;
                        canVote = false;
                        changeMessageVotes(json.msgID, json.positive, json.negative, json.thumb);
                        requestInAction = false;
                    }
                );
            } else {
                $('.ratingTools.'+msgId).addClass('disabled')
                requestInAction = false;
            }
        }
        return false;
    }
    
    
    // News voting
    $("#col1A .ratingTools a").click(function () {
                     
        if (requestInAction == true) { // requestInAction blocks simultaneous ajax calls
            alert(responseMessages.voteInAction);
        } else {
            requestInAction = true;
        
            var msgId = '0';
            var parentId = '';
            
            var thumb = '';
            
            if ($(this).parent().hasClass("ratingNegative")) {
                thumb = 'negative';
            } else if ($(this).parent().hasClass("ratingPositive")) {
                thumb = 'positive';
            }
            
            if ($(this).parent().parent().hasClass('ratingTools')) {
                parentId = $(this).parent().parent().attr('id');
            }
            
            msgId = parentId.substring(4);

            // this is for parsing and passing the forum gen onwards
            hashGen = '';
            if (msgId.substring(0,1) == 'f') {
                hashUrl = $(this).attr('href');
                var regexS = "[\\?&]"+'gen'+"=([^&#]*)";
                var regex = new RegExp( regexS );
                var results = regex.exec( hashUrl );
                if( results != null ) {
                    hashGen = results[1];
                }
            }                                                                                                                                                                    
            
            var registeredVotes = $.cookie('ts_votes');

            var canVote = isEligibleToVote(msgId, registeredVotes);

            if ($('#rat-'+msgId+', #raq-'+msgId).hasClass('disabled')) { // user has already voted
                requestInAction = false; // make other messages votable again
                canVote = false;
                alert(responseMessages.alreadyVoted);
            }

            if (canVote) {
                var positive = $('#rat-'+msgId+' span.pos, #raq-'+msgId + ' span.pos').text();
                var negative = $('#rat-'+msgId+' span.neg, #raq-'+msgId + ' span.neg').text();
                $.getJSON('/cinc/recommend.php', { msgID: msgId, vote_thumb: thumb }, 
                    function(json) {
                        requestInAction = true;
                        canVote = false;
                        changeMessageVotes(json.msgID, json.positive, json.negative, json.thumb);
                        requestInAction = false;
                    }
                );
            } else {
                $("#rat-" + msgId).addClass('disabled')
                requestInAction = false;
            }
        }
        return false;
    });


    // Forum voting
    $("#col1A span.ratingPositive, #col1A span.ratingNegative").click(function () {
                     
        if (requestInAction == true) { // requestInAction blocks simultaneous ajax calls
            alert(responseMessages.voteInAction);
        } else {
            requestInAction = true;
        
            var msgId = '0';
            var parentId = '';
            
            var thumb = '';
            
            if ($(this).hasClass("ratingNegative")) {
                thumb = 'negative';
            } else if ($(this).hasClass("ratingPositive")) {
                thumb = 'positive';
            }
            
                   
            // two paths because the depth is different for 
            if ($(this).parent().attr('id')) {
                parentId = $(this).parent().attr('id');
            } else if ($(this).parent().parent().attr('id')) {
                parentId = $(this).parent().parent().attr('id');
            } else {
                parentId = $(this).parent().parent().parent().attr('id');
            } 
            
            msgId = parentId.substring(4);
            
            var registeredVotes = $.cookie('ts_votes');

            var canVote = isEligibleToVote(msgId, registeredVotes);

            if ($("#rat-" + msgId).hasClass('disabled')) { // user has already voted
                requestInAction = false; // make other messages votable again
                canVote = false;
                alert(responseMessages.alreadyVoted);
            }

            if (canVote) {

                var positive = $("#rat-" + msgId + " span.pos").text();
                var negative = $("#rat-" + msgId + " span.neg").text();
                
                $.getJSON('/cinc/recommend.php', { msgID: msgId, vote_thumb: thumb }, 
                    function(json) {
                        requestInAction = true;
                        canVote = false;
                        changeMessageVotes(json.msgID, json.positive, json.negative, json.thumb);
                        requestInAction = false;
                    }
                );
            }
            
        }
        return false;
    });
});
