	
	green = G_DEFAULT_ICON;
	var def_location;
	var def_lat;
	var def_lng;
	var changecity = false;
	var globalvariable;
	var xhr = {};
	function init() {
		expandtab('maintab', 0);
		
		map = new GMap2(document.getElementById("mymap"));
		Israel = new GMap2(document.getElementById("map_israel"));
		Chile = new GMap2(document.getElementById("map_chile"));
		Ukraine = new GMap2(document.getElementById("map_ukraine"));
		
		//maps = { "one" : [map, new GLatLng(31.75, 35)
		
		Ukraine.setCenter(new GLatLng(50.0, 36.25), 8);
																
		marker = new GMarker(new GLatLng(50, 36.25));
		GEvent.addListener(marker, "click", 
			function() {
				html = "Rabbi Shlomo Asraf<br/>Sumskaya 45<br/>Kharkov, Ukraine <br/>380.572.408.438<br/>tsnef@walla.co.il";
				this.openInfoWindowHtml(html);
			}
		);
		
		Ukraine.addOverlay(marker);
		
		
		Ukraine.addControl(new GLargeMapControl());
		
		Chile.setCenter(new GLatLng(-33.41353208120716, -70.5833387374878), 8);
																
		marker = new GMarker(new GLatLng(-33.416667, -70.583333));
		GEvent.addListener(marker, "click", 
			function() {
				html = "Shimon Vinger<br/>Las Lluvias 208<br/>Lo Barnechea, Chile <br/>305.433.8381<br/>kvinger@vtr.net";
				this.openInfoWindowHtml(html);
			}
		);
		
		Chile.addOverlay(marker);
		
		
		Chile.addControl(new GLargeMapControl());
		
		Israel.setCenter(new GLatLng(31.75, 35.0), 8);
																
		marker = new GMarker(new GLatLng(31.75, 35));
		GEvent.addListener(marker, "click", 
			function() {
				html = "Seymour J. Abrams<br/>22 Keren HaYesod: PO Box 37015<br/>Jerusalem, Israel 91370<br/>972.2.560-9100<br/>tt@ou.org";
				this.openInfoWindowHtml(html);
			}
		);
		
		Israel.addOverlay(marker);
		
		
		Israel.addControl(new GLargeMapControl());
		
				
		$.getJSON("http://www.ncsy.org/getlocation.php", function (json) {urchinTracker("/locations/auto_ip" + json.location); $("#change-location")[0].value = json.location; def_location = json.location; def_lat = json.lat; def_lng = json.lng; startMap()});
		$(document).ajaxError(function(){
		
		for (i in arguments[3]) 
		{
		alert(i + ": " + arguments[3][i]);
		}
		//console.log(arguments);
		})
		
		function openMap(m) {
			maps = {"one" : map, "two" : Chile, "three" : Israel, "four" : Ukraine};
			center = new GLatLng(maps[m].getCenter().lat(), maps[m].getCenter().lng());
			maps[m].checkResize();
			maps[m].setCenter(center);
		}
		
		function startMap() {
			$("#maintab a").click(
				function() {
					openMap($(this).attr("class"));
					return false;
				}
			);
			
			if (GBrowserIsCompatible()) {
				map.setCenter(new GLatLng(def_lat, def_lng), 10);
				map.addControl(new GLargeMapControl());
				GEvent.addListener(map, "moveend", function() { changePage(0, map, changecity) });
				//GEvent.trigger(map, "moveend");
				handleMapMove(map, true);
			}
		}
    }
	
	function handleMapMove(map, changecity) {
		if (globalvariable == 'hello') { changecity = true; globalvariable = ''};
		if (changecity) {
			$(".loc").html('<span>Loading... <img width="16" height="16" style="vertical-align: sub;" src="http://www.ou.org/ncsy/graphics/loader_brown.gif"/></span>');
			$("#marker_list").html("<li>Loading...</li>");
		}
		
		$("#loading-div").remove();
		$("<div/>").attr("id", "loading-div").attr("style", "position: relative; float: left; background: red; color: white; font-weight: bold; font-size: 14pt; padding: 5px").html('<span>Loading... <img width="16" height="16" style="vertical-align: sub;" src="http://www.ou.org/ncsy/graphics/loader_red.gif"/></span>').appendTo($("#mymap"));
		
		var bounds = map.getBounds();
		var maxLat = map.getBounds().getNorthEast().lat();
		var maxLng = map.getBounds().getNorthEast().lng();
		var minLat = map.getBounds().getSouthWest().lat();
		var minLng = map.getBounds().getSouthWest().lng();
		//$('<script/>').attr('src', 'http://www.ou.org/ncsy_pofs/map_data_json/'+minLat+'/'+minLng+'/'+maxLat+'/'+maxLng).appendTo($('head'));
		xhr = $.getJSON('http://www.ncsy.org/ncsy.php?minlat='+minLat+'&minlng='+minLng+'&maxlat='+maxLat+'&maxlng='+maxLng, function(json) {handleJson(json, map, changecity); $("#loading-div").remove() });
	}
	var ncsy_cities = [];
	var markers = [];
	var city_markers = [];
	var orig_ncsy_cities = [];
	
	var page = 0;
	function handleJson(json, map, changecity) {
		if (json.cities.length > 0) {
			ncsy_cities = json.cities;
			closest_mode = false;
		}
		else {
			closest_mode = true;
			ncsy_cities = json.closest_cities;
		}
		markers = [];
		for (i = 0; i < ncsy_cities.length; i++) {
			if (!changecity) {
				markers[i] = createMarker(ncsy_cities[i], ncsy_cities[i].name, ncsy_cities[i].lat, ncsy_cities[i].lng, ncsy_cities[i].events);
			}
			else {
				city_markers[i] = createMarker(ncsy_cities[i], ncsy_cities[i].name, ncsy_cities[i].lat, ncsy_cities[i].lng, ncsy_cities[i].events, green);
				if (json.cities.length > 0) {
					orig_ncsy_cities = json.cities;
				}
				else {
					orig_ncsy_cities = json.closest_cities;
				}
			}
		}
		
		
		if (changecity) {
			$(".reg").html(json.region);
			//city_markers = markers.slice();
			map.clearOverlays();
			populateMap(city_markers, map);
			populateMenu(ncsy_cities, map, closest_mode);
			if ($("#change-location")[0].value == '') {$(".loc").html(def_location);}
			else {$(".loc").html($("#change-location")[0].value);}
		}
		else {
			populateMap(markers, map);
			populateMap(city_markers, map);
		}
	}
	
	function populateMap(nmarkers, map, norefresh) {
		var max = 20;
		var taboo = [];
		for (i = 0; i < city_markers.length; i++) {
			for (j = 0; j < markers.length; j++) {
				if (markers[j] && city_markers[i].getPoint().equals(markers[j].getPoint())) {
					taboo[j] = true;
				}
			}
		}
		if (activeMarker) {
			for (i = 0; i < markers.length; i++) {
				if (!markers[i].getPoint().equals(activeMarker.getPoint())) map.removeOverlay(markers[i]);
			}
			for (i = 0; i < city_markers.length; i++) {
				if (!city_markers[i].getPoint().equals(activeMarker.getPoint())) map.removeOverlay(city_markers[i]);
			}
		}
		else {map.clearOverlays();}
		
		for (i = (0 + page * max); i < Math.min((0 + page * max) + max, city_markers.length); i++) {map.addOverlay(city_markers[i]);}
		
		for (i = 0; i < markers.length; i++) {
			if (!taboo[i]) {map.addOverlay(markers[i]);}
			/*else {//console.log('taboo');}*/
		}
		//myfunc();
	}
	var activeMarker = null;
	
	
	function createMarker(city, name, lat, lng, events, icon) {
		marker = new GMarker(new GLatLng(lat, lng), icon);
		GEvent.addListener(marker, 'click', 
			function() {
				urchinTracker("/map/marker_click/" + city.name);
				events_ul = '<ul>';
				if (events && events.length > 0) {
					for (i = 0; i < events.length; i++) {
						if (events[i] != null)	events_ul += '<li><a target="_blank" href="http://www.ncsy.org/ncsy/event_info/'+events[i].id+'">' + events[i].date + '</a></li>';
					}
				}
				else {
				events_ul += "<li><em>No upcoming events in this region</em></li>";
				}
				events_ul += "</ul>";
				html = "<div id='findme' class='clearfix' style='width: 350px'><div style='float: left; width: 200px; border-right: 1px solid #CCCCCC; padding: 10px;'><strong>" + city.name + "</strong><br/><i>Region Info</i><br/><b>Region:</b> " + city.region + "<br/><b>Director:</b> " + city.director+"<br/><b>Address:</b> "+city.address+"<br/><b>City:</b> "+city.city+", "+city.state+" "+city.postal+"<br/><b>Phone:</b> "+city.phone+"<br/></div><div style='float:left; padding: 10px; width:100px'>" + city.region + " Events" + events_ul + "</div></div><div><a href='http://www.ncsy.org/ncsy/contact'>View all regions</a></div>";
				this.openInfoWindowHtml(html);
			});
		/*GEvent.addListener(marker, 'remove', 
			function() {
				if (map.getInfoWindow().getPoint().equals(this.getPoint())) {
					activeMarker = this;
				}
			});*/
		GEvent.addListener(marker, 'infowindowopen', 
			function() {
				activeMarker = this;
				//console.log(this);
			});
			
		GEvent.addListener(marker, 'infowindowclose', 
			function() {
				activeMarker = null;
			});
		return marker;
	}
	
	function changePage(newPage, map, norefresh) {
		page = newPage;
		if (!norefresh) handleMapMove(map);
		else {
		populateMap(markers, map);
		populateMenu(orig_ncsy_cities, map);
		}
		
	}
	
	function setupPager(ncsy_cities, map) {
		var max = 20;
		page = page * 1;
		var numPages = Math.ceil(ncsy_cities.length / max);
		var start = (0 + page) * max;
		var end = Math.min((0 + page * max) + 9, ncsy_cities.length - 1);
		
		var resultsHtml = "Results " + (start + 1) + " to " + (end + 1) + " of " + ncsy_cities.length;
		var pagesHtml = '';
		
		$('#pager').html('');
		$('#pager').append(resultsHtml + '<br/>Pages ');
		
		if (page - 5 >= 0) {
			$('<a/>').attr('href', 'javascript:void(0)').html('<<').bind('click',function() {changePage(page - 5, map, true)}).appendTo($('#pager'));
			$('#pager').append('&nbsp;');
		}
		
		if (page > 0) {
			$('<a/>').attr('href', 'javascript:void(0)').html('<').bind('click',function() {changePage(page - 1, map, true)}).appendTo($('#pager'));
			$('#pager').append('&nbsp;');
		}
		for (k = (5 * Math.ceil((page + 1) /5)) - 5; k <= (5 * Math.ceil((page + 1)/5)) && k < numPages; k++) {
			link = $('<a/>').attr('href', "#").html(k + 1).attr('pageid', k);
			if (k == page) link.attr('class', 'currentpage');
			link.bind('click', function() { changePage($(this).attr('pageid'), map, true)});
			$('#pager').append(link).append('&nbsp;');
		}
		
		if (page < numPages - 1) {
			$('<a/>').attr('href', 'javascript:void(0)').html('>').bind('click',function() {changePage(page + 1, map, true)}).appendTo($('#pager'));
			$('#pager').append('&nbsp;');
		}
		if (page + 5 < numPages - 1) {
			$('<a/>').attr('href', 'javascript:void(0)').html('>>').bind('click',function() {changePage(page + 5, map, true)}).appendTo($('#pager'));
			$('#pager').append('&nbsp;');
		}
		
		if (ncsy_cities.length > max) {$('#pager').show();}
		else {$('#pager').hide();}
	}
	var globalVar;
	function populateMenu(ncsy_cities, map, closest_mode) {
		max = 20;
		setupPager(orig_ncsy_cities, map);
		$('#marker_list').html('');
		if (closest_mode) {
			$('#marker_list').prepend("<h4>15 Closest Cities</h4>");
		}
		for (i = (0 + page * max); i < Math.min((0 + page * max) + max, orig_ncsy_cities.length); i++) {
			li = $('<li/>');
			link = $('<a/>')
						.attr('href', 'javascript:void(0)')
						.attr('markerid', i)
						.html(orig_ncsy_cities[i].name)
						.appendTo(li)
						.bind('click', 
							function() {
								id = $(this).attr('markerid');
								globalVar = (city_markers[id]);
								GEvent.trigger(city_markers[id], 'click');
							});
			$('#marker_list').append(li);
		}
	};
	