//This file will create a base instance of a google map for the div with an 
//id of 'map'...extra functionality such as adding markers is handled 
//through other, addon javascript files

var lodgingTypes = [];
lodgingTypes[0] = 292;
lodgingTypes[1] = 291;
lodgingTypes[2] = 290;
lodgingTypes[3] = 289;
lodgingTypes[4] = 288;
lodgingTypes[5] = 287;

var lodgingPrices = [];

var lodgingPricesOne = {};
lodgingPricesOne.max = 100;
lodgingPricesOne.min = 0;

lodgingPrices[0] = lodgingPricesOne;

var lodgingPricesTwo = {};
lodgingPricesTwo.max = 200;
lodgingPricesTwo.min = 101;

lodgingPrices[1] = lodgingPricesTwo;

var lodgingPricesThree = {};
lodgingPricesThree.max = 300;
lodgingPricesThree.min = 201;

lodgingPrices[2] = lodgingPricesThree;

var lodgingPricesFour = {};
lodgingPricesFour.max = 10000;
lodgingPricesFour.min = 301;

lodgingPrices[3] = lodgingPricesFour;

$.extend({
	maps:{
		unload:function(){
			google.maps.Unload();
		}
	}
});

$.maps.areas = [];
$.maps.cachedMarkers = [];
$.maps.categoriesOn = [];
$.maps.views = {};
$.maps.markerManager = {};

google.load('maps', '2');

var iMgrBorderPadding = 75;

function test(array){
	lat = array[0].marker.getLatLng().lat();	
	lng = array[0].marker.getLatLng().lng();
	prompt('', lat + ', ' + lng);
}

$(function(){
	if (google.maps.BrowserIsCompatible()){
		//,googleBarOptions:{showOnLoad:true,onMarkersSetCallback:test}
		var mapOptions = {mapTypes:[G_NORMAL_MAP,G_HYBRID_MAP,G_PHYSICAL_MAP]};
		$.maps.map = new google.maps.Map2(document.getElementById("map_tc"), mapOptions);
		
		$.maps.map.enableDoubleClickZoom();
		$.maps.map.enableContinuousZoom();
		$.maps.map.enableScrollWheelZoom();
		//$.maps.map.enableGoogleBar();
		
		$.maps.map.setCenter(new google.maps.LatLng(39.175, -107.73), 7);
		
		$.maps.map.addControl(new google.maps.MapTypeControl());
		$.maps.map.addControl(new google.maps.OverviewMapControl());
		$.maps.map.addControl(new google.maps.ScaleControl());
		$.maps.map.addControl(new google.maps.LargeMapControl());
		
		
		var mgrOptions = {borderPadding:75};
		$.maps.markerManager = new MarkerManager($.maps.map, mgrOptions);
		
		$.maps.listeners = {};
		
		$.maps.views.zoomTo = zoomTo;
		
		$.maps.views.current = new view();
		$.maps.views.last = new view();
		
		
		/////////////////////////////////------filters----///////////////////////
		//Setup the categories expand/contract control
		$('#sidepanelControl').click(function(){
			closeSidepanel();				   
		});	   
		
		//Setup the checkbox click handlers for filters
		$('.marker-category-toggle').click(function(){
			categoryId = $(this).val();
			
			if($(this).attr('checked')){
				addCategory(categoryId);
				
				closeSidepanel();
			}
			else{
				removeCategory(categoryId);
			}
		});
		
		//Setup the price filtering events
		
		$('.lodging-price-filter').click(function(){
			var button = $(this);
				
			if($(this).attr('checked')){
				$.each(lodgingTypes, function(i, value){
					//Loop thorugh all the markers
					if($.maps.cachedMarkers[value] && $.maps.categoriesOn[value]){
						$.each($.maps.cachedMarkers[value], function(i2, marker){
							//Determine if that price point is turned on
							priceLevel = findPriceCategory(marker.price);
							
							if(button.attr('rel') == priceLevel){
								$.maps.markerManager.addMarker(marker, 5);
								$.maps.markerManager.refresh();
							}
						});
					}
					
				});
			}
			else{		
				$.each(lodgingTypes, function(i, value){
					//Loop thorugh all the markers
					if($.maps.cachedMarkers[value]){
						$.each($.maps.cachedMarkers[value], function(i2, marker){
							//Determine if that price point is turned on
							priceLevel = findPriceCategory(marker.price);
							
							if(button.attr('rel') == priceLevel){
								$.maps.map.removeOverlay(marker);
							}
						});
					}
				});
			}
		});
		
		$('#searchMap').click(function(){
			openSidepanel();
			return false;
		});
		
		///////////////////////////////------areas-----/////////////////////
		loadAreas();
		
		//add the default categories to the map
		//addCategories(lodgingTypes);
		
		/*$.maps.listeners.mapZoomEndRefreshView = google.maps.Event.addListener($.maps.map, 'zoomend', function(){$.maps.views.last = $.maps.views.current;$.maps.views.current.refresh();});	*/			
		
		/////////////////////////////////-----points------/////////////////
		//$.maps.listeners.moveEndMarkerTimeout = google.maps.Event.addListener($.maps.map, 'moveend', markersHandleTimeout);
		//$.maps.listeners.zoomEndMarkerTimeout = google.maps.Event.addListener($.maps.map, 'zoomend', markersHandleTimeout);
	
		/*$.maps.listeners.mapMoveEndRefreshView = google.maps.Event.addListener($.maps.map, 'moveend', function(){$.maps.views.last = $.maps.views.current;$.maps.views.current.refresh();});
		$.maps.listeners.mapZoomStartClearOverlays = google.maps.Event.addListener($.maps.map, 'zoomstart', function(){$.maps.map.clearOverlays();});
		*/
	}
	
	else{
		$("#map").html('Your browser is not compatible with Google Maps. Please upgrade to your browser\'s latest release.');
	}
	
	window.onunload = $.maps.unload;
});

