
var old_cfpd = MTAPI_init_checkForPropertyDirectLink;
MTAPI_init_checkForPropertyDirectLink = function(){
	var cook = JCookie.get('get_bargainhunter');
	if(cook && cook == 1){
		document.getElementById("bargainhunter").checked = true;
		setbargainhunter();
		}
	old_cfpd();
	}

// This can override the default zoom level when zooming to a property
//JDetail.prototype.centerAndZoomMap = function() {
//	var e = document.getElementById('detaildata');
//	if (e)
//		MTG_Map.setCenter( new GLatLng(e.getAttribute('lat'), e.getAttribute('lon')), 15);
//	}

var old_chooseBargainColorDot = JDots.prototype.chooseBargainColorDot;

JDots.prototype.chooseBargainColorDot = function(e) {
	var ppsf = parseInt(e.getAttribute('ppsf'));
	var ppbd = parseInt(e.getAttribute('ppbd'));

	var sfhalf = this.sdppsf/2;
	var bdhalf = this.sdppbd/2;

	var dotcolor = 'yellow-dot';

	if (ppsf > 1 && ppbd > 1) {
		if (ppsf < (this.ppsf-sfhalf) && ppbd < (this.ppbd-bdhalf))
			dotcolor = 'green-dot';
		if (ppsf > (this.ppsf+sfhalf) && ppbd > (this.ppbd+bdhalf))
			dotcolor = 'red-dot';
		}

	var lp = parseInt(e.getAttribute('lp'));
	var lpo = parseInt(e.getAttribute('lpo'));
	var dom = parseInt(e.getAttribute('dom'));

	var midx = (1 - ( lp / lpo )) * ( dom / 180)  * 10;
	if(midx > 5)
		dotcolor += '-lg';	
	else if(midx > 1)
		dotcolor += '-md';	
	else	
		dotcolor += '-sm';	

	gdebug.alert( "ppbd, dots.ppbd, dots.sdppbd: "+ppbd+", "+this.ppbd+", "+this.sdppbd+", "+dotcolor);
	return dotcolor;
	}


var old_reDrawAllDots = JDots.prototype.reDrawAllDots;

JDots.prototype.reDrawAllDots = function() {
	gdebug.alert("redraw all dots!!!");
	this.markFavorites();

	var d = document.getElementById('dots');
	var e = null;

	for (var i=0; i<d.childNodes.length; i++) {
		e = d.childNodes[i];
		if (e.nodeName == 'IMG') {   // process the images, skip the results div
			var style = 'n';
			var color = chooseDot(e);
			var size = getDotRadius(color,style);

			e.setAttribute('src', this.composeDotUrl(color,style));
//			e.style.top = (e.getAttribute('y') - (e.height/2)) + 'px';
//			e.style.left = (e.getAttribute('x') - (e.width/2)) + 'px';
			e.style.height = size + 'px';
			e.style.width = size + 'px';
			e.style.top = (e.getAttribute('y') - (size/2)) + 'px';
			e.style.left = (e.getAttribute('x') - (size/2)) + 'px';
			}
		}

	// reset current detail
	MTG_DetailHandler.reset();
	}
function getDotRadius(color,style){
	gdebug.alert("DOT: " + color + ":" + style);
	switch(color){
		case "red-dot":
		case "green-dot":
		case "yellow-dot":
		case "red-dot-sm":
		case "green-dot-sm":
		case "yellow-dot-sm":
			switch(style){
				case 'n':
					return 10;
				case 'h':
				case 'd':
					return 16;
				}
		case "red-dot-md":
		case "green-dot-md":
		case "yellow-dot-md":
			switch(style){
				case 'n':
					return 16;
				case 'h':
				case 'd':
					return 22;
				}
		case "red-dot-lg":
		case "green-dot-lg":
		case "yellow-dot-lg":
			switch(style){
				case 'n':
					return 22;
				case 'h':
				case 'd':
					return 24;
				}
		}
	}
