var event_id = 0;
var current_rsvp = "";
var btn_classes = new Object();
btn_classes.going = "success";
btn_classes.interested = "primary";
btn_classes.not_going = "danger";
var _rsvps = {"going":"yes", "interested":"maybe", "not_going":"no"};

function ChangeRsvp (rsvp, noUpdate, showOptions) {
	current_rsvp = rsvp;
	$(".rsvp_options").removeClass("going");
	$(".rsvp_options").removeClass("interested");
	$(".rsvp_options").removeClass("not_going");
	
	$(".rsvp_options").addClass(current_rsvp);
	$(".rsvp_options").addClass("hide_options");
	if (showOptions)
	{
		$(".rsvp_options").removeClass("hide_options");
	}
	
	$(".rsvp_status_button").hide();
	$("#btn_"+current_rsvp).show();
	
	if (!noUpdate) {
		UpdateRsvp(event_id, _rsvps[rsvp]);
	}
}

function ShowRsvpOptions () {
	//$.facebox({div: "#rsvp_options"});
	
	$("#rsvp_options").slideDown(200);
	
	$("#btn_rsvp_going").click(function () { ChangeRsvp("going", true, true); });
	$("#btn_rsvp_interested").click(function () { ChangeRsvp("interested", true, true); });
	$("#btn_rsvp_not_going").click(function () { ChangeRsvp("not_going", true, true); });
}

function HideRsvpOptions (e) {
	e.preventDefault();
	
	$("#rsvp_options").slideUp(300);
	
	return false;
}

function LikeEvent (e) {
	$.getJSON("/event/like/"+event_id, function (data) {
		// nothing
	});
	return HideRsvpOptions(e);
}
function DislikeEvent (e) {
	$.getJSON("/event/dislike/"+event_id, function (data) {
		// nothing
	});
	return HideRsvpOptions(e);
}

function TweetGoing (e) {
	e.preventDefault();
	TweetWithIntent("going");
	return false;
}

function TweetInterested (e) {
	e.preventDefault();
	TweetWithIntent("interested");
	return false;
}

function TweetWithIntent (intent) {
	var rsvp = intent;
	var btn = $("#"+rsvp+"_tweet_button");
	var loader = $("#"+rsvp+"_tweet_loading");
	var status = $("#"+rsvp+"_tweet_status");
	btn.attr("disabled", "true");
	loader.show();
	status.hide();
	status.html("");
	
	var post_vars = {};
	post_vars.message = $("#"+rsvp+"_tweet").val();
	$.post("/event/tweet/"+event_id, post_vars, function (data) {
		loader.hide();
		if (data.status == "error") {
			status.html("Error!<br />"+data.message);
			btn.removeAttr('disabled');
		} else {
			status.html("Tweeted!");
		}
		status.show();
	}, "json");
}

$(document).ready(function () {
	$(".close", $(".rsvp_options")).click(HideRsvpOptions);
	$("#not_going_like").click(LikeEvent);
	$("#not_going_dislike").click(DislikeEvent);
});

