// Soft Alert Script Provided by Andrew Penry
// for use on MWD Produced Sites
// Learn more about Andrew at http://www.thepenry.net

/* Include the following css:
div.tk_softalert {
 position: absolute;
 left: 25%;
 width: 50%;
 color: white;
 background-color: #333333;
 font-size: 28pt;
 text-align: center;
}
*/

var tk_softAlertClass = Class.create();
tk_softAlertClass.prototype = {
	version : 1.0,
	element : null,
	content : "",
	pageY : null,
	holdUntil : 0,
	mouseX : 0,
	mouseY : 0,
	mouseStartX: 0,
	mouseStartY: 0,
	initialize : function(options) {
		this.options = Object.extend({
			opacity   : .85,
			cssclass  : 'tk_softalert',
			msgid     : 'tk_softalert',
			offsetY   : 100,
			mouseMin  : 50,
			readSpeed : 20 // milliseconds/character
		});

		this.element = new Element('div', { 'class': this.options.cssclass, id: this.options.msgid });
	    document.body.appendChild(this.element);
	    $(this.element).hide();

		document.observe('mousedown', this._hide.bind(this));
		document.observe('keydown', this._hide.bind(this));
		document.observe('mousemove', this._detectmove.bindAsEventListener(this));


	},

	_getScroll : function(){
      	if(typeof(window.pageYOffset) == 'number') {
        	this.pageY = window.pageYOffset;
      	} else if(document.body && (document.body.scrollLeft || document.body.scrollTop)) {
        	this.pageY = document.body.scrollTop;
		} else if(document.documentElement) {
        	this.pageY = document.documentElement.scrollTop;
      	}
	},

	_hide : function(){
		if ($(this.element).getStyle('display') != 'none' ) {
			var timestamp = new Date().getTime();
			var d = this.holdUntil - timestamp;
			d = (d > 0) ? d/1000 : 0;
			$(this.element).fade({delay:d, duration: 0.5, queue: {position:'end', scope: 'tk_softalertq', limit: 1}, to:0, from: this.options.opacity});
		}
	},

	_detectmove : function(e) {
		this.mouseX = e.pointerX();
		this.mouseY = e.pointerY();
		if ($(this.element).getStyle('display') != 'none' ) {
			var move = Math.abs(this.mouseStartX - this.mouseX) + Math.abs(this.mouseStartY - this.mouseY);
			if (move > this.options.mouseMin) {
				this._hide();
			}
		} else {
			this.mouseStartX = e.pointerX();
			this.mouseStartY = e.pointerY();
		}
	},

	_show : function(){
		$(this.element).innerHTML = this.content;
		this._getScroll();
		$(this.element).setStyle({
			position: 'absolute',
			top: this.pageY+this.options.offsetY+'px'
		});
		eff = new Effect.Appear(this.element, { duration: 0.5, queue: {position:'end', scope: 'tk_softalertq', limit: 1}, from:0, to: this.options.opacity });
		// Add a 20 ms delay per each character before the fade can happen. This gives the box a minimum time.
		this.holdUntil = eff.finishOn + (this.content.stripTags().length * this.options.readSpeed);
	},

	show : function(message) {
		this.content = message;
		fx = this._show.bind(this);
		fx.delay(0.2);

	}
}

var mySoftAlert = null;
document.observe("dom:loaded", function() {
	mySoftAlert = new tk_softAlertClass({
		opacity  : .9,
		cssclass : 'tk_softalert',
		msgid    : 'tk_softalert',
		offsetY  : 100
	});

});

function softAlert(message){
	f = mySoftAlert.show.bind(mySoftAlert);
	f(message);
}

