/**
 * GMyWindow for Prototype.js
 * Joseph Bartlett <contact@jdbartlett.com>
 */

GMyWindow = function() {
	this._child = false; // As an alternative to HTML, a child content can be specified.
	this._html = '';
};
GMyWindow.prototype = new GOverlay();

GMyWindow.prototype.close = function() {
	if('function' == typeof this._options.onCloseFn) {
		this._options.onCloseFn();
	}
	this._div.remove();
};

GMyWindow.prototype.hide = function() {
	this._div.hide();
};

GMyWindow.prototype.initialize = function(map) {
	this._map = map;

	var div = $(map.getPane(G_MAP_MAP_PANE)).insert('<div class="GMyWindow" style="display:none; position:absolute">'+this._html+'</div>').getElementsBySelector(':last-child.GMyWindow')[0].hide();
	this._div = div;

	if('boolean' != typeof this._options.noCloseOnClick || !this._options.noCloseOnClick) {
		var self = this;
		div.observe('click', function() { self.close(); });
	}

	if(this._content) {
		alert('Oops.  Unsupported feature.');
	}

	this.redraw();
	this.open();
};

GMyWindow.prototype.open = function() {
	this._div.show();
//	this._map.panTo(new GLatLng(this._latlng.lat(), this._latlng.lng()));

	if('function' == typeof this._options.onOpenFn) {
		this._options.onOpenFn();
	}
};

GMyWindow.prototype.redraw = function(a) {
	var latlngPx = this._map.fromLatLngToDivPixel(this._latlng);

	var offset_x = 0; var offset_y = 0;
	if('object' == typeof this._options.pixelOffset) {
		offset_x = this._options.pixelOffset.width;
		offset_y = this._options.pixelOffset.height;
	};

	this._div.setStyle({
		'left': (latlngPx.x + offset_x) + 'px',
		'top': (latlngPx.y + offset_y) + 'px'
	});
};

GMyWindow.prototype.remove = function() {
	this._div.remove();
};

/**
 * Setter for Window content HTML.
 */
GMyWindow.prototype.setHtml = function(html) {
	this._child = false;
	this._html = html;
	return this;
};

/**
 * Setter for Window location (map latitude/longitude).
 */
GMyWindow.prototype.setLatLng = function(latlng) {
	this._latlng = latlng;
	return this;
};

/**
 * Setter for window options.
 *
 * Supports the following options in the style of GInfoWindowOptions:
 *
 * - noCloseOnClick (Boolean)
 * - onOpenFn (Function)
 * - onCloseFn (Function)
 * - pixelOffset (GSize)
 */
GMyWindow.prototype.setOptions = function(options) {
	if('object' == typeof options) {
		this._options = options;
	}
	return this;
};

GMyWindow.prototype.show = function() {
	this._div.show();
};

/**
 * Just like openInfoWindowHtml.
 */
GMap2.prototype.openMyWindowHtml = function(latlng, html) {
	var options = {};
	if('object' == typeof arguments[2]) options = arguments[2];

	var w = new GMyWindow().setLatLng(latlng).setHtml(html).setOptions(options);
	map.addOverlay(w);
	return w
};