// result message
// these vars has global scope
var msgResponse = "<li>Thank you for voting</li>";
var msgNomore = "<li>You already voted</li>";
var txt = 'In what way is this post offensive?<br /><br /><textarea id="reason" name="reason" style="width:250px"></textarea>';

// what's hot vote
function hotVote(id) {
	checkVote('hot', id);
}

// what's not vote
function notVote(id) {
	checkVote('not', id);
}

// check if the user already voted
function checkVote(action, id)
{
	// ajax call for check the vote
	jQuery.ajax({
  		type: "POST",
  		url: "WhatsHOTWhatsNOT/checkvote/",
  		data: "WhatsID=" + id,
  		success: function (msg) {
  			
  			// 1 = already voted
			if (msg == 1)
			{
				// show error message
				jQuery("#hotVotesButton" + id).html(msgNomore);
			}
			else
			{
				// add new vote
				doVote(action, id);
			}
		} 
	});
}

// add new vote
function doVote(action, id)
{
  jQuery.ajax({
  		type: "POST",
  		url: "WhatsHOTWhatsNOT/" + action + "vote/",
  		data: "WhatsID=" + id,
  		success: function(msg) {
  			
  			// show the votes total
  			jQuery("#" + action + "Votes" + id).html(msg);
			
			// inactive the buttons   			
			jQuery("#disagree-hotVotesButton" + id).remove();
			jQuery("#agree-hotVotesButton" + id).removeClass();
   			
   			// show the thanks message
   			jQuery("#agree-hotVotesButton" + id).html(msgResponse);
		}
	});
}

// mark as offesinve window dialog
function markOffensive(id)
{
	// fill the window's content with the txt global var
	jQuery.prompt(txt, {
	      callback: mycallbackform,
	      buttons: { Send: id, Cancel: null }
	});	
}

// callback function once the user
// send an email to the administrator
function mycallbackform(v,m) {
	
	// if the response message is not null
    if (v != null) {
    	
    	// ajax call to send the email
		jQuery.ajax({
	  		type: "POST",
	  		url: "WhatsHOTWhatsNOT/sendoffesinve/",
	  		data: "id=" + v + "&reason=" + m.children('#reason').val(),
	  		success: function (msg) {
				// don't show anything for now
				//$.prompt(v +' ' + m.children('#alertName').val());
			} 
		});
		
		// show the thanks message		
		jQuery.prompt("Thank you for your help");
	}      
}

