function HZInfoWindow(html, point, width, iconSize, cssClass) {
	HZInfoWindow.removeCurrentHZInfoWindow(true);
	this.point_ = point;
	this.width_ = width;
	this.iconSize_ = iconSize;
	this.cssClass_ = cssClass || "hzInfoWindow";
	this.doBubble = false;
	this.clicked = false;
	this.mouseOver = false;
	this.mouseOut = true;
	this.html_ = html;
	this.map_ = null;
	this.addHeight = 0;
}

HZInfoWindow.currentHZInfoWindow = null; 

HZInfoWindow.toString = function() {
	return "[ [" + this.point_.lat() + "," + this.point_.lng() + '], w: ' + this.width_ + ', h: ' + this.height_ + ', html: ' + this.html_ + ' ]'; 
}


HZInfoWindow.removeCurrentHZInfoWindow = function(force) {
	if (force == undefined) {
		force = true;
	}
	
	if (HZInfoWindow.currentHZInfoWindow && (!HZInfoWindow.currentHZInfoWindow.mouseOver || force)) {
		var map = HZInfoWindow.currentHZInfoWindow.map_;
		if (HZInfoWindow.currentHZInfoWindow.business != null) {
			HZInfoWindow.currentHZInfoWindow.business.hzInfoWindow = null;
		}
		map.removeOverlay(HZInfoWindow.currentHZInfoWindow);
		map.getPane(G_MAP_FLOAT_PANE).innerHTML = '';
	}
}

