
var mapBounds = new GLatLngBounds(new GLatLng(40.6802890421, -74.0614379176), new GLatLng(40.929850943, -73.8700425369));
var mapMinZoom = 13;
var mapMaxZoom = 15;

var opacity = 1;
var map;
var ge;
var hybridOverlay;
var markers = [];

var poiSee = [
	{//1
		'name' : 'STATUE OF LIBERTY',
		'addy' : 'Ferry from Battery Park.',
		'tely' : '',
		'message' : '150 feet from oversized toe to tip-of-torch.  Daily (you must, and should, reserve access to 2nd level, museum and crown in advance at www.statuecruises.com).',
		'lng' : '40.702049',
		'lat' : '-74.018884',
		'img' : 'brown12.png'
	},
	{//2
		'name' : 'CHINATOWN',
		'addy' : 'Mott, Bayard, Pell and Canal Sts.',
		'tely' : '',
		'message' : 'Sample dim sum and green tea ice cream in the largest Chinese community outside of Asia.',
		'lng' : ' 40.71653',
		'lat' : '-73.997431',
		'img' : 'brown13.png'
	},
	{
		'name' 		: 'EMPIRE STATE BUILDING',
		'addy' 		: '350 Fifth Ave. at 33rd St.',
		'tely' 		: '',
		'message' 	: 'Two sky-high observation decks open daily at NYC\'s tallest building.',
		'lng' 		: '40.748433',
		'lat' 		: '-73.985655',
		'img' 		: 'brown14.png'
	},
	{
		'name' 		: 'TIMES SQUARE',
		'addy' 		: '42nd and Broadway.',
		'tely' 		: '',
		'message' 	: 'The centerpiece of New York\'s fabled theatre district (the half-price ticket booth is at 47th St.).',
		'lng' 		: '40.755804',
		'lat' 		: '-73.986398',
		'img' 		: 'brown15.png'
	},
	{
		'name' 		: 'ROCKEFELLER CENTER',
		'addy' 		: '49th St. between 5th and 6th Aves',
		'tely' 		: '',
		'message' 	: 'Home to the Christmas tree, ice skating rink, Radio City Music Hall and "Top of the Rock" observation deck (the views rival the Empire State without the crowds; Daily',
		'lng' 		: '40.75855',
		'lat' 		: '-73.979267',
		'img' 		: 'brown16.png'
	},
	{
		'name' 		: 'ST. PATRICK\'S CATHEDRAL',
		'addy' 		: '5th Ave. at 50th St. ',
		'tely' 		: '',
		'message' 	: '7,300-pipe organ, 26-foot stained glass Rose Window and lots of history. Daily.',
		'lng' 		: '40.758272',
		'lat' 		: '-73.975317',
		'img' 		: 'brown17.png'
	},
	{
		'name' 		: 'LINCOLN CENTER',
		'addy' 		: '',
		'tely' 		: '',
		'message' 	: 'This performing arts complex is home to 12 world renowned resident arts organizations (www.lincolncenter.org for tix and info).',
		'lng' 		: '40.77285',
		'lat' 		: '-73.984105',
		'img' 		: 'brown18.png'
	}

/*
	{
		'name' 		: '',
		'addy' 		: '',
		'tely' 		: '',
		'message' 	: '',
		'lng' 		: '',
		'lat' 		: '',
		'img' 		: '.png'
	}
	*/
];
 
var poscnt = 0;

