// ==UserScript==
// @namespace     http://riddle.pl/-/greasemonkey/alert.hack.user.js
// @name          Alert Hack
// @author				Piotr "Riddle" Petrus
// @description		Overrides default window.alert() to non-modal div layer
// @include       *
// @version				0.1
// ==/UserScript==


/* settings */
	var aboveFlash = true;
	var reorderStack = true;

/* variables */
	var prefix = "GM_customDialog_";
	var offsetDialog = 15;
	var strOK = "OK";
	var strCancel = "Cancel";
	
/* styling */	
	var newCSS = "." + prefix + "frame { position: absolute; top: 0; left: 0; z-index: -1; min-width: 345px; max-width: 601px; min-height: 99px; background-color: threedface; border-style: solid; border-width: 3px; -moz-border-top-colors: threedshadow threedhighlight threedlightshadow; -moz-border-right-colors: threedshadow threedhighlight threedlightshadow; -moz-border-bottom-colors: threedshadow threedhighlight threedlightshadow; -moz-border-left-colors: threedshadow threedhighlight threedlightshadow; } ." + prefix + "alert, ." + prefix + "confirm { padding: 14px 16px 10px 55px; min-height: 39px; font: message-box; color: windowtext; -moz-user-select: none; cursor: default; } ." + prefix + "alert { background: url('chrome://global/skin/icons/Warning.png') 13px 11px no-repeat; } ." + prefix + "confirm { background: url('chrome://global/skin/icons/Question.png') 13px 11px no-repeat; } ." + prefix + "frame .butOne, ." + prefix + "frame .butTwo { text-align: center; padding: 0 0 13px 0; margin-left: 6px; } ." + prefix + "frame button { font: message-box; color: buttontext; width: 75px; padding: 1px 0; -moz-padding-start: 1px; -moz-padding-end: 1px; } ." + prefix + "frame .butTwo button.ok { position: relative; left: -6px; } ." + prefix + "frame .butTwo button.cancel { position: relative; right: -6px; } ." + prefix + "frame button::-moz-focus-inner { padding: 1px 0 2px 0; } ." + prefix + "frame button:focus::-moz-focus-inner { border: 1px dotted threeddarkshadow; }";
	
GM_addStyle(newCSS);

function getElementsByClassName(className) {
  var children = document.getElementsByTagName('*') || document.all;
  var elements = new Array();
 
  for (var i = 0; i < children.length; i++) {
    var child = children[i];
    var classNames = child.className.split(' ');
    for (var j = 0; j < classNames.length; j++) {
      if (classNames[j] == className) {
        elements.push(child);
        break;
      }
    }
  } 
  return elements;
} 

function centerMe(what, offset, zindex) {
	var windowWidth = self.innerWidth;
	var windowHeight = self.innerHeight;	
	var left = Math.floor((windowWidth / 2) - (what.offsetWidth / 2));
	var top = Math.floor((windowHeight / 2) - (what.offsetHeight / 2));	
	what.style.left = left + offset + "px";
	if ((top + offset) >= 0) { what.style.top = top + offset + "px"; }
	else { what.style.top = offsetDialog + offset + "px"; }
	what.style.zIndex = zindex * 100;
}

function topPush(e) {
	var el = e.currentTarget;
	var frames = getElementsByClassName(prefix + 'frame');
	var zindex = 1;
	var biggest = "";
	for (var i = 0; i < frames.length; i++) {
		if (frames[i].style.zIndex > biggest) {
			biggest = frames[i].style.zIndex;
		}
	}
	if (el.style.zIndex != parseInt(biggest)) {
		el.style.zIndex = parseInt(biggest) + 1;
	}
	e.stopPropagation();
	e.preventDefault();
}
 
function customDialog(type, s) {
	function hideDialog(e) {
		var el = e.currentTarget;
		var box = el.parentNode.parentNode;
		box.parentNode.removeChild(box);		
		var frames = getElementsByClassName(prefix + 'frame');
		var count = frames.length;
		if (count > 0) {
			frames[count - 1].childNodes[1].firstChild.focus();
		}
	}	
	var frame = document.createElement("div");
	frame.id = prefix + type;
	frame.className = prefix + 'frame';	
	var inner = document.createElement("div");
	inner.className = prefix + type;
	inner.appendChild(document.createTextNode(s));
	frame.appendChild(inner);	
	var buttoncont = document.createElement("div");	
	buttoncont.className = "butOne";
	var butOk = document.createElement("button");
	butOk.className = "ok";
	butOk.appendChild(document.createTextNode(strOK));
	butOk.addEventListener("click", hideDialog, false);
	buttoncont.appendChild(butOk);	
	frame.appendChild(buttoncont);
	document.body.appendChild(frame);		
	butOk.focus();	
	var frames = getElementsByClassName(prefix + 'frame');
	var count = frames.length - 1;
	centerMe(frame, count * offsetDialog, count + 1);	
	if (reorderStack) frame.addEventListener("click", topPush, false);
}

if (aboveFlash) {
	var embeds = document.getElementsByTagName("embed");
	for (var i = 0; i < embeds.length; i++) {
		embeds[i].setAttribute("wmode", "transparent");
		embeds[i].style.position = "relative";
		embeds[i].style.zIndex = "0";
	}
}

unsafeWindow.alert = function alertWrap(s) { customDialog("alert", s); }
