//Invoke new page
function tabClick(tab) {
	obj=document.getElementById('datatable');
	obj.style.display = 'none';
	obj=document.getElementById('photosets');
	obj.style.display = 'none';
	obj=document.getElementById('graphs');
	obj.style.display = 'none';
	obj=document.getElementById('routedata');
	obj.style.display = 'none';
	switch ( tab ) {
		case 'datatable':
			if ( dirtydatatable ) getRuns() ;
			break ;
		case 'photosets':
			if ( dirtyphotosets ) getPhotos('all') ;
			break ;
		case 'graphs':
			if ( dirtygraphs ) setGraphs() ;
			break ;
		case 'routedata':
			if ( dirtyroutedata ) getRoutes() ;
	}
	obj=document.getElementById(tab) ;
	obj.style.display = 'block' ;
	currenttab = tab ;
}

//Function to get actual width of an element
function getElementWidth(Elem) {
	if(document.getElementById) {
		var elem = document.getElementById(Elem);
	} else if (document.all){
		var elem = document.all[Elem];
	}
	xPos = elem.offsetWidth;
	return xPos;
}

//Change happy sunshine pic to busy swirling pic while something is happening...
function ajaxStart () {
	$("#busyind").attr("src","spinwait.gif");
	$("#busyind").attr("alt","busy getting your data...");
	$("#busyind").attr("title","busy getting your data...");
	countofongoingajax++;
}
function ajaxDone () {
	countofongoingajax--;
	if ( countofongoingajax == 0 ) {
		$("#busyind").attr("src","sun.gif");
		$("#busyind").attr("alt","all data ready for you now...");
		$("#busyind").attr("title","all data ready for you now...");
	}
}
function incrementThumbLoader(obj) {
	countofongoingthumbloading++ ;
	document.getElementById(obj).onload = window.setTimeout('measurePhotoRow()',1000) ;
}
function graphDone() {
	countofongoinggraphloading--;
	if ( countofongoinggraphloading == 0 ) {
		$("#busyind").attr("src","sun.gif");
		$("#busyind").attr("alt","all data ready for you now...");
		$("#busyind").attr("title","all data ready for you now...");
	}
}
function incrementGraphLoader(obj) {
	//countofongoinggraphloading++ ;
	document.getElementById(obj).onload = function() {window.setTimeout('graphDone()',1000);} ;
}
//slide the thumbnails right or left at any one of three speeds
function slidephotos(rorl, slidespeed) {
	thing=document.getElementById('photorow');
	thingleft=parseInt(thing.style.left);

	switch ( slidespeed ) {
		case 5:
			distance=30;
			break;
		case 15:
			distance=20;
			break;
		default:
			distance=5;
			break;
	}

	if ( rorl == 'right' && thingleft < 20 ) {
		thingleft += distance;
		thing.style.left = thingleft + 'px';
	}
	if ( rorl == 'left' && thingleft > (800 - photorowwidth) ) {
		thingleft -= distance;
		thing.style.left = thingleft + 'px';
	}
	window.status = 'pwidth is ' + photorowwidth + ': going ' +  rorl + ' ;' + thingleft + ' :' + photorowwidth;
	photoslidetimer = window.setTimeout('slidephotos("'+rorl+'",'+slidespeed+')',slidespeed);
}

