// ==UserScript==
// @name         devEdit
// @namespace    http://www.five-by-five.se/v2/code/greasemonkey/
// @description  Makes all edit options available from the deviation page.
// @include      http://www.deviantart.com/deviation/*
// @include      http://www.deviantart.com/view/*
// @exclude      http://www.deviantart.com/deviation/*/favourites/*
// ==/UserScript==

/*
	devEdit 0.2: 2006-09-15
	Copyright (c) 2006 Kalle Räisänen.

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

(function()
{
	var devlinks = document.getElementById('deviation-links');

	if(!devlinks) return;

	var editlink  = null, dellink = false;
	var deviation = '';
	var as        = devlinks.getElementsByTagName('a');
	var islit = false, isdl = false, isscrap = false;

	for(var i = 0; i < as.length; i++) {
		if(as[i].innerHTML == 'Edit Deviation') {
			editlink = as[i];
			deviation = String(editlink.href).replace(/[^\d]/g, '');
		} else if(as[i].innerHTML == 'Delete Deviation') {
			dellink = as[i];
		} else if(as[i].innerHTML == 'Download' ||
		          as[i].id == 'zoom-button') {
			isdl = true;
		}
	}

	if(editlink == null)
		return;

	var caturi = document.getElementById('catbutton').getAttribute('menuri');

	if(caturi.match(/^art$/))
		isscrap = true;

	var islit = document.getElementById('lit-view') ? true : false;
	var edits = {
		'title':    ['Title and desc.', true],
		'category': ['Category',        true],
		'preview':  ['Preview',         true],
		'writing':  ['Writing',         islit],
		'file':     ['File',            isdl]
	}

	var span = document.createElement('span');
	span.id = 'devedit-tools';
	span.style.display = GM_getValue('devedit_display', 'inline');
	editlink.innerHTML += ':';
	editlink.href = '#devedit';
	editlink.addEventListener(
		'click',
		function(e) {
			if(e.detail == 2) {
				location.href = 'http://www.deviantart.com/edit/' + deviation;
			} else {
				var s = document.getElementById('devedit-tools');
				s.style.display = (s.style.display == 'none' ? 'inline' : 'none');
				GM_setValue('devedit_display', s.style.display);
			}
		},
		false
	);

	var numadded = 0;

	for(var key in edits) {
		if(edits[key][1]) {
			var a = document.createElement('a');
			a.href = 'http://www.deviantart.com/edit/' + deviation + '/' + key;
			a.innerHTML = edits[key][0];
			a.style.textIndent = '1em';
			span.appendChild(a);
			numadded++;
		}
	}

	devlinks.insertBefore(span, editlink.nextSibling);

	if(!isscrap) {
		var a = document.createElement('a');
		a.innerHTML = 'Move to Scraps';
		a.className = 'c';
		a.href = '#movetoscraps';
		a.addEventListener(
			'click',
			function() {
				if(confirm('Are you sure?'))
					window.location.href = 'http://www.deviantart.com/deviation/' + deviation + '/scrap';
				else
					return false;
			},
			false
		);

		var i = document.createElement('i');
		i.className = 'i10';
		i.setAttribute('style', 'background: url(http://i.deviantart.com/icons/deviation/scrap-deviation.gif) no-repeat');

		devlinks.insertBefore(a, dellink.nextSibling);
		devlinks.insertBefore(i, dellink.nextSibling);
	}

	var dev = document.getElementById('deviation').getElementsByTagName('div')[0];

	if(dev)
		dev.style.marginBottom = (numadded * 2) + 'em';
})();

