﻿//<![CDATA[
var map;
var geocoder;
var blueIcon0 = new GIcon();
blueIcon0.image = "http://www.bayrol.de/fileadmin/templates/images/umkreissuche/mark1.png";
blueIcon0.iconSize = new GSize(32, 32);
blueIcon0.iconAnchor = new GPoint(16, 32);
blueIcon0.infoWindowAnchor = new GPoint(24, 0);
blueIcon0.shadow = "http://www.bayrol.de/fileadmin/templates/images/umkreissuche/base_shadow.png";
blueIcon0.shadowSize = new GSize(48, 32);

var blueIcon1 = new GIcon();
blueIcon1.image = "http://www.bayrol.de/fileadmin/templates/images/umkreissuche/mark1.png";
blueIcon1.iconSize = new GSize(32, 32);
blueIcon1.iconAnchor = new GPoint(16, 32);
blueIcon1.infoWindowAnchor = new GPoint(24, 0);
blueIcon1.shadow = "http://www.bayrol.de/fileadmin/templates/images/umkreissuche/base_shadow.png";
blueIcon1.shadowSize = new GSize(48, 32);

var blueIcon2 = new GIcon();
blueIcon2.image = "http://www.bayrol.de/fileadmin/templates/images/umkreissuche/mark2.png";
blueIcon2.iconSize = new GSize(32, 32);
blueIcon2.iconAnchor = new GPoint(16, 32);
blueIcon2.infoWindowAnchor = new GPoint(24, 0);
blueIcon2.shadow = "http://www.bayrol.de/fileadmin/templates/images/umkreissuche/base_shadow.png";
blueIcon2.shadowSize = new GSize(48, 32);

var blueIcon3 = new GIcon();
blueIcon3.image = "http://www.bayrol.de/fileadmin/templates/images/umkreissuche/mark3.png";
blueIcon3.iconSize = new GSize(32, 32);
blueIcon3.iconAnchor = new GPoint(16, 32);
blueIcon3.infoWindowAnchor = new GPoint(24, 0);
blueIcon3.shadow = "http://www.bayrol.de/fileadmin/templates/images/umkreissuche/base_shadow.png";
blueIcon3.shadowSize = new GSize(48, 32);

var blueIcon4 = new GIcon();
blueIcon4.image = "http://www.bayrol.de/fileadmin/templates/images/umkreissuche/mark4.png";
blueIcon4.iconSize = new GSize(32, 32);
blueIcon4.iconAnchor = new GPoint(16, 32);
blueIcon4.infoWindowAnchor = new GPoint(24, 0);
blueIcon4.shadow = "http://www.bayrol.de/fileadmin/templates/images/umkreissuche/base_shadow.png";
blueIcon4.shadowSize = new GSize(48, 32);

var blueIcon5 = new GIcon();
blueIcon5.image = "http://www.bayrol.de/fileadmin/templates/images/umkreissuche/mark5.png";
blueIcon5.iconSize = new GSize(32, 32);
blueIcon5.iconAnchor = new GPoint(16, 32);
blueIcon5.infoWindowAnchor = new GPoint(24, 0);
blueIcon5.shadow = "http://www.bayrol.de/fileadmin/templates/images/umkreissuche/base_shadow.png";
blueIcon5.shadowSize = new GSize(48, 32);

var blueIcon6 = new GIcon();
blueIcon6.image = "http://www.bayrol.de/fileadmin/templates/images/umkreissuche/mark6.png";
blueIcon6.iconSize = new GSize(32, 32);
blueIcon6.iconAnchor = new GPoint(16, 32);
blueIcon6.infoWindowAnchor = new GPoint(24, 0);
blueIcon6.shadow = "http://www.bayrol.de/fileadmin/templates/images/umkreissuche/base_shadow.png";
blueIcon6.shadowSize = new GSize(48, 32);

var blueIcon7 = new GIcon();
blueIcon7.image = "http://www.bayrol.de/fileadmin/templates/images/umkreissuche/mark7.png";
blueIcon7.iconSize = new GSize(32, 32);
blueIcon7.iconAnchor = new GPoint(16, 32);
blueIcon7.infoWindowAnchor = new GPoint(24, 0);
blueIcon7.shadow = "http://www.bayrol.de/fileadmin/templates/images/umkreissuche/base_shadow.png";
blueIcon7.shadowSize = new GSize(48, 32);

