var blockGetSearchResults = false;
var blockGetWorkInfo = false;
var currentHash = window.location.hash;
var hashTimer;

addEvent(window,"load",initpage);

function initpage()
{
	inittree(true);
}

function inittree(pageload) 
{
	if (pageload)
	{
		// get original values to see if reload is needed on initpage (pageload = true)
		wtListO = document.TreeForm.wt_o.value;
		gtiListO = document.TreeForm.gti_o.value;
		ctListO = document.TreeForm.ct_o.value;
		wIdO = document.TreeForm.w_o.value;
	}
	else
	{
		// get previous values to see if reload is needed on hash change (pageload = false)
		wtListO = document.TreeForm.wt.value;
		gtiListO = document.TreeForm.gti.value;
		ctListO = document.TreeForm.ct.value;
		wIdO = document.TreeForm.w.value;
	}

	// get the state from the hash
	restoreState();

	// new values
	wtList = document.TreeForm.wt.value;
	gtiList = document.TreeForm.gti.value;
	ctList = document.TreeForm.ct.value;
	wId = document.TreeForm.w.value;

	// stop the refresh code while we set the tree state
	blockGetSearchResults = true;
	blockGetWorkInfo = true;

	// reset the trees
	clearWT();
	clearGTI();
	clearCT();
	
	for (i = 1; i <= listLen(wtList); i++)
	{
		x = listGetAt(wtList, i);
		addWT(x);
		expandToItem('treebrowser', 'wt_' + x);
	}
	
	for (i = 1; i <= listLen(gtiList); i++)
	{
		x = listGetAt(gtiList, i);
		addGTI(x);
		expandToItem('treebrowser', 'gti_' + x);
	}
	
	for (i = 1; i <= listLen(ctList); i++)
	{
		x = listGetAt(ctList, i);
		addCT(x);
		expandToItem('treebrowser', 'ct_' + x);
	}

	if (wtList != wtListO || gtiList != gtiListO || ctList != ctListO)
		blockGetSearchResults = false;
		
	if (wId != wIdO)
		blockGetWorkInfo = false;

	getSearchResults(true);

	blockGetSearchResults = false;
	blockGetWorkInfo = false;
	
	hashTimer = setTimeout("checkForHashChange()",200);
}

function removeGeoTag(GeoTagLinkId)
{
	new Ajax.Request(urlbase + '/cfc/Tags.cfc', 
		{
			method: 'post',
			parameters: { method: 'removeGeoTag', GeoTagLinkId: GeoTagLinkId, FormatType: 'browser' },
			onComplete: function(transport)
				{
					$('GeoTags').innerHTML = transport.responseText;
					$('GeoTagEntryBox').value = '';
					$('GeoTagEntryBox').focus();
				}
		});
}

function createUUID() 
{
    // http://www.ietf.org/rfc/rfc4122.txt
    var s = [];
    var hexDigits = "0123456789ABCDEF";
    for (var i = 0; i < 32; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1);
    }
    s[12] = "4";  // bits 12-15 of the time_hi_and_version field to 0010
    s[16] = hexDigits.substr((s[16] & 0x3) | 0x8, 1);  // bits 6-7 of the clock_seq_hi_and_reserved to 01

    var uuid = s.join("");
    return uuid;
}

function checkForHashChange()
{
	if (window.location.hash != currentHash)
	{
		currentHash = window.location.hash;
		inittree(false);
	}
	hashTimer = setTimeout("checkForHashChange()",200);
}

function restoreState()
{
	fieldsRE = /#(.*)_(.*)_(.*)_(.*)/;
	fieldsSpec = window.location.hash;

	if (fieldsSpec == '')
	{
		document.TreeForm.wt.value = document.TreeForm.wt_o.value;
		document.TreeForm.gti.value = document.TreeForm.gti_o.value;
		document.TreeForm.ct.value = document.TreeForm.ct_o.value;
		document.TreeForm.w.value = document.TreeForm.w_o.value;
	}
	else if (!fieldsRE.test(fieldsSpec)) 
	{
		return; // ignore url if invalid
	}
	else
	{
		document.TreeForm.wt.value = fieldsSpec.replace(fieldsRE, "$1");
		document.TreeForm.gti.value = fieldsSpec.replace(fieldsRE, "$2");
		document.TreeForm.ct.value = fieldsSpec.replace(fieldsRE, "$3");
		document.TreeForm.w.value = fieldsSpec.replace(fieldsRE, "$4");
	}
}

