in_package('mapsack.mapping.markers');

mapsack.mapping.markers.BasicMarker = function(latlng, options)
{
	this.latlng = latlng;
	this.options = options;
}

mapsack.mapping.markers.BasicMarker.prototype = new GOverlay();

mapsack.mapping.markers.BasicMarker.prototype.initialize = function(map)
{
	this.map = map;
	this.addElements('ms_marker_basic');	
}

mapsack.mapping.markers.BasicMarker.prototype.addElements = function(class_prefix)
{
	var map = this.map;
	var icon = this.options.icon || G_DEFAULT_ICON;

	var el = new Element('img');
	//el.style.position = 'absolute';
	el.className = class_prefix+'_shadow';
	map.getPane(G_MAP_MARKER_SHADOW_PANE).appendChild(el);
	this.sel = el;
	
	var el = new Element('img');
	el.className = class_prefix+'_main';
	//el.style.position = 'absolute';
	//el.style.cursor = 'pointer';	
	el.onclick = this.onclick.bind(this);
	el.onmouseover = this.onmouseover.bind(this);
	el.onmouseout = this.onmouseout.bind(this);
	map.getPane(G_MAP_MARKER_PANE).appendChild(el);
	this.el = el;
	
	this.setIcon(icon);
}

mapsack.mapping.markers.BasicMarker.prototype.setElImage = function(el, image)
{
	
	
	if (window.ie6)
	{
		el.src = '/images/misc/trans_16x16.gif';
		el.style.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + image + "',sizingMethod='scale')"
	} else {
		el.src = image;
	}
}

mapsack.mapping.markers.BasicMarker.prototype.setIcon = function(icon)
{
	this.options.icon = icon;
	
	var el = this.el;
	this.setElImage(el, icon.image);
	el.style.width = icon.iconSize.width+'px';
	el.style.height = icon.iconSize.height+'px';
	
	var el = this.sel;
	this.setElImage(el, icon.shadow);
	el.style.width = icon.shadowSize.width+'px';
	el.style.height = icon.shadowSize.height+'px';
	
}

mapsack.mapping.markers.BasicMarker.prototype.onclick = function()
{
	GEvent.trigger(this, 'click');
}
mapsack.mapping.markers.BasicMarker.prototype.onmouseover = function()
{
	GEvent.trigger(this, 'mouseover');
}
mapsack.mapping.markers.BasicMarker.prototype.onmouseout = function()
{
	GEvent.trigger(this, 'mouseout');
}

mapsack.mapping.markers.BasicMarker.prototype.copy = function()
{
	
}

mapsack.mapping.markers.BasicMarker.prototype.remove = function()
{
	this.el.parentNode.removeChild(this.el);
}

mapsack.mapping.markers.BasicMarker.prototype.redrawIcon = function()
{
	var icon = this.options.icon || G_DEFAULT_ICON;
	var xy = this.map.fromLatLngToDivPixel(this.latlng);
	this.el.style.left = (xy.x-icon.iconAnchor.x)+'px';
	this.el.style.top = (xy.y-icon.iconAnchor.y)+'px';
	this.sel.style.left = (xy.x-icon.iconAnchor.x)+'px';
	this.sel.style.top = (xy.y-icon.iconAnchor.y)+'px';


	/*var el = new Element('div');
	this.map.getPane(G_MAP_MARKER_SHADOW_PANE).appendChild(el);
	el.setStyles({
		position: 'absolute',
		top : xy.y,
		left : xy.x,
		border: '1px solid red'
	});*/

} 

mapsack.mapping.markers.BasicMarker.prototype.redraw = function(force)
{
	if (!force) return;
	
	this.redrawIcon();
}

mapsack.mapping.markers.BasicMarker.prototype.setTitle = function(title)
{
	this.options.title = title;
}

mapsack.mapping.markers.BasicMarker.prototype.getLatLng = function()
{
	return this.latlng;
}

mapsack.mapping.markers.BasicMarker.prototype.getPoint = mapsack.mapping.markers.BasicMarker.prototype.getLatLng;

mapsack.mapping.markers.BasicMarker.prototype.setLatLng = function(latlng)
{
	this.el.style.zIndex = GOverlay.getZIndex(latlng.lat());
	this.latlng = latlng;
	this.redraw(true);
}

