var enabled = readCookie('showbug');

if (enabled == '1') {
	$(document).ready(function(){
		$.ui.dialog.defaults.bgiframe = true;
		checkFriends();
		checkMessages();
	});
}

function showBug(title, content) {
		$('#bug').dialog({autoOpen: false, resizable: false, title: title, dialogClass: "bug"});
		resetBugPosition();
		$(window).resize(function(){ resetBugPosition(); });
		$(window).scroll(function(){ setTimeout(resetBugPosition, 100); });
		setTimeout(resetBugPosition, 100);
		$('#bug').html(content);
		$('#bug').dialog('open');
}

function showDialog(title, content) {
		$('#dialog').html(content);
		$('#dialog').dialog({
			modal: true, 
			resizable: false, 
			title: title, 
			dialogClass: "bug",
			bgiframe: true,
			autoOpen: false
		});

		$('#dialog').dialog('open');
		// $('#dialog').dialog('option', 'title', title);
		// $('#dialog').dialog('open');
}

function checkFriendsResult(data) {
	if (data.id > 0) {
		content = '<input type=\'hidden\' id=\'req_id\' value=\'' + data.id + '\'/><div>Do you want to make <b>' + data.from + '</b> your friend?</div><br/><br/>';
		if (data.reason) content += "<div style='border: 1px solid #99C; max-height: 100px; overflow-y: scroll;'><i>" + data.reason + "</i></div><br/><br/>";
		content += '<b>Response</b> (optional):<br/><textarea style=\'width:274px;\' id=\'cf_reason\'></textarea><br/><br/><a href=\'#\' onclick=\'cfConfirm('+data.id+'); return false;\'>Confirm</a> | <a href=\'#\' onclick=\'cfDecline('+data.id+'); return false;\'>Decline</a> | <a href=\'#\' onclick=\'cfSkip('+data.id+'); return false;\'>Skip</a> | <a href=\'#\' onclick=\'cfBlock('+data.id+'); return false;\'>Block</a>';
		$('#bug').bind('dialogclose', function(event, ui) {
			if ($('#req_id').val() > 0) $.post('/ajax/friend_request.php', {f:'skip', id:$('#req_id').val()}, cfActionResult, 'json');
		});

		showBug('Friend Request', content);
	} else {
		setTimeout(checkFriends, 15000);
	}
}

function checkMessagesResult(data) {
	content = '';
	if (data.rows.length > 0) {
		$.each(data.rows, function(row){
			content += '<div style=\'clear: both; border-bottom: 1px dashed #666; padding: 3px 0px 3px 0px; width: 274px;\'><div style=\'width: 274px; background-color: #ccc;\'><b>' + data.rows[row].from + '</b><div style=\'float:right;\'><a href=\'#\' onclick=\'javascript:cfMiniReply("' + data.rows[row].from_clean + '", "' + data.rows[row].sid + '"); return false;\'>reply</a></div></div><div style=\'width: 274px; background-color: #ccc;\'>' + data.rows[row].created + '</div>' + data.rows[row].content + '</div>'; 
		});

		content += '<div id=\'reply\' style=\'display:none\'><br/><div id=\'reply_title\'></div><input type=\'hidden\' id=\'reply_to\' value=\'\'/><textarea style=\'width:274px;\' id=\'cf_reply\'></textarea><br/><a href=\'#\' onclick=\'cfSendMiniReply(); return false;\'>Send</a><br/><br/></div>';
		content += '<a href=\'#\' onclick=\'$(\"#bug\").dialog(\"close\"); return false;\'>Close</a>';

		showBug('Messages', content);
		setTimeout(checkMessages, 15000);
	} else {
		setTimeout(checkMessages, 15000);
	}
}

function cfMiniReply(title, recipient) {
	$('#reply_title').html("To: <b>" + title + "</b>" );
	$('#reply_to').val(recipient);
	$('#reply').show();
	$('#cf_reply').val('');
	$('#cf_reply').focus();
	resetBugPosition();
}

function cfSendMiniReply() {
	$.post('/ajax/friend_request.php', {f:'new', recipient:$('#reply_to').val(), content:$('#cf_reply').val()}, cfActionResult, 'json');
	$('#reply').hide();
	resetBugPosition();
}

function cfActionResult(data) {
	if (!data.success) {
		jqAlert('Error', data.error);
	} else {
		checkFriends();
		checkMessages();
	}
}

function cfActionResultAndMessage(data) {
	cfActionResult(data);
	$('#dialog').html(cfMessageForm(data));
	$('#cf_message').focus();
}

function cfConfirm(id) {
	$.post('/ajax/friend_request.php', {f:'confirm', id:id, reason:$('#cf_reason').val()}, cfActionResult, 'json');
	$('#req_id').val(0);
	$('#bug').dialog('close');
}

