// ! jQuery required !
// ! jQuery-UI required !

/**
 * profile show dialog
 */
function createProfileShowDialog() {
	// if already exists, return
	if (document.getElementById('profileShowDialog')) {
		return $('div#profileShowDialog');
	}

	// create new 'div' for dialog
	var $diaWin = $(document.createElement('div'));

	// id, class
	$diaWin.attr('id', 'profileShowDialog');
	$diaWin.addClass('profileShowDialog');

	// dialog content
	$diaWin.append('<p>Profil je viditelný pouze pro přihlášené uživatele.</p>');

	// dialog options
	var options = {
		autoOpen: false,
		buttons: {'Ok' : function() {
			$(this).dialog('close');
		}},
		closeOnEscape: true,
		draggable: false,
		modal: true,
		position: 'center',
		resizable: false,
		width: 400
	};

	$diaWin.dialog(options);
	return $diaWin;
}

/**
 * block user dialog
 */
function createUserBlockDialog(profileName, actionUrl) {
	// if already exists, return
	if (document.getElementById('profileBlockDialog')) {
		return $('div#profileBlockDialog');
	}

	// create new 'div' for dialog
	var $diaWin = $(document.createElement('div'));

	// id, class
	$diaWin.attr('id', 'profileBlockDialog');
	$diaWin.addClass('profileBlockDialog');

	// dialog content
	$diaWin.append('<p>Opravdu si přejete blokovat uživatele "' + profileName + '"?</p>');
	$diaWin.append('<p class="note">(uživatel vám již nebude moci napsat zprávu ani poslat vzkaz na nástěnku, blokaci lze později zrušit)</p>');

	// dialog options
	var options = {
		autoOpen: false,
		buttons: {
			'Ano' : function() {
				window.location = actionUrl;
			},
			'Ne' : function() {
				$(this).dialog('close');
			}
		},
		closeOnEscape: true,
		draggable: false,
		modal: true,
		position: 'center',
		resizable: false,
		width: 430
	};

	$diaWin.dialog(options);
	return $diaWin;
}

/**
 * remove user(s) from blacklist dialog
 */
function createRemoveUserFromBlackListDialog() {
	// if already exists, return
	if (document.getElementById('profileRemoveFromBlackListDialog')) {
		return $('div#profileRemoveFromBlackListDialog');
	}

	// create new 'div' for dialog
	var $diaWin = $(document.createElement('div'));

	// id, class
	$diaWin.attr('id', 'profileRemoveFromBlackListDialog');
	$diaWin.addClass('profileRemoveFromBlackListDialog');

	// dialog content
	$diaWin.append('<p>Opravdu si přejete ze seznamu blokovaných odstranit vybrané uživatele?</p>');

	// dialog options
	var options = {
		autoOpen: false,
		buttons: {
			'Ano' : function() {
				document.getElementById('removeUserFromBlackList').submit();
			},
			'Ne' : function() {
				$(this).dialog('close');
			}
		},
		closeOnEscape: true,
		draggable: false,
		modal: true,
		position: 'center',
		resizable: false,
		width: 430
	};

	$diaWin.dialog(options);
	return $diaWin;
}

/**
 * remove user(s) from blacklist dialog
 */
function createRemoveMessagesDialog() {
	// if already exists, return
	if (document.getElementById('removeMessagesDialog')) {
		return $('div#removeMessagesDialog');
	}

	// create new 'div' for dialog
	var $diaWin = $(document.createElement('div'));

	// id, class
	$diaWin.attr('id', 'removeMessagesDialog');
	$diaWin.addClass('removeMessagesDialog');

	// dialog content
	$diaWin.append('<p>Opravdu si přejete odstranit označené zprávy?</p>');

	// dialog options
	var options = {
		autoOpen: false,
		buttons: {
			'Ano' : function() {
				document.getElementById('messageAction').submit();
			},
			'Ne' : function() {
				$(this).dialog('close');
			}
		},
		closeOnEscape: true,
		draggable: false,
		modal: true,
		position: 'center',
		resizable: false,
		width: 430
	};

	$diaWin.dialog(options);
	return $diaWin;
}

// assign events
$(document).ready(function() {
	// not public profile
	$('a.profileNotClickable').click(function() {
		var $dia = createProfileShowDialog();
		$dia.dialog('open');
	}).css('cursor', 'pointer');

	// add user to blacklist
	$('a.userBlackList').click(function() {
		var profileName = $(this).attr('name');
		var actionUrl = $(this).attr('href');
		var $dia = createUserBlockDialog(profileName, actionUrl);
		$dia.dialog('open');

		// don't go to href location
		return false;
	});

	// remove user from blacklist
	$('form#removeUserFromBlackList').submit(function() {
		if ($(this).find('input[id^=pid]:checkbox:checked').length == 0) {
			return false;
		}

		var $dia = createRemoveUserFromBlackListDialog();
		$dia.dialog('open');
		return false;
	});

	// message actions
	$('form#messageAction').submit(function() {
		var group = $(this).find('select[name=msgg]').val();
		var action = $(this).find('select[name=msga]').val();

		// no action selected
		if (action == '-1') {
			return false;
		}

		// check whether something is selected (only when not working with all messages)
		if (group == '1') {
			// no message selected
			if ($(this).find('input[id^=mid]:checkbox:checked').length == 0) {
				return false;
			}
		}

		// dialog
		if (action == 'rm') {
			var $dia = createRemoveMessagesDialog();
			$dia.dialog('open');
			return false;
		} else {
			return true;
		}
	});
});