var poiSee2 = [
	{
		'name' 		: 'LOWER EAST SIDE',
		'addy' 		: '',
		'tely' 		: '',
		'message' 	: 'Vintage-enthusiasts will love the LES\'s funky shops, bars, and restaurants that radiate from Ludlow and Orchard Streets.  Be sure not to miss Frock (148 Orchard Street) for one of the best-edited collections of vintage clothing and accessories in the city.',
		'lng' 		: '40.718453',
		'lat' 		: '-73.985941',
		'img' 		: 'green3.png'
	},
	{
		'name' 		: 'SOHO',
		'addy' 		: '',
		'tely' 		: '',
		'message' 	: 'SoHo\'s picturesque cobblestone streets and cast-iron buildings are home to the city\'s broadest selection of retail stores. You will find everything from Prada to H&M, in addition to a wide array of artists and young designers selling their wares along the sidewalks.',
		'lng' 		: '40.725584',
		'lat' 		: '-74.000484',
		'img' 		: 'green4.png'
	},
	{
		'name' 		: 'WEST VILLAGE',
		'addy' 		: '',
		'tely' 		: '',
		'message' 	: 'The cozy confluence of cobbled streets at Bedford, Commerce and Barrow is the heart of the quaint West Village.  You\'ll need a cappuccino break at Sant Ambroeus (259 West 4th Street) between shopping at its many local and luxury designer boutiques.  ',
		'lng' 		: '40.731842',
		'lat' 		: '-74.005111',
		'img' 		: 'green5.png'
	},
	{
		'name' 		: 'FIFTH AVE.',
		'addy' 		: 'FROM 49TH TO 59TH Street',
		'tely' 		: '',
		'message' 	: 'Fifth Avenue is the city\'s most famous and preeminent high-end shopping stretch, boasting names like Chanel, Escada and Cartier on its storefronts.  Stop in at Takashimaya\'s Tea Box for an  individual pot of tea and their sweet bento box.  ',
		'lng' 		: '40.762065',
		'lat' 		: '-73.974617',
		'img' 		: 'green6.png'
	},
];
 

var poiSee3 = [
	{
		'name' 		: 'MORGAN LIBRARY',
		'addy' 		: '36th St. and Madison Ave. ',
		'tely' 		: '',
		'message' 	: 'J.P. Morgan\'s astounding manuscript collection on view in his recently-renovated private library. Daily exc. Mon.',
		'lng' 		: '40.748803',
		'lat' 		: '-73.981556',
		'img' 		: 'pink1.png'
	},
	{
		'name' 		: 'THE FRICK COLLECTION',
		'addy' 		: '1 E. 70th St.',
		'tely' 		: '',
		'message' 	: 'One of the country\'s finest "mansion museums"; exceptional art on display in Henry Clay Frick\'s 1914 home. Daily exc. Mon. Push on slightly north to a Beaux Arts masterpiece, the NEUE GALERIE, for a "new" appreciation of German art (1048 5th Ave.; Thurs. to Mon.).',
		'lng' 		: '40.771111',
		'lat' 		: '-73.9675',
		'img' 		: 'pink2.png'
	}
];
 