mapsack.mapping.markers.BasicMarker.prototype.setPoint = mapsack.mapping.markers.BasicMarker.prototype.setLatLng;

mapsack.mapping.markers.BasicMarker.prototype.setImage = function(image)
{
	//this.el.style.background = 'url('+image+') center no-repeat';
	this.setElImage(this.el, image);
}

mapsack.mapping.markers.BasicMarker.prototype.hide = function()
{
	this.el.style.display = 'none';
	this.sel.style.display = 'none';
}

mapsack.mapping.markers.BasicMarker.prototype.show = function(map)
{
	this.el.style.display = '';
	this.sel.style.display = '';
}






/*
*
*/
mapsack.mapping.markers.LabelMarker = function(latlng, options)
{
	this.latlng = latlng;
	this.options = options;
}

mapsack.mapping.markers.LabelMarker.prototype = new mapsack.mapping.markers.BasicMarker(null, null);

mapsack.mapping.markers.LabelMarker.prototype.initialize = function(map)
{
	this.map = map;
	this.addElements('ms_marker_basic');	
	
	var el = new Element('div');
	el.className = class_prefix+'ms_marker_label_label';
	
	/*el.style.position = 'absolute';
	el.style.background = 'white';
	el.style.border = '1px solid #666';
	el.style.fontSize = 'x-small';
	el.style.padding = '1px';
	el.style.whiteSpace = 'nowrap';*/
	//el.style.width = '100px';
	el.style.display = 'none';
	/*el.style.zIndex = 100;*/
	el.innerHTML = this.options.title;
	el.onmouseout = this.onmouseout.bind(this);
	el.onclick = this.onclick.bind(this);
	
	this.lel = el;
	map.getPane(G_MAP_MARKER_PANE).appendChild(el);
}

mapsack.mapping.markers.LabelMarker.prototype.redraw = function(force)
{
	if (!force) return;
	
	this.redrawIcon();
	var icon = this.options.icon || G_DEFAULT_ICON;
	var xy = this.map.fromLatLngToDivPixel(this.latlng);
	this.lel.style.left = (xy.x-icon.iconAnchor.x+icon.iconSize.width/2)+'px';
	this.lel.style.top = (xy.y-icon.iconAnchor.y-5)+'px';
}

mapsack.mapping.markers.LabelMarker.prototype.onmouseover = function()
{	
	GEvent.trigger(this, 'mouseover');
}

mapsack.mapping.markers.LabelMarker.prototype.onmouseout = function(e)
{
	var ev = new Event(e);

	if ((ev.target == this.el) && (ev.relatedTarget == this.lel)) return;
	if ((ev.target == this.lel) && (ev.relatedTarget == this.el)) return;
	
	GEvent.trigger(this, 'mouseout');
}

mapsack.mapping.markers.LabelMarker.prototype.hilight = function()
{
	if (this.hidden) return;
	this.lel.addClass('hover');	
	//this.lel.style.display = '';
}

mapsack.mapping.markers.LabelMarker.prototype.dehilight = function()
{
	if (this.hidden) return;
	this.lel.removeClass('hover');
	//this.lel.style.display = 'none';
}
mapsack.mapping.markers.BasicMarker.prototype.hide = function()
{
	this.hidden = true;
	this.el.style.display = 'none';
	this.sel.style.display = 'none';
	this.lel.style.display = 'none';
}

mapsack.mapping.markers.BasicMarker.prototype.show = function(map)
{
	this.hidden = false;
	this.el.style.display = '';
	this.sel.style.display = '';
	this.lel.style.display = 'none';
}

mapsack.mapping.markers.LabelMarker.prototype.setTitle = function(title)
{
	this.options.title = title;
	this.lel.innerHTML = title;
}


/*
*
*/
mapsack.mapping.markers.DotMarker = function(latlng, options)
{
	this.latlng = latlng;
	this.options = options;
}

mapsack.mapping.markers.DotMarker.prototype = new mapsack.mapping.markers.LabelMarker(null, null);

