﻿/*
 * @param {GLatLng} latlng Point to place bar at
 * @param {Map} map The map on which to display this InfoBox.
 * @param {Object} opts Passes configuration options - content,
 *   offsetVertical, offsetHorizontal, className, height, width
 */
function InfoBox(opts) {
	google.maps.OverlayView.call(this);
	this.latlng_ = opts.latlng;
	this.map_ = opts.map;
	this.html_ = opts.html;
	this.closeButtonId_ = opts.close_button_id;
	this.icon_height = opts.icon_height;
	this.icon_width = opts.icon_width;
	//this.offsetVertical_ = -145;
	//this.offsetHorizontal_ = 5;
	this.offsetHorizontal_ = Math.floor(this.icon_width/2);
	//this.height_ = 130;
	this.width_ = 230;
	this.parent_ = opts.parent;
	
	var me = this;
	/*this.boundsChangedListener_ = google.maps.event.addListener(this.map_, "bounds_changed", function() {
		return me.panMap.apply(me);
	});*/

  // Once the properties of this OverlayView are initialized, set its map so that we can display it.  This will trigger calls to panes_changed and draw.
	this.setMap(this.map_);

}

/* InfoBox extends GOverlay class from the Google Maps API
 */
InfoBox.prototype = new google.maps.OverlayView();

/* Creates the DIV representing this InfoBox
 */
InfoBox.prototype.remove = function() {
	if (this.div_) {
		this.div_.parentNode.removeChild(this.div_);
		this.div_ = null;
		
		this.setMap(null);
		
	} else {
	
//alert("unexpected");
	
	}
};

/* Redraw the Bar based on the current projection and zoom level
 */
InfoBox.prototype.draw = function() {
  // Creates the element if it doesn't exist already.
	this.createElement();
	if (!this.div_) return;
	
	// Calculate the DIV coordinates of two opposite corners of our bounds to
	// get the size and position of our Bar
	var pixPosition = this.getProjection().fromLatLngToDivPixel(this.latlng_);
	if (!pixPosition) return;

	// Now position our DIV based on the DIV coordinates of our bounds
	this.div_.style.width = this.width_ + "px";
	this.div_.style.left = (pixPosition.x + this.offsetHorizontal_) + "px";
	/////this.div_.style.height = this.height_ + "px";
	
	//alert(jQuery(this.div_).height());
//alert((pixPosition.y + jQuery(this.div_).height() )+"px");
	//this.div_.style.top = (pixPosition.y + this.offsetVertical_) + "px";
	//this.div_.style.top = "0px";
	//this.div_.style.top = ( -(jQuery(this.div_).height() )/2)+"px";
	this.div_.style.top = (pixPosition.y - jQuery(this.div_).height())-this.icon_height+"px";
	//this.div_.style.display = 'block';
	jQuery(this.div_).fadeIn("fast");
};

/* Creates the DIV representing this InfoBox in the floatPane.  If the panes object, retrieved by calling getPanes, is null, remove the element from the DOM.  If the div exists, but its parent is not the floatPane, move the div to the new pane. Called from within draw.  Alternatively, this can be called specifically on a panes_changed event.
 */
InfoBox.prototype.createElement = function() {
	var panes = this.getPanes();
	var div = this.div_;
	if (!div) {
		//this does not handle changing panes.  You can set the map to be null and then reset the map to move the div.
		div = this.div_ = document.createElement("div");
		div.style.border = "0px none";
		div.style.position = "absolute";
		div.style.width = this.width_ + "px";
		//div.style.height = this.height_ + "px";
		var contentDiv = document.createElement("div");
		//contentDiv.style.padding = "0"
		contentDiv.innerHTML = this.html_;


		function removeInfoBox(ib) {
			return function() {
				ib.setMap(null);
			};
		}

		//div.appendChild(topDiv);
		div.appendChild(contentDiv);
		div.style.display = 'none';
		panes.floatPane.appendChild(div);
		
		//var button_close = document.getElementById(this.closeButtonId_);
		//google.maps.event.addDomListener(button_close, 'click', removeInfoBox(this));
		
		this.panMap();
		
		
	} else if (div.parentNode != panes.floatPane) {
		//the panes have changed.  Move the div.
		div.parentNode.removeChild(div);
		panes.floatPane.appendChild(div);
	} else {
		//the panes have not changed, so no need to create or move the div
	}
	
	this.parent_.infoboxAddedHandler();
	
}

