/* BFInfoWindow mimics the InfoWindow class found Google Maps API v3 but allows
* the user to define their own info window styles.
* @author Peter Robinett, peter@bubblefoundry.com
* @version 0.1
* An BFInfoWindow is like an info window, but it displays under the marker,
* opens quicker, and has flexible styling. Overload its createElement to style the info window.
* @param {InfoWindowOptions} options An options object
* http://github.com/pr1001/BFInfoWindow/blob/master/bfinfowindow.js
*/
function BFInfoWindow(options) {
  google.maps.OverlayView.call(this);
  this.offsetVertical_ = -111;
  this.offsetHorizontal_ = -66;
  this.height_ = 92;
  this.width_ = 132;
  this.setMap(this.map_);
  this.setOptions(options);
}
BFInfoWindow.prototype = new google.maps.OverlayView();
var myMarkerNow;
BFInfoWindow.prototype.open = function(map, anchor) {
  	myMarkerNow = anchor;
	this.setLocation(anchor.position);
	this.latlng_ = this.getLocation();
	this.map_ = map;
	var self = this;
  	this.setMap(this.map_);
};
BFInfoWindow.prototype.close = function() {this.setMap(null);};
BFInfoWindow.prototype.setOptions = function(options) {this.setValues(options);};
BFInfoWindow.prototype.setContent = function(content) {this.set("content", content);};
BFInfoWindow.prototype.getContent = function() {return this.get("content");};
BFInfoWindow.prototype.setUrl = function(myUrl) {this.set("url", myUrl);};
BFInfoWindow.prototype.getUrl = function() {return this.get("url");};
BFInfoWindow.prototype.setLocation = function(location) {this.set("location", location);};
BFInfoWindow.prototype.getLocation = function() {return this.get("location");};
BFInfoWindow.prototype.setZIndex = function(zIndex) {this.set("zIndex", zIndex);};
BFInfoWindow.prototype.getZIndex = function() {return this.get("zIndex");};
BFInfoWindow.prototype.remove = function() {if (this.myDiv) {this.myDiv.parentNode.removeChild(this.myDiv);this.myDiv = null;}};
BFInfoWindow.prototype.draw = function() {
  	this.createElement();
	if (!this.myDiv) return;
	var pixPosition = this.getProjection().fromLatLngToDivPixel(this.latlng_);
	if (!pixPosition) return;
	this.myDiv.style.width = this.width_ + "px";
	this.myDiv.style.left = (pixPosition.x + this.offsetHorizontal_) + "px";
	this.myDiv.style.height = this.height_ + "px";
	this.myDiv.style.top = (pixPosition.y + this.offsetVertical_) + "px";
	this.myDiv.style.display = 'block';
	this.myDiv.style.width = this.width_ + "px";
	this.myDiv.style.left = (pixPosition.x + this.offsetHorizontal_) + "px";
	this.myDiv.style.height = this.height_ + "px";
	this.myDiv.style.top = (pixPosition.y + this.offsetVertical_) + "px";
	this.myDiv.style.display = 'block';
	var mapDiv = this.map_.getDiv();
	var mapWidth = mapDiv.offsetWidth;
	var mapHeight = mapDiv.offsetHeight;
	var bounds = this.map_.getBounds();
	var boundsSpan = bounds.toSpan();
	var longSpan = boundsSpan.lng();
	var latSpan = boundsSpan.lat();
	var degWidth = (this.width_/mapWidth) * longSpan;
	var degHeight = (this.height_/mapHeight) * latSpan;
	if ((this.latlng_.lng() + (degWidth/2)) > bounds.getNorthEast().lng()) {
		this.myDiv.style.left = pixPosition.x - (((this.latlng_.lng() + degWidth) - bounds.getNorthEast().lng())/longSpan*mapWidth) - 20 + "px";}
	if ((this.latlng_.lng() - (degWidth/2)) < (bounds.getSouthWest().lng())) {
		new_posit = pixPosition.x + this.width_  - (((this.latlng_.lng() + degWidth) - bounds.getSouthWest().lng())/longSpan*mapWidth) + 10 + "px";
		this.myDiv.style.left = new_posit;}
	if ((this.latlng_.lat()) < (bounds.getSouthWest().lat())+((this.offsetVertical_ + this.height_)/mapHeight*latSpan)) {
		this.myDiv.style.top = (pixPosition.y - this.offsetVertical_ - this.height_) + "px";
	}
};
BFInfoWindow.prototype.createElement = function() {
  	var panes = this.getPanes();
	var div = this.myDiv;
	if (!div) {
		div = this.myDiv = document.createElement("div");
		div.style.border = "0px none";
		div.style.position = "absolute";
		div.style.width = this.width_ + "px";
		div.style.height = this.height_ + "px";
		var contentDiv = document.createElement("div");
		contentDiv.id = "BFInfoWindow_content";
		contentDiv.innerHTML = this.getContent();
		div.appendChild(contentDiv);
		div.style.display = 'none';
		panes.floatPane.appendChild(div);
	} else if (	div.parentNode && div.parentNode != panes.floatPane) {
		div.parentNode.removeChild(div);
		panes.floatPane.appendChild(div);
		document.getElementById("BFInfoWindow_content").innerHTML = this.getContent();
	} else {
		if(document.getElementById("BFInfoWindow_content")){
			document.getElementById("BFInfoWindow_content").innerHTML = this.getContent();
		}
	}
};
BFInfoWindow.prototype.removeBFInfoWindow = function(ib) {
	return function() {
		ib.setMap(null);
	};
};