//make some thumbnails appear, either all or a selcted date
function  buildPhotoRow(adate) {
	var j=0 ;
	photocount=0 ;
	var firstthumb=99999 ;
	ajaxStart() ;
	$("#photorow").empty() ;
	if ( adate == 'all' ) {
		for (i=0; i<phototable.length; i++ ) {
			aphoto=phototable[i] ;
			athumb="<span class=\"thumb\" id=\"thumb"+j+"\" onclick=\"setPhoto('"+i+"');\">"+phototable[i].photodate+"<br /><img height=\"64px\" id=\"photo"+j+"\" src=\""+phototable[i].image+"\"><br />"+phototable[i].title ;
			$("#photorow").append(athumb) ;
			incrementThumbLoader("thumb"+j);
			if ( firstthumb == 99999 ) {
				firstthumb=i ;
			}
			j++ ;
		}
		photocount=phototable.length ;
	}
	else {
		for ( i=0; i<phototable.length; i++ ) {
			if ( phototable[i].photodate == adate ) {
				aphoto=phototable[i] ;
				athumb="<span class=\"thumb\" id=\"thumb"+j+"\" onclick=\"setPhoto('"+i+"');\">"+phototable[i].photodate+"<br /><img height=\"64px\" id=\"photo"+j+"\" src=\""+phototable[i].image+"\"><br />"+phototable[i].title ;
				$("#photorow").append(athumb) ;
				incrementThumbLoader("thumb"+j);
				if ( firstthumb == 99999 ) {
					firstthumb=i ;
				}
				j++ ;
				photocount++ ;
			}
		}
	}
	document.getElementById("thumbhdr").innerHTML = photocount ;
	document.getElementById("photorow").style.left = '20px' ;
	setPhoto(firstthumb) ;
	//measurePhotoRow();
	tabClick('photosets') ;
	ajaxDone() ;
}

function setPhoto (thumb) {
	document.getElementById('bigtitle').innerHTML = phototable[thumb].title + ' : ' + phototable[thumb].photodatestring ;
	//$.ajax({url:phototable[thumb].bigimage,type:'HEAD'}).success(function(){document.getElementById('bigphoto').src = ''+phototable[thumb].bigimage;}).error(function(){document.getElementById('bigphoto').src = ''+phototable[thumb].originalimage;});
	document.getElementById('bigphoto').src = ''+phototable[thumb].bigimage ;
	currentthumb=thumb ;
}

function measurePhotoRow () {
	countofongoingthumbloading-- ;
	if ( countofongoingthumbloading == 0 ) {
		photorowwidth = 0 ;
		for ( i=0; i<photocount; i++ ) {
			thumbid = "thumb" + i ;
			photorowwidth += getElementWidth(thumbid)+7 ;
		}
	}
}

function setRouteStats (runid) {
	run=runLU[runid] ;
	var route=runtable[run].route ;
	var speed=Math.round(((runtable[run].runtimesec/(runtable[run].distance*KMTOMI))/60)*100)/100 ;
	var miles=Math.round(runtable[run].distance*62.13712)/100 ;
	var avespeed=Math.round(((routetable[route-1].avgtimesec/(routetable[route-1].distance*KMTOMI))/60)*100)/100 ;
	document.getElementById('routetitle').innerHTML = 'Route Number '+runtable[run].route+ ' : '+runtable[run].description ;
	if (routetable[route-1].hasgmap) {
		document.getElementById('routestats').innerHTML = '<h2><a href="db2mapstatic.php?route=' + route + '" target="_blank" title="See Google Map Arial View!">Click here to see the Google Map view of this route</a></h2>' ;
	}
	else {
		document.getElementById('routestats').innerHTML = '' ;
	}
	document.getElementById('routestats').innerHTML += 'Distance : '+runtable[run].distance+'km = '+miles+' miles, Ascent : '+runtable[run].ascent+'m, Time : '+runtable[run].runtime+', Heart-monitor reading : '+runtable[run].heart+'bpm, Speed : '+speed+' minutes per mile<br /><span style="color: brown;font-size: larger;">'+runtable[run].notes+'</span><br />Route Statistics : Route trotted '+routetable[route-1].frequency+' times, Average time '+routetable[route-1].avgtime+', quickest was '+routetable[route-1].minruntime+', slowest was '+routetable[route-1].maxruntime+'. Average speed : '+avespeed+' Mins/Mile' ;
	document.getElementById('mapimage').src = 'maps/' + 'map'+runtable[run].route+'.jpg' ;
	document.getElementById('selroute2').value=route ;
	currentroute=route ;
}
function setRouteStatsR (routeid) {
	route=routeid-1 ;
	var avespeed=Math.round(((routetable[route].avgtimesec/(routetable[route].distance*KMTOMI))/60)*100)/100 ;
	var miles=Math.round(routetable[route].distance*62.13712)/100 ;
	document.getElementById('routetitle').innerHTML = 'Route Number '+routeid+ ' : '+routetable[route].description ;
	if (routetable[route].hasgmap) {
		document.getElementById('routestats').innerHTML = '<h2><a href="db2mapstatic.php?route=' + routeid + '" target="_blank" title="See Google Map Arial View!">Click here to see the Google Map view of this route</a></h2>' ;
	}
	else {
		document.getElementById('routestats').innerHTML = '' ;
	}
	document.getElementById('routestats').innerHTML += 'Distance : '+routetable[route].distance+'km = '+miles+' miles, Ascent : '+routetable[route].ascent+'m, Route Statistics : Route trotted '+routetable[route].frequency+' times, Average time '+routetable[route].avgtime+', quickest was '+routetable[route].minruntime+', slowest was '+routetable[route].maxruntime+'. Average speed : '+avespeed+' Mins/Mile' ;
	document.getElementById('mapimage').src = 'maps/' + 'map'+routeid+'.jpg' ;
	currentroute=routeid ;
}