/**
 * Lookup the price category (array index) based on the passed in price
 */
function findPriceCategory(price){
	for(i in lodgingPrices){
		if(price > lodgingPrices[i].min && price < lodgingPrices[i].max){
			return i;
		}
	}
	
	//If got here, not found, so return 0 (i guess)
	return 0;
}

function isLodging(categoryId){
	var isLodging = false;
	
	$.each(lodgingTypes, function(i, value){
		if(value == categoryId){
			isLodging = true;
			return false;//break the loop
		}
	});
	
	return isLodging;
}

function view(){
	this.zoomTo = zoomTo;
	this.categories = [];
	this.pricesOn = [];
	this.areasVisible = true;
	
	for(i in lodgingPrices){
		this.pricesOn[i] = true;
	}
	
	this.refresh = function(){
		this.southWestLon = $.maps.map.getBounds().getSouthWest().lng();
		this.southWestLat = $.maps.map.getBounds().getSouthWest().lat();
		this.northEastLon = $.maps.map.getBounds().getNorthEast().lng();
		this.northEastLat = $.maps.map.getBounds().getNorthEast().lat();
		this.zoom = $.maps.map.getZoom();
		
		//Only allow area overlays under zoom level 12
		if(this.zoom > 9 && this.areasVisible == true){
			for(i in $.maps.areas){
				$.maps.map.removeOverlay($.maps.areas[i]);	
			}
			
			this.areasVisible = false;
		}
		else if(this.zoom < 10 && this.areasVisible == false){//redraw them
			for(i in $.maps.areas){
				$.maps.map.addOverlay($.maps.areas[i]);	
			}
			
			this.areasVisible = true;
		}
	}
	this.refresh();
}

function zoomTo(lat,lon,zoom){
	if(zoom < 1){
		zoom = 14;
	}
	
	//apparently this is set as a string in the area object...so just make sure it's a int for gmaps
	zoom = parseFloat(zoom);
	
	closeSidepanel();
	
	//$.maps.map.panTo(new google.maps.LatLng(lat,lon));
	//$.maps.map.setZoom(zoom);
	$.maps.map.setCenter(new google.maps.LatLng(lat,lon), zoom);
	
	return false;
	
}

////////////////////filters////////////////////
function addCategories(categories){
	for(i in categories){
		addCategory(categories[i]);	
	}
}

