



function GUnmarker ( frame, label, debug )
{
	this.debug = debug ? true : false;
	this.frame = frame;
	this.label = label ? label : 'for-reply';
	this.reply = [];
	this.send  = [];



	GUnmarker.prototype.clearReplyMarker = function ()
	{
		var spans = this.frame.getElementsByTagName('span');
		for ( var i = 0; i < spans.length; ++i )
		{
			if ( spans[i].getAttribute('name') == this.label && /^Remove/.exec(spans[i].getAttribute('alt')) )
			{
				var span = spans[i];
				var evt  = document.createEvent('MouseEvents');
				evt.initEvent('click',true,true);

				if ( span.dispatchEvent )
				{
					span.dispatchEvent(evt);
				}
				else if ( span.fireEvent )
				{
					span.fireEvent(evt);
				}

				break;
			}
		}

		this.insertReplyHandlers();
	};



	GUnmarker.prototype.dispose = function ()
	{
		this.frame = null;
		this.reply = [];
		this.send  = [];
	};



	GUnmarker.prototype.insertReplyHandlers = function ()
	{
		var _this = this;
		var divs  = this.frame.getElementsByTagName('div');
		this.send = [];

		for ( var i = 0; i < divs.length; ++i )
		{
			if ( divs[i].getAttribute('idlink') == null )
			{
				continue;
			}

			var spans = divs[i].getElementsByTagName('span');
			for ( var j = 0; j < spans.length; ++j )
			{
				if ( spans[j].innerHTML == 'Reply' )
				{
					var exists = false;
					for ( var k = 0; k < this.reply.length; ++k )
					{
						if ( spans[j] == this.reply[k] )
						{
							exists = true;
							break;
						}
					}

					if ( !exists )
					{
						spans[j].addEventListener('click',function () { setTimeout(function () { _this.insertSendHandlers(); },100); },false);
						this.reply.push(spans[j]);
						if ( this.debug )
						{
							alert('click handler inserted');
						}

						var p   = spans[j].parentNode.parentNode.parentNode.parentNode.parentNode.parentNode;
						var tas = p.getElementsByTagName('textarea');

						for ( var k = 0; k < tas.length; ++k )
						{
							tas[k].addEventListener('mousedown',function () { setTimeout(function() { _this.insertSendHandlers(); },100); },false);
						}
					}
					else if ( this.debug )
					{
						alert('skipping existing click handler');
					}
				}
			}
		}
	};



	GUnmarker.prototype.insertSendHandlers = function ()
	{
		var _this = this;
		var bs    = this.frame.getElementsByTagName('b');

		for ( var i = 0; i < bs.length; ++i )
		{
			if ( bs[i].parentNode.getAttribute('class') == 'goog-imageless-button-content' && bs[i].innerHTML == 'Send' )
			{
				var found = false;
				for ( var j = 0; j < this.send.length; ++j )
				{
					if ( this.send[j] == bs[i] )
					{
						found = true;
						break;
					}
				}

				if ( found )
				{
					continue;
				}

				this.send.push(bs[i]);
				bs[i].addEventListener('click',function () { _this.clearReplyMarker(); },false);
				bs[i].parentNode.addEventListener('click',function () { _this.clearReplyMarker(); },true);
				if ( _this.debug )
				{
					alert('click handler inserted');
				}
			}
		}
	};



	this.insertReplyHandlers();
}



function GUnmarkerScanner ( label, debug )
{
	this.debug = debug ? true : false;
	this.frame = document.getElementById('canvas_frame');
	this.label = label ? label : 'for-reply';
	this.hash  = '';

	if ( this.frame.contentDocument )
	{
		this.frame = this.frame.contentDocument;
	}
	else if ( this.frame.documentElement )
	{
		this.frame = this.frame.documentElement;
	}
	else
	{
		throw new Error('Hrm, the frame document is not accessible in your browser.');
	}




	this.scan_timeout = 300;



	GUnmarkerScanner.prototype.scan = function ()
	{
		var _this = this;

		if ( parent.location.hash == this.hash )
		{
			setTimeout(function () { _this.scan(); },this.scan_timeout);
			return;
		}
		this.hash = parent.location.hash;

		if ( this.gunm )
		{
			this.gunm.dispose();
			delete this.gunm;
			this.gunm = null;
		}

		var divs  = this.frame.getElementsByTagName('div');
		var found = false;

		for ( var i = 0; i < divs.length; ++i )
		{
			if ( divs[i].getAttribute('idlink') == null )
			{
				continue;
			}

			var spans = divs[i].getElementsByTagName('span');
			for ( var j = 0; j < spans.length; ++j )
			{
				if ( spans[j].innerHTML == 'Reply' )
				{
					if ( !this.gunm )
					{
						this.gunm = new GUnmarker(this.frame,this.label,this.debug);
					}

					found = true;
					break;
				}
			}

			if ( found )
			{
				break;
			}
		}

		setTimeout(function () { _this.scan(); },this.scan_timeout);
	};



	this.scan();
}



var gunms = new GUnmarkerScanner(null,false);



