currentInfoWin = false;
currentZoom    = false;

// Handles all data and functions for the home tutor search map, and side bar search results list.
var tmap = {

	indexForMarkerTutorId: function(id) {
		if (!sidebarTutors) { return false; }
		for (var i=0; i < sidebarTutors.length; i++) {
			if (sidebarTutors[i].id==id) { return i; }
		}
		return false;
	},
	
	detailsForMarkerId: function(id) {
		if (!sidebarTutors) { return false; }
		var theIndex = tmap.indexForMarkerTutorId(id);
		if (theIndex>-1) { return sidebarTutors[theIndex]; }
		return false;
	},
	
	popupTextForMarkerId: function(id) {
		var details = tmap.detailsForMarkerId(id);
		if (details) {
			var shortListLink = '';
			if (details.inSL) {
				shortListLink = '<a href="/shortLists"><img src="/img/button-in-sl.png" border="0" /></a>';
			} else {
				shortListLink = '<a href="/shortLists/add/' + details.id + '" onclick="sl.startAjaxAddToShortList(' + id + ');return false;" target="new"><img src="/img/button-add-to-sl.png" id="bubble-add-to-sl-' + details.id + '" border="0"></a>';
			}
			return '<table cellpadding="0"><tr><td valign="top"><img src="' + details.mediumImage + '" border="0" style="border:2px solid #444;width:100px;height:100px;margin-right:10px;" /></td><td valign="top"><span style="display:table;padding:0;margin:0;height:1em;overflow:auto;font:22px Helvetica;white-space:nowrap;">' + details.name +  '</span><table cellpadding=0 cellspacing=5 style="font:12px Helvetica"><tr><th align="left">Distance</th><td align=left style="white-space:nowrap;">' + details.distance + '</span></td></tr><tr><th align=left>' + details.subTitle + '</th><td align=left style="white-space:nowrap;">' + details.sub + '</td></tr></table><div style="margin-top:5px;white-space:nowrap;width:235px;"><a href="#" onClick="viewProfile(' + details.id + ')"><img src="/img/button-view-profile.png" border=0></a> ' + shortListLink + '</div></td></tr></table>';
		}
		return false;
	},
	
	setInfoAreaShortListState: function(id, inSL) {
		var slItems = $$('.tutor-' + id);
	},
	
	removeCurrentInfoWin: function() {
		if (currentInfoWin) {
			map.removeOverlay(currentInfoWin);
			currentInfoWin = false;
			if (selectedId) setMarkerSelectedForId(selectedId, false);
		}
	},
	
	openTutrmeMapBubbleForId: function(id) {
		var details = tmap.detailsForMarkerId(id);

		var newInfoWindow = new tmap.TutrmeMapBubble(details.marker, tmap.popupTextForMarkerId(id));
		currentInfoWin = newInfoWindow;
		map.addOverlay(newInfoWindow);
	},
	
	// Create the TutrmeMapBubble overlay object
	TutrmeMapBubble: function(marker,html) {
		tmap.removeCurrentInfoWin();
		this.html_ = html;
		this.width_ = 'auto';
		this.marker_ = marker;
	}
	
};

/////////////////////////// GLOBAL PROPERTIES AND FUNCTIONS FOR THE POPUP BUBBLE ///////////////////////////

// Use the GOverlay class
tmap.TutrmeMapBubble.prototype = new GOverlay();

// Initialize the container
tmap.TutrmeMapBubble.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;
};

tmap.TutrmeMapBubble.prototype.remove = function() { this.container_.remove(); };

tmap.TutrmeMapBubble.prototype.copy   = function() {
	tmap.removeCurrentInfoWin();
	return new tmap.TutrmeMapBubble(this.marker_,this.html_,this.width_);
};