function addCategory(categoryId){
	if(categoryId == 'rivers'){
		loadRivers();
	}
	else{
		//Only need to load the category if it is not currently active...if there isn't a record in the categories array for this view
		if(!$.maps.views.current.categories[categoryId]){
			
			$.maps.views.current.categories[categoryId] = true;
			
			//Consult the cache, send a request if needed, otherwise just add them
			if(!$.maps.cachedMarkers[categoryId]){
				//Send the ajax request and add the points
				loadMarkers(categoryId);
			}
			else{//alert($.maps.cachedMarkers[categoryId].length);
				//Need to see if the category is lodging (and therefore should go through a price filter)
				if(isLodging(categoryId)){
					for(marker in $.maps.cachedMarkers[categoryId]){
						//Determine if that price point is turned on
						priceLevel = findPriceCategory($.maps.cachedMarkers[categoryId][marker].price);
						//alert(priceLevel);
						//alert($('#filter_lodging-price' + priceLevel).attr('checked'));
						if($('#filter_lodging-price' + priceLevel).attr('checked')){//alert('hi');
							$.maps.markerManager.addMarker($.maps.cachedMarkers[categoryId][marker], 5);
							$.maps.markerManager.refresh();
						}
					}
				}
				else{
					$.maps.markerManager.addMarkers($.maps.cachedMarkers[categoryId], $.maps.views.current.zoom - 1);
					$.maps.markerManager.refresh();
				}
			}
		}
		
		$.maps.categoriesOn[categoryId] = categoryId;
	}
	
	return true;
}

function removeCategory(categoryId){
	if(categoryId == 'rivers'){
		
	}
	else{
		//Remove the current categegory from the view
		$.maps.views.current.categories[categoryId] = false;
		
		//Remove from the marker manager
		for(i in $.maps.cachedMarkers[categoryId]){
			//$.maps.map.removeOverlay($.maps.cachedMarkers[categoryId][i]);	
			$.maps.markerManager.removeMarker($.maps.cachedMarkers[categoryId][i]);
		}
		
		//We have to remove the markerManager entirely to really clear these points, so do it
		/*$.maps.markerManager = new GMarkerManager($.maps.map, {borderPadding:75});
		
		for(i in $.maps.views.current.categories){
			if($.maps.views.current.categories[i]){//if true, re add the markers
				//$.maps.markerManager.addMarkers($.maps.cachedMarkers[i], $.maps.views.current.zoom - 1);
			}
		}
				
		$.maps.markerManager.refresh();*/
	}
	
	$.maps.categoriesOn[categoryId] = null;
}

function closeSidepanel(){
	$('#sidepanelWrapper').animate({width:'10px'}, 350);
	$('#sidepanel').hide();
	$('#sidepanelControl').removeClass('sidepanelClosed').addClass('sidepanelClosed');
	
	$('#sidepanelControl').unbind('click').click(function(){
		openSidepanel();				   
	});	
}

function openSidepanel(){
	$('#sidepanelWrapper').animate({width:'300px'}, 350, 'swing', function(){$('#sidepanel').show();});
	$('#sidepanelControl').removeClass('sidepanelClosed').addClass('sidepanelOpen');
	
	$('#sidepanelControl').unbind('click').click(function(){
		closeSidepanel();				   
	});	
}

/////////////////points//////////////////
var southWestPixelBound;//Track the map movement to see if markers need to be refreshed
var lastRefreshSouthWestPixelBound;
var bNeedsRefresh = true; //Set it as true at the beginning so that the points get loaded with the map load
var request;

function needsRefresh(){
	//This function checks if the map needs refreshed, based on change in bounding box and configured manager border size
	
	//bNeedsRefresh is can be determined by using only one bound
	//Check the x and y components
	//Uses the absolute value
	//alert('in refresh ' + southWestPixelBound.y);
	if((Math.abs(southWestPixelBound.x - lastRefreshSouthWestPixelBound.x) > iMgrBorderPadding) ||
		(Math.abs(southWestPixelBound.y - lastRefreshSouthWestPixelBound.y) > iMgrBorderPadding)){
		bNeedsRefresh = true;
	}
	
	return bNeedsRefresh;
}

