// A Block is a simple overlay that shows a coloured 7x7 block on the map
//
// Kept very small and simple (no events or opacity) so that you can use lots of them
//
// Bill Chadwick 2006
//
// Free for any use
//
function BdccBlock(point, opt_color, tooltip,offset_,icon,iconready) {
  this.point_ = point;
  this.color_ = opt_color || "#888888";
  this.tooltip_ = tooltip;
  if (icon) {
  	this.icon_ 			= new Image();   // icon loading
  	this.icon_.src = icon;
  }
  if (iconready) {
  	this.iconready_ = new Image();
  	this.iconready_.src = iconready;
  }
  this.offset_ 	= offset_;
}

function initBdccBlock(CB) {
	logit("initBdccBlock");
	var cb = CB;
	
	BdccBlock.prototype = new GOverlay();
	
	// Creates the DIV representing this block.
	BdccBlock.prototype.initialize = function(map) {
		var this1 = this;
		var valOpacity = 8;  // between 0 and 10
	
	  var div = document.createElement("DIV");
   	//div.setAttribute('class','block'+this.id_+' '+'bdccBlock'); 
		//div.setAttribute('id','block'+this.id_); 
	  if (this.icon_) {
	  	logit("BdccBlock new ICON:"+this.icon_.src);
		  var iconimg = document.createElement("IMG");
		  //iconimg.src = "images/favicon.ico";
			iconimg.src = this.icon_.src;
		  div.appendChild(iconimg);
		  //$(div).css("background-image","url(images/favicon.ico)");
			// div.style.borderColor  = "#FFFFFF";
		  //div.style.border   = "1px solid #FFFFFF";
		  div.style.zIndex = 1e9;
		  // Icon should have been preloaded
		  if (iconimg.width < 1) {
	 	  	this.minx = 8;
		  	this.miny = 8;
		  } else {
	 	  	this.minx = parseInt(iconimg.width/2);
		  	this.miny = parseInt(iconimg.height/2);
		  }
	  	logit("BdccBlock this.minx/y="+this.minx+"/"+this.miny);
	  	this.iconimg = iconimg;

			if ($.browser.opera) {
				div.style.width = "16px";
				div.style.height = "16px";
			}
		} else {
			//logit("BdccBlock Normal block");
	    div.style.width = "7px";
	    div.style.height = "7px";
		  div.style.backgroundColor = this.color_;
	    div.style.border   = "1px solid #FFFFFF";
	  	div.style.opacity = valOpacity/10;
	  	div.style.filter = 'alpha(opacity=' + valOpacity*10 + ')';
 	  	this.minx = 0;
	  	this.miny = 0;
		}	  
	  div.style.position = "absolute";
	  div.style.overflow = "hidden";

	  if(this.tooltip_ != null){
	      //div.style.cursor = "help";
	      div.style.cursor = "pointer";
	      div.title = this.tooltip_;
	  }
	  
	  if (cb) {
			//
			// Add Click Event
			//
			logit("BdccBlock add click event");
		  GEvent.addDomListener(div, "click", function(event) {
		    this1.clicked_ = 1;
		    GEvent.trigger(me, "click");
		  });
		}
	  // Block is similar to a marker, so add to marker pane
	  map.getPane(G_MAP_MARKER_PANE).appendChild(div);
	
	  //save for later
	  this.map_ = map;
	  this.div_ = div;
	}
	BdccBlock.prototype.iconloading = function() {
		if (this.icon_ && this.iconready_) {
			this.iconimg.src = this.icon_.src;
		}
	}
	BdccBlock.prototype.iconready = function() {
		if (this.icon_ && this.iconready_) {
			this.iconimg.src = this.iconready_.src;
		}
	}
	// Remove the main DIV from the map pane
	BdccBlock.prototype.remove = function() {
	  this.div_.parentNode.removeChild(this.div_);
	}
	
	// Copy our data to a new Block
	BdccBlock.prototype.copy = function() {
	  return new BdccBlock(this.point_, this.color_, this.tooltip_,0);
	}
	
	// Redraw the block based on the current projection and zoom level
	BdccBlock.prototype.redraw = function(force) {
	  // We only need to redraw if the coordinate system has changed
	  //logit("redraw");
	  if (!force) return;
	
	  // Calculate the DIV coordinates of the centre of our block
	  var p = this.map_.fromLatLngToDivPixel(this.point_);
	 
	 // To avoid that 2 same positioned blocks are above each other, place them next to each other
	 if (this.offset_ > 1) {
	  	var addLeft = (this.offset_ - 1) * 4;	
	  } else {
	  	var addLeft = 0;
	  }
	  // Now position our DIV
	  if (this.icon_) {
		  //logitA("BdccBlock new position:",p.x,this.minx,p.y,this.miny);
		  this.div_.style.left = (p.x - this.minx) + "px";
		  this.div_.style.top = (p.y - this.miny) + "px";
		  logit("BdccBlock this.div_.style.left="+this.div_.style.left);
		  logit("BdccBlock this.div_.style.top="+this.div_.style.top);
	  } else {
		  this.div_.style.left = (p.x - 3 + addLeft) + "px";
		  this.div_.style.top = (p.y - 3) + "px";
	  }
	  
	}
	BdccBlock.prototype.highlightBorder = function() {
  	this.div_.style.border = "3px solid yellow";
	}
	BdccBlock.prototype.hide = function() {
		//this.showdisp_ = this.div_.style.display; // save display type
		//logit("BdccBlock showdisp="+this.showdisp_);
  	this.div_.style.display = "none";
	}
	BdccBlock.prototype.show = function() {
  	this.div_.style.display = "";
	}
	BdccBlock.prototype.bringToFront = function() {
	  //var z = GOverlay.getZIndex(this.latlng.lat());
	  this.div_.style.zIndex = 1e8;
	}
	BdccBlock.prototype.sendBack = function() {
	  var z = GOverlay.getZIndex(this.latlng.lat());
	  this.div_.style.zIndex = z;
	}
	BdccBlock.prototype.highlight = function(px,zi,color) {
		if (!color) color = "#0000FF"
		this.div_.style.zIndex = zi;
		this.div_.style.border   = px+"px solid "+color;
		return this.div_;
	}
	BdccBlock.prototype.unhighlight = function() {
	  //var z = GOverlay.getZIndex(this.latlng.lat());
		this.div_.style.zIndex = 10;
		this.div_.style.border   = "1px solid #FFFFFF";
	}
	BdccBlock.prototype.getDiv = function() {
		return this.div_;
	}
	BdccBlock.prototype.setLatLng = function(point,fadeSpeed,CB) {
		logit("BdccBlock setLatLng");
		my = this;
		$(my.div_).fadeOut(fadeSpeed,function() {
			logit("BdccBlock block faded out");
			my.point_ = point;
			my.redraw(true);
			$(this).fadeIn(fadeSpeed,function() {
				logit("BdccBlock block faded in");
				if (!my.onScreen()) {
					my.map_.panTo(my.point_);
				} 
				CB();
			});
		});
	}
	BdccBlock.prototype.onScreen = function() {
		var bounds = this.map_.getBounds();
		
		if (bounds.containsLatLng(this.point_)) {
			logit("BdccBlock Marker on screen:"+this.point_.lat());
			return true;
		} else {
			logit("BdccBlock Marker NOT on screen:"+this.point_.lat());
			return false;
		}
	}
}

