//--------------------------------------------------
// Rectagular Boundary object (for Areas)
// bounds specified in lat/lng for N, S, W, E

function BoundPane(mn, ms, mw, me) {
	this.n_ = mn;
	this.s_ = ms;
	this.w_ = mw;
	this.e_ = me;
	this.weight_ = 2;
	this.color_ = "#ff2020";
	this.side_ = null;	// side being moved
	this.offset_ = 6;
	// this.backgroundColor_ = "#ff2020";
	// this.opacity_ = 0.001;
}
BoundPane.prototype = new GOverlay();

// call back for Google
BoundPane.prototype.initialize = function(map) {
	this.map_ = map;
	var div = document.createElement("div");
	div.style.border = this.weight_ + "px dotted " + this.color_;
	if (this.opacity_) YAHOO.util.Dom.setStyle(div, 'opacity', this.opacity_);
	if (this.backgroundColor_) div.style.backgroundColor = this.backgroundColor_;
	div.style.position = "absolute";
	map.getPane(G_MAP_MARKER_SHADOW_PANE).appendChild(div);
	this.div_ = div;
	
	div = document.createElement("div");
	div.style.border = this.weight_ + "px dotted " + this.color_;
	if (this.opacity_) YAHOO.util.Dom.setStyle(div, 'opacity', this.opacity_);
	if (this.backgroundColor_) div.style.backgroundColor = this.backgroundColor_;
	div.style.position = "absolute";
	map.getPane(G_MAP_MARKER_SHADOW_PANE).appendChild(div);
	this.div360_ = div;
}
var dragobj = null;
var firsttime = 0;
function dragstart(e) {
	if (firsttime >= 1) {
		YAHOO.util.Event.stopPropagation(e);
		this.style.backgroundColor = '#fff';
	}
	firsttime++;
	return true;
}
function dragend() {
	this.style.backgroundColor = null;
}
BoundPane.prototype.remove = function() {
	this.div_.parentNode.removeChild(this.div_);
	this.div360_.parentNode.removeChild(this.div360_);
}
BoundPane.prototype.redraw = function(force) {
	if (!force) return;
	this.div360_.style.visibility = 'hidden';
	var sw = new GLatLng(this.s_, this.w_, true);
	var ne = new GLatLng(this.n_, this.e_, true);
	var b = new GLatLngBounds(sw, ne);
	var ll = this.map_.fromLatLngToDivPixel(b.getSouthWest());
	var ur = this.map_.fromLatLngToDivPixel(b.getNorthEast());
	this.div_.style.height = (ll.y - ur.y) + "px";
	this.div_.style.top = (ur.y - this.weight_) + "px";
	if (b.isFullLng()) { // earth and antartica
		var lng1 = this.map_.fromLatLngToDivPixel(new GLatLng(0, 0)).x;
		var lng2 = this.map_.fromLatLngToDivPixel(new GLatLng(0, 180)).x;
		var w = Math.abs(lng2 - lng1);
		this.div_.style.width = (4 * w) + "px";
		this.div_.style.left = (lng1 - (4 * w) - this.weight_) + "px";
		this.div360_.style.height = this.div_.style.height;
		this.div360_.style.top = this.div_.style.top;
		this.div360_.style.width = this.div_.style.width;
		this.div360_.style.left = (lng1 + this.weight_) + "px";
		this.div360_.style.visibility = 'visible';
	} else {
		var w = ur.x - ll.x;
		if (w < 0) { // if span wrapped
			var d = b.toSpan().lng();
			w = d * w / (d - 360);
			this.div360_.style.height = this.div_.style.height;
			this.div360_.style.top = this.div_.style.top;
			this.div360_.style.width = w + "px";
			this.div360_.style.left = (ur.x - w) + "px";
			this.div360_.style.visibility = 'visible';
		}
		this.div_.style.width = w + "px";
		this.div_.style.left = (ll.x - this.weight_) + "px";
	}
	if (zat.clickable) this.setClickDivs();
}
// create draggable div for each boundary line
BoundPane.prototype.clickdiv = function(side, cursor) {
	var div = document.createElement("div");
	div.id = 'click_' + side;
	YAHOO.util.Dom.setStyle(div, 'opacity', 0.001); // make invisible
	div.style.backgroundColor = '#fff'; // white
	div.style.position = "absolute";
	map.getPane(G_MAP_MARKER_SHADOW_PANE).appendChild(div);
	var dragobj = new GDraggableObject(div);
	if (cursor) {
		dragobj.setDraggableCursor(cursor);
		dragobj.setDraggingCursor(cursor);
		div.style.cursor = cursor;
	}
	div.dragobj = dragobj;
	
	GEvent.addListener(dragobj, 'dragstart', function(e) {
		zat.bound.side_ = side;
		// e.offset X Y not defined on Firefox
		zat.offsetX = (e.offsetX ? e.offsetX : zat.bound.offset_) - zat.bound.offset_;
		zat.offsetY = (e.offsetY ? e.offsetY : zat.bound.offset_) - zat.bound.offset_;
	});
	GEvent.addListener(dragobj, 'drag', function(e) {
		var p = zat.bound.map_.fromContainerPixelToLatLng(new GPoint(e.clientX + zat.mapoffX - zat.offsetX, e.clientY - zat.mapoffY - zat.offsetY));
		zat.bound.moveline(p);
	});
	GEvent.addListener(dragobj, 'dragend', function(e) {
		zat.clickable();
	});
	zat.clickdivs[side] = div;
}
// set location for each draggable div
BoundPane.prototype.setClickDivs = function() {
	var off = zat.bound.offset_;
	var prec = (off * 2) - 1;
	var sw = new GLatLng(this.s_, this.w_, true);
	var ne = new GLatLng(this.n_, this.e_, true);
	var b = new GLatLngBounds(sw, ne);
	var ll = this.map_.fromLatLngToDivPixel(b.getSouthWest());
	var ur = this.map_.fromLatLngToDivPixel(b.getNorthEast());
	
	var div = zat.clickdivs['n'];
	div.style.height = prec + 'px';
	div.style.top = (ur.y - off) + "px";
	div.style.width = (ur.x - ll.x - prec) + "px";
	div.style.left = (ll.x + off) + "px";
	if (div.dragobj) { div.dragobj.top = ur.y - off; div.dragobj.left = ll.x + off; }
	
	div = zat.clickdivs['s'];
	div.style.height = prec + 'px';
	div.style.top = (ll.y - off) + "px";
	div.style.width = (ur.x - ll.x - prec) + "px";
	div.style.left = (ll.x + off) + "px";
	if (div.dragobj) { div.dragobj.top = ll.y - off; div.dragobj.left = ll.x + off; }
	
	div = zat.clickdivs['w'];
	div.style.height = (ll.y - ur.y - prec) + 'px';
	div.style.top = (ur.y + off) + "px";
	div.style.width = prec + "px";
	div.style.left = (ll.x - off) + "px";
	if (div.dragobj) { div.dragobj.top = ur.y + off; div.dragobj.left = ll.x - off; }
	
	div = zat.clickdivs['e'];
	div.style.height = (ll.y - ur.y - prec) + 'px';
	div.style.top = (ur.y + off) + "px";
	div.style.width = prec + "px";
	div.style.left = (ur.x - off) + "px";
	if (div.dragobj) { div.dragobj.top = ur.y + off; div.dragobj.left = ur.x - off; }
	
	div = zat.clickdivs['ne'];
	div.style.width = div.style.height = prec + 'px';
	div.style.top = (ur.y - off) + "px";
	div.style.left = (ur.x - off) + "px";
	if (div.dragobj) { div.dragobj.top = ur.y - off; div.dragobj.left = ur.x - off; }
	
	div = zat.clickdivs['se'];
	div.style.width = div.style.height = prec + 'px';
	div.style.top = (ll.y - off) + "px";
	div.style.left = (ur.x - off) + "px";
	if (div.dragobj) { div.dragobj.top = ll.y - off; div.dragobj.left = ur.x - off; }
	
	div = zat.clickdivs['sw'];
	div.style.width = div.style.height = prec + 'px';
	div.style.top = (ll.y - off) + "px";
	div.style.left = (ll.x - off) + "px";
	if (div.dragobj) { div.dragobj.top = ll.y - off; div.dragobj.left = ll.x - off; }
	
	div = zat.clickdivs['nw'];
	div.style.width = div.style.height = prec + 'px';
	div.style.top = (ur.y - off) + "px";
	div.style.left = (ll.x - off) + "px";
	if (div.dragobj) { div.dragobj.top = ur.y - off; div.dragobj.left = ll.x - off; }
}
// make boundary lines draggable
BoundPane.prototype.interactive = function(upf) {
	// var psw = this.map_.fromLatLngToDivPixel(new GLatLng(this.s_, this.w_));
	// var pne = this.map_.fromLatLngToDivPixel(new GLatLng(this.n_, this.e_));
	this.clickdiv('n', 'n-resize'); this.clickdiv('s', 's-resize'); this.clickdiv('w', 'w-resize'); this.clickdiv('e', 'e-resize');
	this.clickdiv('ne', 'ne-resize'); this.clickdiv('se', 'se-resize'); this.clickdiv('sw', 'sw-resize'); this.clickdiv('nw', 'nw-resize');
	this.setClickDivs()
	zat.clickable = upf;
}
// figure out which boundary line was clicked on
BoundPane.prototype.clickon = function(p) {
	var pixp = this.map_.fromLatLngToDivPixel(p);
	var prec = p.lat() - map.fromDivPixelToLatLng(new GPoint(pixp.x,pixp.y + 10)).lat(); // precision

	if ((new GLatLngBounds(new GLatLng(this.s_ - prec, this.w_ - prec), new GLatLng(this.s_ + prec, this.w_ + prec))).contains(p)) { this.side_ = 'sw'; return; }
	if ((new GLatLngBounds(new GLatLng(this.n_ - prec, this.w_ - prec), new GLatLng(this.n_ + prec, this.w_ + prec))).contains(p)) { this.side_ = 'nw'; return; }
	if ((new GLatLngBounds(new GLatLng(this.n_ - prec, this.e_ - prec), new GLatLng(this.n_ + prec, this.e_ + prec))).contains(p)) { this.side_ = 'ne'; return; }
	if ((new GLatLngBounds(new GLatLng(this.s_ - prec, this.e_ - prec), new GLatLng(this.s_ + prec, this.e_ + prec))).contains(p)) { this.side_ = 'se'; return; }

	if ((new GLatLngBounds(new GLatLng(this.n_ - prec, this.w_), new GLatLng(this.n_ + prec, this.e_))).contains(p)) { this.side_ = 'n'; return; }
	if ((new GLatLngBounds(new GLatLng(this.s_ - prec, this.w_), new GLatLng(this.s_ + prec, this.e_))).contains(p)) { this.side_ = 's'; return; }
	if ((new GLatLngBounds(new GLatLng(this.s_, this.w_ - prec), new GLatLng(this.n_, this.w_ + prec))).contains(p)) { this.side_ = 'w'; return; }
	if ((new GLatLngBounds(new GLatLng(this.s_, this.e_ - prec), new GLatLng(this.n_, this.e_ + prec))).contains(p)) { this.side_ = 'e'; return; }
	
	this.side_ = null;
}
// move one of the boundary lines, and redraw
BoundPane.prototype.moveline = function(p) {
	if (this.side_ == null) {
		this.n_ = p.lat();
		if (p.lat() < this.s_) this.s_ = p.lat();
		if ((new GLatLngBounds(new GLatLng(-85, this.w_), new GLatLng(85, this.w_ + 200))).contains(p)) this.e_ = p.lng();
		else if ((new GLatLngBounds(new GLatLng(-85, this.w_ - 60), new GLatLng(85, this.w_))).contains(p)) this.e_ = this.w_ = p.lng();
		this.redraw(true);
		return;
	}
	if ((this.side_ == 'n' || this.side_ == 'nw' || this.side_ == 'ne') && p.lat() > this.s_) this.n_ = p.lat();
	if ((this.side_ == 's' || this.side_ == 'sw' || this.side_ == 'se') && p.lat() < this.n_) this.s_ = p.lat();
	if ((this.side_ == 'w' || this.side_ == 'sw' || this.side_ == 'nw') && (new GLatLngBounds(new GLatLng(-85, this.e_ - 200), new GLatLng(85, this.e_))).contains(p)) this.w_ = p.lng();
	if ((this.side_ == 'e' || this.side_ == 'se' || this.side_ == 'ne') && (new GLatLngBounds(new GLatLng(-85, this.w_), new GLatLng(85, this.w_ + 200))).contains(p)) this.e_ = p.lng();

	this.redraw(true);
}
