

var locs=[], gmap, gdir, baseIcon, gmapTabs

var gmapAreas = {
  'dnk': {centerLatLng:new GLatLng(56.353078,10.854492),initialZoomLevel:6},
  'swe': {centerLatLng:new GLatLng(58.367156,15.205078),initialZoomLevel:6},
  'pol': {centerLatLng:new GLatLng(52.321911,19.248047, 13.886719),initialZoomLevel:6}
};
		
		
function onGDirectionsLoad() {
};

function loadGMap(gmapElement, countryCode, initialLocation) { 
  if (GBrowserIsCompatible()) { 
    gmap = new GMap2(document.getElementById(gmapElement)); 
    gdir = new GDirections(gmap, document.getElementById("gmap_directions"));
    GEvent.addListener(gdir, "load", onGDirectionsLoad);
    
    window.onunload = GUnload;
    
    gmap.setMapType(G_NORMAL_MAP);     

    gmap.setCenter(gmapAreas[countryCode].centerLatLng, gmapAreas[countryCode].initialZoomLevel);
    
    gmap.addControl(new GLargeMapControl());
    
    gmap.addControl(new GMapTypeControl());
    
    gmap.addControl(new GOverviewMapControl());
    gmap.enableDoubleClickZoom();
    gmap.enableContinuousZoom();
    gmap.enableScrollWheelZoom();
    
    if (countryCode=='dnk') locs['taa']=new TIlocation('taa',14,new GLatLng(55.658748,12.271264),'Gregersensvej 3, 2630 Taastrup, Denmark (Teknologisk Institut)', baseIcon, 'Teknologisk Institut, Taastrup','Gregersensvej 3<br />2630 Taastrup',null,'+45 7220 2000','+45 7220 2019','info@teknologisk.dk','www.teknologisk.dk');
    if (countryCode=='dnk') locs['jyl']=new TIlocation('jyl',15,new GLatLng(55.756854535748374,12.131073474884033),'Møllehaven 16, 4040 Jyllinge, Denmark (Danfysik)', baseIcon, 'Danfysik, Jyllinge','Møllehaven 16<br />4040 Jyllinge',null,'+45 7220 2400','+45 7220 2410','sales@danfysik.com','www.danfysik.com');
    if (countryCode=='dnk') locs['ros']=new TIlocation('ros',15,new GLatLng(55.631055,12.078609),'Maglegårdsvej 2, 4000 Roskilde, Denmark (DMRI)', baseIcon, 'DMRI, Roskilde','Maglegårdsvej 2<br />4000 Roskilde',null,'+45 7220 2000','+45 7220 2744','dmri@teknologisk.dk','www.teknologisk.dk/dmri');
    if (countryCode=='dnk') locs['aar']=new TIlocation('aar',15,new GLatLng(56.1391,10.1863),'Kongsvang Allé 29, Århus C, Denmark (Teknologisk Institut)', baseIcon, 'Teknologisk Institut, Aarhus','Kongsvang Allé 29<br />8000 Århus C',null,'+45 7220 2000','+45 7220 1019','info@teknologisk.dk','www.teknologisk.dk');
    if (countryCode=='dnk') locs['kol']=new TIlocation('kol',15,new GLatLng(55.4846,9.4484),'Holbergsvej 10, 6000 Kolding, Denmark (Teknologisk Institut)',baseIcon, 'Teknologisk Institut, Kolding','Holbergsvej 10<br />6000 Kolding',null,'+45 7220 1900','+45 7220 1919');
    if (countryCode=='dnk') locs['ode']=new TIlocation('ode',15,new GLatLng(55.3695,10.4133),'Forskerparken 10 C, 5230 Odense M, Denmark (Teknologisk Institut)', baseIcon, 'Teknologisk Institut, Odense','Forskerparken Fyn<br />Forskerparken 10 C<br />5230 Odense M',null,'+45 7220 2000','+45 7220 3970');
    if (countryCode=='dnk') locs['hir']=new TIlocation('hir',15,new GLatLng(57.5868,9.9798),'Willemoesvej 2, 9850 Hirtshals, Denmark (Teknologisk Institut)',baseIcon, 'Teknologisk Institut, Hirtshals','Nordsøcentret<br />Postboks 104<br />9850 Hirtshals',null,'+45 7220 3930','+45 7220 3944');
    if (countryCode=='dnk') locs['got']=new TIlocation('got',15,new GLatLng(57.7037,11.9648),'Vallgatan 14, 411 16 Göteborg, Sweden (Teknologisk Institut)',baseIcon, 'Teknologisk Institut AB, Göteborg','Vallgatan 14<br />411 16 Göteborg<br />Sverige','<strong>Postadresse:</strong> Box 2513, 403 17 Göteborg','+46 (0) 31 350 55 00','+46 (0) 31 350 55 10','info@teknologiskinstitut.se','www.teknologiskinstitut.se');
    if (countryCode=='dnk') locs['sto']=new TIlocation('sto',15,new GLatLng(59.33447,18.054357),'Olof Palmes Gata 31, 11112 Stockholm, Sweden (Teknologisk Institut)', baseIcon, 'Teknologisk Institut AB, Stockholm','Olof Palmes Gata 31<br />111 12 Stockholm<br />Sverige','<strong>Postadresse:</strong> Box 823, 101 36 Stockholm','+46 (0) 31 350 55 00','+46 (0) 31 350 55 10','info@teknologiskinstitut.se','www.teknologiskinstitut.se');
    if (countryCode=='dnk') locs['upv']=new TIlocation('upv',15,new GLatLng(59.517856,17.898424),'Anton Tamms väg 1, 19434 Upplands Väsby, Sweden (Teknologisk Institut)', baseIcon, 'Teknologisk Institut Akademi','Anton Tamms väg 1<br />194 34 Upplands Väsby<br />Sverige',null,'+46 (0) 8 590 73 610','+46 (0) 8 590 73 880','info@teknologiskinstitut.se','www.teknologiskinstitut.se');
    if (countryCode=='dnk') locs['kar']=new TIlocation('kar',15,new GLatLng(56.1814,15.5911),'37179 Karlskrona, Sweden (Campus Gräsvik)', baseIcon, 'SWEDCERT AB, Karlskrona','Campus Gräsvik 1<br />371 75 Karlskrona<br />Sverige',null,'+46 (0) 455 305600','+46 (0) 455 10436','office@swedcert.se','www.swedcert.se');  
    if (countryCode=='dnk') locs['war']=new TIlocation('war',15,new GLatLng(52.1700,21.0716),'ul. Marconich 9, 02-954 Warszawa, Polska', baseIcon, 'Firma 2000 Sp. z o.o.', 'Ul. Marconich 9 lok. 19<br />02-954 Warszawa <br />Polen',null,'+48 (22) 642-58-72','+28 (22) 642-58-73 ','mail@firma2000.pl','www.firma2000.pl');  

    if (countryCode=='swe') locs['got']=new TIlocation('got',16,new GLatLng(57.7037,11.9648),'Vallgatan 14, 41116 Göteborg, Sweden (Teknologisk Institut)', baseIcon, 'Teknologisk Institut AB, Göteborg','Vallgatan 14<br />411 16 Göteborg','<strong>Postadress:</strong> Box 2513, 403 17 Göteborg','031-350 55 00','031-350 55 10','info@teknologiskinstitut.se','www.teknologiskinstitut.se','http://kartor.eniro.se/query?&what=map_adr&mop=aq&searchInMap=1&mapstate=7;11.965082;57.703709;s;11.950748489763734;57.708120445689346;11.979404413668263;57.699305049046075;1037;602&mapcomp=;;;Vallgatan;14;;41116;GÖTEBORG;;;;;11.965081809379752;57.703708835708746;0;0;;GÖTEBORG;maps_address.262679.11;0&geo_area=Vallgatan%2014,%20Göteborg&stq=0&pis=0',280);
    if (countryCode=='swe') locs['sto']=new TIlocation('sto',15,new GLatLng(59.33447,18.054357),'Olof Palmes Gata 31, 11112 Stockholm, Sweden (Teknologisk Institut)', baseIcon, 'Teknologisk Institut AB, Stockholm','Olof Palmes Gata 31<br />111 12 Stockholm','<strong>Postadress:</strong> Box 823, 101 36 Stockholm<br /><strong>Godsadress:</strong> Östra Järnvägsgatan 24, 111 20 Stockholm','031-350 55 00','031-350 55 10','info@teknologiskinstitut.se','www.teknologiskinstitut.se','http://kartor.eniro.se/query?&what=map_adr&mop=aq&searchInMap=1&mapstate=7;18.054699;59.334449;s;18.037051185903145;59.33993529675126;18.07233914619831;59.328981946769254;1277;748&mapcomp=;;;Olof%20palmes%20gata;31;;11122;STOCKHOLM;;;;;18.054699112475056;59.334448903301556;0;0;;STOCKHOLM;maps_address.884021.11;0&geo_area=Olof%20palmes%20gata%2031,%20STOCKHOLM&stq=0&pis=0',330);
    if (countryCode=='swe') locs['upv']=new TIlocation('upv',15,new GLatLng(59.517856,17.898424),'Anton Tamms väg 1, 19434 Upplands Väsby, Sweden (Teknologisk Institut)', baseIcon, 'Teknologisk Institut Akademi','Anton Tamms väg 1<br />194 34 Upplands Väsby',null,'08-590 73 610','08-590 73 880','info@teknologiskinstitut.se','www.teknologiskinstitut.se','http://kartor.eniro.se/query?&what=map_adr&mop=aq&searchInMap=1&mapstate=7;17.898580;59.517831;s;17.880921995873645;59.52331604785367;17.91620995616881;59.512362697871666;1277;748&mapcomp=;;;Anton%20tamms%20väg;1;;19434;UPPLANDS%20VÄSBY;;;;;17.898579640695633;59.51783111947368;0;0;;UPPLANDS-VÄSBY;maps_address.1117644.11;0&geo_area=Anton%20Tamms%20väg%201,%2019434%20Upplands%20Väsby&stq=0&pis=0');

    if (initialLocation) TIGmapZoom(initialLocation,true);

  };
  
  function createTIMarker(point, baseIcon) {
    var marker = new GMarker(point, { icon:new GIcon(baseIcon)});       
    return marker;
  };

  function TIlocation(locCode, zoom, LatLng, gdirAddress, baseIcon, title, address, altAddress, phone, fax, email, www, eniro, width) {
    var spacing=3;
    if (!width) width=270;
    this.marker = createTIMarker(LatLng, baseIcon); 
    this.LatLng = LatLng;
    this.zoom = zoom;
    this.title = title;
    this.gdirAddress = gdirAddress;
    this.locCode = locCode;
    
    this.infoWindow = document.createElement('div');
    
    this.infoWindow.style.lineHeight = '120%';
    this.infoWindow.style.fontSize = '11px';
    this.infoWindow.style.width = width+'px';
    
    var innerHTML = '<div style="float:left;width:30px"><img src="/_/images/common/GMapsTILogo.gif" /></div><div style="float:left;width:'+(width-30)+'px"><div style="font-weight:bold;font-size:13px">' + title + '</div><div style="margin-top:'+spacing+'px">' + address + '</div>';
		if (altAddress) innerHTML = innerHTML + '<div style="margin-top:'+spacing+'px">' +altAddress + '</div>';
    innerHTML = innerHTML + '<div style="margin-top:'+spacing+'px">';
    if (phone) innerHTML = innerHTML + '<div style="float:left;width:45px">Telefon:</div><div style="float:left">' + phone + '</div><br />';
    if (fax) innerHTML = innerHTML + '<div style="float:left;width:45px">Fax:</div><div style="float:left">' + fax + '</div>';
    innerHTML = innerHTML + '<div style="clear:both;overflow:hidden;height:0px">&#160;</div></div>';
    if ((email) || (www)) innerHTML = innerHTML + '<div>';
    if (email) innerHTML = innerHTML + '<div style="margin-top:'+spacing+'px"><a href="mail' + 'to:' + email + '">' + email + '</a></div>';
    if (www) innerHTML = innerHTML + '<div style="margin-top:'+spacing+'px"><a href="http://' + www + '">' + www + '</a></div>';
    if ((email) || (www)) innerHTML = innerHTML + '</div>';
    innerHTML = innerHTML + '<div style="clear:both;overflow:hidden;height:0px">&#160;</div></div>';
    innerHTML = innerHTML + '<div style="margin-left:30px">'; 
    innerHTML = innerHTML + '<div style="clear:both;margin-top:5px;margin-bottom:5px;background:#E0E0E0;overflow:hidden;height:1px">&#160;</div><div>';
    innerHTML = innerHTML + '<a href="javascript:TIGmapZoom(\'' + locCode + '\')">Zoom in</a>&#160;&#160;';
    innerHTML = innerHTML + '<a href="javascript:TIGmapDirections(\'' + locCode + '\',false)">Från</a>&#160;&#160;<a href="javascript:TIGmapDirections(\'' + locCode + '\',true)">Till</a>&#160;&#160;';
    if (eniro) innerHTML = innerHTML + '<a href="' + eniro + '" target="_new">Länk till Eniro</a>';
    innerHTML = innerHTML + '</div></div>';
    this.infoWindow.innerHTML = innerHTML;
    
    gmap.addOverlay(this.marker);
    //this.marker.bindInfoWindow(this.infoWindow);

    //GEvent.addListener(gmap, "infowindowopen", function() {locs[locCode].marker.hide()});
    GEvent.addListener(gmap, "infowindowclose", function() {locs[locCode].marker.show();});
    GEvent.addListener(gdir, "error", TIhandleDirectionErrors);

    
		GEvent.addListener(this.marker, "click", function() {
			if (!hasCloseMarker(locCode)) {
				openInfoWindow(locCode);
			};
    });      
  };
};