//This function sends off the actual ajax requests, then processes the returned points
function loadMarkers(categoryId){
	//Load in the loading image
	$('#loadingMessage').show();
	
	//Send off the request with the selected options
	
	//Create a box iMgrBorderPadding larger than the current map
	//neRequest and swRequest are latlng objects corresponding to the (modified) bounding box
	//of the markers to request
	
	//Add on the border padding for the marker manager
	//var neXPixelWithPadding = northEastBound.lng(); + iMgrBorderPadding;
	//var neYPixelWithPadding = northEastBound.lat(); + iMgrBorderPadding;
	//var swXPixelWithPadding = southWestBound.lng(); + iMgrBorderPadding;
	//var swYPixelWithPadding = southWestBound.lat(); + iMgrBorderPadding;
	
	//Make new latlng points from the modified points
	//neRequest = $.maps.map.fromDivPixelToLatLng(new google.maps.Point(neXPixelWithPadding, neYPixelWithPadding));
	//swRequest = $.maps.map.fromDivPixelToLatLng(new google.maps.Point(swXPixelWithPadding, swYPixelWithPadding));
	
	
	
	
	request = $.post("/getLocations.php", {c: categoryId}, function(response){
			   
			$('#loadingMessage').hide();
			   //alert(response);
			   response = eval('(' + response + ')');
			   	if(response.success != 0){//Something WAS found, add them
					
					var latlng;
					var windowHtml;
					
					$.maps.cachedMarkers[categoryId] = [];
					
					for(i in response.points){
						latlng = new google.maps.LatLng(response.points[i].lat, response.points[i].lon);
						
						icon = new google.maps.Icon();
						icon.image;
						switch(categoryId){
							case '194':
								icon.image = 'atv-icon.gif';
								break;
							case '195':
								icon.image = 'camping-icon.gif';
								break;
							case '196':
							case '1112':
								icon.image = 'casino-icon.gif';
								break;
							case '197':
							case '1111':
								icon.image = 'climbing-icon.gif';
								break;
							case '198':
								icon.image = 'fishing-icon.gif';
								break;
							case '199':
								icon.image = '4wheel-icon.gif';
								break;
							case '200':
								icon.image = 'golf-icon.gif';
								break;
							case '201':
								icon.image = 'hiking-icon.gif';
								break;
							case '202':
								icon.image = 'horsebackriding-icon.gif';
								break;
							case '203':
								icon.image = 'hotsprings-icon.gif';
								break;
							case '204':
								icon.image = 'hunting-icon.gif';
								break;
							case '205':
								icon.image = 'downhillbike-icon.gif';
								break;
							case '206':
							case '1113':
								icon.image = 'museums-icon.gif';
								break;
							case '207':
								icon.image = 'nat-park-icon.gif';
								break;
							case '208':
								icon.image = 'rafting-icon.gif';
								break;
							case '281':
								icon.image = 'roadbike-icon.gif';
								break;
							case '282':
								icon.image = 'ski-icon.gif';
								break;
							case '283':
								icon.image = 'snowmobile-icon.gif';
								break;
							case '284':
								icon.image = 'snowshoe-icon.gif';
								break;
							case '285':
							case '1114':
								icon.image = 'theater-icon.gif';
								break;
							case '286':
							case '1115':
								icon.image = 'train-icon.gif';
								break;
							case '292':
							case '291':
							case '290':
							case '289':
							case '288':
							case '287':	
							case '1153':
								switch(response.points[i].priceCategory){
									case '2':
										icon.image = 'lodging-2-icon.gif';
										break;
									case '3':
										icon.image = 'lodging-3-icon.gif';
										break;
									case '4':
										icon.image = 'lodging-3-icon.gif';
										break;
									case '1':
									default:
										icon.image = 'lodging-1-icon.gif';
										break;
								}
								break;
							default:
								icon.image = 'blank-icon.gif';
						}
						
						icon.image = '/images/maps/icons/' + icon.image;
						
						//icon.shadow = '/images/maps/panoramioShadow.png';
						//icon.shadowSize = new google.maps.Size(67, 32);
						icon.iconSize = new google.maps.Size(50, 39);
						icon.iconAnchor = new google.maps.Point(25, 33);
						icon.infoWindowAnchor = new google.maps.Point(19, 0);
						
						markerOpts = {title:response.points[i].name,icon:icon};
						
						var marker = new google.maps.Marker(latlng, markerOpts);
						
						//Create the infoWindow
						/*windowHtml = '<h3><a href="' + response.points[i].link + '" />' + response.points[i].name + '</a></h3>';
						
						if(response.points[i].photo && response.points[i].photo.length > 0){
							windowHtml = windowHtml + '<div style="float:left;margin:5px;"><img src="/' + response.points[i].photo + '" /></div>';	
						}
						
						if(response.points[i].teaser && response.points[i].teaser.length > 0){
							windowHtml = windowHtml + '<p>' + response.points[i].teaser + '</p>';	
						}
						
						if(response.points[i].address && response.points[i].address.length > 0){
							windowHtml = windowHtml + '<p><strong>Address:</strong> ' + response.points[i].address + '</p>';	
						}
						
						if(response.points[i].phone && response.points[i].phone.length > 0){
							windowHtml = windowHtml + '<p><strong>Phone:</strong> ' + response.points[i].phone + '</p>';	
						}
						
						//Add on the price category if this is lodging
						if(response.points[i].type == 'lodging'){
							marker.priceCategory = response.points[i].priceCategory;	
						}
						
						windowHtml = windowHtml + '<p><a href="#" class="mapZoomTo" onclick="zoomTo(' + response.points[i].lat + ', ' + response.points[i].lon + ', 14);">Zoom To</a></p>';*/
						
						marker.bindInfoWindowHtml('<div style="max-width:400px;">' + response.points[i].html + '</div>');
						//marker.windowHtml = windowHtml;
						$.maps.cachedMarkers[categoryId][i] = marker;
						//google.maps.Event.addListener($.maps.cachedMarkers[categoryId][i], 'mouseover', function(){this.openInfoWindowHtml(this.windowHtml);});
						
					}
					
					//if this is lodging, we have to take into account the active prices before added points, so we need to loop through and add individually
					if(isLodging(categoryId)){//the category is one of the known lodging types...set at the top of this doc
						for(marker in $.maps.cachedMarkers[categoryId]){
							//Determine if that price point is turned on or not
							priceLevel = findPriceCategory($.maps.cachedMarkers[categoryId][marker].price);
							//alert(priceLevel);
							//alert($('#filter_lodging-price' + priceLevel).attr('checked'));
							if($('#filter_lodging-price' + priceLevel).attr('checked')){//alert('hi');
								$.maps.markerManager.addMarker($.maps.cachedMarkers[categoryId][marker], 5);
								$.maps.markerManager.refresh();
							}
						}
					}
					else{
						$.maps.markerManager.addMarkers($.maps.cachedMarkers[categoryId], 5);//$.maps.views.current.zoom);
						$.maps.markerManager.refresh();
					}
				}
			});
}

