﻿/* A Bar is a simple overlay that outlines a lat/lng bounds on the
* map. It has a border of the given weight and color and can optionally
* have a semi-transparent background color.
* @param latlng {GLatLng} Point to place bar at.
* @param opts {Object Literal} Passes configuration options - 
*   weight, color, height, width, text, and offset.
*/
function OverlayArea(latlng, opts) {
	this.latlng = latlng;
	
	this.roundtripsCount_ = opts.c;
	this.id_ = opts.i;
	this.areaName_ = opts.n;
	this.href_ = opts.h;
	this.flag_ = opts.f;
}

OverlayArea.prototype = new GOverlay();

OverlayArea.prototype.initialize = function(map) {
	var me = this;

	var root = document.createElement('div');
	root.className = 'root';

	// Create the DIV representing our OverlayArea
	var leftDiv = document.createElement("div");
	root.appendChild(leftDiv);
	leftDiv.className = 'regionLabel';


	var span = document.createElement('span');
	leftDiv.appendChild(span);
	
	var img = document.createElement('img');
	span.appendChild(img);
	img.src = this.flag_;
	
	var ank = document.createElement('a');
	span.appendChild(ank);
	ank.href = this.href_;
	ank.title = this.areaName_;
	ank.innerHTML = this.areaName_;

	span.innerHTML += this.roundtripsCount_;

	GEvent.addDomListener(root, "click", function(event) {
		GEvent.trigger(me, "click");
	});

	map.getPane(G_MAP_MARKER_PANE).appendChild(root);

	this.map_ = map;
	this.div_ = root;
};

/* Remove the main DIV from the map pane
*/
OverlayArea.prototype.remove = function() {
	this.div_.parentNode.removeChild(this.div_);
};

/* Copy our data to a new OverlayArea
* @return {OverlayArea} Copy of bar
*/
OverlayArea.prototype.copy = function() {
	var opts = {};
	opts.color = this.color_;
	opts.image = this.image_;
	opts.imageOver = this.image_;
	return new OverlayArea(this.latlng, opts);
};

/* Redraw the OverlayArea based on the current projection and zoom level
* @param force {boolean} Helps decide whether to redraw overlay
*/
OverlayArea.prototype.redraw = function(force) {

	// We only need to redraw if the coordinate system has changed
	if (!force) return;

	// Calculate the DIV coordinates of two opposite corners 
	// of our bounds to get the size and position of our OverlayArea
	var divPixel = this.map_.fromLatLngToDivPixel(this.latlng);

	// Now position our DIV based on the DIV coordinates of our bounds
	this.div_.style.left = (divPixel.x - this.div_.offsetWidth / 2) + "px"
	this.div_.style.top = (divPixel.y) + "px";
};

OverlayArea.prototype.getZIndex = function(m) {
	return GOverlay.getZIndex(marker.getPoint().lat()) - m.clicked * 10000;
};

OverlayArea.prototype.getPoint = function() {
	return this.latlng;
};

OverlayArea.prototype.setStyle = function(style) {
	for (s in style) {
		this.div_.style[s] = style[s];
	}
};

OverlayArea.prototype.setImage = function(image) {
	this.div_.style.background = 'url("' + image + '")';
};

