var YMap = new function() {
	this.balloonOptions = {
		maxWidth: 150,
		hasCloseButton: true,
		mapAutoPan: true
	}
	
	this.init = function() {
		this.map = new YMaps.Map($('#YMapsID')[0]);
	    this.map.setCenter(new YMaps.GeoPoint(this.getLng(), this.getLat()), 13);
		this.map.addControl(new YMaps.TypeControl());
		this.map.addControl(new YMaps.Zoom());
		
		this.initPlaceMark();
		this.events();
	}
	
	this.initPlaceMark = function() {
		this.placemark = new YMaps.Placemark(new YMaps.GeoPoint(this.getLng(), this.getLat()), {
			draggable: true,
			balloonOptions: this.balloonOptions
		});
		this.setPointTitles();
		this.map.addOverlay(this.placemark);
	}
	
	this.events = function() {
	    YMaps.Events.observe(this.placemark, this.placemark.Events.DragEnd,  function (map) {
	        $('#_lng').val(map.getGeoPoint().toString().split(',')[0]);
	        $('#_lat').val(map.getGeoPoint().toString().split(',')[1]);
	    });

	    $('#_lng').keyup(function() {
	    	YMap.setMapPoint();
	    });
	    $('#_lat').keyup(function() {
	    	YMap.setMapPoint();
	    });
	    
	    $('#_point_name').keyup(function() {
	    	YMap.setPointTitles();
	    });
	    $('#_point_desc').keyup(function() {
	    	YMap.setPointTitles();
	    });
	}

    this.setMapPoint = function() {
    	this.placemark.setGeoPoint(new YMaps.GeoPoint(this.getLng(), this.getLat()));
    	this.map.panTo(this.placemark.getGeoPoint(), { flying: true });
    }
    
    this.setPointTitles = function() {
    	this.placemark.name = $('#_point_name').val();
    	this.placemark.description = $('#_point_desc').val();
    };
    
    this.getLng = function() {
    	return $('#_lng').val();
    }
    this.getLat = function() {
    	return $('#_lat').val();
    }
    
    return this;
}();

var YMapApp = new function() {
	this.oldLng = null;
	this.oldLat = null;
	this.startfly = false;
	this.zoom = 16;
	this.maxWidth = 250;
	
	this.init = function(id) {
		this.balloonOptions = {
			maxWidth: this.maxWidth,
			hasCloseButton: false,
			mapAutoPan: true
		}
		
		if (!id) id = 'YMapsID';
		this.map = new YMaps.Map($('#' + id)[0]);
	    this.map.setCenter(new YMaps.GeoPoint(this.getLng(), this.getLat()), this.zoom);

	    if (this.oldLng === null) this.oldLng = this.getLng();
	    if (this.oldLat === null) this.oldLat = this.getLat();
	    
		this.initPlaceMark();
	}
	
	this.initPlaceMark = function() {
		this.map.openBalloon(new YMaps.GeoPoint(this.getLng(), this.getLat()), this.getContent(), this.balloonOptions);
	}
	
    this.setPointTitles = function() {
    	
    	this.placemark.name = this.point_name;
    	this.placemark.description = this.point_desc;
    };
    
    this.getLng = function() {
    	return this.lng;
    }
    this.getLat = function() {
    	return this.lat;
    }
    
    this.getContent = function() {
    	return '<b>'+this.point_name+'</b><div>'+this.point_desc+'</div>';
    }
    
    this.fly = function() {
    	this.startfly = true;
    	
    	if (this.getLng() == this.oldLng && this.getLat() == this.oldLat) return false;
    	this.oldLng = this.getLng();
    	this.oldLat = this.getLat();
    	
    	this.map.closeBalloon();
    	
    	if (this.startfly) {
	    	YMapApp.map.panTo(
				new YMaps.GeoPoint(YMapApp.getLng(), YMapApp.getLat()),
				{
					flying: true,
					callback: function(result) {
						if (result == 'Success') {
							YMapApp.map.openBalloon(new YMaps.GeoPoint(YMapApp.getLng(), YMapApp.getLat()), YMapApp.getContent(), YMapApp.balloonOptions);
							YMapApp.map.setZoom(YMapApp.zoom);
							YMapApp.startfly = false;
						}
					}
				}
			);
    	}
    }
    
    this.mapAreaIsHide = function() {
    	var areaTop = $('.b-places-tabs').position().top;
    	var windowTop = $('html').scrollTop();
    	return (windowTop > areaTop);
    }
    
    this.showMapAreaCallBack = function() {
    	return true;
    }
    
    this.showMapArea = function() {
    	var areaTop = $('.b-places-tabs').position().top;
    	var windowTop = $('html').scrollTop();
		$('html').animate({
			scrollTop: areaTop + 'px'
		}, 'normal', 'swing', function() { YMapApp.showMapAreaCallBack(); });
    }
	
	return this;
}();
