﻿/*
* LabeledMarker Class
* Copyright 2007 Mike Purvis (http://uwmike.com)
* This class extends the Maps API's standard GMarker class with the ability
* to support markers with textual labels. Please see articles here:
*       http://googlemapsbook.com/2007/01/22/extending-gmarker/
*       http://googlemapsbook.com/2007/03/06/clickable-labeledmarker/
*/

function LabeledMarker(latlng, options){
    this.latlng = latlng;
    this.labelText = options.labelText || "";
    this.labelClass = options.labelClass || "markerLabel";
    this.labelOffset = options.labelOffset || new GSize(17, -15);
    this.clickable = options.clickable || true;
    this.mapItemData = options.mapItemData;
    
    if (options.draggable) { options.draggable = false; }    
    GMarker.apply(this, arguments);
    
    //  ======  The new marker "mouseover" and "mouseout" listeners  ======
    GEvent.addListener(this, "mouseover", function() {
        var map = GMapInit.map;
        var tooltip = LabeledMarker.tooltipDiv;
                
        tooltip.innerHTML = this.mapItemData[3];
        var point=map.getCurrentMapType().getProjection().fromLatLngToPixel(map.getBounds().getSouthWest(),map.getZoom());
        var offset=map.getCurrentMapType().getProjection().fromLatLngToPixel(this.getPoint(),map.getZoom());
        var anchor=this.getIcon().iconAnchor;
        var width=this.getIcon().iconSize.width;
        var pos = new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(offset.x - point.x - anchor.x + width,- offset.y + point.y + anchor.y )); 
        pos.apply(tooltip);
        tooltip.style.visibility="visible";
    });
    
    GEvent.addListener(this, "mouseout", function() {
        LabeledMarker.tooltipDiv.style.visibility="hidden";	    
    });
    
    GEvent.addListener(this, "click", function() {
        this.ShowInfoWin();
    });
}

//Static 변수, 메소드.
LabeledMarker.tooltipDiv = null;
LabeledMarker.infowinDiv = null;
LabeledMarker.initCtlStatic = function(map) //한번 호출 해 주어야 한다.
{
    LabeledMarker.tooltipDiv = document.createElement("div");    
    LabeledMarker.tooltipDiv.style.fontSize='14px';
    LabeledMarker.tooltipDiv.style.fontWeight='bold';
    LabeledMarker.tooltipDiv.style.color='#FFDD11';
    LabeledMarker.tooltipDiv.style.height='14px';
    LabeledMarker.tooltipDiv.style.filter='glow(color:#000000, strength:0)';
    LabeledMarker.tooltipDiv.style.backgroundColor = "#000000";
    
    map.getContainer().appendChild(LabeledMarker.tooltipDiv);    
    LabeledMarker.tooltipDiv.style.visibility="hidden";
    
    LabeledMarker.infowinDiv = MapItemInfo.GetOuterDiv();
	GMapInit.map.getContainer().appendChild(LabeledMarker.infowinDiv);
}

LabeledMarker.prototype = new GMarker(new GLatLng(0, 0));

LabeledMarker.HideInfoWin = function()
{
    MapItemInfo.HideForm();
}

LabeledMarker.prototype.ShowInfoWin = function()
{
    MapItemInfo.LoadData(this.mapItemData); //데이터 로드하고 화면에 보이기..↓위치 지정하기        
    var map = this.map;
    var icon = this.getIcon();
    var iw = icon.iconSize.width;
    var ih = icon.iconSize.height;
    var wh = LabeledMarker.infowinDiv.clientHeight;
    var ww = LabeledMarker.infowinDiv.clientWidth;
    var mh = map.getSize().height;
    var mw = map.getSize().width;

    var proj = map.getCurrentMapType().getProjection();
    var point=proj.fromLatLngToPixel(map.getBounds().getSouthWest(),map.getZoom());
    var offset=proj.fromLatLngToPixel(this.getPoint(), map.getZoom());
    
    var pixel = new GPoint(offset.x - point.x + iw, mh - (point.y - offset.y) );
    if(pixel.x + ww > mw) pixel.x -= (pixel.x + ww - mw);
    if(pixel.y + wh > mh) pixel.y -= (pixel.y + wh - mh);
    var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(pixel.x, pixel.y));        
    pos.apply( LabeledMarker.infowinDiv );
}

// Creates the text div that goes over the marker.
LabeledMarker.prototype.initialize = function(map) {
	// Do the GMarker constructor first.
	GMarker.prototype.initialize.apply(this, arguments);	
	
	var div = document.createElement("div");
	div.className = this.labelClass;
	
	var txt = this.labelText;
	if(txt!=null && txt.length > 4) txt = txt.substring(0,4);	
	div.innerHTML = txt;	
	div.style.position = "absolute";
		
	div.style.tableLayout = 'fixed';
	div.style.whiteSpace = 'nowrap';
	
	div.style.color = '#D1D1D1';//'#FFDD11';
	div.style.filter = 'glow(color:#000000, strength:2)';
	div.style.fontSize = "11px";
	
	map.getPane(G_MAP_MARKER_PANE).appendChild(div);

	if (this.clickable) {
		// Pass through events fired on the text div to the marker.
		var eventPassthrus = ['click', 'dblclick', 'mousedown', 'mouseup', 'mouseover', 'mouseout'];
		for(var i = 0; i < eventPassthrus.length; i++) {
			var name = eventPassthrus[i];
			GEvent.addDomListener(div, name, newEventPassthru(this, name));
		}

		// Mouseover behaviour for the cursor.
		div.style.cursor = "pointer";
	}
	
	this.map = map;
	this.div = div;
}

function newEventPassthru(obj, event) {
	return function() {
		GEvent.trigger(obj, event);
	};
}

// Redraw the rectangle based on the current projection and zoom level
LabeledMarker.prototype.redraw = function(force) {
	GMarker.prototype.redraw.apply(this, arguments);
	if (!force) return;	// We only need to do anything if the coordinate system has changed
	
	// Calculate the DIV coordinates of two opposite corners of our bounds to get the size and position of our rectangle
	var p = this.map.fromLatLngToDivPixel(this.latlng);
	var z = GOverlay.getZIndex(this.latlng.lat());
	
	// Now position our DIV based on the DIV coordinates of our bounds
	this.div.style.left = (p.x + this.labelOffset.width) + "px";
	this.div.style.top = (p.y + this.labelOffset.height) + "px";
	this.div.style.zIndex = z + 1; // in front of the marker
}

// Remove the main DIV from the map pane, destroy event handlers
LabeledMarker.prototype.remove = function() {
	GEvent.clearInstanceListeners(this.div);
	this.div.parentNode.removeChild(this.div);
	this.div = null;
	GMarker.prototype.remove.apply(this, arguments);
}

LabeledMarker.prototype.hide = function() {
    this.div.style.display = 'none';    
	GMarker.prototype.hide.apply(this, arguments);
}

LabeledMarker.prototype.show = function() {
    this.div.style.display = 'block';    
	GMarker.prototype.show.apply(this, arguments);
}

//////////////////////Side Bar Ctl////////////////////////////////
function GSideBar(div)
{
    if(typeof div == 'string')
        div = document.getElementById(div);
     this.outDiv = div; 
}

GSideBar.prototype = new GControl();

GSideBar.prototype.initialize = function(map) {    
    map.getContainer().appendChild(this.outDiv);    
    return this.outDiv;
}

GSideBar.prototype.getDefaultPosition = function() {
    return new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(7, 7));
}