// pan the map to fit the InfoBox
InfoBox.prototype.panMap = function() {
	

	
	// if we go beyond map, pan map
	var map = this.map_;
	var bounds = map.getBounds();
	if (!bounds) return;
	
	//the position of the infowindow
	var position = this.latlng_;
	
	//the dimension of the infowindow
	//var iwWidth = this.width_;
	//var iwHeight = this.height_;
	var iwWidth = jQuery(this.div_).width();
	var iwHeight = jQuery(this.div_).height();
	
	//the offset position of the infowindow
	var iwOffsetX = this.offsetHorizontal_;
	//var iwOffsetY = this.offsetVertical_;
	var iwOffsetY = -iwHeight;
	
	//padding on the infowindow
	var padX = 75;
	var padY = 75;
	
	//the degrees per pixel
	var mapDiv = map.getDiv();
	var mapWidth = mapDiv.offsetWidth;
	var mapHeight = mapDiv.offsetHeight;
	var boundsSpan = bounds.toSpan();
	var longSpan = boundsSpan.lng();
	var latSpan = boundsSpan.lat();
	var degPixelX = longSpan / mapWidth;
	var degPixelY = latSpan / mapHeight;

	//the bounds of the map
	var mapWestLng = bounds.getSouthWest().lng();
	var mapEastLng = bounds.getNorthEast().lng();
	var mapNorthLat = bounds.getNorthEast().lat();
	var mapSouthLat = bounds.getSouthWest().lat();

	//the bounds of the infowindow
	var iwWestLng = position.lng() + (iwOffsetX - padX) * degPixelX;
	var iwEastLng = position.lng() + (iwOffsetX + iwWidth + padX) * degPixelX;
	var iwNorthLat = position.lat() - (iwOffsetY - padY) * degPixelY;
	var iwSouthLat = position.lat() - (iwOffsetY + iwHeight + padY) * degPixelY;
	
	//calculate center shift
	var shiftLng =
		(iwWestLng < mapWestLng ? mapWestLng - iwWestLng : 0) +
		(iwEastLng > mapEastLng ? mapEastLng - iwEastLng : 0);
	var shiftLat =
		(iwNorthLat > mapNorthLat ? mapNorthLat - iwNorthLat : 0) +
		(iwSouthLat < mapSouthLat ? mapSouthLat - iwSouthLat : 0);
	
	//the center of the map
	var center = map.getCenter();
	
	//the new map center
	var centerX = center.lng() - shiftLng;
	var centerY = center.lat() - shiftLat;

	//center the map to the new shifted center
	map.setCenter(new google.maps.LatLng(centerY, centerX));
	
	//remove the listener after panning is complete.
	//google.maps.event.removeListener(this.boundsChangedListener_);
	//this.boundsChangedListener_ = null;
};



/*

Yale School of Medicine
Google Maps component
by Jon Krauss, jonkrauss@jonkrauss.com


_How to instantiate:

jQuery('map-element').gmap_locations(locations_xml_doc, parking_xml_doc, settings );


_Parameters

locations_xml_doc 		XML
parking_xml_doc		XML
settings				Object


_Settings you may want to change:

images_directory		String
display_menu			Boolean
visible_menu_items		Number
initial_latitude		Number
initial_longitude		Number
initial_zoom			Number
use_google_directions    Boolean


_Public methods

selectLocationByIndex( index:Number )


*/

