$(document).ready(function() {

	$("a[title='Event details']").bind('click', function() { // for some specific events, after clicking it, we delete them
		var classNameNumber	= $(this).parent().prev().attr("class").match(/\d+/g);
		var lnk				= $(this).attr("href");
		var eventTypeId		= classNameNumber[0];
		var eventId			= classNameNumber[1];

		if (eventTypeId == 1 || eventTypeId == 12) { // events: friend request, new private message 
			$.ajax({
				type: "POST",
				url: "/community/ajax/event.php",
				data: "action=delete&event_id=" + eventId,
				success: function(response) {
					document.location = lnk;
				}
			});

			return false;
		}
	});


	$("#status_message input.status_message:eq(0)").bind('click', function() {
		if ( $(this).val() == lang_what_are_you_doing ) {
			$(".status_owner").css("display", "block");
			$(this).val(lang_is.toLowerCase() + " ");
		}
	});


	$("#status_message input.status_message:eq(0)").bind('blur', function() {
		if ( $(this).val() == (lang_is.toLowerCase() + " ") ) {
			$(".status_owner").css("display", "none");
			$(this).val(lang_what_are_you_doing);
		}
	});


	$(".profile_pic").hover( // opening / closeing the profile picture editing text's box
		function () { // onmouseover
			$(".profile_editor").slideDown("fast");
		},
		function () { // onmouseout
			$(".profile_editor").slideUp("fast");
		}
	);


	$(".upi_minus_grey").hover( // changing the open / close icon
		function () { // onmouseover
			if ($(this).hasClass("upi_minus_grey")) {
				$(this).addClass("upi_minus");
				$(this).removeClass("upi_minus_grey");
			}
			else {
				$(this).addClass("upi_plus");
				$(this).removeClass("upi_plus_grey");
			}
		},
		function () { // onmouseout
			if ($(this).hasClass("upi_minus")) {
				$(this).addClass("upi_minus_grey");
				$(this).removeClass("upi_minus");
			}
			else {
				$(this).addClass("upi_plus_grey");
				$(this).removeClass("upi_plus");
			}
		}
	);

	$(".upi_minus_grey").bind('click', function() { // opening / closing a box
		if ($(this).hasClass("upi_minus")) { // if it is opened, close it (and change the icon to a plus sign)
			$(this).removeClass("upi_minus");
			$(this).addClass("upi_plus");
			$(this).nextAll(".minimizable:first").slideUp();
		}
		else { // if it is closed, open it (and change the icon to a minus sign)
			$(this).removeClass("upi_plus");
			$(this).addClass("upi_minus");
			$(this).nextAll(".minimizable:first").slideDown();
		}
	});


	$(".upi_maximize_grey").hover( // changing the maximize icon
		function () { // onmouseover
			if ($(this).hasClass("upi_maximize_grey")) {
				$(this).addClass("upi_maximize");
				$(this).removeClass("upi_maximize_grey");
			}
			else {
				$(this).addClass("upi_minimize");
				$(this).removeClass("upi_minimize_grey");
			}
		},
		function () { // onmouseout
			if ($(this).hasClass("upi_minimize")) {
				$(this).addClass("upi_minimize_grey");
				$(this).removeClass("upi_minimize");
			}
			else {
				$(this).addClass("upi_maximize_grey");
				$(this).removeClass("upi_maximize");
			}
		}
	);

	$(".upi_maximize_grey").bind('click', function() { // minimizing / maximizing a box
		var $parent = $(this).parent(":first");

		if ($(this).hasClass("upi_maximize")) { // if it is minimized, maximize it
			$(this).removeClass("upi_maximize");
			$(this).addClass("upi_minimize");
			$parent.find(".maximizable:first").slideDown();
			$parent.find(".maximizable_link:first span:first").html(lang_show_less);

			if ($(this).prev().hasClass("upi_plus_grey")) { // if the box is collapsed, expand it
				$(this).prev().removeClass("upi_plus");
				$(this).prev().addClass("upi_minus");
				$(this).nextAll(".minimizable:first").slideDown();
			}
		}
		else { // if it is maximized, minimize it
			$(this).removeClass("upi_minimize");
			$(this).addClass("upi_maximize");
			$parent.find(".maximizable:first").slideUp();
			$parent.find(".maximizable_link:first span:first").html(lang_show_more);
		}
	});

	$(".maximizable_link").bind('click', function() {
		showMore(this);
		return false;
	});


	$(".remove_event").hover( // delete event icon
		function () { // onmouseover
			$(this).removeClass("ei_close_grey");
			$(this).addClass("ei_close");
		},
		function () { // onmouseout
			$(this).removeClass("ei_close");
			$(this).addClass("ei_close_grey");
		}
	);

	$("div.article a.tt").bind('click', function() {
		var sId = $(this).prevAll("a:first").attr("href");
		sId = parseInt( sId.replace(/.+\-(\d+)\.html/, "$1") );

		// generate a hidden form to send the selected tip's ID with post method to the add/edit travel tip page
		$(this).parent().append('<form method="post" action="/travel-tips/add-edit-travel-tip.php" style="display:none"><input type="hidden" name="suggestion_id" value="' + sId + '"></form>').find("form:first").submit();

		return false;
	});

	// wrap the overall-map image with a link to its page if there is any destination added
	var username = /\/users\/(.+)\/$/.exec(document.location);
	username = username[1];

	$("img.overall_map").wrap('<a href="/community/users/' + username + '/overall_map/" title="' + lang_view_large_map + '" rel="nofollow"></a>');

});



