// ==UserScript==
// @name           Better Mura admin
// @namespace      nordpil.com
// @description    A few shortcuts and hacks to streamline management using the Mura CMS administrator
// @include        http://yoursite/*
// ==/UserScript==

var isAdmin = (document.location.href.match('http:\/\/[^/]*\/admin\/') != null);
var isEditor = (document.location.href.match('http:\/\/[^/]*\/wysiwyg\/') != null);
var isAuth = (document.getElementById('frontEndToolsHandle') != null);

// {{{ Attach accesskey for publish button, on forms
if (isAdmin) {
	var elementList = document.getElementsByTagName('a');
	for (var i = elementList.length - 1; i >= 0; i--) {
		var elementItem = elementList[i];
		if (elementItem.className == 'submit' && elementItem.innerHTML == '<span>Publish</span>') {
			elementItem.accessKey = 'p';
		}
	}
}
// }}}

// Skip the 'save to draft' popup
setTimeout ('formSubmitted = true',1500);

// Skip the javascript 3 hour timeout
window.addEventListener("load", function(e) {
	setTimeout('function CountDown() {}',1000)
}, false);

// {{{ Attach accesskey for edit functionality, for front end
if (!isAdmin && !isEditor) {
	// Are we logged in?
	if (isAuth) {
		// If we have the gmedit flag (set by the accesskey when not logged in), go directly to the edit view...
		if (document.location.href.match('gmedit')) {
			setTimeout("javascript:Shadowbox.open(document.getElementById('adminEditPage').getElementsByTagName('a')[0]);",500);
		} else {
			document.getElementById('adminEditPage').innerHTML = document.getElementById('adminEditPage').innerHTML.replace('href','accesskey="e" href');
		}
	} else {
		// When the accesskey is hit, we'll open a popup to login into the admin, and reload the window with the gmedit flag...
		jsSnippet = 'var admWin = window.open(this.href);function checkAdmWin() {if (admWin.closed || admWin.location.href.match(\'Login\') == null) {admWin.close(); document.location.href = document.location.href+\'?gmedit\';  } else {setTimeout(checkAdmWin,500)}}; setTimeout(checkAdmWin,1000);';
		document.getElementsByTagName('body')[0].innerHTML = '<a onclick="javascript:' + jsSnippet + 'return false;" href="' + document.location.href.match('http:\/\/[^/]*') +'/admin/" accesskey="e" style="DISPLAY: Block; POSITION: Absolute; LEFT: -9999px; TOP: -9999px;" rel="shadowbox">Edit</a>' + document.getElementsByTagName('body')[0].innerHTML;
	}
}
// }}}

// {{{ Attach accesskey for opening the site manager
if (!isAdmin && !isEditor) {
	// Are we logged in?
	if (isAuth) {
		document.getElementById('frontEndTools').innerHTML = document.getElementById('frontEndTools').innerHTML.replace(/(<li id="adminLogOut")/,'<li id="adminSiteMgr"><a href="/admin/" target="_blank" accesskey="s">Site&nbsp;manager</a></li>$1');
	} else {
		document.getElementsByTagName('body')[0].innerHTML = '<a href="/admin/" accesskey="s" target="_blank" style="DISPLAY: Block; POSITION: Absolute; LEFT: -9999px; TOP: -9999px;" rel="shadowbox">Edit</a>' + document.getElementsByTagName('body')[0].innerHTML;
	}
}
// }}}