function fixIeMargin () {
	if ($. browser.msie){
		obj=document.getElementById('photosets') ;
		obj.style.position = 'relative' ;
		obj.style.top = '-1px' ;
		obj=document.getElementById('graphs') ;
		obj.style.position = 'relative' ;
		obj.style.top = '-2px' ;
		obj=document.getElementById('routedata') ;
		obj.style.position = 'relative' ;
		obj.style.top = '-3px' ;
	}
}

function imgSize (size, imgid) {
	switch ( size  ) {
	case 'max' :
		document.getElementById(imgid).style.width='auto';
		document.getElementById(imgid).style.height='auto';
		break;
	case 'width' :
		document.getElementById(imgid).style.width='97%';
		document.getElementById(imgid).style.height='auto';
		break;
	case 'height' :
		document.getElementById(imgid).style.width='auto';
		document.getElementById(imgid).style.height='99%';
		break;
	case 'bigger' :
		currwidth=document.getElementById(imgid).offsetWidth;
		newwidth=currwidth*1.1;
		document.getElementById(imgid).style.width=newwidth+'px';
		document.getElementById(imgid).style.height='auto';
		break;
	case 'smaller' :
		currwidth=document.getElementById(imgid).offsetWidth;
		newwidth=currwidth*0.9;
		document.getElementById(imgid).style.width=newwidth+'px';
		document.getElementById(imgid).style.height='auto';
		break;
	}
}
function launchPhotoWindow(imgid) {
	if ( imgid == 'mapimage' ) {
		imgurl = 'newtrotpic.html?-1&0&&Route ' + currentroute + ' : ' + document.getElementById('routestats').innerHTML + '&' + document.getElementById('mapimage').src;
}
	else {
		index=currentthumb;
		lastindex = phototable.length;
		imgurl = 'newtrotpic.html?'+index+'&'+lastindex+'&'+phototable[index].photodatestring + "&" + phototable[index].title + "&" + phototable[index].bigimage;
	}
	newwin=window.open(imgurl, 'TrotterPic', 'resizable=yes, scrollbars=yes');
	newwin.focus();
	return newwin;
}
function nextImage (index) {
	lastindex = phototable.length;
	picwin.document.getElementById("thepic").src = ""+phototable[index].bigimage ;
	picwin.document.title = phototable[index].title + ":" + phototable[index].photodatestring + " : photo " + (Number(index)+1) + " of " + lastindex ;
	picwin.document.getElementById("infospan").innerHTML = phototable[index].title + ":" + phototable[index].photodatestring + " : photo " + (Number(index)+1) + " of " + lastindex ;
	picwin.nextindex = (index<(lastindex-1)?Number(index)+1:0);
	picwin.previousindex = (index==0?phototable.length-1:Number(index)-1);
	picwin.fitheight();
	//picwin.location="newtrotpic.html?" + index + "&" + lastindex + "&" + phototable[index].photodatestring + "&" + phototable[index].title + "&photos/" + phototable[index].image;
}
function formatRoutesLink (runid, runrow, route, ground, shortdesc)
{
	return "<span  style=\"cursor:default;color:" + (ground=="Fields"?"yellow":"gray") + ";\" onclick=\"setRouteStats(" + runid + ");tabClick('routedata')\" title=\"Click to see Route Data and Map image for : " + shortdesc + "\">" + route + "</span>";
}