(function(jQuery) {

	jQuery.fn.gmap_locations = function(locations_xml_doc, parking_xml_doc, options) {

	//default settings
	var defaults = {
		images_directory: 'images',
		display_menu: true,
		visible_menu_items: 6,
		initial_latitude: 41.3042314,
		initial_longitude: -72.9287230,
		initial_zoom: 8,
		locations_menu_id: 'map-sidebar',
		locations_menu_controls_id: 'location-controls',
		button_parking_id: 'button-toggle-parking',
		close_button_id: "button-close-infowindow",
		total_numbered_icons: 20,
		directions_link: null,
		use_google_directions: false
	};

	var _self = this;

	//extend default options
	var settings = jQuery.extend(defaults, options);

	//declare and set some variables
	var map;
	var infobox;
	
	var parking;
	var locations;

	var marker_icons = [];
	var all_markers = [];
	var icons = [];
	var location_markers = [];
	var parking_markers = [];

	var current_index = 0;
	var active_marker = 0;
	var active_menu_page = 0;

	var parking_loaded = false;
	var locations_loaded = false;

	var total_locations;
	var total_parking;

	var has_parking = false;
	var parking_visible ;
	
	if (parking_xml_doc){
		has_parking = true;	
		parking_visible = true;
	}

	init = function(obj){
		_self.all_markers = all_markers;
		_self.icons = icons;

		preloadIconImages();

		loadLocationsXML();
		
		if (has_parking){
			loadParkingXML();
		}
	};

	loadLocationsXML = function(){
		jQuery.ajax({
			type: "GET",
			url: locations_xml_doc,
			dataType: "xml",
			success: function(xml) {
				locations = xml;
				dataLoadedLocations();
			}
		});
	};

	loadParkingXML = function(){
		jQuery.ajax({
			type: "GET",
			url: parking_xml_doc,
			dataType: "xml",
			success: function(xml) {
				parking = xml;
				dataLoadedParking();
			}
		});
	};

	dataLoadedLocations = function(){

		locations_loaded = true;
		total_locations = jQuery(locations).find("location").size();

		if (has_parking){
			if (parking_loaded){
				allDataLoaded();
			}
		} else {
			allDataLoaded();
		}
	};
	
	dataLoadedParking = function(){

		parking_loaded = true;
		total_parking = jQuery(parking).find("location").size();	

		if (locations_loaded){
			allDataLoaded();	
		}
	};

	allDataLoaded = function(obj){
		var zoom_level;
		
		//create map center
		if (total_locations == 1){
			zoom_level = 16;
			var lat = jQuery(locations).find("latitude").text();
			var long = jQuery(locations).find("longitude").text();
			
			var lat_lng = new google.maps.LatLng( lat, long );
			
		} else {
			zoom_level = settings.initial_zoom;
			var lat_lng = new google.maps.LatLng( settings.initial_latitude, settings.initial_longitude );
		}
		
		//build map initial settings object
		var init_obj = {
			zoom: zoom_level,
			center: lat_lng,
			mapTypeId: google.maps.MapTypeId.ROADMAP,
			mapTypeControl: false,
			scrollwheel: false
		};
		
		//create map
		map = new google.maps.Map( document.getElementById("map-canvas"), init_obj );
		_self.googleMap = map;

		//build locations menu
		if (settings.display_menu){
			buildLocationsMenu();
		}

		//create parking
		if (has_parking){
			setupParking()
		}

		//create locations
		createLocationMarkers();

		if (typeof settings.success === 'function') {
			settings.success.call(settings.scope);
		}

	};
	
	setupParking = function(){

		createParkingMarkers();
		setupParkingButton();
	};
	
	setupParkingButton = function(){

		//show parking button
		jQuery("#"+settings.button_parking_id).show();
		
		//add parking button event
		jQuery("#"+settings.button_parking_id+" a").click(function(e){
			
			toggleParking();
			return false;
		});
		
	};
	
	infoboxAddedHandler = function( ){
	
		//add close button event
		jQuery("a#button-close-infowindow").click(function(e){
			e.preventDefault()
			infobox.remove();	
			return false;
			
		});
		
		if (settings.directions_link){
			//add show directions button event
			jQuery("a#button-directions").click(function(e){
				var xml_index = jQuery(this).attr("rel");
				var url = settings.directions_link +"?index="+ xml_index;
	
				document.location = url;
				
				return false;
			});
		}
		
		if (settings.use_google_directions){
			
			jQuery("a#button-directions").click(function(e){
				
				jQuery("#overlay-footer").hide();
				jQuery("#directions").show();
				
				jQuery("#input-directions").focus();
				
				return false;
			});
			
			//add submit directions button event
			jQuery("a#button-directions-submit").click(function(e){
				e.preventDefault();
				directionsSubmitHandler();
				return false;
			});
		}
	};
	
	directionsSubmitHandler = function(){
		
		//get start address value from form text input
		if (document.getElementById("input-directions").value.length > 0){
		
			var input_value = jQuery("#input-directions").val();
		} else {

			return false;
		}
		
		//convert spaces to pluses
		var start_address = jQuery("#input-directions").val().replace(/\s/g,"+");
		
		var end_location = all_markers[active_marker].address;
		end_location = end_location.replace(/\s/g,"+");
				
		//redirect to google page with directions
		document.location = "http://maps.google.com/maps?saddr="+start_address+"&daddr="+end_location;

		return false;
	};
	
	buildLocationsMenu = function(){
		
		//hide location menu
		jQuery("#"+settings.locations_menu_id).hide();
		
		//cycle through locations array
		for (var i = 0; i < total_locations; i++) {
		
			//add html for individual menu items
			var address = getAddressHtml(i,"locations");
			
			var str_menu_html =
				"<div class='item' rel='"+i+"'>"+
					"<div class='item-number'>"+(i+1)+"</div>"+
					"<h3>"+locations[i][2]+"</h3>"+
					"<p>"+address+"</p>"+
		
					"<div class='item-footer'>"+
						"<p><a>Show Hours/Phone</a></p>"+
					"</div>"+
				"</div>";
			
			//add item html to page
			jQuery("#"+settings.locations_menu_id).append(str_menu_html);
			
		}
		
		//add click events to menu items
		jQuery("#map-sidebar div.item").click(function(e, i){
			var index = jQuery(this).attr("rel");
			selectLocation(index);						   		
		});
		
		setupMenuControls();
		
		menuPaginate();
		
		//show menu
		jQuery("#"+settings.locations_menu_id).show();

	};
	
	preloadIconImages = function(){
		
		if (total_locations == 1){
			//preload icon images without a number
			marker_icons[0] = new Image();
			marker_icons[0].src = settings.images_directory+"/icon_pushpin.gif";
			
		} else {
			//preload icon images for numbered markers
			for (i = 0; i < settings.total_numbered_icons; i++){
				marker_icons[i] = new Image();
				marker_icons[i].src = settings.images_directory+"/icon_marker_"+i+".gif";
			}
		}
		
	};
	
	createLocationMarkers = function (){
		
		//create normal location markers
		jQuery(locations).find("location").each(function(i){
			
			var lat = parseFloat(jQuery(this).find("latitude").text());
			var long = parseFloat(jQuery(this).find("longitude").text());
			
			var point = new google.maps.LatLng(lat, long);
			var name = jQuery(this).find("buildingName").text();
			var xml_index = jQuery(this).find("index").text();
			
			var html = "<p>";
			html += jQuery(this).find("address").text() +"<br />";
			html += jQuery(this).find("city").text() +", "+ jQuery(this).find("state").text() +"<br />";
			if (jQuery(this).find("hours").text()){
				html += "<span class='date-time'>"+ jQuery(this).find("hours").text() +"</span><br />";
			}
			html += "</p><p>";
			if (jQuery(this).find("infoPhone").text()){
				html += "Office "+ jQuery(this).find("infoPhone").text() +"<br />";
			}
			if (jQuery(this).find("faxPhone").text()){
				html += "Fax "+ jQuery(this).find("faxPhone").text() +"<br />";
			}
			if (jQuery(this).find("appointmentPhone").text()){
				html += "Appts "+ jQuery(this).find("appointmentPhone").text() +"<br />";
			}
			html += "</p>";
			
			
			var str_icon_location;
			var icon_height;
			var icon_width;
			var icon_center;
			var zoom_level;
			if (total_locations == 1) {
				str_icon_location = settings.images_directory+"/icon_pushpin.gif";
				icon_height = 24;
				icon_width = 20;
				icon_center = 10;
			} else {
				str_icon_location = settings.images_directory+"/icon_marker_"+(i+1)+".gif";		
				icon_height = 35;
				icon_width = 28;
				icon_center = 14;
			}

			var location_icon = new google.maps.MarkerImage(
				str_icon_location,
				new google.maps.Size(icon_width, icon_height),
				new google.maps.Point(0, 0),
				new google.maps.Point(icon_center, icon_height)
			);
			icons[i + 1] = location_icon;

			var address = getAddress(xml_index, "locations");

			var category = "locations";

			var pbID = parseInt(jQuery(this).find("id").text(), 10);
			var city = jQuery(this).find("city").text();

			var marker = createMarker(point, lat, long, category, i, xml_index, name, html, address, location_icon, icon_height, icon_width, pbID, city);

			location_markers.push(marker);
		});
		
		if (total_locations == 1) {
			
			markerClickHandler(location_markers[0]);	
		}
		
	};
	
	createParkingMarkers = function (){
		
		//create parking markers
		var icon_height = 34;
		var icon_width = 27;
		var icon_center = 13;
		var parking_icon = new google.maps.MarkerImage(
			settings.images_directory+"/icon_parking.gif",
			new google.maps.Size(icon_width, icon_height),
			new google.maps.Point(0,0),
			new google.maps.Point(icon_center, icon_height)
		);

		jQuery(parking).find("location").each(function(i){

			var lat = parseFloat(jQuery(this).find("latitude").text());
			var long = parseFloat(jQuery(this).find("longitude").text());
			
			var point = new google.maps.LatLng(lat, long);
			var name = jQuery(this).find("buildingName").text();
			var xml_index = jQuery(this).find("index").text();
			
			var html = "<p>";
			html += jQuery(this).find("address").text() +"<br />";
			html += jQuery(this).find("city").text() +", "+ jQuery(this).find("state").text();
			html += "</p>";
			
			var address = getAddress(xml_index, "parking");
			
			var category = "parking";

			var marker = createMarker(point, lat, long, category, i, xml_index, name, html, address, parking_icon, icon_height, icon_width);

			parking_markers.push(marker);
		});
	};
	
	createMarker = function(point, lat, long, category, index, xml_index, name, html, address, icon, icon_height, icon_width, pbID, city) {
		
		//instantiate new google maps marker
		var marker = new google.maps.Marker({
			position: point,
			map: map,
			icon: icon,
			title: name,
			zIndex: index
		});

		//add properties to marker
		marker.lat = lat;
		marker.long = long;
		marker.category = category;
		marker.name = name;
		marker.index = index;
		marker.xml_index = xml_index;
		marker.html = html;
		marker.icon_height = icon_height;
		marker.icon_width = icon_width;
		marker.address = address;

		if (isNaN(pbID) === false) {
			marker.pbID = pbID;
		}

		if (typeof(city) === 'string') {
			marker.city = city;
		}

		//store marker in all_markers array
		all_markers.push(marker);

		//add event to marker
		google.maps.event.addListener(marker, 'click', function () {
			markerClickHandler(marker);
		});

		return marker;
	};
	
	getAddress = function(index, marker_type){
		//marker_type must be "locations" or "parking"

		//return string with address from locations or parking	
		if ( marker_type == "locations"){
			tmp_xml = locations;
		} else {
			tmp_xml = parking;
		}
		
		//build string with address data
		//var address = tmp_array[index][3];
		var address;
		jQuery(tmp_xml).find("location").each(function(){
			
			if (jQuery(this).find("index").text() == index){
				address = jQuery(this).find("address").text() +" "+ jQuery(this).find("city").text() +", "+ jQuery(this).find("state").text();
			}
		});
		
		
		return address;
	};
	
	getAddressHtml = function(index, marker_type){
		//marker_type must be "locations" or "parking"
	
		//return formatted html string with address from locations or parking
		if ( marker_type == "locations"){
			tmp_xml = locations;
		} else {
			tmp_xml = parking;
		}
		
		//build string with address data
		var html;
		jQuery(tmp_xml).find("location").each(function(){
			
			if (jQuery(this).find("index").text() == index){
				html = "<p>";
				html += jQuery(this).find("address").text() +"<br />";
				html += jQuery(this).find("city").text() +", "+ jQuery(this).find("state").text();
				html += "</p>";
			}
		});

		return html;
	};
	
	getInfoboxHtml = function(name, html, xml_index){

		//build infobox html
		var infobox_html =
			'<div id="infobox">'+
				'<a id="button-close-infowindow">Close</a>'+
				'<div id="overlay-main" class="clearfix">'+
					'<div id="textarea">'+
						'<h3>'+ name +'</h3>'+
						html +
					'</div>'+
				'</div>';
				
		if (settings.directions_link){
			infobox_html += '<div id="overlay-footer">'+
					'<a href="#" id="button-directions" rel="'+xml_index+'">Get Directions</a>'+
				'</div>';
		}
		if (settings.use_google_directions){
			infobox_html += '<div id="overlay-footer">'+
					'<a href="#" id="button-directions" rel="'+xml_index+'">Get Directions</a>'+
				'</div>'+
				'<div id="directions">'+
					'<label for="input-directions">Start Address</label>'+
					'<input type="text" name="directions" id="input-directions" />'+
					'<a href="#" id="button-directions-submit">Submit</a>'+
				'</div>';
		}
		
		infobox_html += '</div>';
			
		return infobox_html;
	};
	
	markerClickHandler = function(marker){

		active_marker = marker.index;
		
		var index = active_marker;
		var category = marker.category;
		var icon_height = marker.icon_height;
		var icon_width = marker.icon_width;
		var xml_index = marker.xml_index;
		var lat = marker.lat;
		var long = marker.long;
		var title = marker.name;
		var html = marker.html;

		//if infobox exists, remove it
		if (document.getElementById("infobox")){
			infobox.remove();
		}

		//get infobox html for marker
		var infobox_html = getInfoboxHtml(title, html, xml_index);
		
		//add infobox
		_self.infobox = infobox = new InfoBox({
				latlng: marker.getPosition(),
				map: map,
				content: html,
				className: "infobox",
				html:infobox_html,
				close_button_id:settings.close_button_id,
				icon_height:icon_height,
				icon_width:icon_width,
				parent:this
			});
		
		var new_center = new google.maps.LatLng( lat, long );

		//////map.panTo(new_center);
		
		return false;
	};
	
	selectLocation = function(index){
		
		//event handler for locations menu items, simulates marker click
		
		var marker = all_markers[index];
		
		markerClickHandler(marker);
	};
	
	toggleParking = function(){
		
		//show/hide parking icons, change button text

		for (var i = 0; i < total_parking; i++) {
			
			var tmp_marker = parking_markers[i];
			
			if (!parking_visible){
			
				if (tmp_marker.category == "parking") {
					tmp_marker.setVisible(true);
				}
			} else {

				if (tmp_marker.category == "parking") {
					tmp_marker.setVisible(false);
				}
			}
		}
		
		if (!parking_visible){
			
			parking_visible = true;
			jQuery("#"+settings.button_parking_id+" span").html("Hide Parking");
				
		} else {
			
			parking_visible = false;
			jQuery("#"+settings.button_parking_id+" span").html("Show Parking");
			
		}
		
		return false;
	};

	setupMenuControls = function(){
	
		//add previous event
		jQuery("#"+settings.locations_menu_controls_id+" a#control-previous").click(function(e){
			menuPrevious();
			return false;
		});
	
		//add next event
		jQuery("#"+settings.locations_menu_controls_id+" a#control-next").click(function(e){
			menuNext();
			return false;
		});
	};
	
	menuNext = function(){
		
		active_menu_page++;
	
		if (active_menu_page >= total_menu_pages){
			active_menu_page--;
		}

		menuPaginate();
		
		return false;
	};
	
	menuPrevious = function(){
		
		active_menu_page--;
		
		if (active_menu_page < 0){
			active_menu_page++;
		}
		
		menuPaginate();
		
		return false;
	};
	
	menuPaginate = function(){
	
		lower_bound = active_menu_page * settings.visible_menu_items;
		upper_bound = lower_bound + settings.visible_menu_items;

		if (upper_bound > total_locations){
			upper_bound = total_locations;	
		}

		str_text = (lower_bound+1)+"-"+(upper_bound)+" of "+(total_locations)+" Locations";
		
		jQuery("#"+settings.locations_menu_controls_id+" span").html(str_text);
		
		//show/hide active items in menu, adjust controls arrows and text

		jQuery("#"+settings.locations_menu_id+" div.item").each(function(i){

			if (i >= lower_bound && i < upper_bound){
				jQuery(this).show();
			} else {
				jQuery(this).hide();
			}
		});
		
		//activate/deactivate controls if on first or last page
		if (lower_bound == 0){
			
			jQuery("#"+settings.locations_menu_controls_id+" a#control-previous").removeClass().addClass('off');
			
		} else if (upper_bound == total_locations){
			
			jQuery("#"+settings.locations_menu_controls_id+" a#control-next").removeClass().addClass('off');
			
		} else {

			jQuery("#"+settings.locations_menu_controls_id+" a#control-previous").removeClass();
			jQuery("#"+settings.locations_menu_controls_id+" a#control-next").removeClass();
		}

		return false;
	};
	
	this.selectLocationByIndex = function(index){
		markerClickHandler(location_markers[index]);
	};

	this.selectLocationByID = function (id) {

		for (var x = 0; x < all_markers.length; x++) {
			if (location_markers[x].pbID === id) {
				markerClickHandler(all_markers[x]);
				return all_markers[x];
			}
		}

		return null;
	};

	init();
	return this;
};
})(jQuery);

