﻿
/*_____________________________________________________________________________
Class: CBHInfoWindow

*/

var infoWindowTimer = null;

var CBHInfoWindow = Class.create({
	initialize: function(id, marker, html, className)
	{
		this.id = "win" + id;
		this.marker = marker;
		this.html = html;
		this.offsetHeight = 15;
		this.offsetWidth = 50;
		this.panOffsetLat = -20;
		this.panOffsetLng = 0;
		this.mode = 'off';
		this.className = className;
		
	},
	updateHtml: function(html)
	{
		this.html = html;
		//this.innerDiv = this.windowEl.select('div.inside')[0].update(this.html);
	},
	close: function()
	{
		infoWindowTimer = Element.hide.delay(.4, $('overlay'));
	},
	pop: function()
	{
		clearTimeout(infoWindowTimer);
		this.mode = 'pop';
		//shows the window without panning
		this.openWindow();
	},
	openWindow: function()
	{
		if (!$('overlay')){ // need to create the overlay inside the map pane
			var new_obj = document.createElement("div");
			new_obj.style.display = 'none';
			new_obj.innerHTML = $('overlay_temp').innerHTML;
			new_obj.getElementsByTagName('div')[0].id = 'overlay_tab';
			new_obj.id = 'overlay';
			document.body.appendChild(new_obj);
		}
		
		
		
		var windowEl = $('overlay');
		
		windowEl.className = this.className;
		
		windowEl.select('div.inside')[0].update(this.html);
		
		windowEl.style.display = 'block';	
		
		this.mapPoint = this.marker.getLatLng();
		this.mapPointPX = mapRef.fromLatLngToDivPixel(this.mapPoint);
		
		this.vert_loc = parseInt(this.mapPointPX.y) - windowEl.offsetHeight - this.offsetHeight;
		windowEl.style.top = this.vert_loc  + "px";	
		
		this.horiz_loc = parseInt(this.mapPointPX.x) - this.offsetWidth;
		windowEl.style.left = this.horiz_loc + "px";
		
		
		mapRef.getPane(G_MAP_MARKER_PANE).appendChild(windowEl);
	},
	show: function()
	{
		clearTimeout(infoWindowTimer);
		this.mode = 'show';
		this.openWindow();	
		
		this.pan_to = new GLatLng( this.getLatOffSet(.4), this.getLngOffSet(.1))
		mapRef.panTo( this.pan_to );		
	},
	getLatOffSet: function(percent)
	{
		//return this.marker.getPoint().lat();
		var latOffset = ((mapRef.getBounds().getNorthEast().lat() - mapRef.getBounds().getSouthWest().lat()));
		latOffset = parseFloat(latOffset) * parseFloat(percent) + parseFloat(this.marker.getPoint().lat());
		return latOffset;
	},
	getLngOffSet: function(percent)
	{
		//return this.marker.getPoint().lng();
		var lngOffset = ((mapRef.getBounds().getNorthEast().lng() - mapRef.getBounds().getSouthWest().lng()));
		lngOffset = parseFloat(this.marker.getPoint().lng()) + parseFloat(lngOffset) * parseFloat(percent);		
		return lngOffset;
	},
	currentWin: null
});