
// class variables
OtviaLabel.zIndex = 1;

OtviaLabel.prototype = new GOverlay();

OtviaLabel.prototype.initialize = function(map) {
}

OtviaLabel.prototype.remove = function() {
}

OtviaLabel.prototype.copy = function() {
	return null;
}

OtviaLabel.prototype.getKml = function() {
}

OtviaLabel.prototype.redraw = function() {
	this.ourRedraw();
}
 
function OtviaLabel(content, shelterID, opts) {
    if ( typeof opts != "object" ) throw "Invalid OtviaLabel configuration";
    
    if (opts.pane) this.pane = opts.pane;
    if (opts.map) this.map = opts.map;

    if (!this.pane) throw "Invalid label configuration: no map pane";
    if (!this.map) throw "Invalid label configuration: no map";
    
    this.shelterID = shelterID;
    
    this.content = content;
    
    this.height = 0;
    
    if ( opts.offset && typeof opts.offset.width != "undefined" ) 
        this.offset = opts.offset
    else
        this.offset = new GSize(0,0);
    
    this.className = opts.className || "markerTooltip";
    if (opts.marker /* && typeof opts.marker.getLatLng != "undefined" */) {
        this.marker = opts.marker;
        this.anchor = opts.marker.getLatLng();
        this.isStatic = opts.isStatic;
                    
        GEvent.addListener(this.marker, 'mouseover', this.mHover(true));
        this.moEvent = GEvent.addListener(this.marker, 'mouseout', this.mHover(false));
        GEvent.addListener(this.marker, 'click', this.mClick());
//        GEvent.addListener(this.map, 'movestart', this.mVisibilityChanged());        
    }
    else if ( opts.anchor && typeof opts.anchor.lat != "undefined" ) {
        this.isStatic = opts.isStatic;
        this.anchor = opts.anchor
    }

    if (!this.anchor) throw "Invalid label configuration: no anchor point";
        
        this.container = document.createElement("div");
        this.container.className = this.className;
        this.container.style.position = "absolute";
        this.container.style.display = "none";
        
        this.mouseOverListener = GEvent.addDomListener(this.container, "mouseover", this.mouseOver());
        this.mouseOutListener = GEvent.addDomListener(this.container, "mouseout", this.mouseOut());
        
//        GEvent.addDomListener(this.container, "mousemove",  this.stopEvent("mousemove"));
        GEvent.addDomListener(this.container, "mouseup",  this.stopEvent("mouseup"));
        GEvent.addDomListener(this.container, "mousedown",  this.stopEvent("mousedown"));
        GEvent.addDomListener(this.container, "click",  this.stopEvent("click"));

		this.setContent(this.content);
        
        this.pane.appendChild(this.container);
        
        this.height = jQuery(this.container).height();
        
        this.map.addOverlay(this);
}

OtviaLabel.prototype.setContent = function(content) {
    var t = this;
        
    if (content) {
	    if (t.mcEvent != undefined && t.mcEvent != null)
	    {
	        google.maps.Event.removeListener(t.mcEvent);
	        t.mcEvent = null;
	    }
    
        var point = t.container;
        point.innerHTML = "";
        if ( typeof content == "object" ) {
            point.appendChild(content);
            t.content = point.innerHTML;
        } else {
            t.content = content;
            point.innerHTML = t.content;
        }
        
        // GLog.write("content: "+t.content.substr(0, 80));
        
        var closeIcon;
  		// GLog.write("add closeIcon handler");
        closeIcon = jQuery("div.labelCloseIcon", t.container).get(0); // Element.extend(t.container).select('div.labelCloseIcon')[0];
        // GLog.write("closeIcon: "+ closeIcon.tagName + " " + closeIcon.nodeName);
        t.mcEvent = GEvent.addDomListener(closeIcon, "click", t.mClose());
    }
    
    t.height = jQuery(t.container).height();
}

OtviaLabel.prototype.mHover = function(s) {
    var _t = this; var _s = s;
    return function() {
		// GLog.write("entered OtviaLabel.mHover: "+_s);

		//GLog.write("call setStatic ############## 4");
		if (_s) {
	        _t.container.style.zIndex = OtviaLabel.zIndex + "";
	        OtviaLabel.zIndex++;
        }
		
    	_t.setStatic(_s);
    }
}