var poiSee4 = [
	{
		'name' 		: 'MOMA',
		'addy' 		: '11 W. 53rd St.',
		'tely' 		: '',
		'message' 	: 'The gold standard in modern art museums. Daily exc. Tues.',
		'lng' 		: '40.761367',
		'lat' 		: '-73.97751',
		'img' 		: 'lblue7.png'
	},
	{
		'name' 		: 'THE WHITNEY',
		'addy' 		: '945 Madison Ave.',
		'tely' 		: '',
		'message' 	: 'What started in 1931 with 700 pieces of art from Gertrude Vanderbilt Whitney is now one of the world\'s foremost collections of 20th-century American art. Wed. to Sun.',
		'lng' 		: '40.773343',
		'lat' 		: '-73.963598',
		'img' 		: 'lblue8.png'
	},
	{
		'name' 		: 'THE METROPOLITAN MUSEUM OF ART',
		'addy' 		: '5th Ave. at 82nd St.',
		'tely' 		: '',
		'message' 	: 'World class art in a world class setting! Daily exc. Mon.',
		'lng' 		: '40.778954',
		'lat' 		: '-73.962342',
		'img' 		: 'lblue9.png'
	},
	{
		'name' 		: 'THE GUGGENHEIM',
		'addy' 		: '5th Ave. at 89th St.',
		'tely' 		: '',
		'message' 	: '20th-century art in a Frank Lloyd Wright-designed building. Daily exc. Thurs.',
		'lng' 		: '40.782979',
		'lat' 		: '-73.958867',
		'img' 		: 'lblue10.png'
	},
	{
		'name' 		: 'MUSEUM OF NATURAL HISTORY',
		'addy' 		: 'Central Park West at 79th St.',
		'tely' 		: '',
		'message' 	: 'A colossal fossil museum, plus stellar star shows. Daily.',
		'lng' 		: '40.781971',
		'lat' 		: '-73.971763',
		'img' 		: 'lblue11.png'
	}
];
var poiED = [
	{//1
		'name' 		: 'ALLEN & DELANCEY',
		'addy' 		: '115 Allen St. nr. Delancey St., Lower East Side;',
		'tely' 		: '212.253.5400',
		'message' 	: 'Newly interpreted old British favorites in a relaxed cool setting. $$',
		'lng' 		: '40.719713',
		'lat' 		: '-73.990431',
		'img' 		: 'dblue19.png'
	},
	{//2
		'name' : 'BABBO',
		'addy' : '110 Waverly Pl., Village;',
		'tely' : '212.777.0303',
		'message' : 'Mario Batali\'s sophisticated take on Italian cuisine is one of NYC\'s best. $$$',
		'lng' : '40.732286',
		'lat' : '-73.999279',
		'img' 		: 'dblue20.png'
	},
	{
		'name' 		: 'BALTHAZAR',
		'addy' 		: '80 Spring St., SoHo',
		'tely' 		: '212.965.1414',
		'message' 	: 'Traditional French bistro still perfect even after 10 years of "hotness." $$',
		'lng' 		: '40.722723',
		'lat' 		: '-73.99815',
		'img' 		: 'dblue21.png'
	},
	{
		'name' 		: 'BLUE RIBBON BRASSERIE',
		'addy' 		: '97 Sullivan St., SoHo',
		'tely' 		: '212.274.0404',
		'message' 	: 'Eclectic comfort food and a relaxed vibe at a SoHo favorite. $$',
		'lng' 		: '40.725413',
		'lat' 		: '-74.003237',
		'img' 		: 'dblue22.png'
	},
	{//5
		'name' : 'BOULEY BAKERY',
		'addy' : '120 W. Broadway, TriBeCa;',
		'tely' : '212.219.1011',
		'message' : 'Boasts the best croissants and pain au chocolats in New York City. $',
		'lng' : '40.716448',
		'lat' : '-74.008502',
		'img' 		: 'dblue23.png'
	},
	{
		'name' 		: 'CAFE CLUNY',
		'addy' 		: '284 W. 12th St. nr. 4th St., West Village;',
		'tely' 		: '212.255.6900',
		'message' 	: 'Casual, French/American bistro with comfy bar seating and a popular brunch. $$',
		'lng' 		: '40.738372',
		'lat' 		: '-74.004357',
		'img' 		: 'dblue24.png'
	},
	{
		'name' 		: 'CASA MONO/BAR JAM&Oacute;N',
		'addy' 		: '52 Irving Pl. nr. 17th St./125 E. 17th St. nr. Irving Pl., Gramercy',
		'tely' 		: '212.253.2773',
		'message' 	: 'Mario Batali\'s tasty take on Spanish cuisine. $$',
		'lng' 		: '40.735859',
		'lat' 		: '-73.987023',
		'img' 		: 'dblue25.png'
	},
	{
		'name' 		: 'FREEMAN\'S',
		'addy' 		: 'End of Freeman Alley nr. Rivington St., Lower East Side',
		'tely' 		: '212.420.0012',
		'message' 	: 'Taxidermy chic gastropub serving throwback cocktails. $$',
		'lng' 		: '40.721956',
		'lat' 		: '-73.99246',
		'img' 		: 'dblue26.png'
	},
	{//9
		'name' : 'GREENWICH GRILL',
		'addy' : '428 Greenwich St. nr. Laight St., TriBeCa;',
		'tely' : '212.274.0428',
		'message' : 'This TriBeCa newcomer features Japanese & Italian fusion. $$',
		'lng' : '40.722419',
		'lat' : '-74.009972',
		'img' 		: 'dblue27.png'
	},
	{
		'name' 		: 'LOMBARDI\'S',
		'addy' 		: '32 Spring St., Nolita',
		'tely' 		: '(212) 941-7994',
		'message' 	: 'You can\'t visit NYC without sampling this "perfect pizza"; wood-fired, thin crust; an institution! $',
		'lng' 		: '40.721535',
		'lat' 		: '-73.995667',
		'img' 		: 'dblue28.png'
	}, 
	{
		'name' 		: 'JACK\'S LUXURY OYSTER BAR',
		'addy' 		: '101 Second Ave. nr. 6th St., East Village',
		'tely' 		: '212.253.7848',
		'message' 	: 'Inventive, New Orleans-inspired seafood. $$',
		'lng' 		: '40.727371',
		'lat' 		: '-73.988597',
		'img' 		: 'dblue29.png'
	}, 
	{
		'name' 		: 'LITTLE BRANCH',
		'addy' 		: '22 Seventh Ave. South, nr. Leroy St., West Village',
		'tely' 		: '212.929.4360',
		'message' 	: 'Old-fashioned cocktails complement live jazz at this retro watering hole. $',
		'lng' 		: '40.730362',
		'lat' 		: '-74.004698',
		'img' 		: 'dblue30.png'
	}, 
	{ 
		'name' 		: 'MOMOFUKU SSAM BAR',
		'addy' 		: '207 2nd Ave., nr. 13th St., East Village',
		'tely' 		: '(212) 254-3500',
		'message' 	: 'Delicious Asian-inspired casual fare (stylized pork buns) at a huge local fave. $',
		'lng' 		: '40.72907',
		'lat' 		: '-73.984222',
		'img' 		: 'dblue31.png'
	}, 
	{
		'name' 		: 'OMEN',
		'addy' 		: '113 Thompson St. nr. Prince St., SoHo',
		'tely' 		: '212.925.8923',
		'message' 	: 'This hidden SoHo treasure offers true Kyoto-style country cooking. $$',
		'lng' 		: '40.725984',
		'lat' 		: '-74.001954',
		'img' 		: 'dblue32.png'
	}, 
	{
		'name' 		: 'SOCARRAT PAELLA BAR',
		'addy' 		: '259 W. 19th St., nr. Eighth Ave., Chelsea',
		'tely' 		: '212.462.1000',
		'message' 	: 'Aromas of authentic paella waft at this nook named after the crust found on the bottom of a paella pan. $$',
		'lng' 		: '40.742757',
		'lat' 		: '-73.999476',
		'img' 		: 'dblue33.png'
	}, 

	{
		'name' 		: 'THE SPOTTED PIG',
		'addy' 		: '314 W. 11th St. nr. Greenwich St., West Village',
		'tely' 		: '212.620.0393',
		'message' 	: 'Down and dirty neighborhood gastropub serving one of the best burgers in town. $$',
		'lng' 		: '40.735712',
		'lat' 		: '-74.006918',
		'img' 		: 'dblue34.png'
	}, 
	{
		'name' 		: 'WO HOP',
		'addy' 		: '17 Mott St. nr. Mosco St., Chinatown',
		'tely' 		: '212.962.8617',
		'message' 	: 'Established in 1938, this old-fashioned Cantonese restaurant is a Chinatown staple. $',
		'lng' 		: '40.714445',
		'lat' 		: '-73.999357',
		'img' 		: 'dblue35.png'
	}
];

