// set up ajax




// stuff to handle link tuffvote client side stuff. Using JQuery!!

// constants that should match those in the module. 
var TUFFVOTE_AJAX_VOTEOK = 1;
var TUFFVOTE_AJAX_NOTVOTEDYET = 0;
var TUFFVOTE_AJAX_CANTVOTE = -1; 
var TUFFVOTE_AJAX_ALREADYVOTED = -2;
var TUFFVOTE_AJAX_ERROR = -5;
var TUFFVOTE_VALUE = 1;

var TUFFVOTE_BASEURL  = ""; // change this to the base URL of your drupal site. 

// ajax code below. 
$(document).ready(function() { // when document is loaded (not rendered). 
						   
		// $("#tuffvote_widget_votelink").attr("href", "javascript:void(0)"); // erase the server-side link since this will be all done in ajax. 
		// $("#tuffvote_widget_votelink").bind("click", tuffvote_listener); 
		
	}); 


/* function tuffvote_listener(event) {

	alert("VOTE_OK = " + TUFFVOTE_AJAX_VOTEOK ); 
	
} */ 

function tuffvote_vote (nid, nodeType) {

	// prep ajax firing.	
	$("#tuffvote_widget_votebox").html("<p>Sending Vote....</p>"); 

	//prep error handling 
	$("#tuffvote_widget_votebox").ajaxError(tuffvote_error);

	// fire ajax. 
	$.get(TUFFVOTE_BASEURL + "/tuffvote/vote/" + nid + "/" + nodeType + "/" + TUFFVOTE_VALUE + "/" + "1", null, tuffvote_result); 	
	
}

// duplicate the function, comment out the old one and change the .html to your linking. 
function tuffvote_result(data, status) {
			
	data = parseInt(data); // need to convert this to Integer. 
	status = parseInt(status); // need to also convert this to integer. 
	
	switch (data) {
		
		case TUFFVOTE_AJAX_VOTEOK: 
			$("#tuffvote_widget_votebox").html("<p>Vote cast! Thanks for voting.</p>"); 
			break;
		case TUFFVOTE_AJAX_NOTVOTEDYET: 
			$("#tuffvote_widget_votebox").html("<p>You have not yet voted on this content.</p>"); 
			break;
		case TUFFVOTE_AJAX_CANTVOTE: 
			alert("Sorry. Users are currently not allowed to vote on this content."); 
			$("#tuffvote_widget_votebox").html("<p>Sorry. Users are currently not allowed to vote on this content.</p>"); 
			break;
		case TUFFVOTE_AJAX_ALREADYVOTED: 
			alert("Sorry. According to your IP Address, you have already voted on this content. You may only vote once on any content of this type."); 
			$("#tuffvote_widget_votebox").html("<p>You have already voted on this content.</p>"); 
			break;
		default: 
			alert("A general error has occured.\n\n Please reload the page and try again. If this problem persists, please contact us."); 
			$("#tuffvote_widget_votebox").html("<p>A general error has occured.</p>"); 
			break;
			
	}
	
	
}
	
function tuffvote_error() {

			alert("An HTTP error has occured.\n\n Please reload the page and try again. If this problem persists, please contact us."); 
			$("#tuffvote_widget_votebox").html("<p>A general error has occured.</p>"); 		

}
	