var blueIcon8 = new GIcon();
blueIcon8.image = "http://www.bayrol.de/fileadmin/templates/images/umkreissuche/mark8.png";
blueIcon8.iconSize = new GSize(32, 32);
blueIcon8.iconAnchor = new GPoint(16, 32);
blueIcon8.infoWindowAnchor = new GPoint(24, 0);
blueIcon8.shadow = "http://www.bayrol.de/fileadmin/templates/images/umkreissuche/base_shadow.png";
blueIcon8.shadowSize = new GSize(48, 32);

var blueIcon9 = new GIcon();
blueIcon9.image = "http://www.bayrol.de/fileadmin/templates/images/umkreissuche/mark9.png";
blueIcon9.iconSize = new GSize(32, 32);
blueIcon9.iconAnchor = new GPoint(16, 32);
blueIcon9.infoWindowAnchor = new GPoint(24, 0);
blueIcon9.shadow = "http://www.bayrol.de/fileadmin/templates/images/umkreissuche/base_shadow.png";
blueIcon9.shadowSize = new GSize(48, 32);

var blueIcon10 = new GIcon();
blueIcon10.image = "http://www.bayrol.de/fileadmin/templates/images/umkreissuche/mark10.png";
blueIcon10.iconSize = new GSize(32, 32);
blueIcon10.iconAnchor = new GPoint(16, 32);
blueIcon10.infoWindowAnchor = new GPoint(24, 0);
blueIcon10.shadow = "http://www.bayrol.de/fileadmin/templates/images/umkreissuche/base_shadow.png";
blueIcon10.shadowSize = new GSize(48, 32);

var blueIcon11 = new GIcon();
blueIcon11.image = "http://www.bayrol.de/fileadmin/templates/images/umkreissuche/mark11.png";
blueIcon11.iconSize = new GSize(32, 32);
blueIcon11.iconAnchor = new GPoint(16, 32);
blueIcon11.infoWindowAnchor = new GPoint(24, 0);
blueIcon11.shadow = "http://www.bayrol.de/fileadmin/templates/images/umkreissuche/base_shadow.png";
blueIcon11.shadowSize = new GSize(48, 32);

var blueIcon12 = new GIcon();
blueIcon12.image = "http://www.bayrol.de/fileadmin/templates/images/umkreissuche/mark12.png";
blueIcon12.iconSize = new GSize(32, 32);
blueIcon12.iconAnchor = new GPoint(16, 32);
blueIcon12.infoWindowAnchor = new GPoint(24, 0);
blueIcon12.shadow = "http://www.bayrol.de/fileadmin/templates/images/umkreissuche/base_shadow.png";
blueIcon12.shadowSize = new GSize(48, 32);

var blueIcon13 = new GIcon();
blueIcon13.image = "http://www.bayrol.de/fileadmin/templates/images/umkreissuche/mark13.png";
blueIcon13.iconSize = new GSize(32, 32);
blueIcon13.iconAnchor = new GPoint(16, 32);
blueIcon13.infoWindowAnchor = new GPoint(24, 0);
blueIcon13.shadow = "http://www.bayrol.de/fileadmin/templates/images/umkreissuche/base_shadow.png";
blueIcon13.shadowSize = new GSize(48, 32);

var blueIcon14 = new GIcon();
blueIcon14.image = "http://www.bayrol.de/fileadmin/templates/images/umkreissuche/mark14.png";
blueIcon14.iconSize = new GSize(32, 32);
blueIcon14.iconAnchor = new GPoint(16, 32);
blueIcon14.infoWindowAnchor = new GPoint(24, 0);
blueIcon14.shadow = "http://www.bayrol.de/fileadmin/templates/images/umkreissuche/base_shadow.png";
blueIcon14.shadowSize = new GSize(48, 32);

var blueIcon15 = new GIcon();
blueIcon15.image = "http://www.bayrol.de/fileadmin/templates/images/umkreissuche/mark15.png";
blueIcon15.iconSize = new GSize(32, 32);
blueIcon15.iconAnchor = new GPoint(16, 32);
blueIcon15.infoWindowAnchor = new GPoint(24, 0);
blueIcon15.shadow = "http://www.bayrol.de/fileadmin/templates/images/umkreissuche/base_shadow.png";
blueIcon15.shadowSize = new GSize(48, 32);


