(function($) {
	$.fn.votes = function(options) {
		var defaults = {
            urlAddVote: 'http://example.com',
            karma: '1',
            format: 'totalScore',
            messagePositive: 'Vote this up?',
            messageNegative: 'Vote this down?'
            };
		var options = $.extend(defaults, options); 
		
		/*
		 * Available format types include:
		 * -totalScore: displays the total score on the asset by addition of all votes
		 * -dualScore: displays numbers of both positive and negative votes
		 */

		$(this).each(function() {
			/* Plugin Code Here */
			var o = options;  
			var obj = $(this);
			
			//obj.after(' <span class="karmaCount">' + obj.attr('data-karma') + '</span>');
			//obj.next('.karmaCount').html(response.vote.karmaPositive)
			
			obj.click(function () {
	 			if(o.karma > 0){
	 				$.prompt(o.messagePositive,{
	 						 buttons: { Ok: true, Cancel: false },
	 						 callback: checkPrompt
	 				});
	 			}else{
	 				$.prompt(o.messageNegative,{
						 buttons: { Ok: true, Cancel: false },
						 callback: checkPrompt
	 				});
	 			}
	 			
	 			if(status==true){
	 				alert('true!');
	 			}
		    });
			
			function checkPrompt(v, m, f){
			      if(v == true){
			    	  sendVote();
			      }  
			}
			
		 	function sendVote(){
		 		$.post(o.urlAddVote + '/id/' + obj.attr('data-id') + '/karma/' + o.karma,
						{"format" : "json"},
						function(data){
							processResponse(data);
						},'html');
		 	};
		 	
		 	function processResponse(data){
		 		try{
		 			var response = JSON.parse(data);
			 		if(response.vote.status == 'true' && o.format == 'dualScore'){
			 			if(o.karma > 0){
			 				obj.next('.karmaCount').html(response.vote.karmaPositive);
			 			}else{
			 				obj.next('.karmaCount').html(response.vote.karmaNegative);
			 			}
			 		}else if(response.vote.status == 'true' && o.format == 'totalScore'){
			 			$('#totalScore'+obj.attr('data-id')).html(response.vote.karmaTotal);
			 		}else{
			 			$.prompt('Sorry, you\'ve already voted');
			 		}
		 		}catch(err){
		 			$.prompt('You should be logged in to vote. <br/><a href="' + varUrlLogin +'">Log Me In</a> <br/><a href="' + varUrlRegister + '">Register</a>');
		 		}
		 		
		 	};
			
		});
	};
	
 	function privateFunctions(obj) {
 		/* private fn Code Here */
 		obj.html(options.testing);
 	};
})(jQuery);

/*
*Assumes
*<a class="voteup">12</a> <a class="votedown">-2</a>
*/