function loadRivers(){
		request = $.post("/maps/getLocations.php", {c: 'rivers',
		   ne:$.maps.views.current.northEastLat + ',' + $.maps.views.current.northEastLon,sw:$.maps.views.current.southWestLat + ',' + $.maps.views.current.southWestLon}, function(response){
			   //alert(response);
			   response = eval('(' + response + ')');
			   	if(response.success != 0){//Something WAS found, add them
					
					var river;
					
					//$.maps.cachedMarkers[categoryId] = [];
					
					for(i in response.rivers){		if(i>1){break;}		
						river = new google.maps.Polyline.fromEncoded({	
							color:'#0099CC',
							opacity:0.6,
							weight: 4,
							points:response.rivers[i].gPolyline,
							levels:response.rivers[i].gLevels,
							numLevels:4,
							zoomFactor:2
						});
						
						$.maps.map.addOverlay(river);				
					}
				}
			});
}

var loadTimeout;

function markersHandleTimeout(){
	//This function makes sure that quick, successive map moves don't all call getLocations.
	//This is so that when people are scrolling around the map, they don't get the markers, because
	//they probably won't be in any one place long enough to look at them
	clearTimeout(loadTimeout);
	loadTimeout = setTimeout('loadMarkers()', 600);
}

function refreshMap(){
	//This function first looks to see if the points need to be reloaded (if the map has moved a sufficient distance), and then if it does, it calls the appropriate functions to load the new info
	
	//Check to see if the map needs to be refreshed based on the bounding box
	if($.maps.markers.needsRefresh()){
		// alert('fired');
		//Reset the refresh flag
		bNeedsRefresh = false;
		
		//update the last refreshed bounds for use on the next map move
		lastRefreshSouthWestPixelBound = $.maps.map.fromLatLngToDivPixel($.maps.map.getBounds().getSouthWest());
		
		//Reload the relevant points
		$.maps.markers.load();
	}
}

