/*
* LabeledMarker Class
*
* Copyright 2007 Mike Purvis (http://uwmike.com)
* 
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* 
*       http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* This class extends the Maps API's standard GMarker class with the ability
* to support markers with textual labels. Please see articles here:
*
*       http://googlemapsbook.com/2007/01/22/extending-gmarker/
*       http://googlemapsbook.com/2007/03/06/clickable-labeledmarker/
*/

/* Constructor */
function LabeledMarker(latlng, options){
    this.latlng = latlng;
    this.labelText = options.labelText || "";
    this.labelClass = options.labelClass || "markerLabel";
    this.labelOffset = options.labelOffset || new GSize(0, 0);
    
    this.clickable = options.clickable || true;
    
    if (options.draggable) {
    	// This version of LabeledMarker doesn't support dragging.
    	options.draggable = false;
    }
    
    GMarker.apply(this, arguments);
}


/* It's a limitation of JavaScript inheritance that we can't conveniently
   extend GMarker without having to run its constructor. In order for the
   constructor to run, it requires some dummy GLatLng. */
LabeledMarker.prototype = new GMarker(new GLatLng(0, 0));


// Creates the text div that goes over the marker.
LabeledMarker.prototype.initialize = function(map) {
	// Do the GMarker constructor first.
	GMarker.prototype.initialize.apply(this, arguments);
	
	var div = document.createElement("div");
	div.className = this.labelClass;
	div.innerHTML = this.labelText;
	div.style.position = "absolute";
	map.getPane(G_MAP_MARKER_PANE).appendChild(div);

	if (this.clickable) {
		// Pass through events fired on the text div to the marker.
		var eventPassthrus = ['click', 'dblclick', 'mousedown', 'mouseup', 'mouseover', 'mouseout'];
		for(var i = 0; i < eventPassthrus.length; i++) {
			var name = eventPassthrus[i];
			GEvent.addDomListener(div, name, newEventPassthru(this, name));
		}

		// Mouseover behaviour for the cursor.
		div.style.cursor = "pointer";
	}
	
	this.map = map;
	this.div = div;
}

function newEventPassthru(obj, event) {
	return function() { 
		GEvent.trigger(obj, event);
	};
}

// Redraw the rectangle based on the current projection and zoom level
LabeledMarker.prototype.redraw = function(force) {
	GMarker.prototype.redraw.apply(this, arguments);
	
	// We only need to do anything if the coordinate system has changed
	if (!force) return;
	
	// Calculate the DIV coordinates of two opposite corners of our bounds to
	// get the size and position of our rectangle
	var p = this.map.fromLatLngToDivPixel(this.latlng);
	var z = GOverlay.getZIndex(this.latlng.lat());
	
	// Now position our DIV based on the DIV coordinates of our bounds
	this.div.style.left = (p.x + this.labelOffset.width) + "px";
	this.div.style.top = (p.y + this.labelOffset.height) + "px";
	this.div.style.zIndex = z + 1; // in front of the marker
}

// Remove the main DIV from the map pane, destroy event handlers
LabeledMarker.prototype.remove = function() {
	GEvent.clearInstanceListeners(this.div);
	this.div.parentNode.removeChild(this.div);
	this.div = null;
	GMarker.prototype.remove.apply(this, arguments);
}

//** new **//


var map, manager;
var centerLatitude = 40.65630038859761, centerLongitude = -73.95087361335754, startZoom = 12;


function createMarkerClickHandler(marker, text, link, info) {
	return function() {
		var wiki;
		if (link){
			wiki = "<a style='float:right' target='_blank' href='" + link + "'>See what Wikipedia says &raquo;</a></div>";
		}else{
			wiki = "</div>";
		}
		marker.openInfoWindowHtml(
			'<div class="bubble"><h3 style="margin-top:0">' + text + '</h3>' +
			'<p>' + info + '</p>' + wiki
		);
		return false;
	};
}


