//<![CDATA[

var gp = GMap2.prototype;
var icnBlue;

gp.initialize = function() {
	icnBlue = new GIcon();
	icnBlue.image = "images/icnBlue.png";
	icnBlue.iconSize = new GSize(17, 17);
	icnBlue.iconAnchor = new GPoint(8, 4);
	icnBlue.infoWindowAnchor = new GPoint(5, 14);

	this.disableDragging();
	this.home = {ll:new GLatLng(38.627222, -90.197778), z:9};
	curPos = this.home.ll;
	this.idxIcons = [];
	this.goHome();
//addLog("Map position initialized");
	z.moveToRange(this.home.z);
	this.addSqControl();
//addLog("SsControl object initialized");
	this.covmap = new GOverviewMapControl(new GSize(150, 150));
//addLog("GOverviewMapControl created");
	this.addControl(this.covmap);
//addLog("GOverviewMapControl initialized");

	var ov = this.covmap.getOverviewMap();
	GEvent.addDomListener(ov.b, "mouseup", function() {
		updmap = true;
		idxmap.applyMapChanges();
	});

	getProps();
}

// ********************** MAP MOVEMENT / POSITIONS
gp.goHome = function() {
	this.centerMap(this.home.ll, this.home.z);
}

gp.gotoLastPosition = function() {
	this.panTo(curPos);
}

gp.fitToBox = function(box) {
	this.centerMap(this.getBoundboxCenter(box), this.getBoundsZoomLevel(box));
}

gp.centerMap = function(latlng, zoom) {
	this.applyMapChanges(function(){idxmap.setCenter(latlng, zoom)});
}

gp.zoomTo = function(d, isRel) {
	if(isRel)
		d += this.getZoom();

	var self = this;
	this.applyMapChanges(function(){idxmap.setZoom(d)});
}

gp.panBySize = function(x, y) {
	this.applyMapChanges(function(){idxmap.panBy(new GSize(x, y))});
}

gp.applyMapChanges = function(fn) {
	updmap = true;
	showMap();
	this.resetOverlays();
	if(fn)fn();
}

gp.resetOverlays = function() {
	this.removeSqControl();
	this.clearOverlays();
	this.idxIcons = [];
}

gp.removeSqControl = function() {
	if(this.ovmov) {
		this.removeOverlay(this.ovmov);
		delete this.ovmov;
	}
}

gp.addSqControl = function() {
	this.removeSqControl();
	this.ovmov = new IDXOverlay("", G_MAP_MAP_PANE, "ovmov", "", .01);
	this.addOverlay(this.ovmov);
	initdr();
}

gp.updateSqControl = function() {
	this.removeSqControl();
	this.addSqControl();
}

// ********************** MAP COORDS / CONVERSION
gp.getLatLngAt = function(x, y) {
	return this.fromContainerPixelToLatLng(new GPoint(x, y));
}

/*
@posType: box position. Available Values:
	0:center (default)
	1:top-left
	2:top-right
	3:bottom-left
	4:bottom-right
	5:absolute position (needs additional parameters x & y)
*/
gp.getLatLngBox = function(w, h, posType, x, y) {
	var mapSize = this.getSize();
	var mw = mapSize.width;
	var mh = mapSize.height;
	var pos;
	if(w > mw || h > mh)
		return false;
	switch(posType) {
		case 1:pos = {x:0, y:0};break;
		case 2:pos = {x:mw - w, y:0};break;
		case 3:pos = {x:0, y:mh - h};break;
		case 4:pos = {x:mw - w, y:mh - h};break;
		case 5:pos = {x:x, y:y};break;
		default:pos = {x:(mw - w) / 2, y:(mh - h) / 2};break;
	}
	var p1 = this.getLatLngAt(pos.x, pos.y);
	var p2 = this.getLatLngAt(pos.x + w, pos.y + h);
	var box = new GLatLngBounds();
	box.extend(p1);
	box.extend(p2);
	return box;
}

gp.getBoundboxCenter = function(box) {
	var sw = box.getSouthWest();
	var ne = box.getNorthEast();
	var lat1 = sw.lat();
	var lat2 = ne.lat();
	var lng1 = sw.lng();
	var lng2 = ne.lng();
	var lat = lat2 - ((lat2-lat1)/2);
	var lng = lng2 - ((lng2-lng1)/2);
	return new GLatLng(lat, lng);
}

gp.getBoundboxLatLngs = function(box) {
	var sw = box.getSouthWest();
	var ne = box.getNorthEast();
	var lat1 = sw.lat();
	var lat2 = ne.lat();
	var lng1 = sw.lng();
	var lng2 = ne.lng();

	return {lat1:lat1, lat2:lat2, lng1:lng1, lng2:lng2};
}