var homeIcon;
homeIcon = new GIcon();
homeIcon.image = "http://www.bayrol.de/fileadmin/templates/images/umkreissuche/base.png";
homeIcon.iconSize = new GSize(32, 32);
homeIcon.iconAnchor = new GPoint(16, 32);
homeIcon.infoWindowAnchor = new GPoint(24, 0);
homeIcon.shadow = "http://www.bayrol.de/fileadmin/templates/images/umkreissuche/base_shadow.png";
homeIcon.shadowSize = new GSize(48, 32);

function load() {
	if (GBrowserIsCompatible()) {
	map = new GMap2(document.getElementById("map"));
	var point = new GPoint("11.432688","48.108328");

	map.setCenter(new GLatLng(51.151786, 10.415039), 6, G_NORMAL_MAP);
	geocoder = new GClientGeocoder();
	/* #### adding controls to the map #### */
	map.addControl(new GLargeMapControl());
	map.addControl(new GMapTypeControl());

	
	/* set a marker to home location 
	
	// Set up our GMarkerOptions object
	markerOptions = { icon:homeIcon };
	info = '<div id="adressinfo">' +
	'<h3>BAYROL Deutschland GmbH</h3>' +
	'<img src="http://www.bayrol.de/fileadmin/user_upload/bilder/anfahrt/bayrol_gebaeude.jpg" width="200" /><br /><br />' +
	'Robert-Koch-Str. 4<br />' +
	'82152 Planegg-Steinkirchen<br /><br />' +
	'Telefon +49 / 89 / 85 701 - 0<br />' +
	'Fax +49 / 89 / 85 701 - 241<br /><br />' +
	'<a href="http://www.bayrol.de/">www.bayrol.de</a><br /><br />' +
	'<a href="http://maps.google.de/maps?f=d&hl=de&geocode=&saddr=&daddr=Robert-Koch-Str.+4,82152++Planegg&sll=51.151786,10.415039&sspn=9.169265,19.072266&ie=UTF8&t=h&z=16" target="_blank">Routenplan zu uns</a><br />' +
	'</div>';
	var marker = placeMarker(point, info, markerOptions);
	map.addOverlay(marker);
	
	*/
	}
}

// Creates a marker whose info window is displayed on click
function placeMarker(point, content, markerOptions){
	var marker = new GMarker(point, markerOptions);
	// Show this markers index in the info window when it is clicked
	var html = content;
	GEvent.addListener(marker, "click", function() {marker.openInfoWindowHtml(html);});
	return marker;
};

function createCircle(center, radius, color )
{
	var center_lat = center.lat() * Math.PI / 180;
	var center_lon = center.lng() * Math.PI / 180;
	var delta_lat = (center.lat()+1) * Math.PI / 180;
	var delta_lon = (center.lng()+1) * Math.PI / 180;
	
	
	var distLat = 6378.137 * Math.acos(Math.sin(center_lat)*Math.sin(delta_lat) + Math.cos(center_lat)*Math.cos(delta_lat)*Math.cos(center_lon-center_lon));
	var distLon = 6378.137 * Math.acos(Math.sin(center_lat)*Math.sin(center_lat) + Math.cos(center_lat)*Math.cos(center_lat)*Math.cos(delta_lon-center_lon));
	
	var pArray = new Array();	
	for(var i=0; i<360; i=i+5)
	{
		var ancleRad = i * Math.PI/180;
		var p = new GLatLng((radius/distLat * Math.cos(ancleRad))+center.lat(), (radius/distLon * Math.sin(ancleRad))+center.lng());
		pArray.push(p);
	}
	pArray.push(pArray[0]);
	if(color == "red"){
		return new GPolygon(pArray,"#FF0000",1,1,"#FFCCCC",0.3);
	}else if(color == "green"){
		return new GPolygon(pArray,"#00FF00",2,1,"#CCFFCC",0.4);
	}else{
		return new GPolygon(pArray,"#0000FF",1,1,"#FFFFFF",0.3);
	}
}

