// **************************************************
// ***** BEG GLOBAL
// **************************************************

// Initialize namespaces
if (typeof TGN == 'undefined') { var TGN = {}; }
if (typeof TGN.Ancestry == 'undefined') { TGN.Ancestry = {}; }
if (typeof TGN.Ancestry.Search == 'undefined') { TGN.Ancestry.Search = {}; }
if (typeof TGN.Ancestry.Search.UCPage == 'undefined' ) { TGN.Ancestry.Search.UCPage = {}; }

TGN.Ancestry.Search.UCPage.ResourcesLoaded = false;
TGN.Ancestry.Search.UCPage.HistoryLoaded = false;

TGN.Ancestry.Search.UCPage.ParseResponse = function(theResponse)
{
	var bi, ei, theText;
	bi = theResponse.indexOf("<!--UCPAGE_BEG-->");
	ei = theResponse.indexOf("<!--UCPAGE_END-->");
	theText = theResponse.substring(bi + 17, ei);
	return theText;
};

TGN.Ancestry.Search.UCPage.SetScripts = function(el)
{
	var i, theText;
	var x = el.getElementsByTagName("script");
	var length = x.length;
	if (length > 0)
	{
		for (i = 0; i < length; i++)
		{
			theText = x[i].text;

			if ('undefined' != typeof theText && 'undefined' != theText && theText.length > 0)
			{
				var jsNode = document.createElement("script");
				jsNode.setAttribute("language", "text/javascript");
				jsNode.setAttribute("type", "text/javascript");
				jsNode.text = theText;
				el.appendChild(jsNode);
			}
		}
	}
};

TGN.Ancestry.Search.UCPage.GetHistory = function (values)
{
    var el = YAHOO.util.Dom.get('divHistoryTabData');
	var loadImg = YAHOO.util.Dom.get('divLoadImageHistory');
	
	if (TGN.Ancestry.Search.UCPage.ResourcesLoaded)
	{
		return;
	}
	
    var it =
	{
	    handleSuccess: function (o)
	    {
	        var oldText, newText, newNode;

	        oldText = o.responseText;

	        loadImg.style.visibility = 'hidden'; loadImg.style.display = 'none';
	        newText = TGN.Ancestry.Search.UCPage.ParseResponse(oldText);
	        newNode = document.createElement('div');
	        newNode.innerHTML = newText;
	        el.appendChild(newNode);
	        TGN.Ancestry.Search.UCPage.SetScripts(el);
			TGN.Ancestry.Search.UCPage.ResourcesLoaded = true;
	    },

	    handleFailure: function (o)
	    {
	        // alert('failed');
	    },

	    startRequest: function (o)
	    {
	        // Sample URL: http://search.ancestrydev.com/handler/ucpage.aspx?values=47&control=places-history
	        var url = "http://" + searchDomain + "/handler/ucpage.aspx?values=" + values + "&control=places-history";
	        YAHOO.util.Connect.asyncRequest('GET', url, callback, '');
	    }
	};

    var callback =
	{
	    success: it.handleSuccess, failure: it.handleFailure, scope: it
	};
	
    it.startRequest();
};

TGN.Ancestry.Search.UCPage.GetResources = function (values)
{
    var el = YAHOO.util.Dom.get('divResourcesTabData');
	var loadImg = YAHOO.util.Dom.get('divLoadImageResources');
	
	if (TGN.Ancestry.Search.UCPage.HistoryLoaded)
	{
		return;
	}
	
    var it =
	{
	    handleSuccess: function (o)
	    {
	        var oldText, newText, newNode;

	        oldText = o.responseText;

	        loadImg.style.visibility = 'hidden'; loadImg.style.display = 'none';
	        newText = TGN.Ancestry.Search.UCPage.ParseResponse(oldText);
	        newNode = document.createElement('div');
	        newNode.innerHTML = newText;
	        el.appendChild(newNode);
	        TGN.Ancestry.Search.UCPage.SetScripts(el);
			TGN.Ancestry.Search.UCPage.HistoryLoaded = true;
	    },

	    handleFailure: function (o)
	    {
	        // alert('failed');
	    },

	    startRequest: function (o)
	    {
	        // Sample URL: http://search.ancestrydev.com/handler/ucpage.aspx?values=47&control=places-resources
	        var url = "http://" + searchDomain + "/handler/ucpage.aspx?values=" + values + "&control=places-resources";
	        YAHOO.util.Connect.asyncRequest('GET', url, callback, '');
	    }
	};

    var callback =
	{
	    success: it.handleSuccess, failure: it.handleFailure, scope: it
	};
	
    it.startRequest();
};
