var map, geo=new GClientGeocoder(), tpl=new JSmarty()

var CrepeMaps = {
	movedTimer: null,
	init: function(id, options){
		CrepeMaps.initMap(id, options);
		CrepeMaps.initWindowElements();
		
		CrepeMaps.moved();
		
		jQuery("body").unload(function(){
			GUnload();
		});
	},
	initMap: function(id, options){
		var settings = jQuery.extend({lat: 37.517, lng: 137.7, zm: 5, type: "HYBRID" }, options);
		
		map = new GMap2(document.getElementById(id), {
			mapTypes: new Array(G_NORMAL_MAP, G_SATELLITE_MAP, G_HYBRID_MAP, G_PHYSICAL_MAP)
		});
		
		map.enableScrollWheelZoom();
		map.enableContinuousZoom();
		map.addControl(new GMapTypeControl(), new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(10,93)));
		map.addControl(new GLargeMapControl(), new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(10, 10)));
		map.addControl(new SearchPlaceControl());
		map.setCenter(new GLatLng(settings.lat, settings.lng), settings.zm);
		
		switch(settings.type){
			case "NORMAL": map.setMapType(G_NORMAL_MAP); break;
			case "SATELLITE": map.setMapType(G_SATELLITE_MAP); break;
			case "HYBRID": map.setMapType(G_HYBRID_MAP); break;
			case "PHYSICAL": map.setMapType(G_PHYSICAL_MAP); break;
			default: map.setMapType(G_HYBRID_MAP);
		}
		
		GEvent.addListener(map, 'moveend', function(){ CrepeMaps.moved(); });
	},
	initWindowElements: function(){
		jQuery("div.opacityW").opacity(9);
		jQuery("div.opacityB").opacity(8);
		
		jQuery(".labSearch").innerLabel();
		
		jQuery("input[@name='orderBy']").click(function(){
			CrepeMaps.moved();
		});
		
		jQuery("#btnSearch").click(function(){
			CrepeMaps.search( jQuery("#inpSearch").val() );
		});
	},
	message: function(m){
		var messageBox = jQuery("#message").html(m);
		
		messageBox.fadeIn("fast", function(){
			setTimeout(function(){ messageBox.fadeOut("fast"); }, 1500);
		});
	},
	search: function(q){
		jQuery("#showLoading").html("Now Searching...").css("display",　"block");
			
		geo.getLatLng(q, function(p){
			jQuery("#showLoading").empty().css("display",　"none");
			if(!p){
				CrepeMaps.message(q+"に関連する場所は見つかりませんでした。");
			}else{
				map.setZoom(15);
				map.panTo( new GLatLng(p.lat(), p.lng()) );
			}
		});
	},
	clearList: function(){
		map.clearOverlays();
		jQuery("#debug tbody").empty();
		jQuery("#mapPhotoList,#floatList").empty();
	},
	addList: function(obj){
		tpl.assign("photo", obj);
		
		var pop = tpl.fetch("mapMarker_photo.tpl")
		
		var mark = new GMarker(new GLatLng(obj.mapInfo.lat, obj.mapInfo.lon));
		map.addOverlay(mark);
		
		GEvent.addListener(mark, "click", function(){ mark.openInfoWindowHtml(pop); });
		GEvent.addListener(mark, "infowindowopen", function(){ map.savePosition(); GEvent.clearListeners(map, "moveend") });
		GEvent.addListener(mark, "infowindowclose", function(){ map.returnToSavedPosition(); GEvent.addListener(map, 'moveend', function(){ GEvent.clearListeners(map, "moveend"); GEvent.addListener(map, "moveend", function(){ CrepeMaps.moved(); }); }); });
		
		jQuery(tpl.fetch("mapPhotoList_photo.tpl")).appendTo("#mapPhotoList")
		.click(function(){
			mark.openInfoWindowHtml(pop);
		});
		
		jQuery(tpl.fetch("mapPhotoList_photoFloat.tpl")).appendTo("#floatList");
	},
	buildList: function(){
		jQuery("img.openInfo").floatside({prefix: "float", direction: "north"});
		jQuery("div.float").opacity(9);
		jQuery("#showLoading").empty().css("display",　"none");
	},
	moved: function(){
		var c  = map.getCenter();
		
		if( CrepeMaps.movedTimer ){
			clearTimeout( CrepeMaps.movedTimer );
		}
		
		jQuery("#showLoading").html("Now Loading...").css("display",　"block");
		jQuery("div.float").css("display", "none");
		jQuery("#location").html("緯度："+Math.round(c.lat()*1000)/1000+", 経度："+Math.round(c.lng()*1000)/1000);
		
		CrepeMaps.movedTimer = setTimeout(function(){
			var ne = map.getBounds().getNorthEast();
			var sw = map.getBounds().getSouthWest();
			var ob = jQuery("input[@name='orderBy'][@checked]").val();
			
			AjaxPhotoService.getPhotosInArea(c.lat(), c.lng(), ne.lat(), ne.lng(), sw.lat(), sw.lng(), ob, 35, function(photos){
				CrepeMaps.clearList();
				jQuery.each(photos, function(i,obj){
					CrepeMaps.addList(obj);
				});
				CrepeMaps.buildList();
			});
		}, 1000);
	}
}

