CWL.page = {};

CWL.page.AreaSearch = function(){
    var cookieLat = CWL.util.Cookie.get('area_search_map_lat');
    var defaultLat = cookieLat != undefined ? cookieLat : this.DEFAULT_LAT;

    var cookieLon = CWL.util.Cookie.get('area_search_map_lon');
    var defaultLon = cookieLon != undefined ? cookieLon : this.DEFAULT_LON;

    var cookieZoom = CWL.util.Cookie.get('area_search_map_zoom');
    var defaultZoom = cookieZoom != undefined ? cookieZoom : this.DEFAULT_ZOOM;

    this.gmap = new GMap2(document.getElementById(this.GMAP_ELEMENT_ID));
    this.gmap.addControl(new GMapTypeControl());
    this.gmap.addControl(new GLargeMapControl());
    this.gmap.addControl(new GOverviewMapControl());
    this.gmap.setCenter(new GLatLng(defaultLat,defaultLon),eval(defaultZoom));

    GEvent.bind(this.gmap,'moveend',this,this.execute);
};

CWL.page.AreaSearch.prototype = {
    _isInfoWindowOpened : false,

    DEFAULT_LAT : '35.6675',
    DEFAULT_LON : '139.7666',
    DEFAULT_ZOOM : 15,
    GADGET_NUM : 20,
    GMAP_ELEMENT_ID : 'map',
    TARGET_CLASS : 'cwl_dd_target',
    TARGET_ELEMENT_ID : 'point_list',

    execute : function(){
	if(this._isInfoWindowOpened){
	    return null;
	}

	var sw = this.gmap.getBounds().getSouthWest();
	var ne = this.gmap.getBounds().getNorthEast();

	Ext.Ajax.request({
	    method : 'GET',
	    url : '/CWL/json/AreaSearch.cgi',
	    params : {s:sw.lat(),w:sw.lng(),n:ne.lat(),e:ne.lng()},
	    scope : this,
	    success : function(obj){
		var json = eval('(' + obj.responseText + ')');
		this.markerManager = new CWL.gmap.MarkerManager(this.gmap);
		var targetManager = new CWL.dd.TargetManager('.'+this.TARGET_CLASS);

		this.gmap.clearOverlays();
		targetManager.point_list.clearGadgets();

		for(var i=0;i<json.points.length;i++){
		    json.points[i].anchor = {href:'javascript:void(0)',onclick:'CWL.page.OBJ.markerManager.getMarker(\''+json.points[i].id+'\').openInfoWindow();'};
		    json.points[i].body = createBody(json.points[i]);

		    var m = new CWL.gmap.Marker(json.points[i]);
		    m.setInfoWindow(new CWL.gmap.InfoWindow(json.points[i]));
		    this.markerManager.addMarker(m);

		    GEvent.addListener(m.getGMarker(),'infowindowopen',function(){
			CWL.page.OBJ._isInfoWindowOpened = true;
		    });
		    GEvent.addListener(m.getGMarker(),'infowindowclose',function(){
			CWL.page.OBJ._isInfoWindowOpened = false;
		    });

		    if(i < this.GADGET_NUM){
			var g = new CWL.dd.Gadget(json.points[i]);
			g.lock = true;
			targetManager[this.TARGET_ELEMENT_ID].addGadget(g);
		    }
		}

		targetManager.point_list.refresh();
	    }
	});

	CWL.util.Cookie.set('area_search_map_lat',this.gmap.getCenter().lat());
	CWL.util.Cookie.set('area_search_map_lon',this.gmap.getCenter().lng());
	CWL.util.Cookie.set('area_search_map_zoom',this.gmap.getZoom());

	var createBody = function(obj){
	    var I = {};
	    if(obj.image){
		var url = '/CWL/www/point/pict/' + obj.uid + '/' + obj.id + '/T2_' + obj.image + '.jpg';
		I = {tag:'img',src:url,align:'right'};
	    }

	    var desc = CWL.util.HTML.encodeCRLF(obj.description);
	    var DT = {tag:'div',children:[I,desc]};

	    var M = {tag:'a',href:'javascript:void(0)',onclick:'CWL.page.OBJ.markerManager.getMarker(\''+obj.id+'\').showMapBlowup();',html:'地図'};
	    var F = {tag:'div',style:'text-align:right',children:['[',M,']']};

	    var D = {tag:'div',children:[DT,F]};

	    return Ext.DomHelper.createTemplate(D).apply();
	}

    }
};

function load(){
    var a = new CWL.page.AreaSearch();
    a.execute();
    CWL.page.OBJ = a;
}