OtviaLabel.prototype.mClick = function() {

    var _t = this;

    return function() {


    //GLog.write("entered OtviaLabel.mClick()");

        if (_t.isStatic) {
        	//GLog.write("isStatic is true");
            if (_t.moEvent != undefined)
            {
            	//GLog.write("moEvent removed");
                GEvent.removeListener(_t.moEvent);
                _t.moEvent = undefined;
            }
            _t.isClicked = true;
        }
	}
}

OtviaLabel.prototype.mouseOver = function() {
    var _t = this;
    
    return function(evt) {

  	var relTarget = evt.relatedTarget;
  	
  	//GLog.write("entered OtviaLabel.mouseOver()");
  	
  	if (evt.fromElement)
  		relTarget = evt.fromElement;
  		
  	if (!isAChildOf(_t.container, relTarget) && _t.container != relTarget) {

		// GLog.write("OtviaLabel.mouseOver() exec");
	
		//GLog.write("call setStatic ############## 5");
        _t.container.style.zIndex = OtviaLabel.zIndex + "";
        OtviaLabel.zIndex++;
	    _t.setStatic(true);
    }

    }
}

OtviaLabel.prototype.mouseOut = function() {
    var _t = this;
    
    return function(evt) {

  	var relTarget = evt.relatedTarget;

	//GLog.write("entered OtviaLabel.mouseOut()");
  	
  	if (evt.toElement)
  		relTarget = evt.toElement;
  		
  	if (!isAChildOf(_t.container, relTarget) && _t.container != relTarget) {
	    if((_t.isClicked == undefined || _t.isClicked == false) && _t.isStatic == true)
	    {
	        // GLog.write("OtviaLabel.mouseOut() exec");

			//GLog.write("call setStatic ############## 6");
	        _t.setStatic(false);	           
	    }
    }
    }
}

OtviaLabel.prototype.mClose = function() {

    var _t = this;
    return function() {

    //GLog.write("entered OtviaLabel.mClose()");

        if (_t.isStatic) {
        	//GLog.write("isStatic: true mClose()");
            if (_t.isClicked != null && _t.isClicked != undefined && _t.isClicked) {
                _t.moEvent = GEvent.addListener(_t.marker, 'mouseout', _t.mHover(false));
                //GLog.write("mouseout Added mClose()");
            }
        }
        _t.isClicked = false;
        //GLog.write("call setStatic ############## 7");
        _t.setStatic(false);

        if (_t.shelterID != null  && _t.shelterID != undefined && _t.shelterID != 0) {
        	jsShelterLabelController(_t.shelterID, false, true);
        	//GLog.write("jsShelterLabelController invoked mClose()");
        }
    }
}

OtviaLabel.prototype.setAnchor = function(anchor) {
    try {
        if ( anchor.lat() && anchor.lng() ) {
        	// GLog.write("************************* setAnchor()");
            this.anchor = anchor;
            this.ourRedraw();
        }
    } catch(E) {}
}

OtviaLabel.prototype.setOffset = function(offset) {
    try {
    	if (offset) {
    		// GLog.write("************************* setOffset()");
	        this.offset = offset;
	        this.ourRedraw();
        }
    } catch(E) {}
}

OtviaLabel.prototype.setStatic = function(s, doRedraw)
{
    var _t = this; 
    var _s = s;
    var prevState = _t.isStatic;
    
    // GLog.write("setStatic before: "+ _t.isStatic);
    
    if((_s != _t.isStatic) && _s == true) {
        _t.container.style.zIndex = OtviaLabel.zIndex + "";
        OtviaLabel.zIndex++;
    }

    _t.isStatic = _s;
    
    if ((doRedraw == undefined /*&& prevState != _s*/) || doRedraw)
    	_t.ourRedraw(); // default action is to redraw
    	
    // GLog.write("setStatic after: "+ _t.isStatic);
   
}

