// Move Reddit Buttons
// Author: Ictinus
// Released: 16 May 2010, move buttons to the right of vote arrows.
//
// ==UserScript==
// @name			Move Reddit Buttons
// @version 		1.14
// @namespace		http://ictinus.com/mrb/
// @description		Moves the Reddit buttons so that they are in a consistent position regardless of title length (vertical) or comment count (horizontal) changes.
// @include			http://www.reddit.com/
// @include			http://www.reddit.com/r/*/
// @exclude			http://www.reddit.com/r/*/comments/*
// ==/UserScript==

var testurl = document.URL;
//console.log(testurl.match(/reddit\.com([^\/]+|\/(comments[^\/]|(?!comments|message)))*$/));

function GM_testUrl(includes, excludes) {
  regTest = function(url) { return new RegExp(url.replace(/(\/|\?|\.|\^|\,|\+)/g, "\\\$1").replace(/\*/g, ".*")).test(document.URL); }
  if (typeof excludes != "undefined") {
    if (typeof excludes == "string") excludes = [excludes];
    for (exclude in excludes) if (regTest(excludes[exclude])) return false;
  }
  if (typeof includes == "string") includes = [includes];
  for (include in includes) if (regTest(includes[include])) return true;
  return false;
}

if (GM_testUrl(["http://www.reddit.com/","http://www.reddit.com/r/*/"],["http://www.reddit.com/r/*/comments/*","http://www.reddit.com/message/*"])) {

	var moveRedditButtons = {
		version : 1.13,
		init: function () {
			//if(moveRedditButtons.state === 0) return;
			var allDivs, thisDiv;
			allDivs = document.evaluate(
				"//div[contains(@class,'entry')]",
				document,
				null,
				XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,
				null);
			for (var i = 0; i < allDivs.snapshotLength; i++) {
				thisDiv = allDivs.snapshotItem(i);
				//create the moved buttons div
				var newBtns, newClear;
				newBtns = document.createElement('div');
				newBtns.className = 'midcol movedButtons';
				newClear = document.createElement('div');
				newClear.className = 'clearleft';
				newClear.innerHTML = '<!--IESux-->';
				
				//get the button content
				var allULs, btnContent;
				allULs = thisDiv.getElementsByTagName('ul');
				btnContent = allULs[0]; //only one UL to be found
				//insert the button content
				if (typeof(btnContent) != 'undefined') {
					newBtns.innerHTML = '<ul class="buttons">' + btnContent.innerHTML + '</ul>';
		
					//remove unwanted buttons from original button ul
					var allLIs = btnContent.getElementsByTagName('li');
					for (var iLI = allLIs.length-1; iLI >= 0; iLI--) {
						var objLink = allLIs[iLI].getElementsByTagName('a');
						if (typeof(objLink[0]) != 'undefined') {
							if (objLink[0].innerHTML.search(/hide/i) != -1) {
								allLIs[iLI].parentNode.removeChild(allLIs[iLI]);
							}
						}
					}
					//now insert the moved buttons
					var nodeParent = thisDiv.parentNode;
					var divMidCol = nodeParent.getElementsByClassName('midcol');
					nodeParent.insertBefore(newBtns, divMidCol[0].nextSibling);
					nodeParent.insertBefore(newClear, thisDiv.nextSibling);
					
					//remove unwanted buttons from moved Buttons
					var allLIs = newBtns.getElementsByTagName('li');
					for (var iLI = allLIs.length-1; iLI >= 0; iLI--) {
						var objLink = allLIs[iLI].getElementsByTagName('a');
						if (typeof(objLink[0]) != 'undefined') {
							if (objLink[0].innerHTML.search(/hide/i) == -1) {
								allLIs[iLI].parentNode.removeChild(allLIs[iLI]);
							}
						} else {
							allLIs[iLI].parentNode.removeChild(allLIs[iLI]);
						}
					}
				}
			}
			//$('.hide-button').css('font-size',14);
		},
		addGlobalStyle: function(css) {
			var head, style;
			head = document.getElementsByTagName('head')[0];
			if (!head) { return; }
			style = document.createElement('style');
			style.type = 'text/css';
			style.innerHTML = css;
			head.appendChild(style);
		}
	}

	if (document.body) { 
		moveRedditButtons.addGlobalStyle('div.movedButtons{width:14px;height:50px;position:relative;} div.movedButtons ul {-webkit-transform: rotate(90deg);-moz-transform: rotate(90deg);position:absolute;left:-11px;top:16px;} div.movedButtons ul li a {color:#888; font-weight:bold; display:block;text-align:center;font-size:14px;} div.movedButtons ul li{padding:1px;} div.movedButtons ul li:hover a {color:#111; width:100%;}');
		moveRedditButtons.init();
	}

}
