function ShowEventBox (id)
{
	$("#event"+id).slideDown(300);
	$("#event"+id+"show").hide();
	$("#event"+id+"hide").show();
}
function HideEventBox (id)
{
	$("#event"+id).slideUp(300);
	$("#event"+id+"show").show();
	$("#event"+id+"hide").hide();
}

function DisplayEventsByDate (year, month, day)
{
	var thisEventItem;
	
	$("#eventdetails").html("<div class=\"upcomingEvent\"><a class=\"title\">Loading...</a><p>This should only take a second!</p></div>");
	
	$.getJSON("/api/events/date?&year="+year+"&month="+month+"&day="+day,
		function(data){
			$("#eventdetails").html("");
			$.each(data.events, function(i,thisEvent){
				thisEventItem = CreateEventItem(thisEvent);
				thisEventItem.appendTo("#eventdetails");
			});
		}
	);
	return false;
}

function CreateEventItem (data)
{
	var eventItem;
	var myHtml = "";
	var desc = "";
	var tagArray = new Array();
	
	$.each(data.tags, function(i, thisTag){
		tagArray.push("<a href=\"/tag/view/"+thisTag.tag_code+"\">"+thisTag.tag_text+"</a>");
	});
	var strTags = tagArray.join(", ");
	
	desc = data.description;
	if (desc.length > 155 && desc.indexOf(" ", 150) > 0)
	{
		desc = desc.substr(0, desc.indexOf(" ", 150)) + " ... <a href=\"/event/view/"+data.event_id+"\">[Read more]</a>"; 
	}
	
	myHtml = itemTemplate;//"" + $("#displayEventTemplate").html();
	myHtml = myHtml.replace(/{event_id}/g, data.event_id);
	myHtml = myHtml.replace(/{name}/g, data.name);
	myHtml = myHtml.replace(/{description}/g, desc);
	myHtml = myHtml.replace(/{formatted_starttime}/g, data.formatted_starttime);
	myHtml = myHtml.replace(/{tags}/g, strTags);
	myHtml = myHtml.replace(/\n/g, "<br />");
	
	eventItem = $("<div/>").html(myHtml);
	
	return eventItem;
}

function SubscribeTag (tag_id, doit, aSubscribed, aNotSubscribed)
{
	if (doit)
	{
		aSubscribed.style.display = "";
		aNotSubscribed.style.display = "none";
		
		$.getJSON("/tag/subscribe/"+tag_id+"?foo=bar&format=json",
			function(data){
			}
		);
	} else
	{
		aSubscribed.style.display = "none";
		aNotSubscribed.style.display = "";
		
		$.getJSON("/tag/unsubscribe/"+tag_id+"?foo=bar&format=json",
			function(data){
			}
		);
	}
	return false;
}

function BlockTag (tag_id, doit, aBlocked, aNotBlocked)
{
	if (doit)
	{
		aBlocked.style.display = "";
		aNotBlocked.style.display = "none";
		
		$.getJSON("/tag/block/"+tag_id+"?foo=bar&format=json",
			function(data){
			}
		);
	} else
	{
		aBlocked.style.display = "none";
		aNotBlocked.style.display = "";
		
		$.getJSON("/tag/unblock/"+tag_id+"?foo=bar&format=json",
			function(data){
			}
		);
	}
	return false;
}

function ShowRsvpForm ()
{
	$("#rsvpFormHolder").removeClass("hideRsvpForm");
	$("#rsvpFormHolder").addClass("showRsvpForm");
}
function HideRsvpForm ()
{
	$("#rsvpFormHolder").addClass("hideRsvpForm");
	$("#rsvpFormHolder").removeClass("showRsvpForm");
}

var event_rsvp = "maybe";
var rsvpClasses = new Object();
rsvpClasses.yes = "rsvpStatusYes";
rsvpClasses.maybe = "rsvpStatusMaybe";
rsvpClasses.no = "rsvpStatusNo";

function UpdateRSVP (event_id, my_rsvp)
{
	event_rsvp = my_rsvp;
	$.getJSON("/event/rsvp/"+event_id+"/"+my_rsvp,
		function(data){
			for (var k in rsvpClasses)
			{
				if (k == event_rsvp)
				{
					$("#rsvpStatus").addClass(rsvpClasses[k]);
				} else
				{
					$("#rsvpStatus").removeClass(rsvpClasses[k]);
				}
			}
		}
	);
}

var email_notifications = new Array();
function UpdateEventOverrides (event_id)
{
	email_notifications = new Array();
	if ($("#override_dayof").fieldValue().length == 1)
	{
		email_notifications.push("dayof");
	}
	if ($("#override_daybefore").fieldValue().length == 1)
	{
		email_notifications.push("daybefore");
	}
	if ($("#override_1week").fieldValue().length == 1)
	{
		email_notifications.push("1week");
	}
	
	q = "&";
	q += "override=" + ($("#email_notice_override").fieldValue().length == 1 ? 1 : 0) + "&";
	q += "dayof=" + ($("#override_dayof").fieldValue().length == 1 ? 1 : 0) + "&";
	q += "daybefore=" + ($("#override_daybefore").fieldValue().length == 1 ? 1 : 0) + "&";
	q += "1week=" + ($("#override_1week").fieldValue().length == 1 ? 1 : 0);
	$.getJSON("/event/event_overrides/"+event_id+"?"+q,
		function(data){
			var str = "";
			if (email_notifications.length == 0)
			{
				str = "No notifications for this event";
			} else
			{
				str = "Email notifications: ";
				if (email_notifications.length > 1)
				{
					str += "<br />";
				}
				for (var i=0; i<email_notifications.length; i++)
				{
					if (i > 0)
					{
						if (i == email_notifications.length-1)
						{
							str += " and ";
						} else
						{
							str += ", ";
						}
					}
					
					str += "<strong>";
					if (email_notifications[i] == "dayof") { str += "Day Of"; }
					if (email_notifications[i] == "daybefore") { str += "Day Before"; }
					if (email_notifications[i] == "1week") { str += "Week Before"; }
					str += "</strong>";
				}
			}
			$("#currentEmailNotificationSettings").html(str);
		}
	);
}

function UpdateEventGroupOverrides (event_id, group_id)
{
	q = "&";
	q += "override=" + ($("#group_email_notice_override").fieldValue().length == 1 ? 1 : 0) + "&";
	q += "dayof=" + ($("#group_override_dayof").fieldValue().length == 1 ? 1 : 0) + "&";
	q += "daybefore=" + ($("#group_override_daybefore").fieldValue().length == 1 ? 1 : 0) + "&";
	q += "1week=" + ($("#group_override_1week").fieldValue().length == 1 ? 1 : 0) + "&";
	q += "whenadded=" + ($("#group_override_whenadded").fieldValue().length == 1 ? 1 : 0) + "&";
	q += "group_id=" + group_id;
	
	$.getJSON("/event/event_group_overrides/"+event_id+"?"+q,
		function(data){
		}
	);
}

function ShowHideEventListItem ()
{
	if ($(this).parent().hasClass("expanded"))
	{
		$(this).parent().removeClass("expanded");
	} else
	{
		$(this).parent().addClass("expanded");
	}
}

$(document).ready(function() {
    $(".eventListItemHeader").click(ShowHideEventListItem);
});