function formatPhotosLink (rundate, runrow, photocount)
{
	return "<span  style=\"cursor:default;\" onclick=\"getPhotos('" + rundate + "');\"><img title=\"Click to see this day's photos\" src=\"camera.gif\">&nbsp;" + photocount + "</span>";
}

function runReqChange() {
	if (runReq.readyState == 4 && runReq.status == 200 ) {
		runtable = eval(runReq.responseText) ;
		var dobj = document.getElementById( 'runTable' );
		$("#runTable tbody").empty();
		if (runtable.length == 0) {
			$("#runTable tbody").append("<tr style=\"height:30px;\"><td colspan=\"9\" style=\"font-size:14pt;\">NO RESULTS match your Selection Criteria !!<br />Please be a bit less specific.</td></tr>");
			$("#busyind").attr("src","sun.gif");
			$("#busyind").attr("alt","all data ready for you now...");
			return ;
		}
		var lastrundate="";
		var lastrunday="";
		var disttoday=0;
		var timetoday=0;
		var climbtoday=0;
		var hearttoday=0;
		var raintoday=0;
		var routeslist="";
		var lastphotocount="";
		var runrow=1;

		for (i=0; i<runtable.length; i++) {
			run=runtable[i];
			runLU[run.id]=i;
			if (toprun == 0 && run.route != 8 &&  run.route != 78 ) {
				toprun = run.id;
				if ( dirtyroutedata ) getRoutes();
			}
			if (lastrundate == "" || lastrundate == run.rundate) {
				disttoday+=parseFloat(run.distance);
				timetoday+=parseFloat(run.runtimesec);
				climbtoday+=parseFloat(run.ascent);
				hearttoday=(hearttoday < parseFloat(run.heart)?parseFloat(0+run.heart):hearttoday);
				raintoday=run.rain ;
				lastphotocount=run.photocount;
				routeslist += (lastrundate==""?"":", ") + formatRoutesLink(run.id,runrow,run.route,run.ground,run.shortdesc);
			}
			else {
				timebit=new Date(timetoday*1000);
				timemin="0"+timebit.getMinutes();
				timesec="0"+timebit.getSeconds();
				tablerow="<tr><td>" + lastrunday + "</td><td>" + lastrundate + "</td><td>" + disttoday.toFixed(2) + "</td><td>" + (disttoday*KMTOMI).toFixed(2) + "</td><td>" + timebit.getHours() + ":" + (timemin.substr(timemin.length-2)) + ":" + (timesec.substr(timesec.length-2)) + "</td><td>" + ((timetoday/(disttoday*KMTOMI))/60).toFixed(2) + "</td><td>" + hearttoday + "</td><td>" + climbtoday + "</td><td><img src=\"weather/swea" + raintoday + ".gif\" title=\"" + raintoday + ":" + weather_text[raintoday] + "\" height=\"20\"></td><td>" + routeslist + "</td><td>" + (lastphotocount>0?formatPhotosLink(lastrundate, runrow, lastphotocount):"")  + "</td></tr>" ;
				$("#runTable tbody").append(tablerow);
				runrow++;
				lastrundate=run.rundate;
				lastrunday=run.daybit;
				disttoday=parseFloat(run.distance);
				timetoday=parseFloat(run.runtimesec);
				climbtoday=parseFloat(run.ascent);
				hearttoday=parseFloat(0+run.heart);
				raintoday=run.rain;
				lastphotocount=run.photocount;
				routeslist=formatRoutesLink(run.id,runrow,run.route,run.ground,run.shortdesc);
			}
			lastrundate=run.rundate;
			lastrunday=run.daybit;
		}
		timebit=new Date(timetoday*1000);
		timemin="0"+timebit.getMinutes();
		timesec="0"+timebit.getSeconds();
		tablerow="<tr><td>" + lastrunday + "</td><td>" + lastrundate + "</td><td>" + disttoday.toFixed(2) + "</td><td>" + (disttoday*KMTOMI).toFixed(2) + "</td><td>" + timebit.getHours() + ":" + (timemin.substr(timemin.length-2)) + ":" + (timesec.substr(timesec.length-2)) + "</td><td>" + ((timetoday/(disttoday*KMTOMI))/60).toFixed(2) + "</td><td>" + hearttoday + "</td><td>" + climbtoday + "</td><td><img src=\"weather/swea" + raintoday + ".gif\" title=\"" + raintoday + ":" + weather_text[raintoday] + "\" height=\"20\"></td><td>" + routeslist + "</td><td>" + (lastphotocount>0?formatPhotosLink(lastrundate, runrow, lastphotocount):"")  + "</td></tr>" ;
		$("#runTable tbody").append(tablerow);
		dirtydatatable=false;
		$("#runTable").tableSorter({
			sortDir: 'descending',				//
			sortColumn: 'Date',				// Integer or String of the name of the column to sort by.
			sortClassAsc: 'headerSortUp',		// Class name for ascending sorting action to header
			sortClassDesc: 'headerSortDown',	// Class name for descending sorting action to header
			headerClass: 'header',				// Class name for headers (th's)
			highlightClass: 'highlight', 		// class name for sort column highlighting.
			stripingRowClass: ['even','odd'],	// Class names for striping supplyed as a array.
			stripeRowsOnStartUp: true			// Stripe rows on tableSorter init.
		});
		$(".editable").editable("http://www.appelsiini.net/~tuupola/jquery/jeditable/save.php", {
			indicator : "<img src='spinwait.gif'>",
			tooltip   : "Doubleclick to edit...",
			event     : "dblclick"
		});
		ajaxDone();
	}
}

