/*global google, $, map, Poi, extend, show_route, JSON, hide_all_routes */
/*jslint browser: true, devel: true, laxbreak: true, forin: true, sub: true, onevar: true, undef: true, nomen: true, eqeqeq: true, bitwise: true, regexp: true, newcap: true, immed: true */

/**
 * A POI that has an LI and an edit button on info window
 * @param {Object} data from DB
 */
function PoiProximity(data) {
	PoiProximity.superclass.apply(this, arguments);
	this.show();
}
extend(PoiProximity, Poi);




PoiProximity.prototype.open_info_window = function () {
	var content, info_temp, cats, 
		me = this;
		/*proximity_button = $('<input type="button" class="button primary" value="Show routes near here">')
			.click(function() {
				me.show_nearby_routes();
				info_temp.close();
			});*/
	
	if (typeof global_info_window !== 'undefined') {
		global_info_window.close();
	}
	
	this.get_nearby_routes(function (routes) {
		content = '<div style="width:400px;height:160px">'
			+ '<h3>' + me.title + '</h3>'
			+ (me.address ? ('<p>' + me.address  + '</p>') : '')
			+ (routes.length ? ('Routes that serve this school: ' + me.route_ul(routes)) : 'Currently no routes')
			+ '</div>';
	
		info_temp = new google.maps.InfoWindow({
			content: $(content)/*.append(proximity_button)*/[0],
			position: me.marker.position
		});
		info_temp.open(map);
		global_info_window = info_temp;
	});
	
};

PoiProximity.prototype.route_ul = function(routes) {
	var str = '<ul style="overflow:auto;height:105px">';
	for (var i in routes) {
		// also close info window
		str += '<li><a href="../route.php?r=' + routes[i].id + '">Route ' + routes[i].title + '</a> - ' + routes[i].area + '</li>';
	}
	str += '</ul>';
	return str;
};

PoiProximity.prototype.show_nearby_routes = function () {
	hide_all_routes();
	this.get_nearby_routes(function(routes) {
		for (var i in routes) {
			show_route(routes[i]);
		}
	});
};


PoiProximity.prototype.get_nearby_routes = function(callback) {
	// TODO: get 1 list of cats in JS object
	// default distance is 50m
	$.get('../ajax/routes-near?type=1&lat=' + this.marker.position.lat() + '&lng=' + this.marker.position.lng(), function(data) {
		return callback(data);
	});
};


