	/*
	Plugin Name: Post Karma
	Description: Allows you to rate posts up or down.
	Author: Eric Rachlin
	Author URI: http://www.overheardinprovidence.com
	Version: 1.0
	*/ 

	/*
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
	*/

function createXMLHttpRequest(){
    var xmlhttp = null;
    try {
        // Moz supports XMLHttpRequest. IE uses ActiveX.
        // browser detction is bad. object detection works for any browser
        xmlhttp = window.XMLHttpRequest ? new XMLHttpRequest() : new ActiveXObject("Microsoft.XMLHTTP");
    } catch (e) {
        // browser doesn’t support ajax. handle however you want
        //document.getElementById("errormsg").innerHTML = "Your browser doesnt support XMLHttpRequest.";
        alert("Your browser does not support the java script required for voting, our appologies.");
    }
    return xmlhttp;
}

var xhr = createXMLHttpRequest();

function pk_alert(message, id){
    var alert_box = document.getElementById("pk_alert-" + id);
    if (alert_box) {
        alert_box.innerHTML = message;
    } else {
        alert(error_message);
    }
}

function karma(id, action, path, t_prefix){
    xhr.open('get', 'http\://'+ path +'pk-processkarma.php?id='+ id +'&action='+ action +'&path='+ path +'&prefix='+ t_prefix);
    xhr.onreadystatechange = handleResponse;
    xhr.send(null);
}

function handleResponse(){
    if(xhr.readyState == 4){
        var response = xhr.responseText.split('|');
        
        if(response[0] == 'done'){
            if(response[1]){
                //Changes the thumbs to dull gray and disable the action
                //document.getElementById("down-"+response[1]).src        = "http://"+response[3]+'images/gray_down.png';
                //document.getElementById("down-"+response[1]).onclick    = '';
                //document.getElementById("up-"+response[1]).src          = "http://"+response[3]+'images/gray_up.png';
                //document.getElementById("up-"+response[1]).onclick      = '';
                //Update the karma number display
                //Grab prefix for minus and positive numbers
                if(response[2] < 0){
                    var prefix = '-';
                } else {
                    var prefix = '';
                }
                if(!response[2]){
                	pk_alert("An error has occured, please try again later.", response[1]);
                }
                var element = document.getElementById("up-"+response[1]);
                if (element) { element.innerHTML = ""; }
                element = document.getElementById("down-"+response[1]);
                if (element) { element.innerHTML = ""; }

                var karmanumber = prefix + response[2];
                document.getElementById("karma-"+response[1]).innerHTML = karmanumber;
                pk_alert('Thanks for voting. Ratings are updated every 15 minutes.', response[1]);
            } else {
                alert("Thanks for voting. Ratings are updated every 15 minutes.");
            }
        }
        else if(response[0] == 'error')
        {
            var error = 'Uh-oh: '+response[1];
            alert(error);
        } else {
            alert("An error has occured, please try again later.");
        }
    }
}

