/**
 * onArcade 2.4.0
 * Copyright © 2006-2011 Hans Mäesalu & Eveterm OÜ, All Rights Reserved
 **
 * ONARCADE IS NOT FREE SOFTWARE!
 * http://www.onarcade.com
 **/

var profile = {
	user_id: 0,

	// location
	location: "",
	location_button: null,
	location_bubble: null,

	// friends
	friends_page: 0,

	// favorites
	favorites_page: 1,

	// high scores
	scores_page: 1,

	// comments
	comments_page: 1,
	comments: null,
	div_comments: null,

	//send friend request
	friend_request: function(link) {
		loader.insert(link);
		function response_function(response) {
			error.insert(response.message, link.parent(), {error: response.error});
			link.parent().slideUp(250);
		}
		$.getJSON(siteurl + "usercp.php?a=friend_request&f=" + profile.user_id + "&ajax=1", response_function);
	},
	// open user location map
	open_map: function() {
		// create bubble
		if (profile.location_bubble == null) {
			profile.location_bubble = $.createElement("div").addClass("bubble").width("400px");
			$.createElement("div").addClass("bubble_up").css("left", "70px").appendTo(profile.location_bubble);
			$(document.body).append(profile.location_bubble);
			profile.location_bubble.append('<img src="http://maps.google.com/maps/api/staticmap?center=' + encodeURI(profile.location) + '&size=400x350&sensor=false&amp;maptype=hybrid" alt="" />');
			profile.location_bubble.click(function(event) { event.stopPropagation(); });
		} else {
			profile.location_bubble.fadeIn("fast");
		}

		// position
		var position = profile.location_button.offset();
		profile.location_bubble.css({top: position.top + 30, left: position.left - 70});

		// close
		$(document).click(profile.close_map);
	},
	close_map: function() {
		profile.location_bubble.fadeOut("fast");
		$(document).unbind("click", profile.close_map);
	},
	// show user location on map
	init_map: function() {
		var user_location = $("#user_location");
		profile.location = user_location.children("span").text();
		if (profile.location.length > 0) {
			profile.location_button = user_location.children("img").css("display", "inline").click(function() {
				profile.open_map();
				return false;
			});
		}
	},
	// load more favorites from server
	load_favorites: function(e) {
		e.preventDefault();
		var button = $(this);
		loader.replace(button);
		profile.favorites_page++;
		$.get(siteurl +"profile.php?a=favorites&ajax=1&u="+ profile.user_id +"&p="+ profile.favorites_page, function(data) {
			button.remove();
			$("#favorites").append(data);
		}, "html");
	},
	// load user top scores
	load_high_scores: function(e) {
		e.preventDefault();
		var button = $(this);
		loader.replace(button);
		profile.scores_page++;
		$.get(siteurl +"profile.php?a=high_scores&ajax=1&u="+ profile.user_id +"&p="+ profile.scores_page, function(data) {
			button.remove();
			$("#high_scores").append(data);
		}, "html");
	},
	// load user friends
	load_friends: function(e) {
		e.preventDefault();
		var button = $(this);
		loader.replace(button);
		profile.friends_page++;
		$.get(siteurl +"profile.php?a=friends&ajax=1&u="+ profile.user_id +"&p="+ profile.friends_page, function(data) {
			button.remove();
			$("#friends").append(data);
		}, "html");
	},
	// draw profile comments
	draw_comments: function(comm, previous, next) {
		profile.div_comments.empty();

		for (var i in comm) {
			$.tmpl(template.profile_comment, comm[i]
					, profile.div_comments);

			if (comm[i].del) {
				var moderate = '<a href="" class="delete_comment">'+ lang["delete"] +'</a>';
				if (comm[i].ip) {
					moderate += ' - '+ comm[i].ip;
				}
				$("#comment_"+ comm[i].id).append('<p class="center">'+ moderate +'</p>');
			}
		}

		// pagination
		if (previous > 0 || next > 0) {
			var load_comments = function(e) {
				e.preventDefault();
				loader.replace($(this).parent());
				$.getJSON(siteurl +"profile.php?a=comments&ajax=1&p="+ e.data +"&id="+ profile.user_id, function(data) {
					if (data.error) {
						error.insert(data.message, profile.div_comments);
						return;
					}
					profile.draw_comments(data.comments, data.previous, data.next);
				});
			};

			var div_nav = $.createElement("div").addClass("arrow_nav")
							.appendTo(profile.div_comments);
			if (next > 0) {
				$.createElement("a").addClass("next").attr("href", "").html(lang.older +" &gt;")
					.click(next, load_comments)
					.appendTo(div_nav);
			}
			if (previous > 0) {
				$.createElement("a").addClass("previous").attr("href", "").html("&lt; "+ lang.newer)
					.click(previous, load_comments)
					.appendTo(div_nav);
			}
		}
	},
	// submit profile comment to server
	submit_comment: function(e) {
		e.preventDefault();
		var form = $(this);
		var submit = form.find("input[type='submit']").first();
		loader.insert(submit);
		$.post(siteurl + "profile.php?a=submit_comment&u=" + profile.user_id + "&ajax=1", form.serialize(), function(data) {
			loader.remove(submit);
			error.insert(data.message, form, {error: data.error});
			if (data.error) {
				return;
			}
			form.find("textarea,input[type='text']").val("");
			var comment_html = $.tmpl(template.profile_comment, data.comment);
			var insert_before = profile.div_comments.children(":first");
			if (insert_before.size() == 0) {
				comment_html.appendTo(profile.div_comments);
			} else {
				comment_html.insertBefore(insert_before);
			}
			$("#comment_"+ data.comment.id).append('<p class="center"><a href="" class="delete_comment">'+ lang["delete"] +'</a></p>');
		}, "json");
	},
	// delete comment
	delete_comment: function(e) {
		e.preventDefault();
		if (!confirm_delete()) {
			return;
		}
		var button = $(this);
		var div_comment = button.closest(".comment");
		loader.replace(button.parent());
		$.getJSON(siteurl + "profile.php?a=delete_comment&id=" + field_number(div_comment.attr("id")) + "&ajax=1", function(data) {
			if (data.error) {
				error.insert(data.message, div_comment);
				return;
			}
			div_comment.remove();
		});
	},
	// initiate all profile actions
	init: function(user_id) {
		profile.user_id = user_id;

		$("#friend_request").click(function(e) {
			e.preventDefault();
			profile.friend_request($(this));
		});

		$("#more_favorites").live("click", profile.load_favorites);
		$("#more_high_scores").live("click", profile.load_high_scores);
		$("#more_friends").live("click", profile.load_friends);

		if (profile.comments != null) {
			profile.div_comments = $("#comments");
			profile.draw_comments(profile.comments.comments, profile.comments.previous, profile.comments.next);
			$("#comment_form").submit(profile.submit_comment);
			profile.div_comments.delegate(".delete_comment", "click", profile.delete_comment);
		}

		profile.init_map();
	}
};
