// ==UserScript==
// @name				deviantEXPERIENCE:PowerMode
// @namespace			com.deviantart.dancewiththesky
// @description			Adds key shortcuts to deviantART pages
// @include			http://*.deviantart.com/*
// @exclude			http://chat.deviantart.com/chat/*
// @exclude			http://www.deviantart.com/submit/*
// @exclude			http://mail.deviantart.com/*
// @exclude 			http://portfolio.deviantart.com/*	
// @exclude			http://my.deviantart.com/settings/identity* 
// @x-pkg-guid			{b90fa88d-d623-40da-a4eb-7144f85a3139}
// @x-pkg-version		0.2.3.3
// ==/UserScript==

/* ==UpdateInfo==
	0.2.3.3: Exclude identity settings.
	0.2.3.2: Exclude portfolio management.
	0.2.3.1: Exclude staff mail.
	0.2.3: Detects Command key on Mac. New shorcut for one's gallery.
	0.2.2.4: New Update URL.
	0.2.2.3: Exclude submission page
	0.2.2.2: Added new shorcut 'V' for your favourites. Minor enhancements. 
	0.2.2.1: Migrated to ChickenFillet and became compatible with Firefox 3.
	0.2.2: Added a new shortcut key X for the devWATCH list management page. Fixed a bug that made the extension interfere with Staff shortcuts. Disabled the extension from working inside dAmn chat rooms.
	0.2.1: Fixed a bug that made the extension interfere with Firefox default shortcuts.

   ==/UpdateInfo== */


(function(){

	const DEVIANT_NAME = 
		unsafeWindow.deviantART? unsafeWindow.deviantART.deviant.username : null;

    var KEY_MAPPING = {
         'I' : ["deviantART front page (<u>i</u>ndex)", "http://www.deviantart.com/"],
         'T' : ["<u>T</u>oday", "http://today.deviantart.com/"], 
         'F' : ["<u>F</u>orum", "http://forum.deviantart.com/"],
         'H' : ["<u>H</u>elp & FAQ", "http://help.deviantart.com/"],
         'R' : ["<u>R</u>andom deviant", "http://www.deviantart.com/random/deviant"],
         'D' : ["Random <u>d</u>eviation", "http://www.deviantart.com/random/deviation"]
    };

	if (DEVIANT_NAME){ 
		var m = { // shortcuts that requires deviant to be logged in
			'P' : ["Your <u>p</u>rofile", "http://" + DEVIANT_NAME + ".deviantart.com" ],
			'M' : ["Your <u>m</u>essage center", "http://my.deviantart.com/messages/"],
			'G' : ["Your <u>g</u>allery", "http://" + DEVIANT_NAME + ".deviantart.com/gallery"],
			'J' : ["Your <u>j</u>ournal management", "http://my.deviantart.com/journal/"],
			'N' : ["Your <u>n</u>otes", "http://my.deviantart.com/notes/"],
			'W' : ["Your deviant<u>W</u>ATCH",  "http://my.deviantart.com/messages/#view=deviations"],
			'X' : ["Manage your devWATCH list", "http://my.deviantart.com/deviants/"],
			'V' : ["Your fa<u>v</u>uorites",  "http://" + DEVIANT_NAME + ".deviantart.com/favourites/" ],
			'S' : ["<u>S</u>ubmission page", "http://www.deviantart.com/submit/"] 
		};
		
        for(var k in m){
			KEY_MAPPING[k] = m[k];
		}
	}

    function fromSafeTarget(keyEvent){
        var targetTag = keyEvent.target.tagName.toLowerCase();
        return (
				!keyEvent.altKey &&
				!keyEvent.ctrlKey &&
				!keyEvent.metaKey &&
				targetTag != "textarea" &&  
				targetTag != "select" && 
				targetTag != "input"
			);
    }

	function getKey(keyEvent){
		if (keyEvent.which > 65 && keyEvent.which < 90){
			return String.fromCharCode(keyEvent.keyCode);
		}
	} 

	function run(){
		// create a hidden shortcut list
		var shortCutList = document.createElement('div');
		shortCutList.id = '__powermode-shortcutList';
		shortCutList.setAttribute('style', 'background-color: #374341; opacity: 0.95; position: fixed; top: 100px; width: 400px; padding: 20px; border-width: 1px; border-color: white; border-style: solid; color: white; z-index: 60; display: none;');
		shortCutList.style.left = (window.innerWidth/2) - (400/2) + "px";
		var listCode =  "<b>PowerMode Shortcut List</b><br /><br /><table>";
		
		// add login notice
		if(!DEVIANT_NAME){
			listCode += "<tr><td colspan='2'><i>Please log in to enable" + 
						" all supported  shortcuts</i><br /><br /></td></tr>";
		}

		for(k in KEY_MAPPING){
			if (k == 'P'){ // start of 'my' shortcuts
				listCode += "<tr><td colspan='2'>&nbsp;</td></tr>";
			}

			listCode += "<tr><td><b>" + k + "&nbsp;&nbsp;</b></td><td>" + KEY_MAPPING[k][0] + "</td></tr>";
		}

		// add L
		listCode += "<tr><td colspan='2'>&nbsp;</td></tr>";
		listCode += "<tr><td><b>L</b></td><td>Show key shortcut <u>l</u>ist.</td></tr></table>";

		shortCutList.innerHTML = listCode;
		document.body.appendChild(shortCutList);
	
		document.addEventListener('keydown', function(e){
			if (fromSafeTarget(e)) {
				var k = getKey(e);
				if (k == 'L'){
					shortCutList.style.display = "block";
				} else if (KEY_MAPPING[k]){
					window.location.href = KEY_MAPPING[k][1];
				}
			}
		}, false);
	
		document.addEventListener('keyup', function(e){
			if (fromSafeTarget(e) && getKey(e) == 'L'){
				shortCutList.style.display = "none";
			}
		}, false);
	}

	run();

})();