function initHZInfoWindow() {
	HZInfoWindow.prototype = new GOverlay();

	HZInfoWindow.prototype.initialize = function(map) {
		HZInfoWindow.currentHZInfoWindow = this;

		this.map_ = map;

		var outerDiv = document.createElement("div");
		var div = document.createElement("div");
		var bubbleDiv = document.createElement("div");

		this.outerDiv_ = outerDiv;
		this.div_ = div;
		this.bubbleDiv_ = bubbleDiv;

		this.outerDiv_.appendChild(bubbleDiv);
		this.outerDiv_.appendChild(div);

		$(outerDiv).addClass("infoWindowOuterDiv");

		GEvent.bindDom(this.outerDiv_, 'mousedown', this,this.onClick_);
		GEvent.bindDom(this.outerDiv_, 'dblclick', this,this.onClick_);
		GEvent.bindDom(this.outerDiv_, 'DOMMouseScroll', this, this.onClick_);

		$(this.div_).html(this.html_)
			.addClass(this.cssClass_);

		this.setHeight();

		$(div).mouseover(function() {
			if (HZInfoWindow.currentHZInfoWindow == null) {
				return ;
			}
			HZInfoWindow.currentHZInfoWindow.mouseOver = true;
			HZInfoWindow.currentHZInfoWindow.mouseOut = false;

			});
		$(div).mouseout(function() {
			if (HZInfoWindow.currentHZInfoWindow == null) {
				return ;
			}
			HZInfoWindow.currentHZInfoWindow.mouseOver = false;
			HZInfoWindow.currentHZInfoWindow.mouseOut = true;
//			setTimeout("HZInfoWindow.removeCurrentHZInfoWindow(false)", 2000);
//			map.removeOverlay(HZInfoWindow.currentHZInfoWindow);
			});

		map.getPane(G_MAP_FLOAT_PANE).appendChild(outerDiv);
	}
	
	HZInfoWindow.prototype.setHeight = function() {
		var tempElement = this.div_.cloneNode(true);
		tempElement.id = '_tempContents';
		tempElement.style.visibility = 'hidden';	
		tempElement.style.height = 'auto';
		document.body.appendChild(tempElement);
		tempElement = document.getElementById('_tempContents');
		var contentHeight = tempElement.offsetHeight;
		document.body.removeChild(tempElement);

		this.height_ = contentHeight + this.addHeight;
	}

	HZInfoWindow.prototype.onClick_ = function(e) {
		if(navigator.userAgent.toLowerCase().indexOf('msie') != -1 
				&& document.all) { 
			window.event.cancelBubble = true;
			window.event.returnValue = false;
		} else {
			//e.preventDefault();
			e.stopPropagation();
		}
	}
	
	HZInfoWindow.prototype.additionalHeight = function(height) {
		this.addHeight = height;
	}

	// Remove the main DIV from the map pane
	HZInfoWindow.prototype.remove = function() {
		this.outerDiv_.removeChild(this.div_);
		this.outerDiv_.removeChild(this.bubbleDiv_);
	    this.outerDiv_.parentNode.removeChild(this.outerDiv_);
		HZInfoWindow.currentHZInfoWindow = null;
	}

	// Copy our data to a new Rectangle
	HZInfoWindow.prototype.copy = function() {
		return new HZInfoWindow(this.html_, this.point_, this.width_, this.iconSize_, this.cssClass_); 
	}

	HZInfoWindow.prototype.setTopLeft = function() {
		var ttPosToDiv = this.map_.fromLatLngToDivPixel(this.point_);
		var ttPos = this.map_.fromLatLngToContainerPixel(this.point_);
		var ttTop = ttPos.y - this.height_;
		var ttLeft = ttPos.x - this.width_;

		if (this.doBubble) {
			ttTop -= 12;
			ttLeft -= 10;
		}
		if (this.iconSize_ != null) {
			ttLeft -= this.iconSize_.width/2;
			ttTop -= this.iconSize_.height;
		}

		this.positionTop = (ttTop >= 0);
		this.positionLeft = (ttLeft >= 0);

		var odTop = ttPosToDiv.y;
		if (this.positionTop && this.iconSize_ != null) {
			odTop -= this.iconSize_.height;
		}
		this.outerDiv_.style.top = odTop + "px";

		var odLeft = ttPosToDiv.x;
		if (!this.doBubble && this.iconSize_ != null) {
			if (this.positionLeft) {
				odLeft -= this.iconSize_.width/2;
			} else {
				odLeft += this.iconSize_.width/2;
			}
		}
		this.outerDiv_.style.left = odLeft + "px";
	}

	HZInfoWindow.prototype.addCloseButton = function() {
		var closeDiv = document.createElement("div");

		$(closeDiv).addClass("iwClose");
		closeDiv.onclick = HZInfoWindow.removeCurrentHZInfoWindow;

		this.div_.appendChild(closeDiv);
	}
	
	HZInfoWindow.prototype.redraw = function(force) {
		if (!force) return;

		this.setTopLeft();

		this.div_.style.width = this.width_ + "px";

		$(this.div_).addClass(this.cssClass_);
		if (this.doBubble) {
			if (this.positionTop) {
				if (this.positionLeft) {
					$(this.bubbleDiv_).addClass("bubbleTopLeft");
					$(this.div_).addClass("iwBubbleTopLeft");
				} else {
					$(this.bubbleDiv_).addClass("bubbleTopRight");
					$(this.div_).addClass("iwBubbleTopRight");
				}
				this.div_.style.top = "-" + this.height_ + "px";
			} else {
				if (this.positionLeft) {
					$(this.bubbleDiv_).addClass("bubbleBottomLeft");
					$(this.div_).addClass("iwBubbleBottomLeft");
				} else {
					$(this.bubbleDiv_).addClass("bubbleBottomRight");
					$(this.div_).addClass("iwBubbleBottomRight");
				}
			}
		} else {
			if (this.positionTop) {
				if (this.positionLeft) {
					$(this.div_).addClass("iwBubbleTopLeft");
				} else {
					$(this.div_).addClass("iwBubbleTopRight");
				}
				this.div_.style.top = "-" + this.height_ + "px";
			} else {
				if (this.positionLeft) {
					$(this.div_).addClass("iwBubbleBottomLeft");
				} else {
					$(this.div_).addClass("iwBubbleBottomRight");
				}
			}
		}
	}
}


