/* jQuery Cookie Plugin */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

jQuery(document).ready(function() {

    jQuery("#results .result").each(function() {
        var voteCount = parseInt(jQuery(".voteCount", this).text(), 10);
        if (voteCount < -2) {      
            var showLink = '<p class="test"><span class="showmsg">' + voteCount + ' votes. Below viewing threshold.</span> <a href="#" class="showDownvoted">Show</a></p>';
            jQuery(this)
            .addClass('downvoted')
            .append(showLink);
        }
    }); // end #results .result

    jQuery(".showDownvoted").click(function() {
        jQuery(this).parents('li.result').removeClass('downvoted');
        jQuery(this).parent().remove();
        return false;
    }); // end .showDownvoted

    // Grab votes from cookie
    var votes = jQuery.evalJSON(jQuery.cookie('votes'));
    if (!votes) {
        votes = {};
    }

    for (vote in votes) {
        var parent = jQuery("#vote_" + vote);

        jQuery(".rollover", parent)
        .removeClass('rollover')
        .unbind('mouseover')
        .unbind('mouseout');


        if (votes[vote] == 'u') {
            jQuery(".voteUp", parent).children('img').attr('src', '/images/myidea/thumb-up_active.gif');
        }
        else {
            jQuery(".voteDown", parent).children('img').attr('src', '/images/myidea/thumb-down_active.gif');
        }
    } // end for loop (cookie values)



    jQuery(".voteUp").click(function() {
        var id = jQuery(this).parent().attr('id').substring(5);
        // remove vote_ from being
        // Check cookie to determine if vote already happened
        if (votes[id]) {
            return false;
            // already voted
        }
        votes[id] = 'u';
        jQuery.cookie('votes', jQuery.toJSON(votes), {
            expires: 5000,
            path: '/'
        });

        // AJAX call
        jQuery.get(jQuery(this).attr('href'));


        // Set image and counter
        jQuery(this).children('img').attr('src', '/images/myidea/thumb-up_active.gif').removeClass('rollover').unbind('mouseover').unbind('mouseout');
        // Unbind mouseover on sibling vote button
        jQuery(this).siblings('.voteDown').children('img').removeClass('rollover').unbind('mouseover').unbind('mouseout');

        var voteCount = jQuery(this).parent().children('.voteCount');
        voteCount.text(parseInt(voteCount.text(), 10) + 1);

        return false;
    }); // end voteUp

    jQuery(".voteDown").click(function() {
        var id = jQuery(this).parent().attr('id').substring(5);
        //remove vote_ from begin
        // Check cookie to determine if vote already happened
        if (votes[id]) {
            return false;
            // already voted
        }
        votes[id] = 'd';
        jQuery.cookie('votes', jQuery.toJSON(votes), {
            expires: 5000,
            path: '/'
        });

        // AJAX call
        jQuery.get(jQuery(this).attr('href'));

        // Set image and counter
        jQuery(this)
        .children('img')
        .attr('src', '/images/myidea/thumb-down_active.gif')
        .removeClass('rollover')
        .unbind('mouseover')
        .unbind('mouseout');

        // Unbind mouseover on sibling vote button
        jQuery(this).siblings('.voteUp').children('img').removeClass('rollover').unbind('mouseover').unbind('mouseout');


        var voteCount = jQuery(this).parent().children('.voteCount');
        voteCount.text(parseInt(voteCount.text(), 10) - 1);
        return false;
    }); // end voteDown

});

