//

autodel = {};
autodel.enabled = true;
autodel.default_summary = 'Per CSD.';
autodel.delete_talks = true;
autodel.delete_talks_summary = 'Per CSD G8. The reason for deleting the non-talk page was: $1';

if(autodel.enabled) {

function query_string_get(paramName) {
  var cmdRe=RegExp('[&?]'+paramName+'=([^&]*)');
  var h=document.location;
  var m;
  if (m=cmdRe.exec(h)) {
    try { 
      return unescape(m[1]);
    } catch (someError) {}
  }
  return null;
};

addOnloadHook(function() {
	addPortletLink('p-cactions', "javascript:prepare_for_autodel()", "links autodel", 'ca-autodellinks', 'Alter all the links on this page to autodel the page when clicked');
});

if(query_string_get('autodel') != null && query_string_get('action') == 'delete') {
	addOnloadHook(function() {
		var reasonbox = document.getElementById('wpReason');
		if(!reasonbox) window.close();
		var olddelsum = reasonbox.value;
		var newdelsum = query_string_get('autodel')+' '+olddelsum;
		reasonbox.value = newdelsum;
		var inputs = document.getElementsByTagName('input');
		if(!inputs) window.close();
		for (i=0; i<inputs.length; i++) {
			if (inputs[i].name == 'wpConfirmB') {
				inputs[i].click();
				window.close();
			}
		}
		window.close();
	});
}

newlinks = [];
function prepare_for_autodel() {
	var tag = prompt('Please enter a summary:');
	if(tag == null) { alert('Action canceled.'); return; }
	if(tag == "") tag = autodel.default_summary;
	var links = document.getElementById('bodyContent').getElementsByTagName('a');
	len = links.length;
	for(var i=0; i<len; i++) {
		var link = links[i];
		if(link.className == 'new'||link.href.indexOf('linkmodified=yes') != -1) continue;
		if(!((/^.*?\/wiki\/([^:]+:|)([^?]+).*$/).test(link.href))) continue;
		var mtc = link.href.match('^.*?\/wiki\/([^?#]+).*$');
		if(mtc[1] == wgPageName) continue;
		var linkpage = link.href.match(/^.*?\/wiki\/([^:]+:|)([^?]+).*$/);
		var usetalk = true;
		var talkpage = '';
		if(linkpage[1].toLowerCase().indexOf('talk:') != -1) {
			usetalk = false;
		} else {
			if(linkpage[1] == '') {
				talkpage = "Talk:"+linkpage[2];
			} else {
				talkpage = linkpage[1].replace(':', '')+"_talk:"+linkpage[2];
			}
		}
		link.href = link.href+'?linkmodified=yes&action=delete&autodel='+escape(tag);
		if(autodel.delete_talks&&usetalk) {
			var nli = document.createElement('a');
			nli.innerHTML = ' [Talk page]';
			nli.href = '/wiki/'+talkpage+'?linkmodified=yes&action=delete&autodel='+escape(autodel.delete_talks_summary.replace(/\$1/g, tag));
			nli.style.color = "green";
			nli.onmouseup = function() { this.style.color = 'red'; };
			nli.numb = i;
			nli.className = 'temporary';
			newlinks[newlinks.length] = nli;
		}
		link.style.color = "green";
		link.onmouseup = function() { this.style.color = 'red'; }; 
		link.innerHTML = link.innerHTML.replace(/(<([^>]+)>)/g, '');
	}
	
	if(autodel.delete_talks) for(var i = 0; i < newlinks.length; i++) {
		var nlii = newlinks[i];
		var linko = document.getElementById('bodyContent').getElementsByTagName('a')[parseInt(nlii.numb)+i];
		if(linko.nextSibling) {
			linko.parentNode.insertBefore(nlii, linko.nextSibling);
		} else {
			linko.parentNode.appendChild(nlii);		
		}
	}
	var btn = document.getElementById('ca-autodellinks').firstChild;
	btn.href = 'javascript:unprepare_for_autodel();';
	btn.innerHTML = 'REMOVE links autodel';
	btn.title = '';
}

function unprepare_for_autodel() {
	var links = document.getElementById('bodyContent').getElementsByTagName('a');
	for(var i=0; i<links.length; i++) {
		var link = links[i];
		link.onclick = function() { return true; };
		link.onmouseup = function() { return true; };
		link.href = link.href.split('linkmodified=yes')[0];
		if(link.className.indexOf('temporary') != -1) {
			link.style.display = 'none';
			link.className = 'new';
		}
		if(link.style.color != 'red') link.style.color = "";
	}
	alert('All links will now be safe to click...');
	var btn = document.getElementById('ca-autodellinks').firstChild;
	btn.href = 'javascript:prepare_for_autodel();';
	btn.innerHTML = 'links autodel';
	btn.title = '';
}
}

//