function TIGmapHideMarker(locCode) {
	locs[locCode].marker.hide();
};

function TIGmapDirections(locCode,toHere) {
  var gDirForm = document.getElementById('gdir_form');
	if (locCode) {
		var title = locs[locCode].title;
		if (toHere) gDirForm.gdir_to.value = title;
		if (!toHere) gDirForm.gdir_from.value = title;
		gmap.closeInfoWindow();
	};
	var err = validateFieldNonEmpty((!locCode), gDirForm.gdir_from,'Från');
	if (!err) err=validateFieldNonEmpty((!locCode), gDirForm.gdir_to,'Till');
	if (!err) {
		var dFrom=gDirForm.gdir_from.value, dTo=gDirForm.gdir_to.value;
		for (var lc in locs) {
			var loc = locs[lc];
			if (loc.title==dFrom) dFrom = loc.LatLng.lat()+','+loc.LatLng.lng();
			if (loc.title==dTo) dTo = loc.LatLng.lat()+','+loc.LatLng.lng();
		};
	  gdir.handleErrors=true;
	  gdir.load('from: ' + dFrom + ' to: ' + dTo);
	};
};

function TIGmapReverseRoute() {
  var gDirForm = document.getElementById('gdir_form');
	var tmp = gDirForm.gdir_to.value;
	gDirForm.gdir_to.value = gDirForm.gdir_from.value;
	gDirForm.gdir_from.value = tmp;
	TIGmapDirections();
};

