/*global google, $, map, JSON */
/*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 */


function Poi(data) {
	this.cats = [];
	this.lat = 0;
	this.lng = 0;
	
	this.info_temp = null;

	for (var i in data) {
		this[i] = data[i];
	}

	this.icon = this.icon || '../images/bus/poi-small.png';
	
	this.title = 	this.title || 'untitled';
	this.visible = 	true;
	this.marker = 	this.create_marker();

	this.hide();
}

Poi.prototype.create_marker = function () {
	var me = this,
		marker =  new google.maps.Marker({
			map: map,
			title: this.title,
			draggable: false,
			flat: true,
			icon: new google.maps.MarkerImage(
				this.icon,
				null,
				null,
				new google.maps.Point(8, 8)
			),
			position: new google.maps.LatLng(this.lat, this.lng)
		});

	marker.poi = this;

	google.maps.event.addListener(marker, 'click', function (event) {
		me.do_maker_click();
	});

	return marker;
};



Poi.prototype.in_cat = function (cat_id) {
	for (var i in this.cats) {
		if (this.cats[i] == cat_id) {
			return true;
		}
	}
	return false;
};

Poi.prototype.has_any_cats = function (cats) {
	for (var i in cats) {
		if (this.in_cat(cats[i])) {
			return true;
		}
	}

	return false;
};

Poi.prototype.hide = function () {
	if (!this.visible) {
		return;
	}
	this.marker.setMap(null);
	this.visible = false;
};

Poi.prototype.show = function () {
	if (this.visible) {
		return;
	}
	this.marker.setMap(map);
	this.visible = true;
};

Poi.prototype.do_maker_click = function() {
	this.open_info_window();
};

Poi.prototype.get_category_names = function(callback) {
	// TODO: get 1 list of cats in JS object
	$.get('poi-cats-db.php?id=' + this.id, function(data) {
		return callback(JSON.parse(data));
	});
};

Poi.prototype.open_info_window = function () {
	var content, info_temp, cats, me = this;
	
	this.get_category_names(function (cats) {
		content = '<div style="width:200px;height:150px">'
			+ '<h3>' + me.title + '</h3>'
			+ 'Categories: '
			+ cats.join(', ')
			+ '</div>';

		info_temp = new google.maps.InfoWindow({
			content: content,
			position: me.marker.position
		});
		info_temp.open(map);
	});
};




