// Define the overlay, derived from google.maps.OverlayView
function Label(opt_options) {
// Initialization
 this.setValues(opt_options);

 intLoc.labelDiv = this.div_ = document.createElement('div');
 intLoc.labelDiv.id = "map-tooltip"+opt_options.labelId;
 intLoc.labelDiv.style.cssText = 'position: absolute; display: none; width: 165px; height: 47px; background: transparent url(/apps/locator/img/toolTip.png); overflow: hidden;';
 
 this.div_.innerHTML = '<div id="toolTipNumber">'+opt_options.labelId+'</div><div id="toolTipDealer">'+opt_options.labelName+'</div>';
 };
 
Label.prototype = new google.maps.OverlayView;

// Implement onAdd
Label.prototype.onAdd = function() {
	intLoc.markerInPixels.length > 2 ? intLoc.markerInPixels.length = 0 : ""; 
 var pane = this.getPanes().overlayLayer;
 pane.appendChild(this.div_);
 
 // Ensures the label is redrawn if the text or position is changed.
 //var me = this;
 //this.listeners_ = [
   //google.maps.event.addListener(this, 'position_changed',
       //function() { me.draw(); }),
   //google.maps.event.addListener(this, 'text_changed',
       //function() { me.draw(); })
 //];
 
};

// Implement onRemove
Label.prototype.onRemove = function() {
	for (var i = 0; i < intLoc.dealerData.dealers.length; i++) {
		jQuery(document).ready(function(){
			jQuery("#map-tooltip"+i).remove();
		});
	}	
/*intLoc.labelDiv.parentNode.removeChild(this.div_);
intLoc.labelDiv = null;*/
};

// Implement draw
Label.prototype.draw = function() {
	intLoc.markerInPixels.length > 2 ? intLoc.markerInPixels = [] : "";
	var projection = this.getProjection();
	var position = projection.fromLatLngToDivPixel(this.get('position'));
	
	//Position label
	var div = this.div_;
 		div.style.left = (position.x + 60) + 'px';
 		div.style.top = (position.y + -56) + 'px';
	
	//Positioning the map with infoWindow onClick of marker or dealer list
	var xPosition = position.x;
	var yPosition = position.y - 100;
	var pointLatLng = new google.maps.Point(xPosition,yPosition);
	
	var newMarkerPosition = projection.fromDivPixelToLatLng(pointLatLng);
	google.maps.event.addListener(intLoc.map, 'zoom_changed',
		function() { 
			intLoc.markerInPixels.length > 2 ? intLoc.markerInPixels = [] : ""; 
		});
	
	//Push new marker positions into array
	if(intLoc.markerInPixels.length < 3){
		intLoc.markerInPixels.push(newMarkerPosition);
	}
};