function popup(e) {   // e is the image element
	if (e == MTG_DetailHandler.currente)
		return;
	popuponscreen = true;

	// blow up the dot
	es = e.style;
//	e.setAttribute( "src", composeDotUrl(e, 'h'));
//	e.style.top = (e.getAttribute('y') - (e.height/2)) + 'px';
//	e.style.left = (e.getAttribute('x') - (e.width/2)) + 'px';
	
	var style = 'h';
	var color = chooseDot(e);
	var size = getDotRadius(color,style);

	e.setAttribute('src', composeDotUrl(e,style));
//	e.style.top = (e.getAttribute('y') - (e.height/2)) + 'px';
//	e.style.left = (e.getAttribute('x') - (e.width/2)) + 'px';
			e.style.height = size + 'px';
			e.style.width = size + 'px';
	e.style.top = (e.getAttribute('y') - (size/2)) + 'px';
	e.style.left = (e.getAttribute('x') - (size/2)) + 'px';
	
	es.zIndex = gz++;

	// set the content of the popup
	s = composePopup(e);
	ppd = document.getElementById("pp");
	ppd.innerHTML = s;

	// measure the size of the popup
	// this is not accruate until the image is loaded
	//var w = ppd.offsetWidth;
	//var h = ppd.offsetHeight;
	var w = 154;
	var h = 148;

	// calculate location to keep the popup within the frame
	var dotleft = parseInt(es.left);
	var dottop = parseInt(es.top);

	var frametop = 0;
	var frameleft = 0;
	var frameright = parseInt(document.getElementById('mapcontainer').style.width);
	var framebottom = parseInt(document.getElementById('mapcontainer').style.height);

	var horzmidpoint = frameright - (w + 20);
	var vertmidpoint = framebottom / 2;

	var vertoffset = h/2;

	// by default
	var popupleft = dotleft + gxo;
	var popuptop = dottop - vertoffset;

	if (dotleft > horzmidpoint)
		popupleft = dotleft - w - (gxo/2);

	if (popuptop < 2)
		popuptop = 2;
	if (popuptop > (framebottom - h))
		popuptop = framebottom - h;

	// move the popup into place near the dot
	//ppds.left = parseInt(es.left) + gxo + "px";
	//ppds.top = parseInt(es.top) + gyo + "px";
	ppds.left = popupleft + "px";
	ppds.top = popuptop + "px";

	// show the popup
	ppds.visibility = "visible";
	ppds.zIndex=gz + 1;
	}

function popout(e) {     // e is the image element
	// for a repeated click on the same dot, stay down
	if (e == MTG_DetailHandler.currente)
		return;

	// shrink the dot
//	e.setAttribute( "src", composeDotUrl(e, 'n'));
//	e.style.top = (e.getAttribute('y') - (e.height/2)) + 'px';
//	e.style.left = (e.getAttribute('x') - (e.width/2)) + 'px';
	var style = 'n';
	var color = chooseDot(e);
	var size = getDotRadius(color,style);
	
	e.setAttribute('src', composeDotUrl(e,style));
//	e.style.top = (e.getAttribute('y') - (e.height/2)) + 'px';
//	e.style.left = (e.getAttribute('x') - (e.width/2)) + 'px';
			e.style.height = size + 'px';
			e.style.width = size + 'px';
	e.style.top = (e.getAttribute('y') - (size/2)) + 'px';
	e.style.left = (e.getAttribute('x') - (size/2)) + 'px';

	// hide the popup
	ppds.visibility = "hidden";
	popuponscreen = false;
	}

function popdown(e) {     // e is the image element
	// grow the dot
//	e.setAttribute( "src", composeDotUrl(e, 'd'));
//	e.style.top = (e.getAttribute('y') - (e.height/2)) + 'px';
//	e.style.left = (e.getAttribute('x') - (e.width/2)) + 'px';
	var style = 'd';
	var color = chooseDot(e);
	var size = getDotRadius(color,style);

	e.setAttribute('src', composeDotUrl(e,style));
//	e.style.top = (e.getAttribute('y') - (e.height/2)) + 'px';
//	e.style.left = (e.getAttribute('x') - (e.width/2)) + 'px';
			e.style.height = size + 'px';
			e.style.width = size + 'px';
	e.style.top = (e.getAttribute('y') - (size/2)) + 'px';
	e.style.left = (e.getAttribute('x') - (size/2)) + 'px';
	e.style.zIndex = gz++;

	// hide the popup
	if (ppds)
		ppds.visibility = "hidden";
	popuponscreen = false;
	}

