
if (!LWL || (typeof LWL !== 'object')) {
	var LWL = {};
}

(function()
{
	if (!LWL.widget || (typeof LWL.widget !== 'object')) {
		LWL.widget = {};
	}
	if (!LWL.widget.content || (typeof LWL.widget.content !== 'object')) {
		LWL.widget.content = {};
	}
	if (LWL.widget.content.launchtip && (typeof LWL.widget.content.launchtip === 'object')) {
		return; // This module is already loaded.
	}

	/**#@+
	 * Private functions.
	 */

	/**
	 * Move a HTML element to the lower right hand corner of another element.
	 *
	 * @param {Object} soruce  HTML element.
	 * @param {Object} target  HTML element.
	 * @type void
	 */
	function move(source, target)
	{
		var p = target.offsetParent;
		var left = 0, top = 0;

		if (p) {
			do {
				left += p.offsetLeft;
				top += p.offsetTop;
			} while (p = p.offsetParent);
		}

		var style = source.style;
		style.left = (target.offsetLeft + (target.offsetWidth / 2) + left) + 'px';
		style.top = (target.offsetTop + target.offsetHeight + top) + 'px';
	}

	/**#@-*/

	LWL.widget.content.launchtip = function()
	{

		var widget_container = document.createElement('div'),
			widget_style = widget_container.style,
			content = [],
			i = -1,
			_data = {};

		_data.ready = false;

		/**#@+
		 * HTML.
		 */
		content[++i] = '<div style="font-family:Arial,Helvetica,sans-serif; font-size:10px; color:#000000; width:306px;">';
			content[++i] = '<div style="width:306px; height:48px; background:url(\'http://www.ladieswholaunch.com/assets/images/widgets/launchtip_row_top.gif\') no-repeat;">';
				content[++i] = '<span style="position:relative; left:290px; top:31px; font-size:12px; font-weight:bold; cursor:pointer;" onclick="LWL.widget.content.launchtip.deactivate();">X</span>';
			content[++i] = '</div>';
			content[++i] = '<div style="background:url(\'http://www.ladieswholaunch.com/assets/images/widgets/launchtip_row_middle.gif\') repeat-y;">';
				content[++i] = '<div style="text-align:center;"><img src="http://www.ladieswholaunch.com/assets/images/widgets/widget_launchtip_title.gif" alt=""></div>';
				content[++i] = '<div style="width:306px; text-align:center;">';
					content[++i] = '<iframe frameborder="0" scrolling="auto" width="95%" height="200" src="http://www.ladieswholaunch.com/widget/content/mE3cnR315yub61a47Sdkx9h0pcKGb212"></iframe>';
				content[++i] = '</div>';
			content[++i] = '</div>';
			content[++i] = '<div style="width:306px; height:4px; background:url(\'http://www.ladieswholaunch.com/assets/images/widgets/launchtip_row_bottom.gif\') no-repeat;"></div>';
		content[++i] = '</div>';
		/**#@-*/

		widget_style.position = 'absolute';
		widget_style.top = '0';
		widget_style.left = '0';
		widget_style.zIndex = '998';
		widget_style.display = 'none';

		widget_container.innerHTML = content.join('');

		/**#@+
		 * DOM attachment.
		 * Interval = 10 milliseconds.
		 * Retry = 30 seconds.
		 */
		var _t = 0;
		var _timer = setInterval(
			function()
			{
				_t++;

				if (_t > 3000) {
					clearInterval(_timer);
					throw new Error('Unable to create the widget.'); // Cannot attach to DOM.
				}

				if (!document.body) {
					return; // Not ready yet.
				}

				var body = document.body,
					children = body.childNodes,
					child;

				for (var n = 0, counter = children.length || 0; n < counter; n++) {
					child = children[n];
					if (child.nodeType === 1) {
						break;
					}
				}

				if (child && (child.nodeType === 1)) {
					body.insertBefore(widget_container, child);
				} else {
					body.appendChild(widget_container);
				}

				_data.ready = true;

				clearInterval(_timer);
			},
			10
		);
		/**#@-*/

		return {

			/**#@+
			 * Public methods.
			 */

			activate: function(button)
			{
				if (!button || (typeof button !== 'object') || (button.nodeType !== 1)) {
					throw new Error('Invalid button.');
				}

				if (widget_style.display === 'none') {
					move(widget_container, button);
					widget_style.display = '';
				} else {
					// Re-appear in 100 milliseconds.
					widget_style.display = 'none';
					setTimeout(
						function()
						{
							move(widget_container, button);
							widget_style.display = '';
						},
						100
					);
				}
			},

			deactivate: function()
			{
				if (widget_style.display !== 'none') {
					widget_style.display = 'none';
				}
			}

			/**#@-*/

		};

	}();
})();
