﻿var vehicles;var icon;var map;var text;var objXml;var coordinateArray;var coordinates;var l;var line;var lat;var lng;var g;var imageDirection;var i;var request;var marker;var latlng;var type;var mainContainerDiv;var mapContainerDiv;var toolsDiv;var logoDiv;var vehiclePopupDiv;var vehiclePopupVehicle;var vehiclePopupTitleDiv;var nextStopsDiv;var stopNextDepDiv;var X;var T;var V;var U;var panel;var stopPopupMarker;var stopPopupDiv;var stopPopDiv;var stopPopupStop;var checkboxRoute;var checkboxStops;var checkboxFollow;var addressMarker;var pointsString;var levelsString;var routeBounds;var routePolyline;var MY_MAX_ZOOM_LEVEL=12;function resizeUI(){mainContainerDiv.style.width=document.body.clientWidth+"px";mainContainerDiv.style.height=document.body.clientHeight+"px";mapContainerDiv.style.width=mainContainerDiv.offsetWidth-toolsDiv.offsetWidth-30+"px";mapContainerDiv.style.height=mainContainerDiv.offsetHeight-mapContainerDiv.offsetTop-15+"px";toolsDiv.style.left=mainContainerDiv.offsetWidth-toolsDiv.offsetWidth-10+"px";toolsDiv.style.height=mapContainerDiv.offsetHeight-10+"px";logoDiv.style.left=mainContainerDiv.offsetWidth-logoDiv.offsetWidth-10+"px";};function load(){if(GBrowserIsCompatible()){vehicles=new Vehicles();stops=new Stops();mainContainerDiv=document.getElementById("main");mapContainerDiv=document.getElementById("map");toolsDiv=document.getElementById("tools");logoDiv=document.getElementById("logo");vehiclePopupDiv=document.getElementById("vehiclepopup");nextStopsDiv=document.getElementById("nextstops");vehiclePopupTitleDiv=document.getElementById("vehiclepopuptitle");stopPopupDiv=document.getElementById("stoppopup");stopNextDepDiv=document.getElementById("nextdepartures");X=document.getElementById("Div2");T=document.getElementById("Div3");V=document.getElementById("aa");U=document.getElementById("ab");panel=document.getElementById("ctl09_Panel1");X.style.visibility="hidden";T.style.visibility="hidden";V.style.visibility="hidden";U.style.visibility="hidden";resizeUI();map=new GMap2(document.getElementById("map"));map.setCenter(new GLatLng(60.19,24.96),12);map.enableContinuousZoom();map.enableDoubleClickZoom();map.enableScrollWheelZoom();map.addControl(new GSmallMapControl());map.addControl(new GMapTypeControl());drawOverlay('');addressMarker=new GMarker(new GLatLng(0,0),{clickable:false,bouncy:false});map.addOverlay(addressMarker);checkboxRoute=document.getElementById("CheckboxRoute");checkboxStops=document.getElementById("CheckboxStops");checkboxFollow=document.getElementById("CheckboxFollow");var M=map.getMapTypes();for(var i=0;i<M.length;i++){M[i].getMinimumResolution=function(){return 12;}}GEvent.addListener(map,"move",function(){updateVehiclePopupLocation();updateStopPopupLocation();});GEvent.addListener(map,"click",function(marker){if(marker!=null){var oldSelection=vehiclePopupVehicle;var selectedObject=vehicles.findVehicleByMarker(marker);if(selectedObject!=null){if(vehiclePopupVehicle!=null&&vehiclePopupVehicle.j()==marker)onCloseVehiclePopup();else{vehiclePopupVehicle=selectedObject;if(checkboxRoute.checked)displayRoute(vehiclePopupVehicle.K(),vehiclePopupVehicle.H());if(checkboxStops.checked)displayStops(vehiclePopupVehicle.K(),vehiclePopupVehicle.H());setVehiclePopup();updateVehiclePopupLocation();vehiclePopupDiv.style.visibility="visible";X.style.visibility="visible";T.style.visibility="visible";V.style.visibility="visible";U.style.visibility="visible";}if(oldSelection!=null)oldSelection.updateIcon();if(vehiclePopupVehicle!=null)vehiclePopupVehicle.updateIcon();}else{if(marker==stopPopupMarker)onCloseStopPopup();else{selectedObject=stops.findStopByMarker(marker);if(selectedObject!=null)onSelectStop(selectedObject.B());}}}});}else{alert("Google Maps ei ole yhteensopiva selaimesi kanssa.");}};function addLineByName(line){var k="1001";var f="2001";var letterLine="";var letter="";if(line.length==1){f="200"+line;k="100"+line;}else if(line.length==2){f="20"+line;k="10"+line;letterLine="100"+line;}else if(line.length==3){f="2"+line;letterLine="10"+line;}if(line!=null||line!=""||line.length<5)drawOverlay("&lines="+k+letter+".1_"+k+letter+".2_"+f+letter+".1_"+f+letter+".2_"+letterLine+".1_"+letterLine+".2");};var lastCheck=0;var Check=0;var val;function resetVal(){val="";};var rectPolyline;function O(){request="Query.aspx?type=vehicles";request+="&lat1="+map.getBounds().getSouthWest().lat();request+="&lng1="+map.getBounds().getSouthWest().lng();request+="&lat2="+map.getBounds().getNorthEast().lat();request+="&lng2="+map.getBounds().getNorthEast().lng();request+="&online=1";request+="&localdata=hkl";request+="&a="+Math.random();GDownloadUrl(request,v);};function drawOverlay(value){var pointList=new Array();var height=(map.getBounds().getNorthEast().lng()-map.getBounds().getSouthWest().lng())/2+0.08;var width=(map.getBounds().getSouthWest().lat()-map.getBounds().getNorthEast().lat())/2+0.04;request="Query.aspx?type=vehicles";request+="&lat1="+(map.getBounds().getSouthWest().lat());request+="&lng1="+(map.getBounds().getSouthWest().lng());request+="&lat2="+(map.getBounds().getNorthEast().lat());request+="&lng2="+(map.getBounds().getNorthEast().lng());if(value!="")val=value;lastCheck=Check;if(document.getElementById("radioBus").checked){Check=2;request+="&online=2";}else if(document.getElementById("radioTram").checked){Check=3;request+="&online=3";}else if(document.getElementById("radioAll").checked){Check=4;request+="&online=1";}else request+=val;request+="&localdata=hkl";if(value!=""){request+=val;vehicles.removeAllVehicles();document.getElementById("radioTram").checked=false;document.getElementById("radioBus").checked=false;document.getElementById("radioAll").checked=false;}if(lastCheck!=Check){vehicles.removeAllVehicles();val="";}request+="&a="+Math.random();if(value=="")GDownloadUrl(request,v);};function findLine(line){var req="Query.aspx?type=vehicles";req+="&lat1="+map.getBounds().getSouthWest().lat();req+="&lng1="+map.getBounds().getSouthWest().lng();req+="&lat2="+map.getBounds().getNorthEast().lat();req+="&lng2="+map.getBounds().getNorthEast().lng();req+="&lines="+line+".1_"+line+".2";GDownloadUrl(req,v);};function setStopPopup(){if(stopPopupMarker!=null){GDownloadUrl("Query.aspx?type=stopview&stop="+stopPopupStop.B()+"&a="+Math.random(),createStopPopup);}};function createStopPopup(text){stopNextDepDiv.innerHTML=text;};function setVehiclePopup(){if(vehiclePopupVehicle!=null){var Q=document.getElementById("Div2");var P=document.getElementById("Div3");Q.innerHTML=vehiclePopupVehicle.getOperator();P.innerHTML=vehiclePopupVehicle.getName();GDownloadUrl("Query.aspx?type=nextstops&count=50&id="+vehiclePopupVehicle.B()+"&a="+Math.random(),createVehiclePopup);}};function updateStopPopupLocation(){if(stopPopupStop!=null&&stopPopupMarker!=null&&map.getCenter()!=null){var CDivPixel=map.fromLatLngToDivPixel(map.getCenter());var pointDivPixel=map.fromLatLngToDivPixel(stopPopupMarker.getPoint());var fromCenter=subGPoints(pointDivPixel,CDivPixel);var left=fromCenter.x+mapContainerDiv.offsetLeft-stopPopupDiv.offsetWidth/2;var top=fromCenter.y+mapContainerDiv.offsetTop-stopPopupDiv.offsetHeight-30;if(top<mapContainerDiv.offsetTop)top=top+stopPopupDiv.offsetHeight+40;if(left<mapContainerDiv.offsetLeft)left=mapContainerDiv.offsetLeft;if(left>mapContainerDiv.offsetLeft+mapContainerDiv.offsetWidth-stopPopupDiv.offsetWidth)left=mapContainerDiv.offsetLeft+mapContainerDiv.offsetWidth-stopPopupDiv.offsetWidth;if(top<mapContainerDiv.offsetTop)top=mapContainerDiv.offsetTop;if(top>mapContainerDiv.offsetTop+mapContainerDiv.offsetHeight-stopPopupDiv.offsetHeight)top=mapContainerDiv.offsetTop+mapContainerDiv.offsetHeight-stopPopupDiv.offsetHeight;stopPopupDiv.style.left=left+"px";stopPopupDiv.style.top=top+"px";}};function createVehiclePopup(text){var html="";var tokens=text.split("\r\n");if(tokens.length>0)vehiclePopupTitleDiv.innerHTML=tokens[0];else vehiclePopupTitleDiv.innerHTML="Not on route";for(var i=1;i<tokens.length;i++){var stopInfo=tokens[i].split('_');if(stopInfo.length>3){if(stopInfo[3]=="current")html+="<div class=\"currentstop\">";if(stopInfo[3]=="normal"){if(i==1)html+="<div class=\"nextstop\">";else if(i==tokens.length-2)html+="<div class=\"laststop\">";else html+="<div class=\"normalstop\">";}if(stopInfo[3]=="last"){html+="<div class=\"normalstop\">...</div>";html+="<div class=\"laststop\">";}if(i!=1&&stopInfo[1]!=" ")html+="<div style=\"float:right\">"+stopInfo[1]+" min</div>";html+="<a style=\"cursor:pointer\" onclick=\"onSelectStop("+stopInfo[0]+")\">";html+=stopInfo[2]+"</a></div>";}}nextStopsDiv.innerHTML=html;updateVehiclePopupLocation();};function updateVehiclePopupLocation(){if(vehiclePopupVehicle!=null&&map.getCenter()!=null){var CDivPixel=map.fromLatLngToDivPixel(map.getCenter());var pointDivPixel=map.fromLatLngToDivPixel(vehiclePopupVehicle.j().getPoint());var fromCenter=subGPoints(pointDivPixel,CDivPixel);var left=fromCenter.x+mapContainerDiv.offsetLeft-vehiclePopupDiv.offsetWidth/2;var top=fromCenter.y+mapContainerDiv.offsetTop-vehiclePopupDiv.offsetHeight-25;if(top<mapContainerDiv.offsetTop)top=top+vehiclePopupDiv.offsetHeight+50;if(left<mapContainerDiv.offsetLeft)left=mapContainerDiv.offsetLeft;if(left>mapContainerDiv.offsetLeft+mapContainerDiv.offsetWidth-vehiclePopupDiv.offsetWidth)left=mapContainerDiv.offsetLeft+mapContainerDiv.offsetWidth-vehiclePopupDiv.offsetWidth;if(top<mapContainerDiv.offsetTop)top=mapContainerDiv.offsetTop;if(top>mapContainerDiv.offsetTop+mapContainerDiv.offsetHeight-vehiclePopupDiv.offsetHeight)top=mapContainerDiv.offsetTop+mapContainerDiv.offsetHeight-vehiclePopupDiv.offsetHeight;vehiclePopupDiv.style.left=left+"px";vehiclePopupDiv.style.top=top+"px";}};function subGPoints(a,b){return new GPoint(a.x-b.x+mapContainerDiv.offsetWidth/2,a.y-b.y+mapContainerDiv.offsetHeight/2);};function onCloseVehiclePopup(){var oldSelection=vehiclePopupVehicle;vehiclePopupVehicle=null;vehiclePopupDiv.style.visibility="hidden";addressKeyDown("13");X.style.visibility="hidden";T.style.visibility="hidden";V.style.visibility="hidden";U.style.visibility="hidden";if(oldSelection!=null)oldSelection.updateIcon();onCloseStopPopup();if(routePolyline)try{map.removeOverlay(routePolyline);}catch(error){}stops.clearStops();};function onCloseStopPopup(){if(stopPopupStop!=null&&stopPopupMarker!=null){if(stopPopupStop.getType()=="tram")stopPopupMarker.setImage("images/tram_stop_small.png");else if(stopPopupStop.getType()=="metro")stopPopupMarker.setImage("images/metro_stop_small.png");else stopPopupMarker.setImage("images/bus_stop_small.png");}stopPopupStop=null;stopPopupMarker=null;stopPopupDiv.style.visibility="hidden";};function v(text){coordinateArray=text.split("\r\n");for(i=0;i<coordinateArray.length-1;i++){coordinates=coordinateArray[i].split(";");l=coordinates[0];line=coordinates[1];lat=(Number)(coordinates[3]);lng=(Number)(coordinates[2]);g=(Number)(coordinates[4]);direction=(Number)(coordinates[5]);lastStop=coordinates[6];currentStop=coordinates[7];type=coordinates[9];transportOperator=coordinates[10];speed=coordinates[13];name=coordinates[11];if(line!=null&&line!=""){if(line.split(" ").length>0)line=line.split(" ")[0];if(line.length>5)line=line.substring(0,5);if(type=="1"||(line.substring(0,3)=="100"||line.substring(0,4)=="1010"))vehicles.addVehicle(l,line,direction,"tram",lat,lng,g,lastStop,currentStop,type,name,transportOperator,speed);else vehicles.addVehicle(l,line,direction,"bus",lat,lng,g,lastStop,currentStop,type,name,transportOperator,speed);}}vehicles.removeOldVehicles();updateVehiclePopupLocation();updateStopPopupLocation();if(vehiclePopupVehicle!=null&&checkboxFollow.checked)map.panTo(new GLatLng(vehiclePopupVehicle.j().getPoint().lat(),vehiclePopupVehicle.j().getPoint().lng()));setTimeout("drawOverlay('')",1000);}