function saveCurrentState()
{
	var hash;

	var wtList = document.TreeForm.wt.value;
	var gtiList = document.TreeForm.gti.value;
	var ctList = document.TreeForm.ct.value;

	var wtListO = document.TreeForm.wt_o.value;
	var gtiListO = document.TreeForm.gti_o.value;
	var ctListO = document.TreeForm.ct_o.value;

	var wId = document.TreeForm.w.value;
	var wIdO = document.TreeForm.w_o.value;

	if (wtList == wtListO && gtiList == gtiListO && ctList == ctListO && wId == wIdO)
		hash = '';
	else
		hash = wtList + '_' + gtiList + '_' + ctList + '_' + wId;
		
	window.location.hash = hash;
	currentHash = window.location.hash;
}

function getSearchResults(RefreshWork)
{
	var RefreshWork = (RefreshWork == null) ? false : true;
	
	if (!blockGetSearchResults)
	{
		a = document.getElementById('searchresults');
		a.innerHTML = '<div style="padding-top: 100px; padding-bottom: 100px; text-align: center; vertical-align: middle;"><b>Loading</b><br /><br /><br /><img src="images/loading.gif" alt="Loading" /></div>';
		
		new Ajax.Updater('searchresults', urlbase + '/cfc/ResourceBrowser.cfc', 
						 {
							method: 'post',
							evalScripts: true,
							parameters: $('TreeForm').serialize(true),
							onComplete: function(transport)
								{
									if (RefreshWork)
									{
										selectWork(document.TreeForm.w.value, true);
									}
									saveCurrentState();
								}
						 });
	}
	else
	{
		if (RefreshWork)
		{
			selectWork(document.TreeForm.w.value, true);
		}
	}
}

function getWorkInfo()
{
	if (!blockGetWorkInfo)
	{
		var a = document.getElementById('workinfo');
		
		if (document.TreeForm.w.value == '')
		{
			a.innerHTML = '<div style="padding-top: 100px; padding-bottom: 100px; text-align: center; vertical-align: middle;"><b>Select a resource.</b><br /><br /><br /></div>';
		}
		else
		{
			a.innerHTML = '<div style="padding-top: 100px; padding-bottom: 100px; text-align: center; vertical-align: middle;"><b>Loading</b><br /><br /><br /><img src="images/loading.gif" alt="Loading" /></div>';
	
			new Ajax.Updater('workinfo', urlbase + '/cfc/ResourceBrowser.cfc',
							 {
								 method: 'post',
								 evalScripts: true,
								 parameters: { method: 'outputWorkInfo',
											   w: document.TreeForm.w.value },
								onComplete: function(transport)
									{
										saveCurrentState();
									}
							 });
		}
	}
}
							 			   

function selectWork(WorkId, Scroll)
{
	var Scroll = (Scroll == null) ? false : true;

	atr = document.getElementById('searchresults').getElementsByTagName('tr');
	for (i = 0; i < atr.length; i++)
	{
		atr[i].className = 'resitem';
	}

	tr = document.getElementById('tr_w_' + WorkId);
	if (tr != null) 
	{
		tr.className = 'resitemsel';
		if (Scroll) tr.scrollIntoView();
	}
	else
	{
		if (Scroll && $('criteria')) $('criteria').scrollIntoView();
	}
	
	document.TreeForm.w.value = WorkId;
	getWorkInfo();
}

function addWT(wt2)
{
	if (listFind(document.TreeForm.wt.value, wt2) == 0)
	{
		a = document.getElementById('a_wt_' + wt2);
		if (a != null)
		{
			document.TreeForm.wt.value = listAppend(document.TreeForm.wt.value, wt2);
			a.className = 'treelinksel';
			refreshWTall();
			getSearchResults();
		}
	}
}