function cfConfirmAndMessage(id) {
	$.post('/ajax/friend_request.php', {f:'confirm', id:id, reason:$('#cf_reason').val()}, cfActionResultAndMessage, 'json');
	$('#req_id').val(0);
	$('#bug').dialog('close');
}

function cfDecline(id) {
	$.post('/ajax/friend_request.php', {f:'decline', id:id, reason:$('#cf_reason').val()}, cfActionResult, 'json');
	$('#req_id').val(0);
	$('#bug').dialog('close');
}

function cfBlock(id) {
	$.post('/ajax/friend_request.php', {f:'block', id:id, reason:$('#cf_reason').val()}, cfActionResult, 'json');
	$('#req_id').val(0);
	$('#bug').dialog('close');
}

function cfSkip(id) {
	$.post('/ajax/friend_request.php', {f:'skip', id:id}, cfActionResult, 'json');
	$('#req_id').val(0);
	$('#bug').dialog('close');
}

function cfSend(username) {
	$.username = username;
	jqAlert(
		'Notice', 
		'You are using a feature of RIMOFTHEWORLD.net that is currently in development.  It may not function at this time.  Please feel free to test this, but be aware, it might not function as expected.', 
		function(){
			content = '<div id=\'send\' style=\'width: 276px; text-align: center;\'><br/><br/><img src=\'/images/ajax-loader.gif\' width=\'32\' height=\'32\'/></div>';
			showDialog('Send Private Message', content);
			$.post('/ajax/friend_request.php', {f: 'lookup', recipient: $.username}, cfSendResult, 'json');
		}
	);
}

function cfSendResult(data) {
	$('#send').css({'text-align': 'left'})
	if (!data.username) {
		content = 
			'There was a problem sending a message to <b>' + data.full_name + '</b>.  That account might not be fully enabled at this time.<br/><br/>' + 
			'<a href=\'#\' onclick=\'$(\"#dialog\").dialog(\"close\"); return(false);\'>Close</a>';
	} else {
		switch(data.status) {
			case 'none':
				content = 
					'<input type=\'hidden\' id=\'recipient\' value=\'' + data.username + '\' />' +
					'You need to be friends with <b>' + data.full_name + '</b> in order to send a message.<br/><br/>' +
					'Do you want to send a friend request to <b>' + data.full_name + '</b>?<br/><br/><b>Message</b> (optional):<br/><textarea id=\'cf_reason\' style=\'width: 274px;\'></textarea><br/><a href=\'#\' onclick=\'$(\"#dialog\").dialog(\"close\"); cfRequest(); return false;\'>Yes</a> | <a href=\'#\' onclick=\'$(\"#dialog\").dialog(\"close\"); $(\"#dialog\").dialog(\"close\"); return false;\'>No</a>';
				break;
			case 'requested':
				if (data.direction == 'originator') 
					content = 
						'You already requested to be friends with <b>' + data.full_name + '</b>.  You cannot send another request or a message while your request is pending.<br/><br/>' +
						'<a href=\'#\' onclick=\'$(\"#dialog\").dialog(\"close\"); return(false);\'>Close</a>';
				else 
					content = 
						'<b>' + data.full_name + '</b> has requested to be your friend, but you haven\'t approved the request yet.<br/><br/>' +
						'<div style=\'border: 1px solid #99C; max-height: 100px; overflow-y: scroll;\'><i>' + data.reason + '</i></div><br/>' +
						'<b>Response</b> (optional):<br/><textarea style=\'width:274px;\' id=\'cf_reason\'></textarea><br/><br/><a href=\'#\' onclick=\'cfConfirmAndMessage('+data.request_id+'); $(\"#dialog\").dialog(\"close\"); return false;\'>Confirm</a> | <a href=\'#\' onclick=\'cfDecline('+data.request_id+'); $(\"#dialog\").dialog(\"close\"); return false;\'>Decline</a> | <a href=\'#\' onclick=\'cfBlock('+data.request_id+'); $(\"#dialog\").dialog(\"close\"); return false;\'>Block</a>';
				break;
			case 'declined':
				if (data.direction == 'originator') 
					content = 
						'You cannot send <b>' + data.full_name + '</b> a message because your friend request has been declined.<br/><br/>Do you want to send a new friend request?<br/>' + 
						'<input type=\'hidden\' id=\'recipient\' value=\'' + data.username + '\' />' +
						'<b>Message</b> (optional):<br/><textarea style=\'width:274px;\' id=\'cf_reason\'></textarea><br/><a href=\'#\' onclick=\'$(\"#dialog\").dialog(\"close\"); cfRequest(); return false;\'>Request</a> | <a href=\'#\' onclick=\'$(\"#dialog\").dialog(\"close\"); $(\"#dialog\").dialog(\"close\"); return false;\'>Close</a>';
				else 
					content = 
						'You cannot send <b>' + data.full_name + '</b> a message because you declined the friend request.<br/><br/>Do you want to change your mind and approve the previous request?<br/><br/>' +
						'<div style=\'border: 1px solid #99C; max-height: 100px; overflow-y: scroll;\'><i>' + data.reason + '</i></div><br/>' +
						'<input type=\'hidden\' id=\'recipient\' value=\'' + data.username + '\' />' +
						'<b>Message</b> (optional):<br/><textarea style=\'width:274px;\' id=\'cf_reason\'></textarea><br/><a href=\'#\' onclick=\'$(\"#dialog\").dialog(\"close\"); cfRequestAndMessage(' + data.request_id + '); return false;\'>Confirm</a> | <a href=\'#\' onclick=\'$(\"#dialog\").dialog(\"close\"); $(\"#dialog\").dialog(\"close\"); return false;\'>Close</a>';
				break;
			case 'blocked':
				if (data.direction == 'originator') 
					content = 
						'You cannot send <b>' + data.full_name + '</b> a message because you have been blocked by this user.<br/><br/>' +
						'<a href=\'#\' onclick=\'$(\"#dialog\").dialog(\"close\"); return(false);\'>Close</a>';
				else
					content = 
						'You cannot send <b>' + data.full_name + '</b> a message because you blocked this user.<br/><br/>Do you want to change your mind and approve the previous request?<br/><br/>' +
						'<div style=\'border: 1px solid #99C; max-height: 100px; overflow-y: scroll;\'><i>' + data.reason + '</i></div><br/>' +
						'<input type=\'hidden\' id=\'recipient\' value=\'' + data.username + '\' />' +
						'<b>Message</b> (optional):<br/><textarea style=\'width:274px;\' id=\'cf_reason\'></textarea><br/><a href=\'#\' onclick=\'$(\"#dialog\").dialog(\"close\"); cfRequestAndMessage(' + data.request_id + '); return false;\'>Confirm</a> | <a href=\'#\' onclick=\'$(\"#dialog\").dialog(\"close\"); $(\"#dialog\").dialog(\"close\"); return false;\'>Close</a>';
				break;
			case 'approved':
				content = cfMessageForm(data);
				break;
		}
	}

	$('#send').html(content);
	$('#cf_message').focus();
	$('#cf_reason').focus();
}

