// FWindow
// Version 1.0
// 2008-03-22 Initial version
var currentbox = null;

function FWindow(map, imageSrcs, imageSizes, contentWidth, contentHeight, markerArrowOffsetLeft, markerArrowOffsetTop)
{
	try{
		currentbox = this;
		this.map = map;
		this.container = null;
		this.imageSizes = imageSizes;
		this.markerArrowOffsetLeft = markerArrowOffsetLeft || 0;
		this.markerArrowOffsetTop = markerArrowOffsetTop || 0;
		this.displayMe = false;
		this.hidden = true;
		
		if (markerArrowOffsetLeft != "auto")
			markerArrowOffsetLeft = markerArrowOffsetLeft + "px";
			
		if (markerArrowOffsetTop != "auto")
			markerArrowOffsetTop = markerArrowOffsetTop + "px";
		
		// To handle callbacks
		var that = this;
		
		// Setup layout
		this.container = document.createElement("div");
		this.container.style.position = "absolute";
		this.container.id = "FWindow";
		this.container.style.zindex = 1000;
		this.container.style.display = "none";
		$("body").append(this.container);
		
		// Insert the table for the background images
		var tmp;
		tmp  = '<div id="FWindowcontent"></div>';
		this.container.innerHTML = tmp;
		jQuery('#mapHolder').append(this.container);
		
		// Close the window if the map moves
		GEvent.addListener(map, "dragstart", function() {
			that.hide();
			try {
				mapmanager.fwindowClose();
			}
			catch(e){
				
			}
		});
		GEvent.addListener(map, "moveend", function() {
			that.hide();
		});
		
		// Close on clicks and mousedowns as well
		GEvent.addDomListener($("#map")[0], "click", function() {
			if (that.isHidden() == false)
			{
				that.hide();
			}
		});
	}catch(ex){
		if(debug) alert(ex);
	}
}
      
FWindow.prototype.openOnMarker = function(marker, html, afterPan, minWidth, maxWidth, useMarkerGetHTML, markerArrowOffsetLeft)
{	
	try{
		if (useMarkerGetHTML == true)
			this.html = marker.getHTML();
		else
			this.html = html;
	
		if (afterPan == true)
		{
			this.hide();
			this.marker = marker;
			this.minWidth = minWidth;
			this.maxWidth = maxWidth;
			this.useMarkerGetHTML = useMarkerGetHTML;
			this.markerArrowOffsetLeft = markerArrowOffsetLeft;
			this.displayMe = true;
		}
		else
		{
			// Offset the final position by the markers anchor-position
			var vx = marker.getIcon().iconAnchor.x - marker.getIcon().infoWindowAnchor.x;
			var vy = marker.getIcon().iconAnchor.y - marker.getIcon().infoWindowAnchor.y;
			if (vy < 32)
				vy = 32;
			
			this.openOnMap(marker.getPoint(), this.html, new GPoint(vx,vy), false, minWidth, maxWidth, markerArrowOffsetLeft);
		}
	}catch(ex){	if(debug) alert(ex); }
}

FWindow.prototype.updateWindowPosition = function()
{
	try{
		// pixel relative to map world
		var p = this.map.fromLatLngToDivPixel(this.point);
		
		// map world relative to map container
		var dragObject = this.map.getPane(G_MAP_MAP_PANE).parentNode;
		var x = p.x + parseInt(dragObject.style.left);
		var y = p.y + parseInt(dragObject.style.top);
		
		// map container relative to the parent
		y += this.map.getContainer().offsetTop;
		x += this.map.getContainer().offsetLeft;
		
		// offset by the requested parent position
		//y += $(".wPod-1-col")[0].offsetTop;
		//x += $("#container")[0].offsetLeft;
		
		// offset by the specified offset position
		y -= this.offset.y;
		x -= this.offset.x;
		
		x -= 29; // TODO: g�r snyggare l�sning f�r att flytta fwindow till v�nster, /Stefan
		y += 33; // TODO: g�r snyggare l�sning f�r att flytta fwindow till v�nster, /Stefan
			
		// offset by the markerArrow's position on the container
		x -= this.markerArrowOffsetLeft + this.imageSizes[0].x/2;
		
		// Move the markerArrow's position
		
		// make it visible a while so we have offsetHeight and offsetWidth
		this.show();
		
		// Raise the container by the height of the container (we want bottom at the marker)
		y -= this.container.offsetHeight;
		
		// Apply those values 
		this.container.style.left = x+"px";
		this.container.style.top = y+"px";
	}catch(ex){	if(debug) alert(ex); }
}

FWindow.prototype.openOnMap = function(point, html, offset, afterPan, minWidth, maxWidth, markerArrowOffsetLeft)
{
	try{
		this.hidden = false;
		this.offset = offset || new GPoint(0,0);
		this.point = point;
		
		mapmanager.getPointData();
	}catch(ex){	if(debug) alert(ex); }
}
      
FWindow.prototype.show = function()
{
	try{
		this.hidden = false;
		$(this.container).show();
		this.marker.hide(); 
	}catch(ex){	if(debug) alert(ex); }
}

FWindow.prototype.hide = function()
{	
	try{
		if (this.displayMe == true)
		{
			this.displayMe = false;
			this.openOnMarker(this.marker, this.html, false, this.minWidth, this.maxWidth, this.useMarkerGetHTML, this.markerArrowOffsetLeft);
			
		}
		else
		{
			this.hidden = true;
			$(this.container).hide();
			if(this.marker) this.marker.show();
			
		}
	}catch(ex){	if(debug) alert(ex); }
}

FWindow.prototype.isHidden = function()
{
	return this.hidden;
}

FWindow.prototype.supportsHide = function()
{
	return true;
}

FWindow.prototype.toggleMarkerArrow = function()
{
	//$("#FWindowMarkerArrow").toggle();		
}
FWindow.prototype.showMarkerArrow = function()
{
	//$("#FWindowMarkerArrow").show();		
}
FWindow.prototype.hideMarkerArrow = function()
{
	//$("#FWindowMarkerArrow").hide();		
}

