function purgedata(str) {
count=0;
pos=str.indexOf("|");
while(pos!=-1) {
count++;
pos=str.indexOf("|",pos+1);
}
return count;
}

if (GBrowserIsCompatible()) {

var map;

reportDiv=document.getElementById("report_outer");

timer=0;

animspeed=new Array(60,40,20,10,5);

places=[];

herdname=[];
districtname=[];
data=[];

mainpath=[];
step_path=[];
latstep=[];
lngstep=[];
pts=[];
date=[];

latdata=[];
lngdata=[];
activity=[];
daynum=[];
monthnum=[];
yearnum=[];

distance=[];
legdist=[];
mapsymbol=[];

totalpoints=[];
menulist=[];

months=["","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];

symbol="";
reportstr="";

steps=8

minzoom=4;
maxzoom=14;

GDownloadUrl("resources/data.txt",function(datafile) {

datafile=datafile.replace(/\r\n/g,"*");
datafile=datafile.replace(/\r/g,"*");
datafile=datafile.replace(/\n/g,"*");

ok=1;
while(ok==1) {
z=datafile.charAt(datafile.length-1);
if(z=="*") {
ok=1;
datafile=datafile.substring(0,datafile.length-1);
}
else ok=0;
}
datafile+="**";

data=datafile.split("Herd*");

for(herd=1;herd<data.length;herd++) {

data[herd]=data[herd].replace("*","#");
data[herd]=data[herd].replace("*","#");

temp=data[herd].split("#");
herdname[herd]=temp[0];
districtname[herd]=temp[1];
temp2=temp[2].split("**");
data[herd]=[];
latdata[herd]=[];
lngdata[herd]=[];
activity[herd]=[];
daynum[herd]=[];
monthnum[herd]=[];
yearnum[herd]=[];
legdist[herd]=[];
distance[herd]=[];
totalpoints[herd]=[];

temp2b=[];
for(month=0;month<temp2.length;month++) {
if(purgedata(temp2[month])>3 || temp2[month]=="") temp2b.push(temp2[month]);
}

for(month=0;month<temp2b.length;month++) {
latdata[herd][month]=[];
lngdata[herd][month]=[];
activity[herd][month]=[];
daynum[herd][month]=[];
monthnum[herd][month]=[];
yearnum[herd][month]=[];
legdist[herd][month]=[];
distance[herd][month]=[];
data[herd][month]=temp2b[month].split("*");

dist=0;
for(day=0;day<data[herd][month].length;day++) {
temp3=data[herd][month][day].split("|");
latdata[herd][month][day]=parseFloat(temp3[1]);
lngdata[herd][month][day]=parseFloat(temp3[2]);

if(day>0) {
p1=new GLatLng(latdata[herd][month][day-1],lngdata[herd][month][day-1]);
p2=new GLatLng(latdata[herd][month][day],lngdata[herd][month][day]);
legdist[herd][month][day-1]=parseFloat((p1.distanceFrom(p2)/1000).toFixed(3));
dist+=legdist[herd][month][day-1];
distance[herd][month][day-1]=dist.toFixed(3);
}

activity[herd][month][day]=temp3[3];
tempdate=temp3[0].split("/");
daynum[herd][month][day]=parseFloat(tempdate[0]);
monthnum[herd][month][day]=parseFloat(tempdate[1]);
yearnum[herd][month][day]=parseFloat(tempdate[2]);
}

totalpoints[herd][month]=daynum[herd][month].length-1;
}
}

plc="";
firstherd=[];
for(b=1;b<districtname.length;b++) {
if(plc.indexOf(districtname[b])==-1) {
plc+=districtname[b]+",";
firstherd.push(b)
}
}
plc=plc.substring(0,plc.length-1);
places=plc.split(",");

thisdistrict=places[0];

thisherd=1;thismonth=0;thisday=0;

thisdistrict=districtname[thisherd];




map=new GMap2(document.getElementById("map"));

mt=map.getMapTypes();
for(i=0;i<mt.length;i++) {
mt[i].getMinimumResolution=function() {return eval(minzoom);}
mt[i].getMaximumResolution=function() {return eval(maxzoom);}
}

symbolIcon=new GIcon();
symbolIcon.iconSize=new GSize(63,45);
symbolIcon.iconAnchor=new GPoint(0,45);

map.addControl(new GSmallMapControl());

map.setCenter(new GLatLng(latdata[thisherd][thismonth][thisday],lngdata[thisherd][thismonth][thisday]),11);


map.setMapType(G_SATELLITE_MAP);

controlbox=document.getElementById("control_box");
document.getElementById("map").appendChild(controlbox);

blobIcon=new GIcon();
blobIcon.iconSize=new GSize(17,17);
blobIcon.iconAnchor=new GPoint(6,6);


makeform();


reset_all();


});

}
else alert("Sorry, Google Maps will not work with this browser");




function makeform() {

str='<form name="eleform">';

str+='district <select name="setplace" id="dropdownstyle" onChange="changeplace()">';
for(p=0;p<places.length;p++) {
if(places[p]==thisdistrict) str+='<option selected value='+p+'>'+places[p]+'</option>';
else str+='<option value='+p+'>'+places[p]+'</option>';
}
str+='</select>';


str+=' herd <select name="setherd" id="dropdownstyle" onChange="changeherd()">';
for(h=0;h<herdname.length;h++) {
if(districtname[h]==thisdistrict) {
if(h==thisherd) str+='<option selected value='+h+'>'+herdname[h]+'</option>';
else str+='<option value='+h+'>'+herdname[h]+'</option>';
}
}
str+='</select>';


str+=' period <select name="setmonth" id="dropdownstyle" onChange="changemonth()">';
for(m=0;m<(monthnum[thisherd].length-1);m++) {
str+='<option value='+m+'>'+months[monthnum[thisherd][m][0]]+' '+yearnum[thisherd][m][0]+'</option>';
}
str+='</select>';

str+='</form>';
str+='<div id="spacer4"> </div>';
str+='<form name="optionsform">';

str+='<div id="helpdiv">';
str+='<a href="javascript:helpbutton_down()" onmouseover="javascript:mouseoverhelp()" onmouseout="javascript:mouseouthelp()"><img name="helpbutton" src="images/help.gif" border=0 align="absbottom" width=28 height=19></a>';
str+='</div>';
str+='&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;speed ';
str+='<select name="setspeed" id="dropdownstyle" style="width: 36px;">';
str+='<option value=1>1</option>';
str+='<option value=2>2</option>';
str+='<option selected value=3>3</option>';
str+='<option value=4>4</option>';
str+='<option value=5>5</option>';
str+='</select>';
str+='&nbsp;&nbsp;&nbsp;';
str+='<div id="playdiv"></div>';
str+='<div id="leftdiv"></div>';
str+='<div id="rightdiv"></div>';
str+='&nbsp;&nbsp;&nbsp;';
str+='<div id="resetdiv">';
str+='<a href="javascript:resetbutton_down()" onmouseover="javascript:mouseoverreset()" onmouseout="javascript:mouseoutreset()"><img name="resetbutton" src="images/reset.gif" border=0 align="absbottom" width=32 height=19></a>';
str+='</div>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
str+='</form>';
controlbox.innerHTML=str;
}



function changeplace() {
thisdistrict=places[document.eleform.setplace.selectedIndex];
thisherd=firstherd[document.eleform.setplace.selectedIndex];
thismonth=0;thisday=0;
makeform();
map.panTo(new GLatLng(latdata[thisherd][thismonth][0],lngdata[thisherd][thismonth][0]));
reset_all();
}

function changeherd() {
thisherd=document.eleform.setherd.options[document.eleform.setherd.selectedIndex].value;
makeform();
map.panTo(new GLatLng(latdata[thisherd][thismonth][0],lngdata[thisherd][thismonth][0]));
reset_all();
}

function changemonth() {
thismonth=document.eleform.setmonth.selectedIndex;
map.panTo(new GLatLng(latdata[thisherd][thismonth][0],lngdata[thisherd][thismonth][0]));
reset_all();
}

function helpbutton_down() {
if(document.getElementById('helpnotes').style.visibility=='visible') {closehelpnotesdiv(); return;}
document.getElementById('helpnotes').style.visibility='visible';
document.getElementById('closehelpnotes').style.visibility='visible';
}

function closehelpnotesdiv() {
document.getElementById('helpnotes').style.visibility='hidden';
document.getElementById('closehelpnotes').style.visibility='hidden';
}

function hideplaybutton() {
str='<img src="images/playgrey.gif" border=0 align="absbottom" width=18 height=19>';
document.getElementById("playdiv").innerHTML=str;
}

function hideleftbutton() {
str='<img src="images/stepleftgrey.gif" border=0 align="absbottom" width=18 height=19>';
document.getElementById("leftdiv").innerHTML=str;
}

function hiderightbutton() {
str='<img src="images/steprightgrey.gif" border=0 align="absbottom" width=18 height=19>';
document.getElementById("rightdiv").innerHTML=str;
}

function showplaybutton() {
str='<a href="javascript:playbutton_down()" onmouseover="javascript:mouseoverplay()" onmouseout="javascript:mouseoutplay()"><img name="playbutton" src="images/play.gif" border=0 align="absbottom" width=18 height=19></a>';
document.getElementById("playdiv").innerHTML=str;
}

function showleftbutton() {
str='<a href="javascript:stepback()" onmouseover="document.stepleftbutton.src=\'images/stepleftH.gif\'" onmouseout="document.stepleftbutton.src=\'images/stepleft.gif\'"><img name="stepleftbutton" src="images/stepleft.gif" border=0 align="absbottom" width=18 height=19></a>';
document.getElementById("leftdiv").innerHTML=str;
}

function showrightbutton() {
str='<a href="javascript:stepforward()" onmouseover="document.steprightbutton.src=\'images/steprightH.gif\'" onmouseout="document.steprightbutton.src=\'images/stepright.gif\'"><img name="steprightbutton" src="images/stepright.gif" border=0 align="absbottom" width=18 height=19></a>';
document.getElementById("rightdiv").innerHTML=str;
}

function mouseoverplay() {
if(animation_status==1) document.playbutton.src='images/pauseH.gif';
else document.playbutton.src='images/playH.gif';
}

function mouseoutplay() {
if(animation_status==1) document.playbutton.src='images/pause.gif';
else document.playbutton.src='images/play.gif';
}

function mouseoverreset() {
document.resetbutton.src='images/resetH.gif';
}

function mouseoutreset() {
document.resetbutton.src='images/reset.gif';
}

function mouseoverhelp() {
document.helpbutton.src='images/helpH.gif';
}

function mouseouthelp() {
document.helpbutton.src='images/help.gif';
}

function reset_all() {
document.getElementById("report").style.visibility="hidden";
showplaybutton();
hideleftbutton();
showrightbutton();
clearTimeout(timer);
map.clearOverlays();
icon=new GIcon(blobIcon);
icon.image="markers/marker.png";
point=new GLatLng(latdata[thisherd][thismonth][thisday],lngdata[thisherd][thismonth][thisday]);
blob=new GMarker(point,icon);
map.addOverlay(blob);
animation_status=0;
singlestep=0;
moving=0;
reportstr="";
show_report();
}


function resetbutton(bt,im) {
eval("document."+bt+"button.src='images/"+im+".gif'");
}

function resetbutton_down() {
resetbutton("reset","resetD");
setTimeout('resetbutton("reset","reset")',400);
reset_all();
}

function playbutton_down() {
// start animation
if(animation_status<1) {
animation_status=2;
playB();
}
// pause
else if(animation_status==1) {
animation_status=2;
resetbutton("play","pauseD");
setTimeout('resetbutton("play","play")',400);
showleftbutton();
showrightbutton();
}
// end pause
else if(animation_status==2) {
animation_status=1;
singlestep=0;
resetbutton("play","playD");
setTimeout('resetbutton("play","pause")',400);
hideleftbutton();
hiderightbutton();

if(pathcount<=totalpoints[thisherd][thismonth]) setTimeout('playB3()',100);

}
}

function playB() {
resetbutton("play","playD");
setTimeout('playB1()',400);
}

function playB1() {
reset_all();

hiderightbutton();
resetbutton("play","pause");
animation_status=1;
timer=setTimeout("playB2()",400);
}

function playB2() {
pathcount=0;
setTimeout("playB3()",100);
}

function playB3() {
sp=document.optionsform.setspeed.selectedIndex;
thisspeed=animspeed[sp];
animate_leg();
}

function animate_leg() {

document.getElementById("report").style.visibility="visible";

create_report();

symbol="";
if(activity[thisherd][thismonth][pathcount]=="C") symbol="crop";
if(activity[thisherd][thismonth][pathcount]=="X") symbol="injury";
if(activity[thisherd][thismonth][pathcount]=="H" || activity[thisherd][thismonth][pathcount]=="P") symbol="damage";

if(symbol!="") do_picture();

lat1=latdata[thisherd][thismonth][pathcount];lng1=lngdata[thisherd][thismonth][pathcount];
lat2=latdata[thisherd][thismonth][pathcount+1];lng2=lngdata[thisherd][thismonth][pathcount+1];


ystep=(lat2-lat1)/steps;xstep=(lng2-lng1)/steps;

latstep[0]=lat1;latstep[steps-1]=lat2;
lngstep[0]=lng1;lngstep[steps-1]=lng2;
for(s=1;s<(steps-1);s++) {
latstep[s]=lat1+s*ystep;lngstep[s]=lng1+s*xstep;
}

stepcount=0;
animate_step();
}

function animate_step() {
// draw steps

pts[0]="new GLatLng("+latstep[stepcount]+","+lngstep[stepcount]+")";
pts[1]="new GLatLng("+latstep[stepcount+1]+","+lngstep[stepcount+1]+")";

if(stepcount==(steps-2)) {

pts[0]="new GLatLng("+latdata[thisherd][thismonth][pathcount]+","+lngdata[thisherd][thismonth][pathcount]+")";
pts[1]="new GLatLng("+latdata[thisherd][thismonth][pathcount+1]+","+lngdata[thisherd][thismonth][pathcount+1]+")";

mainpath[pathcount]=eval('new GPolyline(['+pts+'],"#FFFF00",2,1)');
map.addOverlay(mainpath[pathcount]);
}
else {
step_path[stepcount]=eval('new GPolyline(['+pts+'],"#FFFF00",2,1)');
map.addOverlay(step_path[stepcount]);
}

stepcount++;
blob.setPoint(new GLatLng(latstep[stepcount],lngstep[stepcount]));


if(stepcount<(steps-1)) {
timer=setTimeout("animate_step()",thisspeed);
return;
}

pathcount++;
show_report();

if(pathcount<totalpoints[thisherd][thismonth]) {

// remove small steps
for(s=0;s<(steps-2);s++) {
map.removeOverlay(step_path[s]);
}

if(symbol!="") ;//do nothing
else {
if(singlestep==0 && animation_status<2) timer=setTimeout("animate_leg()",thisspeed);
else {
// leg done
showplaybutton();
showleftbutton();
showrightbutton();
return;
}
}
}
else { // end animation

// remove small steps
for(s=0;s<(steps-2);s++) {
map.removeOverlay(step_path[s]);
}

showplaybutton();
showleftbutton();
hiderightbutton();
animation_status=-1;
}
}

function do_picture() {
icon=new GIcon(symbolIcon);
icon.image="markers/"+symbol+".png";

point=new GLatLng(latdata[thisherd][thismonth][pathcount],lngdata[thisherd][thismonth][pathcount]);

bigsymbol=new GMarker(point,icon);
map.addOverlay(bigsymbol);
delay=thisspeed*20;
timer=setTimeout("make_small_symbol(symbol,point)",delay);
}

function make_small_symbol(symbol,oldpoint) {
smallsymbolIcon=new GIcon();
smallsymbolIcon.iconSize=new GSize(32,23);
smallsymbolIcon.iconAnchor=new GPoint(-3,26);
icon=new GIcon(smallsymbolIcon);
icon.image="markers/"+symbol+"_small.png";

mapsymbol[pathcount]=new GMarker(oldpoint,icon);
map.addOverlay(mapsymbol[pathcount]);
map.removeOverlay(bigsymbol);

if(pathcount>=totalpoints[thisherd][thismonth]) return;

if(singlestep==0 && animation_status<2) animate_leg();
else {
// leg done
showplaybutton();
showleftbutton();
showrightbutton();
return;
}
}


// *** SINGLE STEPS ***

function stepforward() {
singlestep=1;
resetbutton("stepright","steprightD");
setTimeout('stepforward2()',50);
}

function stepforward2() {
if(animation_status==0) {animation_status=2; playB2();}
else playB3();
}

function stepback() {
if(animation_status<0) animation_status=2;
resetbutton("stepleft","stepleftD");
setTimeout('stepback1()',10);
}

function stepback1() {
setTimeout('resetbutton("stepleft","stepleft")',200);
showrightbutton();
map.removeOverlay(mapsymbol[pathcount]);
pathcount--;
map.removeOverlay(mainpath[pathcount]);
blob.setPoint(new GLatLng(latdata[thisherd][thismonth][pathcount],lngdata[thisherd][thismonth][pathcount]));
if(pathcount==0) {hideleftbutton();document.getElementById("report").style.visibility="hidden";}
pathcount--;
create_report();
show_report();
pathcount++;
}



function create_report() {
reportstr="";
for(r=0;r<=pathcount;r++) {
thissymbol="";
thismessage="";
if(activity[thisherd][thismonth][r]=="C") {thissymbol="crop"; thismessage="damage to crops";}
if(activity[thisherd][thismonth][r]=="X") {thissymbol="injury"; thismessage="human injury";}
if(activity[thisherd][thismonth][r]=="H" || activity[thisherd][thismonth][r]=="P") {thissymbol="damage"; thismessage="damage to property";}

if(r==pathcount) reportstr+="<div style='color: red;'>";

reportstr+="<span class='bold'>"+daynum[thisherd][thismonth][r]+" "+months[monthnum[thisherd][thismonth][r]]+" "+yearnum[thisherd][thismonth][r]+"</span><div id='spacer4'></div>distance travelled (km)<div id='report_left'><span class='bold'>"+legdist[thisherd][thismonth][r]+"</span></div><div id='report_right'>total <span class='bold'>"+distance[thisherd][thismonth][r]+"</span></div><div style='clear:both;'></div>";

if(thissymbol>"") reportstr+="<div id='spacer4'></div><div id='report_left2'><img src='markers/"+thissymbol+".gif'></div><div id='report_right2'>"+thismessage+"</div><div style='clear:both;'></div>";
if(r==pathcount) reportstr+="</div>";
reportstr+="<div id='spacer1'></div><hr>";
}
}


function show_report() {
document.getElementById("report").innerHTML=reportstr;
if(reportDiv.scrollHeight>0) reportDiv.scrollTop=reportDiv.scrollHeight;
else reportDiv.scrollTop=reportDiv.offsetHeight;
}

function showpic() {
document.getElementById("elepic").style.visibility="visible";
document.getElementById("closepic").style.visibility="visible";
}

function closepic() {
document.getElementById("elepic").style.visibility="hidden";
document.getElementById("closepic").style.visibility="hidden";
}
