	
	var _areaId = '';
	var _overlay = false;
	var req = false;
	
	function showOverlay () {
		if (e = document.getElementById('overlay')) {
			_overlay = true;
			e.style.display = 'block';
			e.style.width = findWidth(document.body) + 'px';//'100%';
			e.style.height = (findHeight(document.body) > windowHeight() ? findHeight(document.body) : windowHeight()) + 'px';
		}
	}
	
	function hideOverlay () {
		_overlay = false;
		hideById('overlay');
	}
	
	function centerizeId (idName) {
		if (element = document.getElementById(idName)) {
			offset = findOffset();
			element.style.left = (offset[0] + (windowWidth() / 2) - (findWidth(element) / 2)) + 'px';
			element.style.top = (offset[1] + (windowHeight() / 2) - (findHeight(element) / 2)) + 'px';
		}
	}
	
	function showById (idName) {
		if (element = document.getElementById(idName)) {
			element.style.display = 'block';
		}
	}
	
	function hideById (idName) {
		if (element = document.getElementById(idName)) {
			element.style.display = 'none';
		}
	}
	
	function showMap (title, areaId, size) {
		// Show Overlay
		if (areaId != _areaId) {
			highlightRow(areaId);
		}
		
		// Map HTML
		var body = '<p><img src="maps/' + areaId + '/' + size + '/large.jpg" width="540" height="386" alt="Map of ' + title + '" /></p>';
		body += '<p>';
		for (i = 0; i < _sizes.length; i++) {
			_size = _sizes[i];
			if (i > 0)
				body += ', ';
			body += _size == size ? '<strong>1:' + size + 'K</strong>' : '<a href="#" onclick="return showMap(\'' + (title.replace(/\'/g,'\\\'')) + '\',\'' + areaId + '\',\'' + _size + '\')">1:' + _size + 'K</a>';
		}
		body += '</p>';
		body += '<p class="copyright centered">(c) Crown Copyright 2007. All rights reserved. Leicestershire County Council 100019271.</p>';
		title += ' (1:' + size + 'K)';
		showBox(title,body);
		return false;
	}
	
	function highlightRow (idName) {
		if (!_overlay && (e = document.getElementById('row-' + idName))) {
			if (idName != _areaId) {
				e.className = 'highlight';
				if (old = document.getElementById('row-' + _areaId)) {
					old.className = (old.rowIndex % 2 == 1) ? 'row1' : 'row2';
				}
				_areaId = idName;
			}
			
		}
		return;
	}	
	
	/*
		Thanks Phill
	*/
	function findOffset() {
		var x,y;
		if (window.pageYOffset) // all except Explorer
		{
			x = window.pageXOffset;
			y = window.pageYOffset;
		}
		else if (document.documentElement && document.documentElement.scrollTop)
			// Explorer 6 Strict
		{
			x = document.documentElement.scrollLeft;
			y = document.documentElement.scrollTop;
		}
		else if (document.body) // all other Explorers
		{
			x = document.body.scrollLeft;
			y = document.body.scrollTop;
		}
		return [x,y];
	}
	
	function findWidth(obj) {
		if (typeof( obj.innerWidth ) == 'number') {
			return obj.innerWidth;
		}
		else if (obj.clientWidth) {
			return obj.clientWidth;
		}
		return 0;
	}
	
	function findHeight(obj) {
		if (typeof( obj.innerHeight ) == 'number') {
			return obj.innerHeight;
		}
		else if (obj.clientHeight) {
			return obj.clientHeight;
		}
		return 0;
	}
	
	function windowWidth() {
		var w = 0;
		if( typeof( window.innerWidth ) == 'number' )
			w = window.innerWidth;
		else if (document.documentElement &&  document.documentElement.clientWidth)
			w = document.documentElement.clientWidth;
		else if(document.body && document.body.clientWidth)
			w = document.body.clientWidth;

		return w;
	}
	
	function windowHeight() {
		var h = 0;
		if( typeof( window.innerHeight ) == 'number' )
			h = window.innerHeight;
		else if (document.documentElement &&  document.documentElement.clientHeight)
			h = document.documentElement.clientHeight;
		else if(document.body && document.body.clientHeight)
			h = document.body.clientHeight;

		return h;
	}
	
	function showIA (baseUrl) {
		if (_areaId > '') {
			baseUrl += '?highlight=' + _areaId;
		}
		window.open(baseUrl, '_blank', 'width=960,height=720,top=25,left=50,location=no,menubar=no,scrollbars=no,resizable=yes,status=no,toolbar=no');
		return false;
	}
	
	function showChart (title, style, viewId, setId, sortedBy) {	
		switch (style) {
			default:
				swf = 'Bar2D';//'ScrollColumn2D';
			break;
			
			case 'pie':
				swf = 'Pie2D';
			break;
		}
		
		var chart = new FusionCharts("static/FusionCharts/Charts/" + swf + ".swf", "ChartId", "700", "400", "0", "0");
		chart.setDataURL(escape('http://www.lsr-online.org/stats/view/' + viewId + '/charts.xml?type=' + style + '&set=' + setId + '&sort=' + sortedBy + '&hl=' + _areaId));
		chart.render('lightbox-body');
		
		showBox (title);
		
	}
	
	function showBox (title, body) {
		if (lbox = document.getElementById('lightbox-wrap')) {
			lbox.scrollTop = 0;
			lbox.style.maxHeight = 600;
			if (windowHeight() < 700) {
				lbox.style.height = (windowHeight() - 150) + 'px';
			}
			lbox.style.overflowY = 'scroll';
		}
		document.getElementById('lightbox-header').innerHTML = (title != '') ? '<h2 class="heading">' + title + '</h2>' : '';
		if (body != undefined) {
			document.getElementById('lightbox-body').innerHTML = body;
		}
		if (document.getElementById('lightbox-footer').innerHTML == '') {
			document.getElementById('lightbox-footer').innerHTML = '<p class="centered"><a href="#" onclick="hideBox(); return false">[ Close ]</a></p>';
		}
		hideById('areas');
		showOverlay();
		showById('lightbox');
		
		centerizeId('lightbox');
	}
	
	function hideBox () {
		if (element = document.getElementById('areas')) {
			element.style.display = '';
		}
		hideById('lightbox');
		hideOverlay();
	}
	
	function showSet (setId) {
		url = 'stats/sets/' + setId + '/xml';
		
		if (window.XMLHttpRequest) {
			// Mozilla and Friends
			req = new XMLHttpRequest();
		}
		else if (window.ActiveXObject) {
			// Internet Explorer
			req = new ActiveXObject("Microsoft.XMLHTTP");
		}
		
		req.onreadystatechange = _showSet;
		req.open("GET", url, true);
		req.send(null);
	}
	
	function _showSet () {
		 // wait until the request is done
		if (req.readyState == 4) {
			// Make sure request came back OK
			if (req.status == 200) {
				//req.responseXML.getElementsByTagName('title')[0].firstChild.data
				showBox('Dataset Information', req.responseXML.getElementsByTagName('description')[0].firstChild.data);
			}
		}
	}
	
	function checkSelectedNodes () {
	
	}
	
	function toggleTree (idName, className) {
		if (tree = document.getElementById('tree' + idName)) {
			if (className != undefined) {
				tree.className = className;
			}
			else {
				tree.className = tree.className == 'open' ? 'closed' : 'open';
			}
		}
		return false;
	}
	
	function openTrees () {
		if (elements = document.getElementsByName('sets[]')) {
			for (i = 0; i < elements.length; i++) {
				input = elements[i];
				if (input.checked == true) {
					li = input.parentNode;
					while (li.parentNode && li.parentNode.parentNode && li.parentNode.parentNode.className == 'closed') {
						li = li.parentNode.parentNode;
						li.className = 'open';
					}
				}
			}
		}
	}
	
	function selectSets (element, selected) {
		
		if (list = element.parentNode.parentNode) {
			boxes = list.getElementsByTagName('INPUT');
			for (i=0; i < boxes.length; i++) {
				box = boxes[i];
				if (box.parentNode.parentNode == list) {
					box.checked = selected;
				}
			}
		}
		
		return false;
	}
	
	function selectAreas (selected) {
		if (areas = document.getElementById('areas')) {
			for(i = 0; i < areas.options.length; i++) {
				areas.options[i].selected = selected;
			}
		}
		return false;
	}
	