function toggleWT(wt2)
{
	var x = listFind(document.TreeForm.wt.value, wt2)
	if (x == 0)
	{
		document.TreeForm.wt.value = listAppend(document.TreeForm.wt.value, wt2);

		a = document.getElementById('a_wt_' + wt2);
		a.className = 'treelinksel';
	}
	else
	{
		document.TreeForm.wt.value = listDeleteAt(document.TreeForm.wt.value, x);
		
		a = document.getElementById('a_wt_' + wt2);
		a.className = 'treelink';
	}

	refreshWTall();
	getSearchResults();
}

function addCT(ct2)
{
	if (listFind(document.TreeForm.ct.value, ct2) == 0)
	{
		a = document.getElementById('a_ct_' + ct2);
		if (a != null)
		{
			document.TreeForm.ct.value = listAppend(document.TreeForm.ct.value, ct2);
			a.className = 'treelinksel';
			refreshCTall();
			getSearchResults();
		}
	}
}

function toggleCT(ct2)
{
	var x = listFind(document.TreeForm.ct.value, ct2)
	if (x == 0)
	{
		document.TreeForm.ct.value = listAppend(document.TreeForm.ct.value, ct2);

		a = document.getElementById('a_ct_' + ct2);
		a.className = 'treelinksel';
	}
	else
	{
		document.TreeForm.ct.value = listDeleteAt(document.TreeForm.ct.value, x);
		
		a = document.getElementById('a_ct_' + ct2);
		a.className = 'treelink';
	}

	refreshCTall();
	getSearchResults();
}

function addGTI(gti2)
{
	if (listFind(document.TreeForm.gti.value, gti2) == 0)
	{
		a = document.getElementById('a_gti_' + gti2);
		if (a != null)
		{
			document.TreeForm.gti.value = listAppend(document.TreeForm.gti.value, gti2);
			a.className = 'treelinksel';
			refreshGTIall();
			getSearchResults();
		}
	}		
}

function toggleGTI(gti2)
{
	var x = listFind(document.TreeForm.gti.value, gti2)
	if (x == 0)
	{
		document.TreeForm.gti.value = listAppend(document.TreeForm.gti.value, gti2);

		a = document.getElementById('a_gti_' + gti2);
		a.className = 'treelinksel';
	}
	else
	{
		document.TreeForm.gti.value = listDeleteAt(document.TreeForm.gti.value, x);
		
		a = document.getElementById('a_gti_' + gti2);
		a.className = 'treelink';
	}

	refreshGTIall();
	getSearchResults();
}

function refreshWTall()
{
	a = document.getElementById('a_wt_all');

	if (listLen(document.TreeForm.wt.value) == 0)
	{
		a.className = 'treelinksel';
		a.innerHTML = 'viewing all';
	}
	else
	{
		a.className = 'treelink';
		a.innerHTML = 'view all';
	}
}

function refreshCTall()
{
	a = document.getElementById('a_ct_all');

	if (listLen(document.TreeForm.ct.value) == 0)
	{
		a.className = 'treelinksel';
		a.innerHTML = 'viewing all';
	}
	else
	{
		a.className = 'treelink';
		a.innerHTML = 'view all';
	}
}

function refreshGTIall()
{
	a = document.getElementById('a_gti_all');

	if (listLen(document.TreeForm.gti.value) == 0)
	{
		a.className = 'treelinksel';
		a.innerHTML = 'viewing all';
	}
	else
	{
		a.className = 'treelink';
		a.innerHTML = 'view all';
	}
}

function clearWT()
{
	document.TreeForm.wt.value = '';
	
	ar = document.getElementById('wt_0').getElementsByTagName('a');
	for (i = 0; i < ar.length; i++)
	{
		ar[i].className = 'treelink';
	}
	
	refreshWTall();
	getSearchResults();
}

function clearCT()
{
	document.TreeForm.ct.value = '';
	
	ar = document.getElementById('ct_0').getElementsByTagName('a');
	for (i = 0; i < ar.length; i++)
	{
		ar[i].className = 'treelink';
	}
	
	refreshCTall();
	getSearchResults();
}

function clearGTI()
{
	document.TreeForm.gti.value = '';

	ar = document.getElementById('gti_0').getElementsByTagName('a');
	for (i = 0; i < ar.length; i++)
	{
		ar[i].className = 'treelink';
	}
	
	refreshGTIall();
	getSearchResults();
}