mapsack.mapping.markers.DotMarker.prototype.initialize = function(map)
{
	this.map = map;
	this.addElements('ms_marker_basic');	
	
	var el = new Element('div');
	
	el.className = 'ms_marker_dot_label';
	el.innerHTML = this.options.title;
	this.lel = el;
	
	map.getPane(G_MAP_MARKER_PANE).appendChild(el);
}

mapsack.mapping.markers.DotMarker.prototype.redraw = function(force)
{
	if (!force) return;
	
	this.redrawIcon();
	var icon = this.options.icon || G_DEFAULT_ICON;
	var xy = this.map.fromLatLngToDivPixel(this.latlng);
	this.lel.style.left = (xy.x-icon.iconAnchor.x+icon.iconSize.width+2)+'px';
	this.lel.style.top = (xy.y-icon.iconAnchor.y)+'px';
}

mapsack.mapping.markers.DotMarker.prototype.hilight = function()
{
	if (this.hidden) return;
	this.lel.addClass('hover');
}

mapsack.mapping.markers.DotMarker.prototype.dehilight = function()
{
	if (this.hidden) return;
	this.lel.removeClass('hover');
}

mapsack.mapping.markers.DotMarker.prototype.show = function(map)
{
	this.hidden = false;
	this.el.style.display = '';
	this.sel.style.display = '';
	this.lel.style.display = '';
}











/*
*
*/
mapsack.mapping.markers.MultiMarker = function(latlng, options)
{
	this.latlng = latlng;
	this.options = options;
	this.mode = 0;
}

mapsack.mapping.markers.MultiMarker.prototype = new mapsack.mapping.markers.LabelMarker(null, null);

mapsack.mapping.markers.MultiMarker.prototype.initialize = function(map)
{
	this.map = map;
	this.addElements('ms_marker_basic');	
	
	var el = new Element('div');
	
	el.className = 'ms_marker_label_label';
	el.innerHTML = this.options.title;
	this.lel = el;
	el.onmouseout = this.onmouseout.bind(this);
	el.onclick = this.onclick.bind(this);
	
	map.getPane(G_MAP_MARKER_PANE).appendChild(el);
}

mapsack.mapping.markers.MultiMarker.prototype.setMode = function(mode)
{
	this.mode = mode;
	
	if (mode ==0)
	{
		this.lel.className = 'ms_marker_label_label';
		this.lel.onclick = this.onclick.bind(this);
		
	} else {
		this.lel.className = 'ms_marker_dot_label';
		this.lel.onclick = null;
	}
	
	this.redraw(true);
}

mapsack.mapping.markers.MultiMarker.prototype.redraw = function(force)
{
	if (!force) return;
	
	this.redrawIcon();

	if (this.mode == 0)
	{
		this.redrawLabel();
	} else {
		this.redrawDot();
	}
}

mapsack.mapping.markers.MultiMarker.prototype.redrawLabel = function(force)
{
	var icon = this.options.icon || G_DEFAULT_ICON;
	var xy = this.map.fromLatLngToDivPixel(this.latlng);
	this.lel.style.left = (xy.x-icon.iconAnchor.x+icon.iconSize.width/2)+'px';
	this.lel.style.top = (xy.y-icon.iconAnchor.y-5)+'px';
}

mapsack.mapping.markers.MultiMarker.prototype.redrawDot = function(force)
{
	var icon = this.options.icon || G_DEFAULT_ICON;
	var xy = this.map.fromLatLngToDivPixel(this.latlng);
	this.lel.style.left = (xy.x-icon.iconAnchor.x+icon.iconSize.width+2)+'px';
	this.lel.style.top = (xy.y-icon.iconAnchor.y)+'px';
	//this.lel.style.border = '1px solid red';
}

mapsack.mapping.markers.MultiMarker.prototype.hilight = function()
{
	if (this.hidden) return;
	this.lel.addClass('hover');
}

mapsack.mapping.markers.MultiMarker.prototype.dehilight = function()
{
	if (this.hidden) return;
	this.lel.removeClass('hover');
}

mapsack.mapping.markers.MultiMarker.prototype.show = function(map)
{
	this.hidden = false;
	this.el.style.display = '';
	this.sel.style.display = '';
	this.lel.style.display = '';
}