tmap.TutrmeMapBubble.prototype.redraw = function(force) {
	if (!force) { return; }

	this.container_.innerHTML = '';

	var sizer = document.createElement("span");
	sizer.innerHTML = this.html_;
	sizer.style.font='10px verdana';
	sizer.style.margin='0';
	sizer.style.padding='0';
	sizer.style.border='0';
	sizer.style.display='table';
	sizer.style.width = 'auto';
	sizer.style.height = 'auto';
	sizer.style.visibility='hidden';

	this.map_.getContainer().appendChild(sizer);

	var contentWidth = sizer.offsetWidth;
	var contentHeight = sizer.offsetHeight;

	sizer.parentNode.removeChild(sizer);
	
	
	var cornerWidth = 19;
	
	//get the content div
	var content = document.createElement("div");
	content.style.font       = '10px verdana';
	content.style.margin     = '0';
	content.style.padding    = '0';
	content.style.display    = 'block';
	content.style.width      = (contentWidth + cornerWidth) + 'px';
	content.style.height     = (contentHeight + cornerWidth) + 'px';
	content.style.color      = 'black';
	content.style.position   = 'absolute';
	content.id               = 'bubble-content-div';
	
	var cells = [
		{   // Shadow
			type:   'img',
			src:    '/img/bubble-shadow.png',
			width:  '408',
			height: '128',
			border: '0',
			bottom: '-64',
			left:   '-20'
		},
		{   // Top Left Corner
			top:    '0',
			left:   '0',
			bg:     'url("/img/bubble-sprite.png") no-repeat left top',
			width:  '19',
			height: '19'
		},
		{   // Top Right Corner
			top:    '0',
			right:  '0',
			bg:     'url("/img/bubble-sprite.png") no-repeat -19px top',
			width:  '19',
			height: '19'
		},
		{   // Bottom Left Corner
			bottom: '0',
			left:   '0',
			bg:     'url("/img/bubble-sprite.png") no-repeat left -19px',
			width:  '19',
			height: '19'
		}, 
		{   // Bottom Right Corner
			bottom: '0',
			right:  '0',
			bg:     'url("/img/bubble-sprite.png") no-repeat -19px -19px',
			width:  '19',
			height: '19'
		}, 
		{   // Top Middle Outline
			top:    '0',
			left:   '19',
			right:  '19',
			bg:     'url("/img/bubble-sprite.png") repeat-x -19px -38px',
			height: '19'
		},
		{   // Bottom Middle Outline
			bottom:    '0',
			left:   '19',
			right:  '19',
			bg:     'url("/img/bubble-sprite.png") repeat-x -19px -481px',
			height: '19'
		},
		{   // Left Middle Outline
			left:   '0',
			top:    '19',
			bottom: '19',
			bg:     'url("/img/bubble-sprite.png") repeat-y 0px -50px',
			width:  '19'
		},
		{   // Right Middle Outline
			right:  '0',
			top:    '19',
			bottom: '19',
			bg:     'url("/img/bubble-sprite.png") repeat-y right -50px',
			width:  '19'
		},
		{   // Bubble Pointer
			left:   '100',
			bottom: '-61',
			bg:     'url("/img/bubble-sprite.png") no-repeat left bottom',
			width:  '66',
			height: '64'
		},
		{   // Middle HTML Container
			left:   (cornerWidth/2),
			top:    (cornerWidth/2),
			bottom: '19',
			right:  '19',
			cName:  'bubble-html-holder',
			bg:     'url("/img/bubble-sprite.png") repeat-y -10px -50px',
			html:   this.html_
		},
		{   // Close X
			type:   'img',
			src:    'http://maps.gstatic.com/intl/en_ALL/mapfiles/iw_close.gif',
			width:  '12',
			height: '12',
			right:  '10',
			top:    '10',
			cursor: 'pointer',
			click:  function() { tmap.removeCurrentInfoWin(); }
		}
	];
	
	// Use the cells array to create DOM elements within the bubble
	for (var i=0; i < cells.length; i++) {
		var cell = cells[i];
		if (!cell.type) cell.type = 'div';
		
		var e = document.createElement(cell.type);
		e.style.position                    = 'absolute';
		e.style.margin                      = '0';
		e.style.padding                     = '0';
		e.style.display                     = 'block';
		if (cell.src)    e.src              = cell.src;
		if (cell.width)  e.style.width      = cell.width   + 'px';
		if (cell.height) e.style.height     = cell.height  + 'px';
		if (cell.border) e.style.border     = cell.border;
		if (cell.top)    e.style.top        = cell.top     + 'px';
		if (cell.right)  e.style.right      = cell.right   + 'px';
		if (cell.bottom) e.style.bottom     = cell.bottom  + 'px';
		if (cell.left)   e.style.left       = cell.left    + 'px';
		if (cell.left)   e.style.left       = cell.left    + 'px';
		if (cell.bg)     e.style.background = cell.bg;
		if (cell.html)   e.innerHTML        = cell.html;
		if (cell.cName)  e.className        = cell.cName;
		if (cell.cursor) e.style.cursor     = cell.cursor;
		
		if (cell.click)  e.observe('click', cell.click);
		
		content.appendChild(e);
	};
	
	var pixelLocation = this.map_.fromLatLngToDivPixel(this.marker_.getPoint());
	var bubbleHeight = cornerWidth + contentHeight+ 64;
	var containerLocation = {
		x: pixelLocation.y-bubbleHeight-20,
		y: pixelLocation.x-100
	};
	
	content.style.left = containerLocation.y + "px";
	content.style.top  = containerLocation.x + "px";
	
	this.container_.appendChild(content);
	this.container_.style.display = "block";
};