////////////--------------------areas------------//////////////
function loadAreas(){
	$.post('/getAreas.php', {}, function(response){
		response = eval('(' + response + ')');
		
		if(response.success != 0){//Something WAS found, add them
			//var standardColor = '#FFC000';
			//var hoverColor = '#FFC000';
			var nid;
			//$.maps.cachedMarkers[categoryId] = [];
			
			$.each(response.areas, function(i, area){
				nid = area.nid;
				
				$.maps.areas[nid] = new google.maps.Polygon.fromEncoded({
					fill:true,
					opacity:0,
					outline:true,
					color:'#7aa5e2',
					polylines:[{
						points:area.gPolyline,
						levels:area.gLevels,
						numLevels:4,
						zoomFactor:2,
						weight:7,
						opacity:0.4,
						color:'#008900'
					}]
				});
				
				$.maps.areas[nid].nid = nid;
				$.maps.areas[nid].name = area.name;
				$.maps.areas[nid].lat = area.lat;
				$.maps.areas[nid].lon = area.lon;
				$.maps.areas[nid].zoom = area.zoom;
				$.maps.areas[nid].url = area.url;
				
				
				google.maps.Event.addListener($.maps.areas[nid], 'mouseover', function(latlng){
					this.setFillStyle({opacity:0.4});
					//this.tool = new tooltip(new google.maps.LatLng(this.lat, this.lon), this.name);
					//$.maps.map.openInfoWindowHtml(new google.maps.LatLng(this.lat, this.lon), '<h3><a href="/destinations/' + this.nid + '/' + this.name + '">' + this.name + '</a></h3>');
					//$.maps.map.addOverlay(this.tool);
				});
				
				google.maps.Event.addListener($.maps.areas[nid], 'mouseout', function(){
					this.setFillStyle({opacity:0});
					//$.maps.map.removeOverlay(this.tooltip);
					//this.tooltip = null;
				});
				
				google.maps.Event.addListener($.maps.areas[nid], 'click', function(){
					location.href = this.url;
				});
				
				//$.maps.areas[nid].enableEditing();
				
				$.maps.map.addOverlay($.maps.areas[nid]);
				/*
				//$.maps.areas[i].show();
				
				//google.maps.Event.addListener($.maps.areas[nid], 'lineupdated', function(){
					//alert(this.points);
				//});*/
			});
		}									  
	});
}

function activateArea(nid){
	var area = $.maps.areas[nid];
	
	//turn off all other areas
	for(i in $.maps.areas){
		$.maps.areas[i].setFillStyle({opacity:0});
	}
	zoomTo(area.lat, area.lon, area.zoom);
	area.setFillStyle({opacity:0.4});
	
	closeSidepanel();
	
	return false;
}

function tooltip(latlng, text){
	this.latlng_ = latlng;
	this.text_ = text;
}

//tooltip.prototype = new google.maps.Overlay();

tooltip.prototype.initialize = function(map){
	var div = document.createElement("div");
	div.appendChild(document.createTextNode(this.text_));
	div.className = 'tooltip';
	div.style.position = 'absolute';
	//div.style.visibility = 'hidden';
	var markerPos = this.map_.fromLatLngToDivPixel(this.latlng_, $.maps.map.views.current.zoom);
	var xPos = markerPos.x;
	var yPos = markerPos.y;
	this.div_.style.top = yPos + 'px';
	this.div_.style.left = xPos + 'px';
	map.getPane(G_MAP_FLOAT_PANE).appendChild(div);
	this.map_ = map;
	this.div_ = div;
}

tooltip.prototype.remove = function(){
	this.div_.parentNode.removeChild(this.div_);
}

tooltip.prototype.copy = function(){
	return new tooltip(this.marker_,this.text_);
}

tooltip.prototype.redraw = function(force){
	if (!force) return;
	var markerPos = this.map_.fromLatLngToDivPixel(this.latlng_, $.maps.map.views.current.zoom);
	//var iconAnchor = this.marker_.getIcon().iconAnchor;
	//var xPos = Math.round(markerPos.x - this.div_.clientWidth / 2);
	//var yPos = markerPos.y - iconAnchor.y - this.div_.clientHeight - this.padding_;
	var xPos = markerPos.x;
	var yPos = markerPos.y;
	this.div_.style.top = yPos + 'px';
	this.div_.style.left = xPos + 'px';
}

tooltip.prototype.show = function(){
	this.div_.style.visibility = 'visible';
}

tooltip.prototype.hide = function(){
	this.div_.style.visibility = 'hidden';
}

function addFavorite(link){
		 // paths will be of the form session-favorites/{action}/nid. We
		// need to call session-favorites/js/{action}/nid
		var sFurl = link.href.replace('?', '/js?');
		
		document.getElementById('favorite-wrapper').innerHTML = 'Saving...';
	
		// only proceed if it actually matched
		if (sFurl != link.href) {
			
		// Make the request
		$.ajax({
		  type: 'GET',
		  url: sFurl,
		  data: '',
		  dataType: 'json',
		  success: function (data) {
				$('#favorite-wrapper').html(data.content);
			return false;
		  },
		  error: function (xmlhttp) {
			alert('An HTTP error '+ xmlhttp.status +' occured.\n'+ link.href);
		  }
		});

     	 return false;
	  
	  }

	}
