// Steam Community (re)Namer
// Copyright 2011 Dave Transom
// --------------------------------------------------------------------
// ==UserScript==
// @name           Steam Community (re)Namer
// @namespace      CSharpVitamins
// @description    Replaces Quake/ID based naming conventions with proper markup/colour coding
// @author         Dave Transom
// @homepage       http://www.singular.co.nz/sandbox/games/namer.html
// @include        http://steamcommunity.com/*
// @require        http://ajax.googleapis.com/ajax/libs/jquery/1.5.1/jquery.min.js
// ==/UserScript==

(function() {

    function Part(index, text) {
		this.index = index;
		this.text = text;
	}

	Part.defaultColorIndex = 7;

	Part.getColor = function(index) {
		switch (index) {
			case 0: return "#000"; // Black
			case 1: return "#f00"; // Red
				//case 2: return "#2e8b57"; // SeaGreen
			case 2: return "#008000"; // SeaGreen
			case 3: return "#ff0"; // Yellow
			case 4: return "#00f"; // Blue
			case 5: return "#0ff"; // Cyan
			case 6: return "#f0f"; // Magenta
			default:
			case 7: return "#fff"; // White
			case 8: return "#fff"; // Default Map Color
			case 9: return "#808080"; // Gray

			case 10: return "#006400"; // DarkGreen
			case 11: return "#8b0000"; // DarkRed
			case 12: return "#00008b"; // DarkBlue
		}
	};

	Part.prototype = {
		index: 0,
		text: "",
		isDark: function() {
			return this.index === 0;
		},
		toNode: function() {
			return $("<span/>").text(this.text).css("color", Part.getColor(this.index));
		}
	}

	function Name(code) {
		this.parse(code);
	};

	Name.prototype = {
		code: "",
		parts: [],
		isDark: false,

		parse: function(code) {
			var container = this;
			container.code = code;
			container.isDark = false;
			var parts = container.parts = [];

			if (!!code) {
				$.each(("$$^7" + code).split(/\^/g), function(i, val) {
					if (i === 0) // don't add dummy "$$^7" used for parsing
						return;

					var part = new Part(Part.defaultColorIndex, val);
					if (/\d/.test(val[0]) === true) {
						part.index = parseInt(val[0], 10);
						part.text = val.substring(1);
					}
					else if (parts.length > 0) {
						part.index = parts[parts.length - 1].index;
						part.text = "^" + part.text;
					}

					if (part.isDark() && part.text)
						container.isDark = true;

					parts.push(part);
				});
			}

			return container;
		},

		renderTo: function($node) {
			$node.hide().empty();
			$.each(this.parts, function(i, part) {
				part.toNode().appendTo($node);
			});
			$node
				.attr("title", this.code)
				.toggleClass("darker", this.isDark === true)
				.toggle(this.parts.length > 0);
			return this;
		}
	};
	
	$("h1:contains('^')").each(function(){
		var $h1 = $(this);
		if($h1.children().length == 0)
			new Name($h1.text()).renderTo($h1);
	});
	
	$("a[href^='http://steamcommunity.com/']:contains('^')").each(function(){
		var $a = $(this);
		if(/^http:\/\/steamcommunity\.com\/(id|profiles)\//i.test($a.attr("href")) && 
			$a.children().length == 0){
			var name = new Name($a.text()).renderTo($a);
			$a.css({
				"backgroundColor": name.isDark ? "#a9a9a9" : "#222",
				"padding": "1px 2px",
				"border-radius": "2px",
				"-moz-border-radius": "2px",
				"-webkit-border-radius": "2px"
			});
		}
	});

})();
