function handleFanResponse(data, link){
	var msgType = $(data).find("messageType").text();	
	if(msgType == "1"){
		$(link).removeClass("group_fan_link");
		$(link).addClass("group_unfan_link");
		
		$(link).unbind("click");
		link.href = link.href.replace("group/fan", "group/unfan");
		$(link).click(function(e){
			fan(this, true);
			e.preventDefault();
		});
				
	}else if(msgType == "0"){
		alert($(data).find("message").text());
	}
}

function handleUnfanResponse(data, link){
	var msgType = $(data).find("messageType").text();	
	if(msgType == "1"){
		$(link).removeClass("group_unfan_link");
		$(link).addClass("group_fan_link");
		
		$(link).unbind("click");
		link.href = link.href.replace("group/unfan", "group/fan");
		$(link).click(function(e){
			fan(this, false);
			e.preventDefault();
		});
		
	}else if(msgType == "0"){
		alert($(data).find("message").text());
	}
}

function showFanError(){
	alert("Oops, an unexpected error occurred and we could not complete your request");
}

function fan(link, isFan){
	if(!isFan){
		$.ajax({
			type:"post",
			url:link.href,
			dataType:"xml",
			success:function(xml){handleFanResponse(xml, link);},
			error:"showFanError"
		});
	}else if(isFan){
		$.ajax({
			type:"post",
			url:link.href,
			dataType:"xml",
			success:function(xml){handleUnfanResponse(xml, link);},
			error:"showFanError"
		});
	}
}

function handleJoinResponse(data, link){
	var msgType = $(data).find("messageType").text();	
	if(msgType == "1"){
		$(link).removeClass("group_join_link");
		$(link).addClass("group_leave_link");
		
		$(link).unbind("click");
		link.href = link.href.replace("group/join", "group/leave");
		$(link).click(function(e){
			join(this, true);
			e.preventDefault();
		});
			
		$.modal('<div id="modalbox">' + $(data).find("message").text() + '</div>');
	}else if(msgType == "0"){
		alert($(data).find("message").text());
	}
}

function handleLeaveResponse(data, link){
	var msgType = $(data).find("messageType").text();	
	if(msgType == "1"){
		$(link).removeClass("group_leave_link");
		$(link).addClass("group_join_link");
		
		$(link).unbind("click");
		link.href = link.href.replace("group/leave", "group/join");
		$(link).click(function(e){
			join(this, false);
			e.preventDefault();
		});
			
	}else if(msgType == "0"){
		alert($(data).find("message").text());
	}
}

function join(link, isMember){
	if(!isMember){
		$.ajax({
			type:"post",
			url:link.href,
			dataType:"xml",
			success:function(xml){handleJoinResponse(xml, link);},
			error:"showFanError"
		});
	}else if(isMember){
		$.ajax({
			type:"post",
			url:link.href,
			dataType:"xml",
			success:function(xml){handleLeaveResponse(xml, link);},
			error:"showFanError"
		});
	}
}

function groupSearch(){
	window.location.href = "/groups/search?gs=" + escape($("#search").val());
}

function groupAlphaSort(){
	if($("#alpha").val() != "")
		window.location.href = "/groups/sort/alpha?l=" + $("#alpha").val();
}

function groupTypeSort(){
	if($("#groupType").val() != "")
		window.location.href = "/groups/sort/type?t=" + $("#groupType").val();
}

$(document).ready(function(){
	$(".group_fan_link").click(function(e){
		fan(this, false);
		e.preventDefault();
	});
	
	$(".group_unfan_link").click(function(e){
		fan(this, true);
		e.preventDefault();
	});
	
	$(".group_join_link").click(function(e){
		join(this, false);
		e.preventDefault();
	});
	
	$(".group_leave_link").click(function(e){
		join(this, true);
		e.preventDefault();
	});
	
	$("#search").focus(function(){
		if(this.value == "Search Groups")
			this.value = "";
		else
			this.select();
		
		$(this).css("color", "#000000");
	});
	
	$("#search, #search_btn").keydown(function(e){
		var code = (e.keyCode ? e.keyCode : e.which);
		if(code == 13)
			groupSearch();
	});
	
	$("#search_btn").click(function(e){
		groupSearch();
		e.preventDefault();
	});
	
	if($("#search").val() != "Search Groups")
		$("#search").css("color", "#000000");
	
	$("#alpha").change(function(){
		groupAlphaSort();
	});
	
	$("#groupType").change(function(){
		groupTypeSort();
	});
});