gp.getBoundBoxFromLatLngs = function(lat1, lat2, lng1, lng2) {
	var box = new GLatLngBounds();
	var latLng1 = new GLatLng(lat1, lng1);
	var latLng2 = new GLatLng(lat2, lng2);
	box.extend(latLng1);
	box.extend(latLng2);
	return box;
}

gp.checkOverview = function() {
	if(this.covmap.ee)return;
	var i, r;
	var l = this.idxIcons.length;

	var b = this.getLatLngBox(150, 150, 4);

	for(i=0;i<l;i++)
		if(b.contains(this.idxIcons[i].getPoint())) {
			r = true;
			break;
		}

	return r;
}

gp.hideWarning = function(hideov) {
return;
	this.removeOverlay(this.warningOverlay);
	if(hideov) {
		this.covmap.hide();
		if(this.activeIDXIcon)
			onIconClick(this.activeIDXIcon);
	}
}

gp.showOvWarning = function() {
return;
	this.hideWarning();
	var b = this.getLatLngBox(150, 80, 2);
	var h = "<div class='btn' onclick='idxmap.hideWarning()'>X</div><img src='images/warning.gif'/>Some icons are behind the overview map. Click <a href='javascript:idxmap.hideWarning(true)'>here</a> to close it (or click the small arrow at the bottom right-corner of the map)";
	this.warningOverlay = new IDXOverlay(b, G_MAP_FLOAT_PANE, "warningOverlay", h, .85);
	this.addOverlay(this.warningOverlay);
}

// ICON EVENTS
gp.icnClicked = function(m) {
	this.activeIDXIcon = m;
	onIconClick(m);
}

gp.showBMap = function() {
	this.activeIDXIcon.showMapBlowup({mapType:G_HYBRID_MAP,zoomLevel:16});
}

// ********************** ICONS
gp.plot = function() {
	var i, marker, cur;
	var l = rs.length;

	this.idxIcons = [];

	for(i=0;i<l;i++) {
		cur = rs[i];
		marker = new GMarker(new GLatLng(cur.lat, cur.lon), {icon: icnBlue, draggable:false, title:cur.address + ": $" + cur.price});
		marker.pid = cur.id;
		marker.clickable = true;

		var lis = GEvent.callbackArgs(this, this.icnClicked, marker);
		GEvent.addListener(marker, "click", lis);
		this.addOverlay(marker);
		this.idxIcons.push(marker);
	}
}

gp.getIdxIcon = function(ID) {
	var i, m;
	var l = this.idxIcons.length;

	for(i=0;i<l;i++) {
		m = this.idxIcons[i];
		if(m.pid == ID)
			break;
	}

	return m;
}

// ********************** CUSTOM OVERLAYS
function IDXOverlay(bounds, pane, cls, innerContent, bgOpacity, bgColor, border_weight, border_color) {
	this.bounds_ = bounds;
	this.pane_ = pane;
	this.cls_ = cls;
	this.innerContent_ = innerContent;
	this.weight_ = border_weight || 0;
	this.color_ = border_color || "#888888";
	this.backgroundColor_ = bgColor;
	this.opacity_ = bgOpacity;
}
IDXOverlay.prototype = new GOverlay();
IDXOverlay.prototype.initialize = function(map) {
	if(!this.bounds_)
		this.bounds_ = map.getBounds();
	var div = document.createElement("div");
	div.style.position = "absolute";
	if(this.innerContent_) {
		if(typeof(this.innerContent_) == "string")
			div.innerHTML = this.innerContent_;
		else
			div.appendChild(this.innerContent_);
	}
	if(this.cls_)
		div.className = this.cls_;
	else {
		div.style.border = this.weight_ + "px solid " + this.color_;
		div.style.backgroundColor = this.backgroundColor_;
	}
	setCSSOpacity(div, this.opacity_);

	map.getPane(this.pane_).appendChild(div);
	this.map_ = map;
	this.div_ = div;
}
IDXOverlay.prototype.remove = function() {
	this.div_.parentNode.removeChild(this.div_);
}
IDXOverlay.prototype.copy = function() {
	return new IDXOverlay(this.bounds_, this.pane_, this.cls_, this.innerContent_, this.opacity_, this.backgroundColor_, this.weight_, this.color_);
}
IDXOverlay.prototype.redraw = function(force) {
	if(!force)
		return;
	var c1 = this.map_.fromLatLngToDivPixel(this.bounds_.getSouthWest());
	var c2 = this.map_.fromLatLngToDivPixel(this.bounds_.getNorthEast());
	this.div_.style.width = Math.abs(c2.x - c1.x) + "px";
	this.div_.style.height = Math.abs(c2.y - c1.y) + "px";
	this.div_.style.left = (Math.min(c2.x, c1.x) - this.weight_) + "px";
	this.div_.style.top = (Math.min(c2.y, c1.y) - this.weight_) + "px";
}

//]]>