function searchLocations() {
	var homeaddress = document.getElementById('addressInput').value;
	geocoder.getLatLng(homeaddress, function(latlng) {
		var status = document.getElementById('status');
		if (!latlng) {
			document.getElementById('addressInput').className = "formfielderror";
			status.innerHTML = '<span class="err_msg">Adresse wurde nicht gefunden</span>';
	
		} else {
			document.getElementById('addressInput').className = "";
			status.innerHTML = "";
			searchLocationsNear(latlng);
		}
	});
}

function searchLocationsNear(center) {
	var radius = document.getElementById('radiusSelect').value;
	var centerAdd = document.getElementById('addressInput').value;
	var searchUrl = 'includes/umkreissuche2xml.php?glat=' + center.lat() + '&glong=' + center.lng() + '&radius=' + radius + '&centerAdd=' + encodeURIComponent(centerAdd);
	GDownloadUrl(searchUrl, function(data) {
		var xml = GXml.parse(data);
		var markers = xml.documentElement.getElementsByTagName('marker');

		var liste = document.getElementById('ergebnisliste');
		var status = document.getElementById('status');
		var intro = document.getElementById('suchintro');
		liste.innerHTML = '';
		status.innerHTML = '';
		document.getElementById('radiusSelect').className = "";
		var bounds = new GLatLngBounds();
		var suchinfo = "Suchadresse: <br />" + document.getElementById('addressInput').value;
		if (markers.length == 0) {
			if(document.getElementById('radiusSelect').value == "100"){
				status.innerHTML = '<span class="err_msg">Leider wurden keine Händler im Umkreis Ihrer angegebenen Adresse gefunden! Die Datenbank befindet sich momentan noch im Aufbau, bitte wenden Sie sich in der Zwischenzeit an unsere Service-Mitarbeiter, damit diese Ihnen Händler -Adressen in Ihrer Nähe mitteilen können.</span>';
			}else{
				status.innerHTML = '<span class="err_msg">Keine Händler im Umkreis gefunden. Versuchen Sie es mit einer größeren Entfernungsstufe!</span>';
			}
			intro.innerHTML = "<h1>Suchen Sie nach einem Händler in Ihrem Umkreis</h1><p>Geben Sie Ihre Adresse in die Suchmaske ein, um einen BAYROL-Händler in Ihrem Umkreis zu finden.</p>";
			document.getElementById('radiusSelect').className = "formfielderror";
			map.clearOverlays();
			// place the home marker
			markerOptions = { icon:homeIcon };

			marker = placeMarker(center, suchinfo, markerOptions);
			map.addOverlay(marker);
			var polygonecircle = new GPolygon(); 
			polygoncircle = createCircle(center, radius, "red");
			map.addOverlay(polygoncircle);
			var bounds1 = polygoncircle.getBounds();
      		var zoom = map.getBoundsZoomLevel(bounds1);  
     		var mapcenter = bounds1.getCenter();
     		map.setCenter(mapcenter,zoom);
			//bounds.extend(center);
			//map.setCenter(bounds.getCenter(), (map.getBoundsZoomLevel(bounds)-1));
			return;
		}

		// Set intro text
		
		intro.innerHTML = "<h1>" + markers.length + " Händler im Umkreis von <em>" + document.getElementById('addressInput').value +"</em> gefunden</h1><p>Weitere Informationen finden Sie unter den entsprechenden Links.</p>";
		map.clearOverlays();
		// place the marker for the search starting point
		markerOptions = { icon:homeIcon };
		marker = placeMarker(center, suchinfo, markerOptions);
		map.addOverlay(marker);
		bounds.extend(center);
		// create table for results
		liste.innerHTML += '<table width="695" border="0" cellpadding="4" cellspacing="4" id="result_table"><thead><tr><th align="left" valign="top" scope="col">Standort</th><th align="left" valign="top" scope="col">Optionen</th><th valign="top" scope="col">Entfernung</th><th valign="top" scope="col">Nr.</th></tr></thead></table>';
		
		// place markers for stores
		for (var i = 0; i < markers.length; i++) {
		var point = new GLatLng(parseFloat(markers[i].getAttribute('glat')),
								parseFloat(markers[i].getAttribute('glong')));
		var locationInfo = {	'company':markers[i].getAttribute('company'),
								'address':markers[i].getAttribute('address'),
								
								'name':markers[i].getAttribute('name'),
								'phone':markers[i].getAttribute('phone'),
								'fax':markers[i].getAttribute('fax'),
								'mobile':markers[i].getAttribute('mobile'),
								'email':markers[i].getAttribute('email'),
								'email_act':markers[i].getAttribute('email_act'),
								'www':markers[i].getAttribute('www'),
								'imagelink':markers[i].getAttribute('imagelink'),
								'description':markers[i].getAttribute('description'),
								'opening':markers[i].getAttribute('opening'),
								'isopen':markers[i].getAttribute('isopen'),
								'routeurl':markers[i].getAttribute('routeurl'),
								'custcat':markers[i].getAttribute('custcat'),
								'distance':parseFloat(markers[i].getAttribute('distance'))
								}
								  

		// Set up our GMarkerOptions object
		var opt = { icon:eval("blueIcon"+eval(i+1)) };
		var markertext = "<b>"+locationInfo.company+"</b><br />"+locationInfo.address;
		var marker = placeMarker(point, markertext, opt);
		map.addOverlay(marker);
		// write a list of the stores
		var sidebarEntry = createListEntry(marker, locationInfo, i);
		var resultTable = document.getElementById('result_table');
		resultTable.appendChild(sidebarEntry);
		bounds.extend(point);
		}
		var polygonecircle = new GPolygon(); 
		polygoncircle = createCircle(center, radius, "green");
		map.addOverlay(polygoncircle);
		var bounds1 = polygoncircle.getBounds();
		var zoom = map.getBoundsZoomLevel(bounds1);  
		var mapcenter = bounds1.getCenter();
		map.setCenter(mapcenter,zoom);
		//map.setCenter(bounds.getCenter(), (map.getBoundsZoomLevel(bounds)-1));
	});
}

