// JavaScript Document (AEA)
// For Surrey Heath Air Quality pages
var d = document;
try {
	document.execCommand('BackgroundImageCache', false, true);
} catch(e) {}


//Function to change the Google Map Zoom level
function change_zoom_view() {
	if(document.getElementById('zoom_view_button').src.indexOf('button_zoomtonearbylevels') > 0) {
		//Zoom to Surrey Heath
		document.getElementById('button_nearby').innerHTML = '<a href="#" onclick="zoomTo(51.324008,-0.679283, 11);change_zoom_view();return false;"><img id="zoom_view_button"  src="images/button_zoomtosurreyheath.png" alt="Change Current View" border="0" /></a>';
	} else {
		//Zoom out
		document.getElementById('button_nearby').innerHTML = '<a href="#" onclick="zoomTo(51.361599,-0.691452, 9);change_zoom_view();return false;"><img id="zoom_view_button"  src="images/button_zoomtonearbylevels.png" alt="Change Current View" border="0" /></a>';
	}//end if
}//end function


//Function switch between site photos
function change_site_image(filename, title) {
	document.getElementById('large_site_image').src = filename;	
	document.getElementById('photo_title').innerHTML = title;	
	
}//end function


//function to get the query string
function get_selected_view() {
	var query_string;
	
	if (location.href.lastIndexOf('?') !=-1) {
		firstpos=location.href.lastIndexOf('?')+1;
		lastpos=location.href.length;
		query_string=location.href.substring(firstpos,lastpos);
	} else {
		query_string = 't_action=data';
	}//end if
	
	
	if (query_string.indexOf('t_action=data') > -1) {
		return "data";
	}//end if
	if (query_string.indexOf('t_action=graph') > -1) {
		return "graph";
	}//end if
	if (query_string.indexOf('t_action=sitedetails') > -1) {
		return "sitedetails";
	}//end if
	if (query_string.indexOf('t_action=sitephotos') > -1) {
		return "sitephotos";
	}//end if
	
	//default
	return "data";
	
}//end function

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 get_selected_sitetype() {
	var site_type = '';
	
	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_type=") > -1) {
				arr_vals[i] = arr_vals[i].replace("site_type=", "");
				site_type = arr_vals[i];
				break;
			}//end if
	
		}//end for
		
		if (site_type == '') {
			site_type = 'aql';		
		}//end if
		return site_type;
	} else {
		//no query string
		return '';
	}//end if
	
}//end function




function change_latest_data_panel(site_id, data) {

	d.getElementById('site_data').innerHTML = data;
		
}//end function

function process_tab_change_request(site_id, selected_view, site_type) {
	//As we have loaded the Google Maps API - use the AJAX function.
	GDownloadUrl("ajax_process/marker_onclick_process.php?site_id="+site_id+"&t_action="+selected_view+"&site_type="+site_type, function(data, responseCode) {
	// To ensure against HTTP errors that result in null or bad data,
	// always check status code is equal to 200 before processing the data
	// alert("response "+responseCode);
	d.getElementById('site_data').innerHTML = '<p>Loading...</p>';
	
	if(responseCode == 200) {
		//change content panel tabs
		change_latest_data_panel(site_id, data);
	} else if(responseCode == -1) {
		alert("Sorry,  timeout");
		//If timeout, attempt to load the page manually
		document.location="index.php?site_id="+site_id+"&t_action="+selected_view+"&site_type="+site_type;
	} else { 
		alert("Sorry, an error occurred.");
		//If misc error, attempt to load the page manually
		document.location="index.php?site_id="+site_id+"&t_action="+selected_view+"&site_type="+site_type;
	}
   });//end function
	
}//end function

function changeKidsContent(pageid, itemid, itemlabel) {
	
	if (!itemlabel) {
		itemlabel = null;	
	}
	
	$.ajax({
	   type: "GET",
	   url: "../ajax_process/change_content.php",
	   data: 'doajax=true&pageid='+pageid,
	   success: function(html){
			$("#pageContentText").html(html);
			$("#pageArrow").attr("class", "item"+itemid);
			$("#kidsSideNav li a").removeAttr("id");

			if ($("#kidsSideNav").attr("class").indexOf("pollutants") > -1) {
				$("#kidsSideNav").removeClass().addClass("pollutants "+itemlabel);
				$("#babyPollutant").removeClass().addClass(itemlabel+"bottom");
			}
			
			$("#liSubNav"+itemid+" a").attr("id", "currentSub");



		}
	 });
	
}
