/*********************************************************************
 *  File: points.js
 *  Script: After-All Bash
 *  Version: 1.0
 *  Author: 2007, m1chu (m1chu@m1chu.eu)
 *  http://www.m1chu.eu
 ********************************************************************/

function explode( delimiter, string, limit ) {
    // http://kevin.vanzonneveld.net
 
    var emptyArray = { 0: '' };
    
    // third argument is not required
    if ( arguments.length < 2
        || typeof arguments[0] == 'undefined'
        || typeof arguments[1] == 'undefined' )
    {
        return null;
    }
 
    if ( delimiter === ''
        || delimiter === false
        || delimiter === null )
    {
        return false;
    }
 
    if ( typeof delimiter == 'function'
        || typeof delimiter == 'object'
        || typeof string == 'function'
        || typeof string == 'object' )
    {
        return emptyArray;
    }
 
    if ( delimiter === true ) {
        delimiter = '1';
    }
    
    if (!limit) {
        return string.toString().split(delimiter.toString());
    } else {
        // support for limit argument
        var splitted = string.toString().split(delimiter.toString());
        var partA = splitted.splice(0, limit - 1);
        var partB = splitted.join(delimiter.toString());
        partA.push(partB);
        return partA;
    }
}

function setError(message, block)
{
    var inputRegExp = /\^u/g;
    var outputRegExp = /#\^u/g;
    message = message.replace(outputRegExp, '</span>');
    message = message.replace(inputRegExp, '<span style="text-decoration: underline;">');
    document.getElementById('message-container').style.display = block;
    document.getElementById('message-container').innerHTML = message; 
    return false;
}

$(document).ready(function() {
	var points = 0;
	var object = "";
	var msg;
	$('a.vote').click(function() {
		$('.vote').css('display', 'none');
		object = this;
		$.ajax({
   			type: "GET",
   			url: object.href,
   			processData: false,
   			timeout: 5000,
   			beforeSend: function() {
   				$(object.parentNode).children('.vote').css('display', 'none');
   				$('#message-container').css('display', 'block');
   			},
   			error: function(req, type) {
   				$('#message-container').text(req.status + " " + type);
   				$('.vote').css('display', 'inline');
   			},
   			success: function(response) {
   				msg = explode(';', response);
   				$(object.parentNode).children('.votesbold').css('visibility', 'visible');
     			$('#message-container').text(msg[0]);
     			points = parseInt($(object.parentNode).children('.votesbold').text())
				if ( object.className.match('plus') && msg[1] == '1' ) 
				{
					$('#pointsVal' + msg[2]).text(points + 1);
				} 
				else if ( object.className.match('minus') && msg[1] == '1' ) 
				{
					$('#pointsVal' + msg[2]).text(points - 1);
				}
				$('.vote').css('display', 'inline');
				$(object).parent().children('a.vote').css('display', 'none');
   			}
 		});
 		return false;
	});
});