function createListEntry(marker, locationInfo, counter) {
	var newBody = document.createElement('tbody');
	var newEntry = document.createElement('tr');
	
	var infoTD;
	var tdAttrib;
	var div;
	var outdiv1;
	var outdiv2;
	var html;
	var divID;
	//first cell : Infos
	var infoTD = document.createElement('td');
	var tdAttrib = document.createAttribute("valign");
	tdAttrib.nodeValue = "top";
	infoTD.setAttributeNode(tdAttrib);
	infoTD.className = "bez";
	//cell 1 outerdiv
	outdiv1 = document.createElement('div');
	// main div 
	var div = document.createElement('div');
	var html = '<h3><a href="#" onclick="infodiv(' + counter + '); return false;">' + locationInfo.company + '</a></h3><p>'+ locationInfo.address + '</p>';
	
	div.innerHTML = html;
	div.className = 'infomain';
	
	outdiv1.appendChild(div);
	
	// infodiv initially hidden
	div = document.createElement('div');
	var html = '<p>';
	if(locationInfo.email != "" && locationInfo.email_act != 0){
		html += '<strong>Email: <a href="mailto:'+ locationInfo.email + '">'+ locationInfo.email + '</a></strong>';
	}
	if(locationInfo.name != ""){
		html += '<br />Ansprechpartner: '+ locationInfo.name;
	}
	if(locationInfo.phone != ""){
		html += '<br />Tel: &nbsp;'+ locationInfo.phone;
	}
	if(locationInfo.fax != ""){
		html += '<br />FAX: '+ locationInfo.fax;
	}
	if(locationInfo.www != ""){
		html += '<br />Web: <a href="'+ locationInfo.www + '" target="_blank" rel="nofollow">'+ locationInfo.www + '</a>';
	}
	html += '</p>';
	if(locationInfo.imagelink != ""){
		html += '<img src="'+ locationInfo.imagelink + '" />';
	}
	if(locationInfo.description != ""){
		html += '<p>'+ locationInfo.description + '</p>';
	}
	div.innerHTML = html;
	div.className = 'infodiv_closed';
	var divID = document.createAttribute("id");
	divID.nodeValue = 'infobox_'+counter;
	div.setAttributeNode(divID);
	
	outdiv1.appendChild(div);
	
	GEvent.addDomListener(outdiv1, 'click', function() {
		GEvent.trigger(marker, 'click');
		//infodiv(counter);
	});
	/*
	GEvent.addDomListener(outdiv1, 'mouseover', function() {
		outdiv1.style.backgroundColor = '#fff';
	});
	GEvent.addDomListener(outdiv1, 'mouseout', function() {
		outdiv1.style.backgroundColor = 'none';
	});
	*/
	infoTD.appendChild(outdiv1);
	newEntry.appendChild(infoTD);
	
	//second cell : options
	var infoTD = document.createElement('td');
	var tdAttrib = document.createAttribute("valign");
	tdAttrib.nodeValue = "top";
	infoTD.setAttributeNode(tdAttrib);
	infoTD.className = "opt";
	//cell 1 outerdiv
	outdiv2 = document.createElement('div');
	// main opt div 
	var div = document.createElement('div');
	
	
	var html = '<p><a href="#" onclick="infodiv(' + counter + '); return false;">Mehr Informationen &gt;&gt;</a></p><p><a href="'+ locationInfo.routeurl +'" target="_blank">Anfahrtsbeschreibung &gt;&gt;</a></p>'
	if(locationInfo.isopen){
		html += '<p class="isopen">Das Geschäft ist aktuell ' + locationInfo.isopen + '</p>';
	}
	
	div.innerHTML = html;
	div.className = 'optmain';
	
	outdiv2.appendChild(div);
	
	// optdiv initially hidden
	div = document.createElement('div');
	if(locationInfo.opening){
		var html = '<p><strong>Öffnungszeiten:</strong><br />'+ locationInfo.opening + '</p>';
		div.innerHTML = html;
	}
	div.className = 'optdiv_closed';
	var divID = document.createAttribute("id");
	divID.nodeValue = 'optbox_'+counter;
	div.setAttributeNode(divID);
	
	outdiv2.appendChild(div);
	
	GEvent.addDomListener(outdiv2, 'click', function() {
		GEvent.trigger(marker, 'click');
		//infodiv(counter);
	});
	/*
	GEvent.addDomListener(outdiv2, 'mouseover', function() {
		outdiv2.style.backgroundColor = '#fff';
	});
	GEvent.addDomListener(outdiv2, 'mouseout', function() {
		outdiv2.style.backgroundColor = 'none';
	});
	*/
	infoTD.appendChild(outdiv2);
	newEntry.appendChild(infoTD);
	
	//third cell : distance
	var infoTD = document.createElement('td');
	var tdAttrib = document.createAttribute("valign");
	tdAttrib.nodeValue = "top";
	infoTD.setAttributeNode(tdAttrib);
	infoTD.innerHTML = locationInfo.distance.toFixed(1) + ' km';
	newEntry.appendChild(infoTD);
	
	//fourth cell : number
	infoTD = document.createElement('td');
	var tdAttrib = document.createAttribute("valign");
	tdAttrib.nodeValue = "top";
	infoTD.setAttributeNode(tdAttrib);
	infoTD.innerHTML = eval(counter+1);
	newEntry.appendChild(infoTD);
	/*
	GEvent.addDomListener(newEntry, 'click', function() {
		newEntry.trigger(marker, 'click');
		//infodiv(counter);
	});
	*/
	
	if(locationInfo.custcat == "0"){
		newEntry.className = "std";
		GEvent.addDomListener(newEntry, 'mouseover', function() {
			newEntry.className = 'std_high';
		});
		GEvent.addDomListener(newEntry, 'mouseout', function() {
			newEntry.className  = 'std';
		});
	}else if(locationInfo.custcat == "1"){
		newEntry.className = "premium";
		GEvent.addDomListener(newEntry, 'mouseover', function() {
			newEntry.className = 'premium_high';
		});
		GEvent.addDomListener(newEntry, 'mouseout', function() {
			newEntry.className  = 'premium';
		});
	}
	
	newBody.appendChild(newEntry);
	
	return newBody;
}
	
function infodiv(row){
	var id = "infobox_" + row;
	var id2 = "optbox_" + row;
	if(document.getElementById(id).className == "infodiv_open"){
		document.getElementById(id).className = "infodiv_closed";
		document.getElementById(id2).className = "optdiv_closed";
	} else{
		document.getElementById(id).className = "infodiv_open";
		document.getElementById(id2).className = "optdiv_open";
	}	
}
//]]>