/**
 * @version 2008-10-01 01:41
 */

function CoolMarker(latLng, options) {
	var hasOpts    = typeof options != 'undefined';
	this.latLng_   = latLng;
	this.infoHtml_ = options.infoHtml;
	this.color_    = (hasOpts && options.color)
	               ? options.color
	               : '#fff';
	if (hasOpts && options.title) {
		this.title_ = options.title;
	}
	this.image_ = (hasOpts && options.image) ? options.image : false;
	this.draggable_ = (hasOpts && options.draggable);
	this.draggableElement_ = false;
}

CoolMarker.prototype = new GOverlay();

CoolMarker.prototype.initialize = function(map) {
	var element = document.createElement('div');
	    element.className = 'marker';

	var color = document.createElement('div');
	    color.className = 'color';
	    color.style.background = this.color_;

	var mask = document.createElement('div');
	    mask.className = 'mask';
	
	switch (this.image_) {
		case 'existing':
			mask.className += ' existing';
		break;
		case 'nonexistent':
			mask.className += ' nonexistent';
		break;
		default:
			if (!this.image_) {
				mask.className += ' regular';
			}
			else {
				mask.style.backgroundImage = 'url(' + this.image_ + ')';
			}
		break;
	}
	   

	element.appendChild(color);
	element.appendChild(mask);

	element.style.position = 'absolute';
	if (this.title_) {
		element.title = this.title_;
	}
	if (this.draggable_) {
		this.draggableElement_ = new GDraggableObject(element);
		this.draggableElement_.moveTo(map.fromLatLngToDivPixel(this.latLng_));
	}
	map.getPane(G_MAP_MARKER_PANE).appendChild(element);
	this.element_ = element;
	this.size_    = 20;
	this.map_     = map;
	this.setColor(this.color_);
}

CoolMarker.prototype.remove = function() {
	this.element_.parentNode.removeChild(this.element_);
}

CoolMarker.prototype.copy = function() {
	return new CoolMarker(this.latLng_);
}

CoolMarker.prototype.redraw = function(force) {
	if (!force) return;

	var pixelLatLng = this.map_.fromLatLngToDivPixel(this.latLng_);
	this.element_.style.left = (pixelLatLng.x - this.size_/2) + 'px';
	this.element_.style.top  = (pixelLatLng.y - this.size_/2) + 'px';
}

CoolMarker.prototype.getElement = function() {
	return this.element_;
}

CoolMarker.prototype.getColorElement = function() {
	var elems = this.element_.getElementsByTagName('div');
	return elems[0];
}

CoolMarker.prototype.getLatLng = function() {
	return this.latLng_;
}

/* Можно применять только ПОСЛЕ добавления маркера на карту */
CoolMarker.prototype.setColor = function(color) {
    this.getColorElement().style.backgroundColor = color;
}

/* Можно применять только ПОСЛЕ добавления маркера на карту */
CoolMarker.prototype.setTitle = function(title) {
	this.element_.title = title;
}

CoolMarker.prototype.setClickHandler = function(fn) {
	$(this.element_).click(fn);
}

CoolMarker.prototype.openInfoWindowHtml = function(balloonHtml) {
	if (balloonHtml == null) {
		balloonHtml = this.infoHtml_;
	}
	this.map_.openInfoWindowHtml(this.getLatLng(), balloonHtml, {pixelOffset: new GSize(5, -12)})
}

CoolMarker.prototype.setInfoWindowClickHandler = function() {
	var elem = this;
	$(this.element_).click(function() {
		elem.openInfoWindowHtml(elem.infoHtml_);
	});
};