function createMarker(pointData) {
	var latlng = new GLatLng(pointData.latitude, pointData.longitude);
	var icon = new GIcon();
	icon.image = 'http://langrealtyny.com/images/gMapMarker.png';
	icon.iconSize = new GSize(32, 32);
	icon.iconAnchor = new GPoint(16, 16);
	icon.infoWindowAnchor = new GPoint(25, 7);

	opts = {
		"icon": icon,
		"clickable": true,
		"labelText": pointData.abbr,
		"labelOffset": new GSize(-16, -16)
	};
	var marker = new LabeledMarker(latlng, opts);
	
	var handler = createMarkerClickHandler(marker, pointData.name, pointData.wp, pointData.info);
	
	GEvent.addListener(marker, "click", handler);

	var listItem = document.createElement('li');
	listItem.innerHTML = '<div class="gMapLabel">'+pointData.abbr+'</div><a href="' + pointData.wp + '">' + pointData.name + '</a>';
	listItem.getElementsByTagName('a')[0].onclick = handler;
	
	var gList;
	var count=document.getElementById('gMapList1').getElementsByTagName('li').length;
	if (count < 40){gList="gMapList1"}else{gList="gMapList2"}
	
	document.getElementById(gList).appendChild(listItem);

	return marker;
}



function init() {
	
	map = new GMap(document.getElementById("gMap"));
	map.addControl(new GSmallMapControl());
	map.setCenter(new GLatLng(centerLatitude, centerLongitude), startZoom);
	map.addControl(new GMapTypeControl());

	manager = new GMarkerManager(map);
	
	var markers = new Array();



var myMarker = new Object(); myMarker.abbr = "BS"; myMarker.name = "Bedford Stuyvesant"; myMarker.latitude = "40.69150718598089"; myMarker.longitude = "-73.94579887390137"; myMarker.wp = "http://en.wikipedia.org/wiki/Bedford-Stuyvesant,_Brooklyn"; myMarker.info = "<span style=\"font-weight:bold\">Bed-Stuy</span> is bordered by Flushing Avenue to the north (bordering Williamsburg); Bedford Avenue to the west (bordering Clinton Hill, although, prior to the 1980s, the area now called Clinton Hill was considered to be Bedford-Stuyvesant as well); Broadway and Van Sinderen Avenue to the east (bordering Bushwick and East New York); Park Place to the south; Ralph Avenue to the east (bordering Crown Heights); and as far as East New York Avenue to the south (bordering Brownsville)"; markers.push(myMarker);


var myMarker = new Object(); myMarker.abbr = "BoH"; myMarker.name = "Boerum Hill"; myMarker.latitude = "40.6856660459685"; myMarker.longitude = "-73.98521661758423"; myMarker.wp = "http://en.wikipedia.org/wiki/Boerum_Hill,_Brooklyn"; myMarker.info = "<span style=\"font-weight:bold\">Boerum Hil</span> l is a small neighborhood in the northwestern portion of the New York City borough of Brooklyn that occupies 36 blocks bounded by State Street to the north, 4th Avenue to the east, Court Street to the west, and Warren Street to the south. There are commercial strips along Smith Street, Court Street, and Atlantic Avenue."; markers.push(myMarker);



var myMarker = new Object(); myMarker.abbr = "BrH"; myMarker.name = "Brooklyn Heights"; myMarker.latitude = "40.694395025661926"; myMarker.longitude = "-73.99804830551147"; myMarker.wp = "http://en.wikipedia.org/wiki/Brooklyn_Heights,_Brooklyn"; myMarker.info = "<span style=\"font-weight:bold\">Brooklyn Heights</span>  stretches from Old Fulton Street near the Brooklyn Bridge south to Atlantic Avenue and from the East River east to Court Street and Cadman Plaza. Adjacent neighborhoods are: DUMBO, Downtown Brooklyn, Cobble Hill and Boerum Hill. It is directly across the East River from Manhattan, and easily accessible to Downtown and multiple subway lines."; markers.push(myMarker);

var myMarker = new Object(); myMarker.abbr = "Bus"; myMarker.name = "Bushwick"; myMarker.latitude = "40.693329386717686"; myMarker.longitude = "-73.9151680469513"; myMarker.wp = "http://en.wikipedia.org/wiki/Bushwick,_Brooklyn"; myMarker.info = "<span style=\"font-weight:bold\">Bushwick</span>  is a neighborhood in the northeastern part of the New York City borough of Brooklyn. It is bounded by East Williamsburg to the northwest, Bed-Stuy to the southwest"; markers.push(myMarker);

var myMarker = new Object(); myMarker.abbr = "CG"; myMarker.name = "Carroll Gardens"; myMarker.latitude = "40.680654297738165"; myMarker.longitude = "-73.99833798408508"; myMarker.wp = "http://en.wikipedia.org/wiki/Carroll_Gardens,_Brooklyn"; myMarker.info = "<span style=\"font-weight:bold\">Carroll Gardens</span>  shares its northern boundary with Cobble Hill at Degraw Street and Boerum Hill at Warren Street, while extending south to Hamilton Avenue and Red Hook. Carroll Gardens is now sprinkled with cafes, boutiques and antique shops. "; markers.push(myMarker);


var myMarker = new Object(); myMarker.abbr = "ClH"; myMarker.name = "Clinton Hill"; myMarker.latitude = "40.6924671015089"; myMarker.longitude = "-73.96653771400452"; myMarker.wp = "http://en.wikipedia.org/wiki/Clinton_Hill,_Brooklyn"; myMarker.info = "<span style=\"font-weight:bold\">Clinton Hill</span>  is a neighborhood in the north-central portion of the borough of Brooklyn in New York City. It is bordered on the east by Bedford-Stuyvesant, on the west by Fort Greene, on the north by Wallabout Bay and on the south by Prospect Heights."; markers.push(myMarker);
var myMarker = new Object(); myMarker.abbr = "CoH"; myMarker.name = "Cobble Hill"; myMarker.latitude = "40.68626808127797"; myMarker.longitude = "-73.99579524993896"; myMarker.wp = "http://en.wikipedia.org/wiki/Cobble_Hill,_Brooklyn"; myMarker.info = "<span style=\"font-weight:bold\">Cobble Hill</span>  is a neighborhood in the New York City borough of Brooklyn, USA. Bordered by Atlantic Avenue on the north, Hicks Street to the west, Smith Street on the east and Degraw Street to the south, Cobble Hill sits adjacent to Boerum Hill and Brooklyn Heights with Carroll Gardens to the south."; markers.push(myMarker);

var myMarker = new Object(); myMarker.abbr = "CH"; myMarker.name = "Crown Heights"; myMarker.latitude = "40.66630038859761"; myMarker.longitude = "-73.95087361335754"; myMarker.wp = "http://en.wikipedia.org/wiki/Crown_Heights,_Brooklyn"; myMarker.info = "<span style=\"font-weight:bold\">Crown Heights</span>  is a neighborhood in the central portion of the New York City borough of Brooklyn. The main thoroughfare through this neighborhood is Eastern Parkway, a tree-lined boulevard designed by Frederick Law Olmsted extending two miles (3 km) east-west. Conveniently located near transportation, Brooklyn Museum, Botanical Gardens and Prospect Park. Crown Heights is bounded by Washington Avenue (to the west), Atlantic Avenue (to the north), Ralph Avenue (to the east) and Empire Boulevard (to the south). It is about two miles (3 km) long and two miles (3 km) deep. The neighborhoods that border Crown Heights are: Prospect Heights (to the west); Prospect Lefferts Gardens (to the southwest); Wingate and Rugby (to the south); Brownsville (to the east); and Bedford-Stuyvesant (to the north)."; markers.push(myMarker);


var myMarker = new Object(); myMarker.abbr = "DP"; myMarker.name = "Ditmas Park"; myMarker.latitude = "40.63811249691458"; myMarker.longitude = "-73.9614737033844"; myMarker.wp = "http://en.wikipedia.org/wiki/Ditmas_Park,_Brooklyn"; myMarker.info = "<span style=\"font-weight:bold\">Ditmas Park</span>  is a neighborhood in western Flatbush in the New York City borough of Brooklyn, east of Kensington, and is one of three Flatbush neighborhoods which have been officially designated Historic Districts. Located on land that remained rural until the early 20th century, the neighborhood consists of many large, free-standing Victorian homes built in the 1900s."; markers.push(myMarker);
var myMarker = new Object(); myMarker.abbr = "Dum"; myMarker.name = "Dumbo"; myMarker.latitude = "40.701626277811634"; myMarker.longitude = "-73.98678302764893"; myMarker.wp = "http://en.wikipedia.org/wiki/DUMBO,_Brooklyn"; myMarker.info = "<span style=\"font-weight:bold\">DUMBO</span> , an acronym for <span style=\"font-weight:bold\">Down Under the Manhattan Bridge Overpass</span> , is a neighborhood in the New York City, New York borough of Brooklyn. It encompasses two sections; one located between the Manhattan and Brooklyn Bridges, which connect Brooklyn to Manhattan across the East River, and another which continues east from the Manhattan Bridge to the Vinegar Hill area."; markers.push(myMarker);

var myMarker = new Object(); myMarker.abbr = "EF"; myMarker.name = "East Flatbush"; myMarker.latitude = "40.64639183931191"; myMarker.longitude = "-73.93258094787598"; myMarker.wp = "http://en.wikipedia.org/wiki/East_Flatbush,_Brooklyn"; myMarker.info = "<span style=\"font-weight:bold\">East Flatbush</span>  is a neighborhood in the New York City borough of Brooklyn. The area is part of Brooklyn Community Board 17[1] Though the borders of East Flatbush are highly subjective, its northern border is roughly at Empire Boulevard and East New York Avenue east of East 91st Street, its southern border is in the vicinity of the LIRR Bay Ridge Branch, its eastern border is roughly at East 98th Street and its western border is roughly at New York Avenue."; markers.push(myMarker);
var myMarker = new Object(); myMarker.abbr = "ENY"; myMarker.name = "East New York"; myMarker.latitude = "40.66892077716713"; myMarker.longitude = "-73.88425827026367"; myMarker.wp = "http://en.wikipedia.org/wiki/East_New_York,_Brooklyn"; myMarker.info = "<span style=\"font-weight:bold\">East New York</span>  boundaries, starting from the north and moving clockwise are: Cypress Hills Cemetery to the north, the neighborhood of City Line to the east, Jamaica Bay to the south, and the train next to Van Sinderen Avenue to the west."; markers.push(myMarker);





var myMarker = new Object(); myMarker.abbr = "FG"; myMarker.name = "Fort Greene"; myMarker.latitude = "40.68975001664651"; myMarker.longitude = "-73.97300720214844"; myMarker.wp = "http://en.wikipedia.org/wiki/Fort_Greene,_Brooklyn"; myMarker.info = "<span style=\"font-weight:bold\">Fort Greene</span>  is a neighborhood in the New York City borough of Brooklyn. Fort Greene is listed on the New York State Registry and on the National Register of Historic Places, and is a New York City-designated Historic District. It is located in north west Brooklyn, above Prospect Park. Fort Greene contains many superb examples of mid-19th century Italianate and Eastlake architecture, most of which is well preserved. Fort Greene is known for its many graceful, tree-lined streets and elegant low-rise housing. Fort Greene is also home to the Williamsburgh Savings Bank Tower, the tallest building in Brooklyn. The neighborhood is close to the Flatbush Avenue train station and has access to most major subway lines."; markers.push(myMarker);





var myMarker = new Object(); myMarker.abbr = "Gra"; myMarker.name = "Gravesend"; myMarker.latitude = "40.59495700827619"; myMarker.longitude = "-73.97365093231201"; myMarker.wp = "http://en.wikipedia.org/wiki/Gravesend,_New_York"; myMarker.info = "<span style=\"font-weight:bold\">Gravesend</span>  lies between Coney Island Avenue to the east, Stillwell Avenue to the west, Kings Highway to the north, and Coney Island Creek and Shore Parkway to the south. To the east of Gravesend is Sheepshead Bay, to the northeast Midwood, to the northwest Bensonhurst, and to the west Bath Beach. To the south, across Coney Island Creek, lies the neighborhood of Coney Island, and across Shore Parkway lies Brighton Beach."; markers.push(myMarker);
var myMarker = new Object(); myMarker.abbr = "Gre"; myMarker.name = "Greenpoint"; myMarker.latitude = "40.73174669050531"; myMarker.longitude = "-73.95000457763672"; myMarker.wp = "http://en.wikipedia.org/wiki/Greenpoint,_Brooklyn"; myMarker.info = "<span style=\"font-weight:bold\">Greenpoint</span>  is the northernmost neighborhood in the New York City borough of Brooklyn. It is bordered on the southwest by Williamsburg at the Bushwick inlet, on the southeast by the Brooklyn-Queens Expressway and East Williamsburg, on the north by Newtown Creek and Long Island City, Queens at the Pulaski Bridge, and on the west by the East River."; markers.push(myMarker);


var myMarker = new Object(); myMarker.abbr = "Ken"; myMarker.name = "Kensington"; myMarker.latitude = "40.64531729239498"; myMarker.longitude = "-73.97665500640869"; myMarker.wp = "http://en.wikipedia.org/wiki/Kensington,_Brooklyn,_New_York"; myMarker.info = "<span style=\"font-weight:bold\">Kensington</span>  is a neighborhood in the center of the New York City borough of Brooklyn. It is the area south of Prospect Park and the Green-Wood Cemetery. It is bordered by Coney Island Avenue to the east, Caton Avenue/ Ft. Hamilton Parkway to the north, Dahill Road and 36th St to the west, and Foster Avenue and 47th Street to the south. The neighborhoods that border it are Ditmas Park and Prospect Park South to the east (both of which are parts of Flatbush), Windsor Terrace to the north, Borough Park to the west, and Midwood to the south."; markers.push(myMarker);




var myMarker = new Object(); myMarker.abbr = "Mid"; myMarker.name = "Midwood"; myMarker.latitude = "40.62209634167975"; myMarker.longitude = "-73.95318031311035"; myMarker.wp = "http://en.wikipedia.org/wiki/Midwood,_Brooklyn"; myMarker.info = "<span style=\"font-weight:bold\">Midwood</span>  is bounded on the north by the New York and Atlantic Railway freight line tracks just above Avenue I and the Brooklyn College campus of the City University of New York, and on the south by Avenue P and Kings Highway. The eastern border is Nostrand Avenue or Flatbush Avenue (depending on whom you ask), and Coney Island Avenue or Ocean Parkway to the west is the other boundary (again, depending upon whom you ask).[1]"; markers.push(myMarker);









var myMarker = new Object(); myMarker.abbr = "PS"; myMarker.name = "Park Slope"; myMarker.latitude = "40.66774893910512"; myMarker.longitude = "-73.97953033447266"; myMarker.wp = "http://en.wikipedia.org/wiki/Park_Slope,_Brooklyn"; myMarker.info = "<span style=\"font-weight:bold\">Park Slope</span>  is a neighborhood in the western section of Brooklyn, New York City's most populous borough. Park Slope is roughly bounded by Prospect Park West to the east, Fourth Avenue to the west, Park Place to the north and 15th Street to the south. It takes its name from its location on the western slope of neighboring Prospect Park. Seventh Avenue and Fifth Avenue are its primary commercial streets, while its east-west side streets are populated by many historic brownstones."; markers.push(myMarker);

var myMarker = new Object(); myMarker.abbr = "LG"; myMarker.name = "Prospect Lefferts Gardens"; myMarker.latitude = "40.65948038538384"; myMarker.longitude = "-73.95811557769775"; myMarker.wp = "http://en.wikipedia.org/wiki/Prospect_Lefferts_Gardens,_Brooklyn"; myMarker.info = "<span style=\"font-weight:bold\">Prospect-Lefferts Gardens</span>  is the name given to a neighborhood adjacent to Prospect Park in the  New York City borough of Brooklyn. The area is bounded by Empire Boulevard to the north, Clarkson Avenue to the south, New York Avenue to the east, and Ocean Avenue/Prospect Park to the west. Many one and two family homes were constructed in the early 20th Century[2]. Presently, other areas of Prospect-Lefferts Gardens contain a mixture of single-family and multi-family homes as well as larger apartment houses."; markers.push(myMarker);
var myMarker = new Object(); myMarker.abbr = "PH"; myMarker.name = "Prospect Heights"; myMarker.latitude = "40.677798"; myMarker.longitude = "-73.9688223"; myMarker.wp = "http://en.wikipedia.org/wiki/Prospect_Heights,_Brooklyn"; myMarker.info = "<span style=\"font-weight:bold\">Prospect Heights</span>  is a neighborhood in the northwest of the New York City borough of Brooklyn. It is bounded by Flatbush Avenue to the west, Atlantic Avenue to the north, Eastern Parkway to the south, and Franklin Avenue to the east. Along the southern boundary, Eastern Parkway, from Grand Army Plaza to Washington Avenue is reminiscent of Manhattan's Fifth Avenue \"Museum Mile\". Immense, opulent buildings line the north side of the parkway, and the south side features the Brooklyn Public Library, Mount Prospect Park (not to be confused with Prospect Park), the Brooklyn Botanic Garden and the recently renovated Brooklyn Museum. To its north lies Fort Greene, to the south, Prospect Park, to its west, Park Slope and to its east, Crown Heights."; markers.push(myMarker);






var myMarker = new Object(); myMarker.abbr = "SPS"; myMarker.name = "South Park Slope"; myMarker.latitude = "40.663321809482355"; myMarker.longitude = "-73.98549556732178"; myMarker.wp = "http://en.wikipedia.org/wiki/South_Park_Slope"; myMarker.info = "<span style=\"font-weight:bold\">South Park Slope</span>  is a neighborhood in Brooklyn, New York. It is sandwiched between Greenwood Heights and Park Slope, bordered by 15th Street to the north, 24th Street and Green-Wood Cemetery to the south, Fourth Avenue to the west, and Prospect Park West to the east. The area was officially created when it was rezoned by the New York City Department of City Planning in 2005."; markers.push(myMarker);



var myMarker = new Object(); myMarker.abbr = "SP"; myMarker.name = "Sunset Park"; myMarker.latitude = "40.643656594948524"; myMarker.longitude = "-74.00570869445801"; myMarker.wp = "http://en.wikipedia.org/wiki/Sunset_Park,_Brooklyn"; myMarker.info = "<span style=\"font-weight:bold\">Sunset Park</span>  is a neighborhood in the western section of the New York City borough of Brooklyn, USA. The oft-disputed boundaries are generally recognized as at least 36th Street, Greenwood Heights and Green-Wood Cemetery to the north, 9th Avenue and Borough Park to the east, 65th Street and Bay Ridge to the south and Upper New York Bay to the west."; markers.push(myMarker);


var myMarker = new Object(); myMarker.abbr = "Wi"; myMarker.name = "Williamsburg"; myMarker.latitude = "40.70702683593489"; myMarker.longitude = "-73.94627094268799"; myMarker.wp = "http://en.wikipedia.org/wiki/Williamsburg,_Brooklyn"; myMarker.info = "<span style=\"font-weight:bold\">Williamsburg</span>  is a neighborhood in the New York City borough of Brooklyn, bordering Greenpoint, Bedford-Stuyvesant, and Bushwick."; markers.push(myMarker);
var myMarker = new Object(); myMarker.abbr = "WT"; myMarker.name = "Windsor Terrace"; myMarker.latitude = "40.65244805290929"; myMarker.longitude = "-73.9799165725708"; myMarker.wp = "http://en.wikipedia.org/wiki/Windsor_Terrace,_Brooklyn"; myMarker.info = "<span style=\"font-weight:bold\">Windsor Terrace</span>  is a neighborhood in the New York City borough of Brooklyn. It is bounded by Prospect Park to the northeast and Green-Wood Cemetery, a National Historic Landmark, to the southwest. Its southeastern boundary is Caton Avenue, while to the northwest it is bordered by Prospect Park West. It is between the neighborhoods of Park Slope to the northwest and Kensington to the southeast."; markers.push(myMarker);





















	// This is a sorting trick, don't worry too much about it.
	markers.sort(function(a, b) { return (a.abbr > b.abbr) ? +1 : -1; }); 

	batch = [];
	for(id in markers) {
		batch.push(createMarker(markers[id]));
	}
	manager.addMarkers(batch, 11);
	manager.refresh();
}

window.onload = init;
window.onunload = GUnload;




