// Help namespace
//     Contains tools to display help popup messages
Designer.Help = {

	// Options namespace
	//     Contains component options
	Options : {

		// Indicates whether or not to disable the interface while the help popup is displayed
		DisableInterface : false
	},

	// Data namespace
	//     Contains various information related to the behaviour of this component
	Data : {

		// Holds an array with instructions for each step
		Instructions : [],

		// Indicates whether or not the Help component was initialised
		Initialised : false,

		// Indicates whether or not the help popup is visible
		Visible : false,

		// IE6 handlers interval ID
		IE6Interval : null
	},

	// Initialises the Help component (if not already called)
	Initialise : function() {

		if (Designer.Help.Initialised) {

			return false;
		}

		var popup = document.createElement('div');

		popup.id = 'help-popup';

		document.body.insertBefore(popup, document.body.firstChild);

		if (Designer.Help.Options.DisableInterface) {

			var backgroundTile = document.createElement('div');

			backgroundTile.id = 'help-background-tile';

			document.body.insertBefore(backgroundTile, document.body.firstChild);
		}

		Designer.Help.Initialised = true;

		return true;
	},

	// Fixes IE6 CSS support with JavaScript handlers
	//     install - Indicates whether to install or remove IE6 handlers
	IE6 : function(install) {

		var backgroundTile = $('help-background-tile');
		var popup = $('help-popup');

		if (install) {

			popup.style.position = 'absolute';

			if (Designer.Help.Options.DisableInterface) {

				backgroundTile.style.position = 'absolute';
			}

			Designer.Help.IE6Interval = window.setInterval(function() {

				if (Designer.Help.Options.DisableInterface) {

					backgroundTile.style.top = ((document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop) + 'px');
					backgroundTile.style.left = ((document.documentElement && document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft) + 'px');
					backgroundTile.style.width = ((document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body.clientWidth) + 'px');
					backgroundTile.style.height = ((document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight) + 'px');
				}

				popup.style.top = (64 + (document.documentElement && document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop)) + 'px';

			}, 500);

		} else {

			window.clearInterval(Designer.Help.IE6Interval);
		}
	},

	// Shows the help popup box with the given title and contents
	//     title - Title of the popup
	//     contents - Contents of the popup
	Show : function(title, contents) {

		Designer.Help.Initialise();

		var popup = $('help-popup');

		var html = '';

		html += '<div class="top"><div class="wrapper">';
		html +=   '<div class="title"><strong>Help: ' + title + '</strong></div>';
		html += '</div></div>';
		html += '<div class="middle"><div class="wrapper">';
		html +=   '<div class="contents">' + contents + '</div>';
		html += '</div></div>';
		html += '<div class="bottom"><div class="wrapper">';
		html +=   '<div class="close"><a href="/close" onclick="Designer.Help.Close(); return false;"><span>Close</span></a></div>';
		html += '</div></div>';

		/*@cc_on if(@_jscript_version < 5.7) {

			Designer.Help.IE6(true);

			html += '<div class="ie6bottom"><div class="wrapper"></div></div>';
		} @*/

		popup.innerHTML = html;

		Designer.Data.Visible = true;

		if (Designer.Help.Options.DisableInterface) {

			$('help-background-tile').style.display = 'block';
		}

		popup.style.display = 'block';
	},

	// Closes the active help popup
	Close : function() {

		/*@cc_on if(@_jscript_version < 5.7) Designer.Help.IE6(false); @*/

		$('help-popup').style.display = 'none';

		if (Designer.Help.Options.DisableInterface) {

			$('help-background-tile').style.display = 'none';
		}

		Designer.Data.Visible = false;
	},

	// Shows a help popup message with the selected instructions index
	ShowInsructions : function(index) {

		if ((index) &&
			(Designer.Help.Data.Instructions) &&
			(typeof (Designer.Help.Data.Instructions[index]) != 'undefined')) {

			Designer.Help.Show(
				Designer.Help.Data.Instructions[index].title,
				Designer.Help.Data.Instructions[index].contents);

			return true;

		} else {

			Designer.Help.Show('Not Available', 'We are sorry, but there is no help topic associated with this step.');
		}

		return false;
	}
};
