/**
 * Application Javascript Helper
 */
Date.prototype.conv = function () {
	this.getFullYear() + "/" + ( this.getMonth() + 1 ) + "/" + this.getDate();
}

Date.prototype.melty = function () {
    return {
			"year" : this.getFullYear(),
			"month" : ( this.getMonth() + 1 ),
			"date" : this.getDate(),
			"week" : this.getDay()
    };
}

Date.prototype.cal = function ( dd ){
    this.setTime( this.getTime() + dd * 24 * 60 * 60 * 1000 );
}

Date.prototype.toString = function () {
    return this.conv();
}

Array.prototype.debug = function () {
    var dstr = "[\n";

    if( arguments.length ) dstr = arguments[0];

		this.each ( function ( arg ) {
            if( ( (typeof x == "object") && (x.constructor == Array) ) ){
                return this.debug(dstr);
            }else{
                dstr += "\t" + arg.toString() + "\n";
            }
        }
				   );

    dstr += "]\n"

    return dstr;
}

function LandmarkMarker(){
	this.marker = new Array();
	this.point = null;
	this.initialize.apply(this, arguments);
}

LandmarkMarker.prototype = {
	requestURL : "",
	request : null,
	initialize : function (id, point){
		this.id = id;
		this.point = point;
		
		this.marker = new GMarker(point);
		this.marker.is_infowindow_opened = false;
		this.marker.id = id;

		GEvent.addListener(this.marker, "infowindowclose", function (){ 
			this.is_infowindow_opened = false; 
		} );
	}, 
	callbackMarker : function (){
		LandmarkMarker.prototype.request.open("GET", requestURL + "/" + this.id, false);
		LandmarkMarker.prototype.request.send(null);

		if(LandmarkMarker.prototype.request){
			res = LandmarkMarker.prototype.request.responseText;
			this.marker.openInfoWindowHtml(res, {"maxWidth" : 100});
		}
	},
	getMarker : function (){
		return this.marker;
	}
}

function ChangeArea(){
	this.initialize.apply(this, arguments);
}

ChangeArea.prototype = {
	map : null,
	point :  null,
	markers : new Array(),
	request : null,
	zoomLevel : 0,
	callbackRequest : Prototype.emptyFunction,
	callbackMarkerClick: Prototype.emptyFunction,
	initialize : function (){
		GEvent.addListener(ChangeArea.prototype.map, 'zoomend', function(oldZoomLevel, newZoomLevel) { 
			ChangeArea.prototype.zoomLevel = newZoomLevel; 
			this.clearOverlays();
		} );
		GEvent.addListener(ChangeArea.prototype.map, 'moveend', callbackMoveEnd );
	}
}

function callbackMoveEnd(){
	var map = ChangeArea.prototype.map;
	var request = ChangeArea.prototype.request;
	var zoomLevel = ChangeArea.prototype.zoomLevel;
	var i = 0;
	var markers = ChangeArea.prototype.markers;

	if(markers.length > 0){
		for(var i = 0; i < markers.length; i ++){
			//map.clearOverlays();
			if( !markers[i].getMarker().is_infowindow_opened ){
				map.removeOverlay(markers[i].getMarker());
			}
		}
	}

	if ( zoomLevel >= 15 ) {
		var bounds = map.getBounds();

		var responseXML = ChangeArea.prototype.callbackRequest(request, bounds);

		if(request){
			var xmlDoc = responseXML;

			if(xmlDoc){
				var pos = xmlDoc.documentElement.getElementsByTagName("pos");
				var currentMarkers = ChangeArea.prototype.markers;

				ChangeArea.prototype.markers = new Array(pos.length);

				for(var i = 0; i < pos.length; i ++ ){
					var id = pos[i].getAttribute("id");

					point      = new GLatLng(pos[i].getAttribute("lat"), pos[i].getAttribute("lng"));
					ChangeArea.prototype.markers[i] = new LandmarkMarker(id, point);

					map.addOverlay(ChangeArea.prototype.markers[i].getMarker());

					GEvent.addListener(ChangeArea.prototype.markers[i].getMarker(), "click", function (){
						callbackMarkerClick(this);
					} );
				}
			}
		}
	}
}