OtviaLabel.prototype.show = function(point, offset) {
    try {
        var t = this;

        var p = point  || t.anchor;
        var o = offset || t.offset || new GSize(0,0);
        if ( p && p.lat ) {
        
        	//GLog.write("call setStatic ############## 8");
        	t.setStatic(true, false);
        	
        	// if not clicked and no mouseout handler add it
        	if ((t.isClicked == undefined || !t.isClicked) && t.moEvent == undefined)
        		t.moEvent = GEvent.addListener(t.marker, 'mouseout', t.mHover(false));        		

            t.anchor = p; t.offset = o;
            var pix = t.map.fromLatLngToDivPixel(p);
            t.container.style.left = ( pix.x + o.width ) + "px";
            t.container.style.top  = ( pix.y + o.height ) + "px";
            t.container.style.display = "";
            
            if (t.scrollPosition) {
				var stopList=null;
				for (var n=0; n<t.container.childNodes.length; n++) {
					if (t.container.childNodes[n].tagName == "DIV" && t.container.childNodes[n].className == "LabelShelterGroupStopsList") {
						stopList = t.container.childNodes[n];
						break;
					}
				}

				if (stopList != null) {
					stopList.scrollTop = t.scrollPosition;            
            		//GLog.write("$$$$$$$$$$$$$$$ showed scroll position $$$$$$$$$$$$$$$$$$$$$");
            	}
            }

            // GLog.write("OtviaLabel.show() exec scrollPosition="+t.scrollPosition);
            
        }
    } catch(e) { }
}

OtviaLabel.prototype.hide = function() {
    try {
        var t = this;

		//GLog.write("call setStatic ############## 9");
		t.setStatic(false, false);
        t.isClicked = false;
        t.container.style.display = "none";
        t.container.style.zIndex = "0";
        
        // GLog.write("OtviaLabel.hide() exec");
    } catch(e) { }
}

OtviaLabel.prototype.ourRedraw = function() {	
    if ( this.isStatic ) {
        this.show();
    } else {
        /* Show dynamic tooltips only on events, not on redraws */
        this.hide();
    }
}

OtviaLabel.prototype.stickIt = function() {
	var t = this;
	
	//GLog.write("call setStatic ############## 10");
	t.setStatic(true, false);	
	t.mClick()();
	t.show();	
}

OtviaLabel.prototype.stopEvent = function(eventName) {
	var t = this;

	return function(eventObject) {
	
    if (eventObject && eventObject.stopPropagation) {
        eventObject.stopPropagation();
    }
//    if (eventObject.cancelBubble ) {
        eventObject.cancelBubble = true;
//    }
    
    if (eventObject && eventObject.preventDefault) {
        eventObject.preventDefault();
    }
//    if (eventObject) {
        eventObject.returnValue = false;
//    }
    
	// GEvent.trigger(t.container, eventName, eventObject);    
    
    }
}

OtviaLabel.prototype.setScrollPosition = function(pos) {
	var t=this;
	
	if (pos != undefined) {
		// set the input position
		t.scrollPosition = pos;
	} else {
	// save current position
	if (t.container && t.container.childNodes) {
	
		var stopList=null;
		for (var n=0; n<t.container.childNodes.length; n++) {
			if (t.container.childNodes[n].tagName == "DIV" && t.container.childNodes[n].className == "LabelShelterGroupStopsList") {
				stopList = t.container.childNodes[n];
				break;
			}
		}

		t.scrollPosition = stopList.scrollTop;
		//GLog.write("****************** set scroll position "+t.scrollPosition);
		
		/*
		GLog.write("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
		for (i in t.container)
			GLog.write(i+" = "+t.container[i]);
		GLog.write("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");

		GLog.write("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
		for (i in t.container.childNodes[1])
			GLog.write(i+" = "+t.container.childNodes[1][i]);
		GLog.write("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
		*/
	}
	}
}

OtviaLabel.prototype.clearScrollPosition = function() {
	var t=this;
	
	t.scrollPosition = undefined;
	//GLog.write("!!!!!!!!!!!!!! cleared scroll position !!!!!!!!!!!!!!!!!!!");
}

OtviaLabel.prototype.getScrollPosition = function() {
	//GLog.write("!!!!!!!!!!!!!! get scroll position !!!!!!!!!!!!!!!!!!!");
	return this.scrollPosition;
}

OtviaLabel.prototype.destroy = function() {
	this.map.removeOverlay(this);
	if (this.container != undefined && this.container != null)
		this.pane.removeChild(this.container);
	this.container = null;
} 

function isAChildOf(_parent, _child)
{
   if (_parent === _child) { return false; }
      while (_child && _child !== _parent)
   { _child = _child.parentNode; }

   return _child === _parent;
}    