function statReqChange() {
	if (statReq.readyState == 4 && statReq.status == 200 ) {
		stattable = eval('('+statReq.responseText+')') ;
		stdate = new Date($("#startdate").val().replace('-',' ','g'));
		stweekday=weekdays[stdate.getDay()];
		stmonth=months[stdate.getMonth()];
		stday=stdate.getDate();
		styear=stdate.getFullYear();
		$("#statfromdate").html(stweekday+" "+stmonth+" "+stday+", "+styear);
		selday=$("#selday").val();
		selrain=$("#selrain").val();
		selroute=$("#selroute").val();
		selground=$("#selground").val();
		seltext="";
		if ( selday != "" ) seltext += "on a " + weekdays[daystonums[selday]] ;
		if ( selrain != "" ) seltext += " through " + weather_hdr[selrain] +" or worse " ;
		if ( selroute != "" ) seltext += " along route " + selroute ;
		if ( selground != "" ) seltext += " over " + selground ;
		$("#selectiontext").html(seltext);
		$("#statruncount").html(stattable["runcount"]);
		$("#statkm").html(stattable["distancekmtodate"]);
		$("#statmiles").html(stattable["distancemilestodate"]);
		$("#statdays").html(stattable["daystodate"]);
		$("#stathours").html(stattable["hourstodate"]);
		$("#statmins").html(stattable["minutestodate"]);
		$("#statsecs").html(stattable["secondstodate"]);
		$("#statavekm").html(stattable["avedistancekm"]);
		$("#statavemiles").html(stattable["avedistancemiles"]);
		timebit=new Date(stattable["averuntime"]*1000);
		timemin="0"+timebit.getMinutes();
		timesec="0"+timebit.getSeconds();
		$("#statavetime").html(timebit.getHours() + ":" + (timemin.substr(timemin.length-2)) + ":" + (timesec.substr(timesec.length-2)));
		$("#statavekph").html(stattable["avespeedkph"]);
		$("#statavemph").html(stattable["avespeedmph"]);
		$("#statavempm").html(stattable["avespeedmpm"]);
		$("#statascent").html(stattable["ascenttodate"]);
		ajaxDone();
	}
}
function routeReqChange() {
	if ( routeReq.readyState == 4 && routeReq.status == 200 ) {
		routetable = eval('('+routeReq.responseText+')') ;
		for (i=routetable.length-1; i>=0; i-- ) {
			aroute=routetable[i];
			opttext=aroute.routepad.replace(new RegExp( '^0+'),' ')+':'+aroute.shortdesc+':'+aroute.distance+'km, '+aroute.frequency+' times';
			$("#selroute").addOption(aroute.route, opttext);
			$("#selroute2").addOption(aroute.route, opttext);
			routeLU[aroute.route]=i;
		}
		setRouteStats(toprun);
		$("#selroute").selectOptions("", true);
		$("#selroute2").selectOptions("", true);
		dirtyroutedata=false;
		ajaxDone();
	}
}
function photoReqChange() {
	if (photoReq.readyState == 4 && photoReq.status == 200 ) {
		phototable = eval('('+photoReq.responseText+')') ;
		dirtyphotosets=false;
		buildPhotoRow(pickdate);
		ajaxDone();
	}
}
function getRuns() {
	if(window.XMLHttpRequest) {
		try {
			runReq = new XMLHttpRequest();
		} catch(e) {
			runReq = false;
		}
	} else {
		if(window.ActiveXObject) {
			try {
				runReq = new ActiveXObject('Msxml2.XMLHTTP');
			} catch(e) {
				try { runReq = new ActiveXObject('Microsoft.XMLHTTP');
				} catch(e) {
					runReq = false;
				}
			}
		}
	}
	if(runReq) {
		ajaxStart();
		url = "getruns.php?wherebit=" + wherebit + "&t="+((new Date()).valueOf());
		runReq.onreadystatechange = runReqChange;
		runReq.open('GET', url, true);
		runReq.send('');
	}
}
function getStats() {
	if(window.XMLHttpRequest) {
		try {
			statReq = new XMLHttpRequest();
		} catch(e) {
			statReq = false;
		}
	} else {
		if(window.ActiveXObject) {
			try {
				statReq = new ActiveXObject('Msxml2.XMLHTTP');
			} catch(e) {
				try { statReq = new ActiveXObject('Microsoft.XMLHTTP');
				} catch(e) {
					statReq = false;
				}
			}
		}
	}
	if(statReq) {
		ajaxStart();
		url = "getstats.php?wherebit=" + wherebit + "&t="+((new Date()).valueOf());
		statReq.onreadystatechange = statReqChange;
		statReq.open('GET', url, true);
		statReq.send('');
	}
}
function getRoutes() {
	if(window.XMLHttpRequest) {
		try {
			routeReq = new XMLHttpRequest();
		} catch(e) {
			routeReq = false;
		}
	} else {
		if(window.ActiveXObject) {
			try {
				routeReq = new ActiveXObject('Msxml2.XMLHTTP');
			} catch(e) {
				try { routeReq = new ActiveXObject('Microsoft.XMLHTTP');
				} catch(e) {
					routeReq = false;
				}
			}
		}
	}
	if(routeReq) {
		ajaxStart();
		url = "getroutes.php?t="+((new Date()).valueOf());
		routeReq.onreadystatechange = routeReqChange;
		routeReq.open('GET', url, true);
		routeReq.send('');
	}
}
function getPhotos( adate ) {
	pickdate = adate;
	if ( dirtyphotosets ) {
		url="getphotos.php";
		if(window.XMLHttpRequest) {
			try {
				photoReq = new XMLHttpRequest();
			} catch(e) {
				photoReq = false;
			}
		} else {
			if(window.ActiveXObject) {
				try {
					photoReq = new ActiveXObject('Msxml2.XMLHTTP');
				} catch(e) {
					try { photoReq = new ActiveXObject('Microsoft.XMLHTTP');
					} catch(e) {
						photoReq = false;
					}
				}
			}
		}
		if(photoReq) {
			ajaxStart();
			url = url + "?t="+((new Date()).valueOf());
			photoReq.onreadystatechange = photoReqChange;
			photoReq.open('GET', url, true);
			photoReq.send('');
		}
	}
	else {
		buildPhotoRow(adate);
	}
}

