var CustomInfoWindow = function(marker, html, height) {
	this.marker = marker;
	this.html = html;
	this.height = height;
}
CustomInfoWindow.prototype = new GOverlay();

CustomInfoWindow.prototype.initialize = function(map) {
	this.map = map;
	var container = document.createElement("div");
	container.style.display='none';
	map.getPane(G_MAP_FLOAT_PANE).appendChild(container);
	this.container = container;
}

CustomInfoWindow.prototype.remove = function() {
	this.container.parentNode.removeChild(this.container);
}

CustomInfoWindow.prototype.copy = function() {
	return new CustomInfoWindow(this.marker, this.html);
}
CustomInfoWindow.prototype.redraw = function(force) {
	if (!force) return;
	
	//get the content div
	var content = document.getElementById("CustomInfoWindow");
	
	if (content == null) {
		content = document.createElement("div");
		content.id = "CustomInfoWindow";
		$(content).height(this.height);
		GEvent.addDomListener(content, "mouseover", function(){
			map.disableDoubleClickZoom();
			map.disableScrollWheelZoom();
		});
		GEvent.addDomListener(content, "mouseout", function(){
			map.enableDoubleClickZoom();
			map.enableScrollWheelZoom();
		});
		$(content).bind("mousedown dblclick", function(event) {
			event.stopImmediatePropagation();
		});		
		//make it invisible for now
		content.style.visibility='hidden';
		 
		//temporarily append the content to the map container
		this.map.getContainer().appendChild(content);

		InfoWindowTop = document.createElement("div");
		InfoWindowTop.id = "CustomInfoWindowTop";
		InfoWindowBottom = document.createElement("div");
		InfoWindowBottom.id = "CustomInfoWindowBottom";
		
		closeButton = document.createElement("a");
		closeButton.href = "#";
		closeButton.className = "closeButton";
		closeImage = document.createElement("img");
		closeImage.src='layouts/main_site/images/closebutton.png'
		closeImage.alt='Close';
		closeButton.appendChild(closeImage);
		
		content.appendChild(InfoWindowTop);
		content.appendChild(closeButton);
		content.appendChild(this.html);
		content.appendChild(InfoWindowBottom);
		
		//add any event handlers like the close box
		var marker = this.marker;
		$(closeButton).bind("click", function(){
			var outlet = $("span.ui-state-active").parent().attr("id").replace("outlet", "outletdetails");
			$("#"+outlet).trigger("click");
		});
	}
	
	//retrieve the rendered width and height
	var contentWidth = content.offsetWidth - content.style.padding * 2;
	var contentHeight = content.offsetHeight;
	
	//remove the content from the map
	content.parentNode.removeChild(content);
	content.style.visibility='visible';
	 
	//get the X,Y pixel location of the marker
	var pixelLocation = this.map.fromLatLngToDivPixel( this.marker.getLatLng() );
	 
	//position the container div for the window
	this.container.style.position='absolute';
	this.container.style.left = pixelLocation.x- (contentWidth / 2 - this.marker.getIcon().iconSize.width / 2) + "px";
	this.container.style.top = (pixelLocation.y
	- contentHeight
	- 25
	- this.marker.getIcon().iconSize.height
	) + "px";
	 
	this.container.style.display = 'block';
	//append the styled info window to the container
	this.container.appendChild(content);
	 
	var mapNE = this.map.fromLatLngToDivPixel(
	this.map.getBounds().getNorthEast()
	);
	 
	var panX=0;
	var panY=0;
	 
	if(this.container.offsetTop < mapNE.y) {
	//top of window is above the top edge of the map container
	panY = mapNE.y - this.container.offsetTop;
	}
	 
	if(this.container.offsetLeft+contentWidth+10 > mapNE.x) {
	//right edge of window is outside the right edge of the map container
	panX = (this.container.offsetLeft+contentWidth+10) - mapNE.x;
	}
	 
	if(panX!=0 || panY!=0) {
	//pan the map
	this.map.panBy(new GSize(-panX-10,panY+5));
	}
}

GMap2.prototype.CustomInfoWindowInstance = null;
GMarker.prototype.openCustomInfoWindow = function(html, height) {
	if (map.CustomInfoWindowInstance != null)
		this.closeCustomInfoWindow();

	map.CustomInfoWindowInstance = new CustomInfoWindow(this, html, height);
	map.addOverlay(map.CustomInfoWindowInstance);
}

GMarker.prototype.closeCustomInfoWindow = function() {
	if (map.CustomInfoWindowInstance != null) {
		map.removeOverlay(map.CustomInfoWindowInstance);
		map.CustomInfoWindowInstance = null;
	}
	
	map.enableDoubleClickZoom();
	map.enableScrollWheelZoom();
}
