// JavaScript Document
// Northern Ireland Air Quality

var d = document;
try {
	document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}


//Code adapted from the example at from http://jennifermadden.com/javascript/stringEnterKeyDetector.html
function checkEnter(e){ //e is event object passed from function invocation
	var characterCode; //literal character code will be stored in this variable

	if(e && e.which){ //if which property of event object is supported (NN4)
		e = e
		characterCode = e.which //character code is contained in NN4's which property
	} else{
		e = event
		characterCode = e.keyCode //character code is contained in IE's keyCode property
	}

	if(characterCode == 13){ //if character code is equal to ascii 13 (if enter key)
		handle_map_zoom(document.getElementById('postcode').value);
		return false //return false to the event handler
	} else{
		return true //return true to the event handler
	} 

}

function handle_map_zoom(postcoderegion) {
		$.ajax({
		   type: "GET",
		   url: "ajax_process/change_map_zoom.php",
		   data: 'doajax=true&search_value='+postcoderegion,
		   success: function(returned_values){
				var return_tag = returned_values.substring(0, 5);
				var return_value = returned_values.substring(7);
				
				switch (return_tag) {
					case "COORD":
						var array_lat_long = return_value.split(',');
						d.getElementById('search_error').innerHTML = '';
						zoomTo(array_lat_long[0], array_lat_long[1], 11);
						break;
					default:
					case "ERROR":
						d.getElementById('search_error').innerHTML = '<br /><span class="HIGH red"><strong>'+return_value+'</strong></span>';
						break;
				}//end switch
		   }
		 });
	
}


function get_selected_siteid() {
	var site_id;
	
	if (location.href.lastIndexOf('?') !=-1) {
		var firstpos=location.href.lastIndexOf('?')+1;
		var lastpos=location.href.length;
		var query_string=location.href.substring(firstpos,lastpos);
		
		var arr_vals = query_string.split("&");
		
		for (i=0;i<arr_vals.length;i++) {
			arr_vals[i] = arr_vals[i].replace("&", "");
			arr_vals[i] = arr_vals[i].replace("?", "");

			if (arr_vals[i].indexOf("site_id=") > -1) {
				arr_vals[i] = arr_vals[i].replace("site_id=", "");
				site_id = arr_vals[i];
				break;
			}//end if
	
		}//end for
		
		return site_id;
	} else {
		//no query string
		return '';
	}//end if
	
}//end function

function insertResetLink() {
	d.getElementById('map_reset').innerHTML = '<a href="#" onclick="resetZoom();return false;" title="Reset Map"><img src="images/button_mapreset.gif" alt="Reset Map" border="0" /></a>';	
}

function switch_graph(site_id, g) {

		$.ajax({
		   type: "GET",
		   url: "ajax_process/switch_graph_type.php",
		   data: 'doajax=true&site_id='+site_id+'&g='+g,
		   beforeSend:	function(){
				$("#loading_content_message").show();
		   },
		   complete: function(){
				$("#loading_content_message").fadeOut("fast");
		   },
		   success: function(html){
				d.getElementById('tabContent').innerHTML = html;
		
		
			}
		 });
		
}//end function

function switch_statistics_content(site_id, year, param) {
		
		$.ajax({
		   type: "GET",
		   url: "ajax_process/switch_statistics_content.php",
		   data: 'doajax=true&site_id='+site_id+'&year='+year+'&param='+param,
		   beforeSend:	function(){
				$("#loading_content_message").show();
		   },
		   complete: function(){
				$("#loading_content_message").fadeOut("fast");
		   },
		   success: function(html){
				d.getElementById('statistics_content').innerHTML = html;
		
		
			}
		 });
}

function resetFilterSites() {
		var checkArray = d.getElementsByTagName('input');
		
		//Loop through checkboxes and reset
		for(var g=0;g<checkArray.length;g++) {
			if (checkArray[g].type == 'checkbox') {
				checkArray[g].checked = true;				
			}
		}
		
		//Show all markers
		for(id in aql_markers) {
			aql_markers[id].show();
		}
}

function changeSwitcher(turnOn) {
	var switcher_areas = Array('switcherFilters', 'switcherKey', 'switcherPostcode');
	
	//Loop through and turn off all layers
	for(var t=0;t<switcher_areas.length;t++) {
		d.getElementById(switcher_areas[t]).style.display = 'none';
		d.getElementById("li_"+switcher_areas[t]).className = '';
	}
	
	d.getElementById(turnOn).style.display = 'block';
	d.getElementById("li_"+turnOn).className = 'selected';
	
}

function switchSummaryForecast(type, ag) {

		$.ajax({
		   type: "GET",
		   url: "ajax_process/switch_summary_forecast.php",
		   data: 'doajax=true&view='+type+'&ag='+ag,
		   beforeSend:	function(){
				$("#loading_content_message2").fadeIn("fast");
		   },
		   complete: function(){
				$("#loading_content_message2").fadeOut("fast");
		   },
		   success: function(html){
				d.getElementById('summary_forecast_area').innerHTML = '';
				d.getElementById('summary_forecast_area').innerHTML = html;
		
			}
		 });
}

function show_hide_exceedence_statistics(exarea) {

	for (var u=1;u<10;u++) {
		if (d.getElementById('exstat_'+u)) {
			$("#exstat_"+u).slideUp("fast");
		}
		
	}

	$("#"+exarea).slideDown("fast");
	
}

function showExtraContent(content_id) {
	var mode = '';
	
	if (d.getElementById(content_id).style.display == 'block' ) {
		mode = 'hide';
	} else {
		mode = 'show';
	}

	switch (mode) {
		case "show":
			$("#"+content_id).slideDown("fast");
			break;
		case "hide":
			$("#"+content_id).slideUp("fast");
			break;
		
	}
	

}

function getGlossaryDefinition(g_id) {
	//No loading message for this AJAX request

	//Only do the AJAX request if needed
	if (d.getElementById('glossaryterm'+g_id).style.display == 'none') {
		$.ajax({
		   type: "GET",
		   url: "ajax_process/show_glossary_definition.php",
		   data: 'doajax=true&g_id='+g_id,
		   success: function(html){
				d.getElementById('glossaryterm'+g_id).innerHTML = html;
				showExtraContent('glossaryterm'+g_id);
		
			}
		 });
	
	} else {
		showExtraContent('glossaryterm'+g_id);
	}
	
}