function TIGmapZoom(locCode,openInfo) {
  var loc = locs[locCode];

  gmap.setZoom(loc.zoom);

  gmapTabs.open(0);
  if (openInfo) 
		openInfoWindow(locCode)
	else 
		gmap.closeInfoWindow();
	
};
  

function openInfoWindow(locCode) {
	gmap.openInfoWindow(locs[locCode].LatLng,locs[locCode].infoWindow);
	locs[locCode].marker.hide();
};
    
function initGmap(siteHostRootURL, countryCode, initialLocation) {

	gmapTabs = new tabList();
	gmapTabs.add('tab1','pane1','active');
	gmapTabs.add('tab2','pane2');
	
	baseIcon = new GIcon();
	baseIcon.image = siteHostRootURL + '_/images/common/GMapsTILogo.png';
	baseIcon.iconSize = new GSize(30, 39);3
	baseIcon.shadowSize = new GSize(37, 34);
	baseIcon.iconAnchor = new GPoint(7, 39);
	baseIcon.infoWindowAnchor = new GPoint(7, 39);
	baseIcon.infoShadowAnchor = new GPoint(0, 0); 
	

	loadGMap('gmap', countryCode, initialLocation);
};

function TIhandleDirectionErrors() {
	if (gdir.handleErrors) {
		gdir.handleErrors = false;
		switch (gdir.getStatus().code) {
			case G_GEO_UNKNOWN_ADDRESS:
				alert('En av de angivna adresserna kunde inte placeras på kartan. Detta kan bero på att adressen felaktig eller ännu inte känd av Google Maps.'); break;
			// case G_GEO_SERVER_ERROR:
			//	alert('A geocoding or directions request could not be successfully processed, yet the exact reason for the failure is not known.\n Error code: ' + gdir.getStatus().code); break;
			// case G_GEO_MISSING_QUERY:
			//	alert('The HTTP q parameter was either missing or had no value. For geocoder requests, this means that an empty address was specified as input. For directions requests, this means that no query was specified in the input.\n Error code: ' + gdir.getStatus().code);
			// case G_GEO_BAD_KEY:
			//	alert('The given key is either invalid or does not match the domain for which it was given. \n Error code: ' + gdir.getStatus().code); break;
			// case G_GEO_BAD_REQUEST:
			//	alert('A directions request could not be successfully parsed.\n Error code: ' + gdir.getStatus().code); break;
			default:
				alert('På grund av ett okänt fel kunde rutten inte beräknas. ');
		};		 
	};
};

function hasCloseMarker(locCode) {
  var minDist=30.0,b=2.0040000840272;
  
  var clickedLocationPoint = gmap.fromLatLngToContainerPixel(locs[locCode].LatLng);
  for (var lc in locs) {
		if (lc!=locCode) {
			var ldist = dist(locs[locCode].LatLng, locs[lc].LatLng),pdist=Math.pow(b,gmap.getZoom())*ldist;
			if ((pdist < minDist) && (pdist>0)) {
				var newZoom = Math.ceil(Math.log(minDist/ldist)/Math.log(b));
				gmap.setCenter(locs[locCode].LatLng, newZoom);
				return true;        
			};
		};
  };
	return false;
};

function TIgmapSetArea(countryCode) {
  gmap.setCenter(gmapAreas[countryCode].centerLatLng, gmapAreas[countryCode].initialZoomLevel);
};
    
function dist(c1,c2) {
  return Math.sqrt(Math.pow(c1.lng()-c2.lng(),2)+Math.pow(c1.lat()-c2.lat(),2));
};   