var stations = [
	{
		'name' 		: 'Franklin St Station',
		'msg'		: 'Subway services from this station: 1',
		'lng' 		: '40.71915',
		'lat' 		: '-74.006928',
		'img' 		: 'm.png'
	},
	
	{
		'name' 		: 'Canal St Station',
		'msg'		: 'Subway services from this station: A, C, E',
		'lng' 		: '40.721907',
		'lat' 		: '-74.005405',
		'img' 		: 'm.png'
	},
	
	/*{
		'name' 		: 'Canal St Station',
		'msg'		: 'Subway services from this station: 1',
		'lng' 		: '40.722592',
		'lat' 		: '-74.006374',
		'img' 		: 'm.png'
	}*/
]
/*
 * Full-screen Window Resize
 */

function getWindowHeight() {
	if (self.innerHeight) return self.innerHeight;
	if (document.documentElement && document.documentElement.clientHeight)
		return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight;
	return 0;
}

function getWindowWidth() {
	if (self.innerWidth) return self.innerWidth;
	if (document.documentElement && document.documentElement.clientWidth)
		return document.documentElement.clientWidth;
	if (document.body) return document.body.clientWidth;
	return 0;
}

function resize() {  

} 

function markerPoints( p, ul ){
	point = new GLatLng( p.lng, p.lat );
	var icn;
	icn = new GIcon();
	icn.image = ("img/"+p.img);
	icn.iconSize = new GSize(16, 16);
	icn.iconAnchor = new GPoint(8, 8);
	icn.infoWindowAnchor = new GPoint(5, 1);
	var marker = new GMarker(point,icn);
	
	map.addOverlay(marker);
	
	var html = p.name + "<br/>" + p.addy + "<br/>" +  p.message;
	$.extend(marker, p);
	
	GEvent.addListener(marker, "click", function() {
		var html = this.name + "<br/>";
		
		if( this.tely.length ){
			html +=  this.tely+  "<br/>";
		}
		html += this.addy +  "<br/>" +  this.message;
		this.openInfoWindowHtml( html,{maxWidth:200});
	});
	
	$('<li><a href="#">'+ (++poscnt) + '. ' +p.name+'</a></li>')
	.appendTo( ul )
	.find('a')
	.data('marker',marker)
	.click(function(){
		GEvent.trigger( $(this).data('marker') , "click");
		return false;
	});
	
	markers.push( marker );
}