function cfRequest() {
	$.post('/ajax/friend_request.php', {f:'request', recipient:$('#recipient').val(), reason:$('#cf_reason').val()}, cfActionResult, 'json');
}

function cfRequestAndMessage() {
	$.post('/ajax/friend_request.php', {f:'request', recipient:$('#recipient').val(), reason:$('#cf_reason').val()}, cfActionResultAndMessage, 'json');
}

function cfMessageForm(data) {
	content = 
		'To: <b>' + data.full_name + '</b><br/><br/>' +
		'<b>Message</b>:<br/><textarea style=\'width:274px;\' id=\'cf_message\'></textarea><br/><a href=\'#\' onclick=\'$(\"#dialog\").dialog(\"close\"); cfSendMessage(\"' + data.username + '\"); return false;\'>Send</a> | <a href=\'#\' onclick=\'$(\"#dialog\").dialog(\"close\"); $(\"#dialog\").dialog(\"close\"); return false;\'>Cancel</a>';
	return content;
}

function cfSendMessage(recipient) {
	$.post('/ajax/friend_request.php', {f:'new', recipient:recipient, content:$('#cf_message').val()}, cfActionResult, 'json');
}

function checkFriends() {
	if ($('#bug').dialog('isOpen')) {
		setTimeout(checkFriends, 15000);
	} else {
		$.post('/ajax/friend_request.php', {f: 'next_req'}, checkFriendsResult, 'json');
	}
}

function checkMessages() {
	if ($('#bug').dialog('isOpen')) {
		setTimeout(checkMessages, 15000);
	} else {
		$.post('/ajax/friend_request.php', {f: 'next_msg'}, checkMessagesResult, 'json');
	}
}

function resetBugPosition() {
	// $('.bug.ui-dialog').css({left: $(window).width()-315, top: $(window).height()-1651, position:'fixed'});
	$('.bug.ui-dialog').css({margin: '-10px'});
	$('#bug').dialog('option', 'position', ['right','bottom']);
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function jqAlert(title, text, done) {
	$("#alert").html(text);

	$("#alert").dialog({
		title: title,
		bgiframe: true,
		modal: true,
		resizable: false,
		autoOpen: false,
		buttons: {
			Ok: function() {
				$(this).dialog('close');
				done();
			}
		}
	});

	$("#alert").dialog('open');

		

}

