﻿//<reference path="loader.js"/>
//<reference path="jquery/jQueryComments.js"/>
/* **********************************************************/
/* This script file defines functions used by the glossary  */
/*                                                          */
/* **********************************************************/
function CGlossary() { }
CGlossary.prototype =
{
	/* ************************************ */
	/* Properties                           */
	/* ************************************ */
	_glossaryOpen: false,
	_jContainer: null,
	_jLinkToggle: null,
	_jImgArrow: null,
	_jFullGlossary: null,
	_jLinkFullGlossary: null,

	/* ************************************ */
	/* Private Methods                      */
	/* ************************************ */
	_resetContainer: function (sSpeed) {
		if (typeof (sSpeed) == "undefined" || !sSpeed) sSpeed = "normal";

		//remove existing contents
		this._jContainer.slideUp(
			sSpeed,
			function () {
				gGlossary._jContainer.empty();
				//setup 'loading' placeholder
				gGlossary._jContainer.html("<div style=\"width:100%;text-align:center;\"><img src=\"/images/5l-loading.gif\" /></div>");
			}
		);

		//show the right arrow
		//this._jImgArrow.css("background-image", "url(\"/images/linkArrowOnWhite.png\")");
	},
	_preloadContainer: function () {
		this._jContainer.slideDown("normal");
		//this._jImgArrow.css("background-image", "url(\"/images/linkArrowOnWhite.png\")");linkArrowOnWhite_Down.png
	},
	/* ************************************ */
	/* Public Methods                       */
	/* ************************************ */
	AttachEvents: function () {
		this._jLinkToggle.attr("href", "javascript:void(false)").attr("target", "");
		this._jLinkToggle.click(function () { gGlossary.ToggleGlossary(); });
	},
	Initialise: function () {
		this._jContainer = jQuery("#divGlossary");
		this._jLinkToggle = jQuery("#ancGlossaryLink");
		this._jImgArrow = jQuery("#divGlossaryLink");
		//this._jFullGlossary = jQuery("#divFullGlossaryContainer");
		this._jLinkFullGlossary = jQuery("#lnkFullGlossary");
		this._glossaryOpen = false;

		//Hide any previously loaded glossary content
		this._resetContainer();

		this.AttachEvents();
	},
	ToggleGlossary: function () {
		//set a flag indicating whether glossary is currently open
		this._glossaryOpen = !(this._glossaryOpen);

		//if open then hide and set image to right else populate glossary and set image to down arrow
		if (!this._glossaryOpen) gGlossary.HideGlossary();
		else gGlossary.PageGlossaryPopulate();
	},
	PageGlossaryPopulate: function () {
		this._resetContainer();
		this._preloadContainer();

		var webserviceMethodLocation = "/webservices/Glossary.asmx/GetGlossary";
		var pageDetail = { bodyHtml: "<div>" + jQuery("#divContentContainer").html().replace(/<\/?[^>]+>/gi, ' ').replace(/&nbsp;/gi, ' ').replace(/\n/gi, ' ').replace(/\t/gi, ' ') + "</div>" }; //.replace("/g", "\\\"")

		jQuery.ajax({
			type: "POST",
			url: webserviceMethodLocation,
			data: pageDetail, // Set Method Params
			cache: false,
			dataType: "xml",
			success: this.DisplayGlossary,
			error: this.DisplayError
		});
	},
	DisplayGlossary: function (result) {
		try {
			//remove the 'loading' message
			gGlossary._jContainer.empty();

			// result contains a json string of term:definition pairs where term is contained in the text of the body passed to webmethod
			if (result.documentElement.textContent == null) gGlossary._jContainer.html(result.documentElement.text);
			else gGlossary._jContainer.html(result.documentElement.textContent);
			gGlossary._jLinkFullGlossary.click(function () { gGlossary.ShowFullGlossary(); });
			gGlossary._jContainer.slideDown("slow");
			gGlossary._jImgArrow.css("background-image", "url(\"/images/linkArrowOnWhite_Down.png\")");
			gGlossary._jImgArrow.children("a").css("color", "#393939");
			gGlossary._glossaryOpen = true;
		}
		catch (e) {
			gGlossary._glossaryOpen = !this._glossaryOpen;
			gGlossary.HideGlossary();
		}
	},
	HideGlossary: function () {
		this._resetContainer("slow");
		this._jImgArrow.css("background-image", "");
		gGlossary._jImgArrow.children("a").css("color", "");
		gGlossary._glossaryOpen = false;
	},
	ShowFullGlossary: function (result) {
		if (this._jFullGlossary == null || jQuery("#divFullGlossaryContainer") == null || jQuery("#divFullGlossaryContainer").length == 0) {
			jQuery("body").append("<div id=\"divFullGlossaryContainer\"></div>");
			this._jFullGlossary = jQuery("#divFullGlossaryContainer");
			this._jFullGlossary.load("/Glossary #divFullGlossary", function () {
				jQuery("#closeWindow").attr("href", "javascript:void(false)").attr("target", "");
				jQuery("#closeWindow").click(function () { gGlossary.HideFullGlossary(); });
			});
			jQuery(".popupBackground").css("height", jQuery("body").css("height"));
			this._jFullGlossary.show();
		}
		else {
			this._jFullGlossary.show();
		}


	},
	HideFullGlossary: function (result) {
		this._jFullGlossary.hide();
	},
	DisplayError: function (result) {
		alert("error:" + result);
	}
}
var gGlossary = null;

jQuery(
    function () {
    	if ((jQuery("#divGlossary") != null && jQuery("#divGlossary").length > 0)) {
    		jQuery("#ancGlossaryLink").text("Glossary of terms on this page");
    		gGlossary = new CGlossary();
    		gGlossary.Initialise();

    	}
    }
);

    


