var helpViewerStandAlone = false;

function MoreHelpClick()
{
	$("div[id$=PanelHelp]").hide();
	RenderHelpViewer();
	GetHelp("001", "");
}

function GetHelp(topicId, question)
{
	if (topicId.indexOf(".aspx") != -1)
	{
	    if (topicId.indexOf("Feedback.aspx") !=  -1)
	        location = "mailto:webmaster@photoreflect.com?subject=Customer Feedback";
	    else
	        location = topicId;
		return;
	}

	var viewer = $("div[id$=HelpViewer]");

	if (!helpViewerStandAlone)
	{
		$.blockUI
		(
			{
				message: viewer,
				css:
				{
					margin: "4em",
					width: "720px",
					marginLeft: "-360px",
					marginTop: "-200px"
				}
			}
		);
		
		viewer.parent().block
		(
			{
				message: String.format("<img src='{0}' alt='' align='absmiddle' />", "/pr3/images/ajax-loading.gif")
			}
		);

		var page = $("div.blockPage");
		var help = $("div#HelpViewer");

		if ($.browser.msie == false)
		{
		    page.css("-webkit-border-radius","10px");
		    page.css("-moz-border-radius","10px");
		    help.css("-webkit-border-radius","10px");
		    help.css("-moz-border-radius","10px");

	    }
	}
	
	$.get
	(
		"/pr3/AjaxData.asmx/GetHelp",
		{
			"topicId": topicId
		},
		function(data)
		{
			var xml = "";
			if (data.text && data.text.length > 0)
				xml = data.text;
			else if (data.childNodes)
			{
				$.each
				(
					data.childNodes,
					function(i, item)
					{
						xml = item.textContent;
					}
				);
			}

			var help = JSON.parse(xml);
			RenderHelpContent(help, question);
		}
	);
}

function HelpInit()
{
	$("a[id$=HyperLinkMoreHelp]").attr("href", "javascript:MoreHelpClick()");
	$("input[id$=ButtonCloseHelp]").click
	(
		function()
		{
			$('div[id$=PanelHelp]').slideToggle();
			return false;
		}
	);
}

$(document).ready(HelpInit);

function RenderHelpViewer(standAlone)
{
	helpViewerStandAlone = standAlone;
	viewer = $("div[id$=HelpViewer]");
		
	if (viewer.length > 0)
		return;

	viewer = $(document.createElement("div")).attr("class", "help_white");
	viewer[0].id = "HelpViewer";
	
	$("body").append(viewer);
	var container = $(document.createElement("center"));
    container.addClass ("helpContainer");
	$("body").append(container);
}

function RenderHelpContent(help, question)
{
	var viewer = $("div[id$=HelpViewer]");
	viewer.empty();

	var container = $(document.createElement("center"));
	container.addClass ("helpContainer");
	viewer.append(container);

	// begin breadcrumb
	var breadcrumb = $(document.createElement("div")).attr("id", "breadcrumb");
	breadcrumb.addClass ("helpCrumb");
	container.append(breadcrumb);
	
	for (var i = 0; i < help.Breadcrumb.length; i++)
	{
		var topic = help.Breadcrumb[i];
		
		if (i < help.Breadcrumb.length - 1)
		{
			var a = $(document.createElement("a"));
			a.attr("href", String.format("javascript:GetHelp('{0}');", topic.Id));
			a.text(topic.Title);
			
			breadcrumb.append(a);
		}
		else
			breadcrumb.append(topic.Title);
		
		if (i < help.Breadcrumb.length - 1)
			breadcrumb.append(" &gt; ");
	}
	// end breadcrumb
	
	if (!helpViewerStandAlone)
	{
		// begin close link
		var closeLink = $(document.createElement("a")).text(L10n.Global.Close.toLowerCase()).attr("href", "javascript:if (false) {}");
		closeLink.addClass ("helpClose");
		//closeLink[0].style.position = "absolute";
		//closeLink[0].style.top = "1em";
		//closeLink[0].style.right = "1em";
		closeLink.click
		(
			function()
			{
				if (!helpViewerStandAlone)
					$.unblockUI();
			}
		);
		container.append(closeLink);
		// end close link
	}
	
	// begin question
	if (question && question.length > 0)
	{
		var questionDiv = $(document.createElement("div")).append(question);
		questionDiv[0].style.fontWeight = "bold";
		container.append(questionDiv);
	}
	// end question
	
	// begin topic text
	var textDiv = $(document.createElement("div"));
	textDiv.append(help.Text);
	container.append(textDiv);
	// end topic text

	if (help.Id != "002004") // hide self referencing option
	{
		// begin options
		var options = $(document.createElement("table"));
		container.append(options);
		
		for (var i = 0; i < help.Options.length; i++)
		{
			var option = help.Options[i];

			if (help.Id == "001" && (help.Id != "001" || option.Yes == "001"))
				continue;

			if (option.Yes == "002004" && (help.Text.length === 0 || option.Yes != "002004"))
				continue;

			var tr = $(document.createElement("tr"));
			options.append(tr);

			var index = options.find("tr").length;
			var numTd = $(document.createElement("td")).append(index).append(".");
			tr.append(numTd);

			var textTd = $(document.createElement("td"));
			textTd[0].style.paddingLeft = "1em";
			textTd.append(option.Text);
			tr.append(textTd);

			var yesTd = $(document.createElement("td"));
			yesTd[0].style.paddingLeft = "1em";
			tr.append(yesTd);
			var yesButton = $(document.createElement("input")).attr("type", "button").attr("id", option.Yes).attr("name", "").click
			(
				function()
				{
					GetHelp(this.id, $(this).attr("name"));
				}
			);

			if (option.Yes != "001")
				yesButton.attr("name", option.Text);

			yesButton.val(L10n.Global.Yes);
			yesTd.append(yesButton);

			var noTd = $(document.createElement("td"));
			noTd[0].style.paddingLeft = "1em";
			tr.append(noTd);

			if (option.No.length === 0)
				continue;

			if (option.No == "close" && helpViewerStandAlone)
				continue;

			var noButton = $(document.createElement("input")).attr("type", "button").click
			(
				function()
				{
					if (option.No == "close")
						closeLink.click();
					else
						GetHelp(option.No, "");
				}
			);
			noButton.val(L10n.Global.No);
			noTd.append(noButton);
		}
		// end options
	}
	
	// fix up the buttons
	$("div[id$=HelpViewer] input[type=button]").each
	(
		function()
		{
			this.style.paddingLeft = ".5em";
			this.style.paddingRight = ".5em";
		}
	);

	if (!helpViewerStandAlone)
		viewer.parent().unblock();
	else
		viewer.show();
}
