/**
 * Wirtschaftsvereinigung Laichingen
 * members map
 *
 * (c) 2008 EZdesign.de
 *
 * Created:       2008-10-11
 * Last modified: 2008-10-11
 */


var map = false;
var searchId = 0;

$(document).ready(function() {
	// initialize
	$('#wvlmap_map').gMap();
	$('body').unload(GUnload);
	
	// logos
	$('#wvl_logos div.wvl_logo').hover(function() {
		var i = $(this).attr('id').substr(9);
		showDetails(i, true);
	}, function() {
		var i = $(this).attr('id').substr(9);
		hideDetails(i);
	}).click(function() {
		var i = $(this).attr('id').substr(9);
		goToMember(i);
	});
	
	// filter
	$('#wvlmap_filter input').eq(0).css('color', '#999').attr('value', 'Suchbegiff').focus(function() {
		var el = $(this);
		if (el.attr('value') == 'Suchbegiff') {
			el.css('color', '#000').attr('value', '');
		}
	}).blur(function() {
		var el = $(this);
		if (el.attr('value') == '') {
			el.css('color', '#999').attr('value', 'Suchbegiff');
		}
	}).keyup(function() {
		// search
		var el = $(this);
		searchId++;
		var localSearchId = searchId;
		var val = el.attr('value').toLowerCase().replace(/ö/, '&ouml;').replace(/ü/, '&uuml;').replace(/ä/, '&auml;').replace(/ß/, '&szlig;');
		var search = val.split(' ');
		for (var i = 0; i < gmapSettings.markers.length; i++) {
			if (localSearchId != searchId) {
				break;
			}
			var match = true;
			if (val != '') {
				var searchin = gmapSettings.markers[i].searchin;
				for (var j = 0; j < search.length; j++) {
					if (search[j] != '') {
						if (searchin.indexOf(search[j]) == -1) {
							match = false;
							break;
						}
					}
				}
			}
			if (match) {
				// show logo
				$('#wvl_logo_'+i).show();
			} else {
				// hide logo
				$('#wvl_logo_'+i).hide();
			}
		}
	});
	
});

/**
 * jquery gmap plugin
 */

$.fn.gMap = function(settings) {
	var el = $(this);
	var settings = settings;
	
	if (GBrowserIsCompatible()) {
		// initialize map
		map = new GMap2(document.getElementById(el.attr('id')));
		//map.addControl(new GMapTypeControl());
		map.addControl(new GSmallMapControl());
		
		// icon
		var myIcon = new GIcon();
		myIcon.image = "http://labs.google.com/ridefinder/images/mm_20_red.png";
		myIcon.shadow = "http://labs.google.com/ridefinder/images/mm_20_shadow.png";
		myIcon.iconSize = new GSize(12, 20);
		myIcon.shadowSize = new GSize(22, 20);
		myIcon.iconAnchor = new GPoint(6, 20);
		myIcon.infoWindowAnchor = new GPoint(5, 1);
		
		// marker manager
		var mgr = new GMarkerManager(map);
		var minLat  = 0;
		var maxLat  = 0;
		var minLon  = 0;
		var maxLon  = 0;
		for (var i = 0; i < gmapSettings.markers.length; i++) {
			var ob = gmapSettings.markers[i];
			if (ob.lat > 0 && ob.lon > 0) {
				var marker = new GMarker(new GLatLng(ob.lat, ob.lon), {icon: myIcon});
				
				gmapSettings.markers[i].gMarker = marker;
				
				GEvent.addListener(marker, 'click', _createClickListener(i));
				GEvent.addListener(marker, 'mouseover', _createMouseoverListener(i));
				GEvent.addListener(marker, 'mouseout', _createMouseoutListener(i));
				mgr.addMarker(marker, 0);
				$(marker).find('img').css('border', '2px solid red');
				/*
				if (ob.lat > 48.48 && ob.lat < 48.49) {
					if (minLat == 0 || ob.lat < minLat)
						minLat = ob.lat;
					if (maxLat == 0 || ob.lat > maxLat)
						maxLat = ob.lat;
					if (minLon == 0 || ob.lon < minLon)
						minLon = ob.lon;
					if (maxLon == 0 || ob.lon > maxLon)
						maxLon = ob.lon;
				}
				*/
			}
		}
		
		// center & zoom
		//var offset = 0.003;
		//var bounds = new GLatLngBounds(new GLatLng(minLat-offset, minLon-offset), new GLatLng(maxLat+offset, maxLon+offset));
		//map.setCenter(bounds.getCenter(), parseInt(map.getBoundsZoomLevel(bounds)));
		map.setCenter(new GLatLng(48.4903, 9.69434), 14);
		
		// add markers
		mgr.refresh();
		
	} else {
		el.html('Ihr Browser ist leider nicht mit der google Map kompatibel.<br />Bitte verwenden Sie einen anderen.');
	}
	
	function _createClickListener(i) {
		return function(marker, point) {
			goToMember(i);
		};
	}
	
	function _createMouseoverListener(i) {
		return function(marker, point) {
			showDetails(i);
		};
	}
	
	function _createMouseoutListener(i) {
		return function(marker, point) {
			hideDetails(i);
		};
	}
	
	return el;
};

function showDetails(i, showCrosshair) {
	var showCrosshair = (showCrosshair == true ? 1 : 0);
	var ob = gmapSettings.markers[i];
	var offset = map.fromLatLngToContainerPixel(new GLatLng(ob.lat, ob.lon));
	if (ob.lat > 0 && ob.lon > 0 && offset.x > 0 && offset.x < 471 && offset.y > 0 && offset.y < 471) {
		$('#wvlmap_company').html(ob.name);
		if (showCrosshair == 1) {
			$('#map_crosshair').css({
				marginLeft: (offset.x-16) + 'px',
				marginTop:  (offset.y-25) + 'px'
			}).show();
		}
		$('#wvlmap_info').css({
			marginLeft: (offset.x < 253 ? offset.x+12+(showCrosshair*10) : offset.x-210-(showCrosshair*10)) + 'px',
			marginTop:  (offset.y-30) + 'px'
		}).show();
	}
	$('#wvl_logo_'+i).addClass('highlighted');
}

function hideDetails(i) {
	var ob = gmapSettings.markers[i];
	$('#wvlmap_info').hide();
	$('#wvl_logo_'+i).removeClass('highlighted');
	$('#map_crosshair').hide();
}

function goToMember(i) {
	var ob = gmapSettings.markers[i];
	window.location.href = 'front_content.php?idart=6&idmem='+ob.idwvlmember;
}
