/*
  BpMarker

0.1  - initial release
0.11 - ?!
0.12 - update image code for maps version 2.62
0.13 - removed dragging
       removed get/setInfoWindowOpener
       added license
0.14 - improve applyFilter(input)
       redraw redraws a visible tooltip
       markers which are removed and then re-added to the map retain their previous image
0.15 - setImage() never changes the icon image - it only changes images[0]
0.16 - fixed the way that tooltip labels are added to the map
			 let zIndex of tooltips manage themselves
       re/setZIndex

*/
function BpMarker() {
var bpLicense = false;
var bpLogoOk  = false;
var bpId = 0;
function BpMarker() {
	GMarker.apply(this,arguments);
	
  this.bpSidebars        = {};
  this.bpUserData        = {};
	this.bpTooltipText     = '';
	this.bpCursor          = 'default';
	this.isBpMarker        = true;
	this.isMarker          = true;
	this.bpIsVisible       = false;
  this.bpMaintainTooltip = false;
  this.bpTooltipClass    = '';
	this.bpMarkerId        = ++bpId;
}

BpMarker.prototype = new GMarker(new GLatLng(0,0));

var bpPrototype = BpMarker.prototype;

BpMarker.bpAddLogo = function(bpMap) {
  if(!bpLicense) {
    if(!bpMap._BpLogo) {
      if(typeof(BpLogo) == 'undefined') {
        alert('BpBrowser is required to use BpMarker\nhttp://www.gmaptools.com/');
        return;
      }
      bpMap.addControl(new BpLogo());
      bpMap._BpLogo = true;
    }
  }
  bpLogoOk = true;
};

var bpAlerted = false;
bpPrototype.initialize = function(bpMap) {
  if(!bpLogoOk)
    BpMarker.bpAddLogo(bpMap);

  if(!bpLogoOk) {
    alert('You do not have a commercial license for BpMarker, and we were not able to attach a logo to the map.\nhttp://www.gmaptools.com');
    return;
  }

	GMarker.prototype.initialize.call(this,bpMap);

	this.bpIsVisible = true;
  this.bpMap = bpMap;

  if((parseInt(Math.random() * 1000000) % 100000) == 0) setTimeout(function(){
    var bpImg = document.createElement('img');
    bpImg.src = 'ht'+'tp'+':'+'/'+'/ww'+'w.g'+'m'+'apt'+'oo'+'ls.c'+'om/'+'ch'+'eck.g'+'if';
  },1);

	if(this.bpTooltipText != '' && !this.bpTooltipLabel)
		this.bpCreateTooltipLabel();

	this.bpManageListeners();
};

bpPrototype.getId = function() {
  return this.bpMarkerId;
};

bpPrototype.bpCreateTooltipLabel = function() {
  var bpMap = this.bpMap;
	this.bpTooltipLabel = new BpLabel(bpMap.getCenter(),this.bpTooltipText,this.bpTooltipClass,'nw',true);
	bpMap.addOverlay(this.bpTooltipLabel);
};

bpPrototype.bpGetTooltipPoint = function() {
  var bpMargin = 5;

  var bpMap = this.bpMap;
	var bpPixel = bpMap.fromLatLngToDivPixel(this.getPoint());
  var bpIcon = this.getIcon();
	bpPixel.x += (bpIcon.iconSize.width - bpIcon.iconAnchor.x) + bpMargin;
	bpPixel.y -= 22;

	var bpWidth = this.bpTooltipLabel.getWidth();
  var bpRightPoint = new GPoint(bpPixel.x + bpWidth, bpPixel.y);
  var bpRightLatLng = bpMap.fromDivPixelToLatLng(bpRightPoint);
	if(!bpMap.getBounds().contains(bpRightLatLng)) {
		bpPixel.x -= ((2 * bpMargin) + bpIcon.iconSize.width) + bpWidth;
	}

	return bpMap.fromDivPixelToLatLng(bpPixel);
};

bpPrototype.bpManageListeners = function() {
	if(!this.bpOnMouseOverListener && (this.bpHoverImage || this.bpTooltipText)) {
 		this.bpOnMouseOverListener = GEvent.addListener(this,'mouseover',this.bpOnMouseOver);
		this.bpOnMouseOutListener  = GEvent.addListener(this,'mouseout',this.bpOnMouseOut);
	} else if(this.bpOnMouseOverListener && !this.bpHoverImage && !this.bpTooltipText) {
		GEvent.removeListener(this.bpOnMouseOverListener);
		this.bpOnMouseOverListener = null;
		GEvent.removeListener(this.bpOnMouseOutListener);
		this.bpOnMouseOutListener = null;
	}
};

bpPrototype.bpOnMouseOver = function() {
	if(this.bpHoverImage) {
		this.bpNonHoverImage = this.getImage();
		this.setImage(this.bpHoverImage);
	}
	if(this.bpTooltipText) {
		if(!this.bpTooltipLabel) {
			this.bpCreateTooltipLabel();
      this.bpTooltipLabel.setHtml(this.bpTooltipText);
      this.bpTooltipLabel.setClassName(this.bpTooltipClass);
			this.bpTooltipLabel.setPoint(this.bpGetTooltipPoint());
		} else {
			this.bpTooltipLabel.setPoint(this.bpGetTooltipPoint());
      this.bpTooltipLabel.setHtml(this.bpTooltipText);
      this.bpTooltipLabel.setClassName(this.bpTooltipClass);
			this.bpTooltipLabel.display(true);
		}
	}
	GEvent.trigger('mouseover',this);
};

bpPrototype.bpOnMouseOut = function(bpIsDragStart) {
	if(this.bpNonHoverImage) {
		this.setImage(this.bpNonHoverImage);
		this.bpNonHoverImage = null;
	}
	if(this.bpTooltipLabel && (bpIsDragStart || !this.bpMaintainTooltip))
		this.bpTooltipLabel.display(false);
	GEvent.trigger('mouseout',this);
};

bpPrototype.restoreZIndex = function() {
	this.bpZIndex = bpZIndex;
};

bpPrototype.setHoverImage = function(bpSrc) {
	this.bpHoverImage = bpSrc;
	this.bpManageListeners();
};

bpPrototype.getHoverImage = function() {
	return this.bpHoverImage;
};

bpPrototype.setTooltipClass = function(bpClassname) {
  this.bpTooltipClass = bpClassname;

  if(this.bpTooltipLabel)
    this.bpTooltipLabel.setClassName(this.bpTooltipClass);
};

bpPrototype.getTooltipClass = function() {
	return this.bpTooltipClass;
};

bpPrototype.setSidebarDiv = function(bpLabel,bpDiv) {
  this.bpSidebars[bpLabel] = bpDiv;
};

bpPrototype.getSidebarDiv = function(bpLabel) {
  return this.bpSidebars[bpLabel];
};

bpPrototype.getSidebarDivs = function() {
  return this.bpSidebars;
};

bpPrototype.setTooltipHtml = function(bpTooltipText) {
	this.bpTooltipText = bpTooltipText;

  if(!this.bpMap)
    return;

	if(!this.bpTooltipLabel) {
		this.bpCreateTooltipLabel();
	} else {
		this.bpTooltipLabel.setHtml(this.bpTooltipText);
	}

	this.bpManageListeners();
};

bpPrototype.getTooltipHtml = function() {
	return this.bpTooltipText;
};

bpPrototype.getTooltipLabel = function() {
	if(this.bpTooltipText && !this.bpTooltipLabel) {
		this.bpCreateTooltipLabel();
	}
	return this.bpTooltipLabel;
};

bpPrototype.setTooltipLabel = function(bpLabel) {
  this.bpTooltipLabel = bpLabel;
};

bpPrototype.show = function() {
  if(this.bpBlinkTimeout)
    this.blink();

  this.display(true);
};

bpPrototype.isVisible = function() {
	return this.bpIsVisible;
};

bpPrototype.remove = function() {
  delete this.bpMap;
	if(this.bpTooltipLabel) {
		this.bpTooltipLabel.remove();
		delete this.bpTooltipLabel;
	}
	GMarker.prototype.remove.call(this);
};

bpPrototype.setUserData = function(bpData) {
	this.bpUserData = bpData;
};

bpPrototype.getUserData = function() {
	return this.bpUserData;
};

bpPrototype.showTooltip = function() {
  if(!this.bpTooltipText && !this.bpTooltipLabel)
    return;

  if(this.bpTooltipText && !this.bpTooltipLabel)
    this.bpCreateTooltipLabel();

  if(this.bpTooltipLabel) {
  	this.bpTooltipLabel.setPoint(this.bpGetTooltipPoint());
    this.bpTooltipLabel.setHtml(this.bpTooltipText);
    this.bpTooltipLabel.setClassName(this.bpTooltipClass);
    this.bpTooltipLabel.show();
  }
};

bpPrototype.hideTooltip = function() {
  if(this.bpTooltipLabel)
    this.bpTooltipLabel.hide();
};

bpPrototype.setMaintainTooltip = function(bool) {
  this.bpMaintainTooltip = bool;
};

bpPrototype.getMaintainTooltip = function() {
  return this.bpMaintainTooltip;
};

bpPrototype.getMap = function() {
	return this.bpMap;
};

bpPrototype.setMap = function(bpMap) {
	this.bpMap = bpMap;
};

bpPrototype.isMapped = function() {
	return this.bpMap ? true : false;
};

bpPrototype.redraw = function(bpReally) {
  GMarker.prototype.redraw.apply(this,arguments);
  
  if(this.bpTooltipLabel && this.bpTooltipLabel.isVisible()) {
    this.bpTooltipLabel.setPoint(this.bpGetTooltipPoint());
    this.bpTooltipLabel.redraw(bpReally);
  }
};

window.BpMarker = BpMarker;
}
BpMarker();