var Map = {
	map: null,
	onComplete: null,
	defaultIconImage: null,

	// basic map initialization
	load: function(account,callbackUrl) {
		Map.map = new GMap2(document.getElementById("map"));
		Map.map.addControl(new GLargeMapControl());
    	Map.map.addControl(new GMapTypeControl());
	},

	moveTo: function(lat,lng,zoom) {
		point = new GLatLng(lat,lng);
		Map.map.setCenter(point, zoom);
	},

	formatBalloon: function(place) {
		//if(place.photo.indexOf('maps:cbk0.google.com') != -1){
		//	place.photo = place.photo.substr(place.photo.indexOf('cbk0.google.com'), place.photo.length);
		//	place.photo = 'http://'+place.photo;
		//}
		//if(place.photo.indexOf('maps:base.googlehosted.com') != -1){
		//	place.photo = place.photo.substr(place.photo.indexOf(':base.googlehosted.com')+1, place.photo.length);
		//	place.photo = 'http://'+place.photo;
		//}
		//if(place.photo.indexOf('cbk0.google.com') == 0){
		//	place.photo = 'http://'+place.photo;
		//}
		website = (place.website.length > 27) ? place.website.substr(0, 27)+'...' : place.website;
		var ret = '<div class="msiw" style="">'
			+'<div class="msinfotitle" dir="ltr" align="left">'+place.title+'</div>'
			+'<div class="msinfotitlegap"></div>'
			+'<div class="description" dir="ltr" align="left"></div>'
			+'<div class="iw">'
				+'<table><tbody>'
					+'<tr>'
						+'<td>';
						if(place.cid != ""){
							ret += '<a href="http://maps.google.com/maps?ie=UTF8&amp;hl=en&amp;cid='+place.cid+'">'+place.title+'</a>';
						}
						ret += '<br>'
							+'<span>'+place.address1+'</span>'
							+'<br>'
							+'<span>'+place.address2+'</span>'
							+'<br>'
							+'<span>'+place.phone+'</span>'
							+'<br>'
							+'<a class="homepage" target="_blank" href="http://'+place.website+'">'+website+'</a>'
							+'<br>'
						+'</td>';
		if(place.photo != ""){
			ret += '<td class="msiwpdphotocol"><img class="rimg" src="'+place.photo+'" alt="" width="90" height="70" onerror="javascript:this.width=0;this.height=0;" /></td>';
		}
		ret +=  '</tr>'
				+'</tbody></table>'
			+'</div>'
		+'</div>';
		
		return ret;
	},

	createMarker: function(place) {
		var icon = new GIcon();
		icon.image = baseUrl + "/img/ipa_glass_icon35.png";
		icon.iconSize = new GSize(32, 32);
		icon.shadow = null;
		icon.shadowSize = new GSize(22, 20);
		icon.iconAnchor = new GPoint(15, 20);
		icon.infoWindowAnchor = new GPoint(15, 5);

		var marker=new GMarker(new GLatLng(place.latitude, place.longitude), {"icon":icon});
		GEvent.addListener(marker,"click",function(){
			marker.openInfoWindowHtml(Map.formatBalloon(place));
		});
		return marker;
	},
	addAll: function(places){
		markers = [];
		var style = [{
	        url: baseUrl + "/img/ipa_glass_icon35.png"
	        , height: 35
	        , width: 35
	        //, opt_anchor: [18, 15]
	      },
	      {
	        url: baseUrl + "/img/ipa_glass_icon46.png"
	        , height: 46
	        , width: 46
	        //,opt_anchor: [30, 17]
	      },
	      {
	        url: baseUrl + "/img/ipa_glass_icon.png"
	        , height: 52
	        , width: 52
	        //, opt_anchor: [15, -15]
	      }];
	    
	    //markers.push(marker);
		for (var i = 0; i < places.length; ++i) {
		    //var latlng = new GLatLng(places[i].latitude, places[i].longitude);
		    var marker = Map.createMarker(places[i]);
		    markers.push(marker);
		}
		
		
		var markerCluster = new MarkerClusterer(Map.map, markers, {gridSize: 40, styles: style, maxZoom: 13});
		
		//markerCluster.resetViewport();
	},
	add: function(place){
		//var pl = ;
		var icon = new GIcon();
		icon.image = baseUrl + "/img/averylogo64.png";
		icon.iconSize = new GSize(58, 58);
		icon.shadow = null;
		icon.shadowSize = new GSize(22, 20);
		icon.iconAnchor = new GPoint(20, 20);
		icon.infoWindowAnchor = new GPoint(30, 5);

		var marker = new GMarker(new GLatLng(place.lat, place.lng), {"icon": icon, "zIndexProcess": Map.order});
		GEvent.addListener(marker,"click",function(){
			marker.openInfoWindowHtml(Map.formatBalloon(place));
		});
		Map.map.addOverlay(marker);
	},
	order: function(marker, b){
		return -99999;
	}
}

$(window).bind('unload', function(){
	GUnload();
});