function setGraphs() {
	countofongoinggraphloading=6;
	$("#busyind").attr("src","spinwait.gif");
	$("#busyind").attr("alt","busy getting your data...");
	$("#busyind").attr("title","busy getting your data...");
	$("#graph1").attr("src","monthlymileage.php?wherebit="+wherebit+"&w=500&h=400&");
	$("#graph1zoom").attr("href","monthlymileage2.php?wherebit="+wherebit+"&w=1000&h=800");
	incrementGraphLoader("graph1") ;
	$("#graph2").attr("src","surfacepie2.php?wherebit="+wherebit+"&w=250&h=200&");
	$("#graph2zoom").attr("href","surfacepie2.php?wherebit="+wherebit+"&w=500&h=400&");
	incrementGraphLoader("graph2") ;
	$("#graph3").attr("src","surfacedistancepie2.php?wherebit="+wherebit+"&w=250&h=200&");
	$("#graph3zoom").attr("href","surfacedistancepie2.php?wherebit="+wherebit+"&w=500&h=400&");
	incrementGraphLoader("graph3") ;
	$("#graph4").attr("src","heartgraph2.php?wherebit="+wherebit+"&w=500&h=400&");
	$("#graph4zoom").attr("href","heartgraph2.php?wherebit="+wherebit+"&w=1000&h=800&");
	incrementGraphLoader("graph4") ;
	$("#graph5").attr("src","weatherpie2.php?wherebit="+wherebit+"&w=250&h=320&");
	$("#graph5zoom").attr("href","weatherpie2.php?wherebit="+wherebit+"&w=1000&h=600&");
	incrementGraphLoader("graph5") ;
	$("#graph6").attr("src","12monthsum.php?t="+((new Date()).valueOf()));
	$("#graph6zoom").attr("href","biggraph12monthsum.php?t="+((new Date()).valueOf()));
	incrementGraphLoader("graph6") ;
	dirtygraphs=false;
}
function newSelect() {
	$("#selclickout").click();
	wherebit="runs.rundate<='" + $("#enddate").val() + "' AND runs.rundate>='" + $("#startdate").val() + "'";
	selday=$("#selday").val();
	selrain=$("#selrain").val();
	selroute=$("#selroute").val();
	selground=$("#selground").val();
	if ( selday != "" ) wherebit += " AND left(dayname(runs.rundate),3)='" + selday + "'" ;
	if ( selrain != "" ) wherebit += " AND runs.rain>=" + selrain ;
	if ( selroute != "" ) wherebit += " AND runs.route='" + selroute + "'" ;
	if ( selground != "" ) wherebit += " AND routes.ground='" + selground + "'" ;
	$("#enddate").val('2100-01-01');
	getStats();
	dirtydatatable=true;
	//dirtyphotosets=true;
	dirtygraphs=true;
	switch ( currenttab ) {
		case 'datatable':
			getRuns('getruns.php?wherebit='+wherebit);
			break;
		case 'photosets':
			getPhotos();
			break;
		case 'graphs':
			setGraphs();
			break;
//		case 'routedata':
//			getRoutes();
	}
	$('#selclickout a').click();
}