function removeEvent(obj, eventId) {
	if (confirm(lang_sure_remove_event)) {
		$(obj).parent().remove();
		$.ajax({
			type: "POST",
			url: "/community/ajax/event.php",
			data: "action=remove&event_id=" + eventId,
			success: function(response) {}
		});
	}
}


function showMore(lnkObj) {
	var $parent = $(lnkObj).parents(".brdr_lr:first");

	if ($parent.find(".maximizable:first").css("display") == "none") {
		$parent.find(".upi_maximize_grey:first")
			.removeClass("upi_maximize_grey")
			.addClass("upi_minimize_grey");
		$parent.find(".maximizable:first").slideDown();
		$(lnkObj).children("span:first").html(lang_show_less);
	}
	else {
		$parent.find(".upi_minimize_grey:first")
			.removeClass("upi_minimize_grey")
			.addClass("upi_maximize_grey");
		$parent.find(".maximizable:first").slideUp();
		$(lnkObj).children("span:first").html(lang_show_more);
	}
}


function changeProfilePic(obj) {
	var imgObj = $(obj).find("img").get(0);
	var imgWidth = imgObj.width;
	var imgHeight = (imgObj.height < 100) ? 100 : imgObj.height;

	var content = '<div style="text-align:justify">'
				+	'<img src="' + imgObj.src + '" align="left" width="' + imgObj.width + '" height="' + imgObj.height + '" title="' + lang_your_current_profile_picture + '" style="margin-right:10px">'
				+	lang_here_upload_profile_pic + '<br><br>' + lang_image_restrict
				+ '</div><div class="defloater"></div><br>'
				+ '<form enctype="multipart/form-data" method="post" action="/community/upload_profile_pic.php">'
				+	'<div style="float:left">' + lang_file + ': <input type="file" name="profile_pic" size="18"></div>'
				+	'<div style="float:right"><input type="submit" value="' + lang_upload + '"></div>'
				+ '</form>';

	Opacity.create();
	Opacity.add(lang_upload_picture, content, 400, imgHeight + 50 + 70, false);
}


function saveStatusMessage(username) {
	$.ajax({
		type: "POST",
		url: "/community/ajax/user.php",
		data: "action=update_status_message&msg=" + urlencode($("input.status_message").val()),
		success: function(response) {
			$("span.status_message").html("&nbsp;" + username + " " + $("input.status_message").val() + ' <span class="note">' + lang_now + ' - <a href="#" onclick="clearStatusMessage();return false" title="' + lang_clear_my_status_msg + '">' + lang_clear + '</a></span><br><br>');
			$(".status_owner").css("display", "none");
			$("input.status_message").val(lang_what_are_you_doing);
		}
	});
}

function clearStatusMessage() {
	$.ajax({
		type: "POST",
		url: "/community/ajax/user.php",
		data: "action=clear_status_message",
		success: function(response) {
			$("span.status_message").html("");
			$(".status_owner").css("display", "none");
			$("input.status_message").val(lang_what_are_you_doing);
		}
	});
}

function urlencode(str) {
	var histogram = {}, histogram_r = {}, code = 0, tmp_arr = [];
	var ret = str.toString();

	var replacer = function(search, replace, str) {
		var tmp_arr = [];
		tmp_arr = str.split(search);
		return tmp_arr.join(replace);
	};

	// The histogram is identical to the one in urldecode.
	histogram['!']   = '%21';
	histogram['%20'] = '+';

	// Begin with encodeURIComponent, which most resembles PHP's encoding functions
	ret = encodeURIComponent(ret);

	for (search in histogram) {
		replace = histogram[search];
		ret = replacer(search, replace, ret) // Custom replace. No regexing
	}

	// Uppercase for full PHP compatibility
	return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
		return "%"+m2.toUpperCase();
	});

	return ret;
}