function markerPointstation( p ){
	point = new GLatLng( p.lng, p.lat );
	var icn;
	icn = new GIcon();
	icn.image = ("img/"+p.img);
	icn.iconSize = new GSize(16, 16);
	icn.iconAnchor = new GPoint(8, 8);
	icn.infoWindowAnchor = new GPoint(5, 1);
	var marker = new GMarker(point,icn);

	map.addOverlay(marker);
	
	$.extend(marker, p);

	GEvent.addListener(marker, "click", function() {
		this.openInfoWindowHtml(this.name + "<br/>" +  "<br/>" +  this.msg,{maxWidth:200});
	});
}

/*
 * Main load function:
 */

function load() {

	var url=window.location.href;
	var pattern=/thegreenwichhotel.com\/neighborhood\//;
	
	if( url.match(pattern)){
		if(GBrowserIsCompatible()){
		map = new GMap2( document.getElementById("mapbox"), { backgroundColor: '#fff' } );
		map.addControl(new GLargeMapControl3D());
		map.disableContinuousZoom();
		map.disableDoubleClickZoom();
		
		var mercator = new GMercatorProjection(mapMaxZoom+1);

		// ============================================================
		// ====== Create a copyright entry =====
		var copyright = new GCopyright(1,mapBounds,0, "The Greenwich Hotel");
		
		// ============================================================
		// ====== Create a copyright collection and add the copyright to it =====
		var copyrightCollection = new GCopyrightCollection('Map Data:');
		copyrightCollection.addCopyright(copyright);
		
		CustomGetTileUrl = function(tile,zoom) {
			if ((zoom < mapMinZoom) || (zoom > mapMaxZoom)) {
				return "img/blank.jpg";
			} 
			var ymax = 1 << zoom;
			var y = ymax - tile.y -1;
			var tileBounds = new GLatLngBounds(
			mercator.fromPixelToLatLng( new GPoint( (tile.x)*256, (tile.y+1)*256 ) , zoom ),
			mercator.fromPixelToLatLng( new GPoint( (tile.x+1)*256, (tile.y)*256 ) , zoom )
			);
			if (mapBounds.intersects(tileBounds)) {
				return zoom+"/"+tile.x+"/"+y+".png";
			} else {
				return "img/blank.jpg";
			}
		}// end : CustomGetTileUrl
		
		// ============================================================
		// ===== Create the GTileLayer and apply the CustomGetTileUrl to it =====
		var tilelayers = [new GTileLayer(copyrightCollection,mapMinZoom,mapMaxZoom)];
		tilelayers[0].getTileUrl = CustomGetTileUrl;
		
		var custommap = new GMapType(tilelayers, new GMercatorProjection(mapMaxZoom+1), "Campus", {errorMessage:"No data available", alt:"Show locations near The Greenwich Hotel"});
		map.addMapType(custommap);
		map.setCenter(new GLatLng(40.737828,-73.991976), 13, custommap);
		
		var mt = map.getMapTypes();
		// Overwrite the getMinimumResolution() and getMaximumResolution() methods
		for (var i=0; i<mt.length; i++) {
			mt[i].getMinimumResolution = function() {return mapMinZoom;}
			mt[i].getMaximumResolution = function() {return mapMaxZoom;}
		}
		
		bullseye = new GIcon();
		bullseye.name= 'tst';
		bullseye.image = ("img/bullseye.png");
		bullseye.iconSize = new GSize(45, 45);
		bullseye.iconAnchor = new GPoint(22, 22);
		bullseye.infoWindowAnchor = new GPoint(22, 22);
		var bullseyemarker = new GMarker(new GLatLng( 40.719846,-74.009854 ),bullseye, true);
		map.addOverlay(bullseyemarker);
		
		bullseye = new GIcon();
		bullseye.image = ("img/logo.png");
		bullseye.iconSize = new GSize(181, 58);
		bullseye.iconAnchor = new GPoint(180, 57 );
		//bullseye.infoWindowAnchor = new GPoint(22, 22);
		var bullseyemarker = new GMarker(new GLatLng( 40.719846,-74.009854 ),bullseye, true);
		map.addOverlay(bullseyemarker);
		
		for( var i in stations ){
			markerPointstation( stations[i] );
		}
		
		//#l1 is hidden treasure
		var jqul = $('<ul>').appendTo('#l1');
		for( var i in poiSee3 ){
			markerPoints( poiSee3[i], jqul );
		}
		
		//#l2 is superior shopping
		jqul = $('<ul>').appendTo('#l2');
		for( var i in poiSee2 ){
			markerPoints( poiSee2[i], jqul );
		}
		
		//#l3 is best museum
		jqul = $('<ul>').appendTo('#l3');
		for( var i in poiSee4 ){
			markerPoints( poiSee4[i], jqul );
		}
		
		// #l4 is landmark
		jqul = $('<ul>').appendTo('#l4');
		for( var i in poiSee ){
			markerPoints( poiSee[i], jqul );
		}
		
		// #l5 is great place to eat
		jqul = $('<ul>').appendTo('#l5');
		for( var i in poiED ){
			markerPoints( poiED[i], jqul );
		}
		
		$('#distinationbox ul li[id]')
		.toggle(
			function(){
				$(this)
				.siblings('.isopened')
				.removeClass('isopened')
					.children(':not(span)')
						.stop()
						.trigger('click')
					.end()
				.end()
				.addClass('isopened')
				.children(':not(span)')
					.stop()
					.slideDown();
			},
			function(){
				$(this).removeClass('isopened')
				.children(':not(span)')
					.stop().slideUp();
			}
		);
		}
   }
   else if(!url.match(pattern))
   		alert('Google API key works only for URL in production');
   resize();
}

onresize=function(){ resize(); };
var img;
$(window)
.load(function(){
	var int = setInterval(function(){
		img = $('img[src="img/dblue9.png"]');
		if( img.css('z-index') != 1 ){
			img.css('z-index', 1);
		}
		else{
			clearInterval( int );
		}
	},1000);
});
