(function($) {

/*

Twinkle.welcome = function friendlywelcome {
	if (mw.util.getParamValue('friendlywelcome')) {
		if (mw.util.getParamValue('friendlywelcome') === 'auto') {
			Twinkle.welcome.auto;
			Twinkle.welcome.semiauto;
		Twinkle.welcome.normal;

Twinkle.welcome.auto = function {
	if (mw.util.getParamValue('action') !== 'edit') {
		// userpage not empty, aborting auto-welcome
		return;

	Twinkle.welcome.welcomeUser;

Twinkle.welcome.semiauto = function {
	Twinkle.welcome.callback(mw.config.get('wgRelevantUserName'));

Twinkle.welcome.normal = function {
	if (mw.util.getParamValue('diff')) {
		// check whether the contributors' talk pages exist yet
		var $oList = $('#mw-diff-otitle2').find('span.mw-usertoollinks a.new:contains(talk)').first;
		var $nList = $('#mw-diff-ntitle2').find('span.mw-usertoollinks a.new:contains(talk)').first;

		if ($oList.length > 0 || $nList.length > 0) {
			var spanTag = function {
				var span = document.createElement('span');
				span.style.color = color;
				span.appendChild(document.createTextNode(content));
				return span;

			var welcomeNode = document.createElement('strong');
			var welcomeLink = document.createElement('a');
			welcomeLink.appendChild(spanTag);
			welcomeLink.appendChild(spanTag);
			welcomeLink.appendChild(spanTag);
			welcomeNode.appendChild(welcomeLink);

			if ($oList.length > 0) {
				var oHref = $oList.attr('href');

				var oWelcomeNode = welcomeNode.cloneNode(true);
				oWelcomeNode.firstChild.setAttribute('href', oHref + '&' + $.param({
					'friendlywelcome': Twinkle.getPref('quickWelcomeMode') === 'auto' ? 'auto' : 'norm',
					'vanarticle': Morebits.pageNameNorm
				$oList[0].parentNode.parentNode.appendChild(document.createTextNode(' '));
				$oList[0].parentNode.parentNode.appendChild(oWelcomeNode);

			if ($nList.length > 0) {
				var nHref = $nList.attr('href');

				var nWelcomeNode = welcomeNode.cloneNode(true);
				nWelcomeNode.firstChild.setAttribute('href', nHref + '&' + $.param({
					'friendlywelcome': Twinkle.getPref('quickWelcomeMode') === 'auto' ? 'auto' : 'norm',
					'vanarticle': Morebits.pageNameNorm
				$nList[0].parentNode.parentNode.appendChild(document.createTextNode(' '));
				$nList[0].parentNode.parentNode.appendChild(nWelcomeNode);
	if (mw.config.get('wgRelevantUserName')) {
		Twinkle.addPortletLink(function {
			Twinkle.welcome.callback(mw.config.get('wgRelevantUserName'));

Twinkle.welcome.welcomeUser = function welcomeUser {
	Morebits.status.init(document.getElementById('mw-content-text'));
	$('#catlinks').remove;

	var params = {
		template: Twinkle.getPref('quickWelcomeTemplate'),
		article: mw.util.getParamValue('vanarticle') || '',
		mode: 'auto'

	var userTalkPage = mw.config.get('wgFormattedNamespaces')[3] + ':' + mw.config.get('wgRelevantUserName');
	Morebits.wiki.actionCompleted.redirect = userTalkPage;
	Morebits.wiki.actionCompleted.notice = '환영을 완료했습니다. 수 초 안에 토론 문서를 다시 불러옵니다';

	var wikipedia_page = new Morebits.wiki.page;
	wikipedia_page.setFollowRedirect(true);
	wikipedia_page.setCallbackParameters(params);
	wikipedia_page.load(Twinkle.welcome.callbacks.main);

Twinkle.welcome.callback = function friendlywelcomeCallback(uid) {
	if (uid === mw.config.get('wgUserName') && !confirm('자기 자신에게 환영하시겠습니까?...')) {
		return;

	var Window = new Morebits.simpleWindow;
	Window.setTitle('사용자 환영');
	Window.setScriptName('트윙클');
	Window.addFooterLink;
	Window.addFooterLink;

	var form = new Morebits.quickForm(Twinkle.welcome.callback.evaluate);

	form.append({
		type: 'select',
		name: 'type',
		label: '환영 종류: ',
		event: Twinkle.welcome.populateWelcomeList,
		list: [
		]

	form.append({
		type: 'div',
		id: 'welcomeWorkArea',
		className: 'morebits-scrollbox'

	var previewlink = document.createElement('a');
	$(previewlink).click(function {
		Twinkle.welcome.callbacks.preview(result); // |result| is defined below
	previewlink.style.cursor = 'pointer';
	previewlink.textContent = '미리 보기';
	form.append;

	form.append({ type: 'submit' });

	var result = form.render;
	Window.setContent(result);
	Window.display;

	// initialize the welcome list
	var evt = document.createEvent('Event');
	evt.initEvent;
	result.type.dispatchEvent(evt);

Twinkle.welcome.populateWelcomeList = function(e) {
	var type = e.target.value;

	var container = new Morebits.quickForm.element({ type: 'fragment' });

	if ((type === 'standard' || type === 'anonymous') && Twinkle.getPref('customWelcomeList').length) {
		container.append;
		container.append({
			type: 'radio',
			name: 'template',
			list: Twinkle.getPref('customWelcomeList'),
			event: function {
				e.target.form.article.disabled = false;

	var sets = Twinkle.welcome.templates[type];
	$.each(sets, function {
		container.append;
		container.append({
			type: 'radio',
			name: 'template',
			list: $.map(templates, function {
				return {
					value: template,
					label: ': ' + properties.description + (properties.linkedArticle ? '\u00A0*' : ''), // U+00A0 NO-BREAK SPACE
					tooltip: properties.tooltip // may be undefined
			event: function(ev) {
				ev.target.form.article.disabled = !templates[ev.target.value].linkedArticle;

	var rendered = container.render;
	$(e.target.form).find('div#welcomeWorkArea').empty.append(rendered);

	var firstRadio = e.target.form.template[0];
	firstRadio.checked = true;
	var vals = sets[Object.keys(sets)[0]];
	e.target.form.article.disabled = vals[firstRadio.value] ? !vals[firstRadio.value].linkedArticle : true;

// A list of welcome templates and their properties and syntax

// The four fields that are available are "description", "linkedArticle", "syntax", and "tooltip".
// The three magic words that can be used in the "syntax" field are:
// - $USERNAME$ - replaced by the welcomer's username, depending on user's preferences
// - $ARTICLE$ - replaced by an article name, if "linkedArticle" is true
// - $HEADER$ - adds a level 2 header (most templates already include this)

Twinkle.welcome.templates = {
	'standard': {
		'일반 환영 틀': {
			'환영합니다': {
				description: '표준 환영',
				linkedArticle: false,
				syntax: ' ~~~~'

Twinkle.welcome.getTemplateWikitext = function {
	// the iteration is required as the type=standard has two groups
	var properties;
	$.each(Twinkle.welcome.templates[type], function {
		properties = templates[template];
		if (properties) {
			return false; // break
	if (properties) {
		return properties.syntax.
			replace('$USERNAME$', Twinkle.getPref('insertUsername') ? mw.config.get('wgUserName') : '').
			replace.
			replace.
			replace; // EXTRA is not implemented yet
	return '' +
			(Twinkle.getPref('customWelcomeSignature') ? ' ~~~~' : '');

Twinkle.welcome.callbacks = {
	preview: function(form) {
		var previewDialog = new Morebits.simpleWindow;
		previewDialog.setTitle('환영합니다 틀 미리 보기');
		previewDialog.setScriptName('사용자 환영');
		previewDialog.setModality(true);

		var previewdiv = document.createElement('div');
		previewdiv.style.marginLeft = previewdiv.style.marginRight = '0.5em';
		previewdiv.style.fontSize = 'small';
		previewDialog.setContent(previewdiv);

		var previewer = new Morebits.wiki.preview(previewdiv);
		var input = Morebits.quickForm.getInputData(form);
		previewer.beginRender(Twinkle.welcome.getTemplateWikitext, '사용자토론:' + mw.config.get('wgRelevantUserName')); // Force wikitext/correct username

		var submit = document.createElement('input');
		submit.setAttribute;
		submit.setAttribute;
		previewDialog.addContent(submit);

		previewDialog.display;

		$(submit).click(function {
			previewDialog.close;
	main: function(pageobj) {
		var params = pageobj.getCallbackParameters;
		var text = pageobj.getPageText;

		// abort if mode is auto and form is not empty
		if (pageobj.exists && params.mode === 'auto') {
			Morebits.status.info;
			Morebits.wiki.actionCompleted.event;
			return;

		var welcomeText = Twinkle.welcome.getTemplateWikitext;

		if (Twinkle.getPref('topWelcomes')) {
			text = welcomeText + '\n\n' + text;
			text += '\n' + welcomeText;

		var summaryText = '위키백과에 오신 것을 환영합니다!';
		pageobj.setPageText(text);
		pageobj.setEditSummary(summaryText + Twinkle.getPref('summaryAd'));
		pageobj.setWatchlist(Twinkle.getPref('watchWelcomes'));
		pageobj.setCreateOption('recreate');
		pageobj.save;

Twinkle.welcome.callback.evaluate = function friendlywelcomeCallbackEvaluate(e) {
	var form = e.target;

	var params = Morebits.quickForm.getInputData(form); // : type, template, article
	params.mode = 'manual';

	Morebits.simpleWindow.setButtonsEnabled(false);
	Morebits.status.init(form);

	var userTalkPage = mw.config.get('wgFormattedNamespaces')[3] + ':' + mw.config.get('wgRelevantUserName');
	Morebits.wiki.actionCompleted.redirect = userTalkPage;
	Morebits.wiki.actionCompleted.notice = '환영을 완료했습니다. 수 초 안에 토론 문서를 다시 불러옵니다';

	var wikipedia_page = new Morebits.wiki.page;
	wikipedia_page.setFollowRedirect(true);
	wikipedia_page.setCallbackParameters(params);
	wikipedia_page.load(Twinkle.welcome.callbacks.main);

Twinkle.addInitCallback;