// JavaScript Document
var compat = GBrowserIsCompatible();
var bounds = null;
var imgBase = '';
var markers = new Array();
var map = null;
var bub = null;

if ( compat ) {
	function hood_icon() {
		icon = new GIcon();
		icon.image = imgBase + 'pin_neigh.png';
		icon.shadow = imgBase + 'pin_neigh_shadow.png';
		icon.shadowSize = new GSize(48,36);
		icon.iconSize = new GSize(31,36);
		icon.iconAnchor = new GPoint(15, 35);
		icon.infoWindowAnchor = new GPoint(15, 10);
		return icon;
	}

	function house_icon(colpin,num) {
		icon = new GIcon();
		if ( num > 8 ) num = 'plus';
		icon.image = ''+ imgBase + 'pin_'+ colpin +'_'+ num +'.png';
		icon.shadow = imgBase + 'pin_shadow.png';
		icon.shadowSize = new GSize(43,36);
		icon.iconSize = new GSize(22,36);
		icon.iconAnchor = new GPoint(11, 35);
		icon.infoWindowAnchor = new GPoint(1, 2);
		return icon;
	}

	function type_icon(t) {
		icon = new GIcon();
		icon.image = ''+ imgBase + 'pin_'+ t +'.png';
		icon.shadow = imgBase + 'pin_shadow.png';
		icon.shadowSize = new GSize(43,36);
		icon.iconSize = new GSize(22,36);
		icon.iconAnchor = new GPoint(11, 35);
		icon.infoWindowAnchor = new GPoint(1, 2);
		return icon;
	}

	function standardControls() {
		map.addControl(new GSmallMapControl());
		map.addControl(new GMapTypeControl(), new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(3, 20)));
	}
	
	function extraMarker(css,point,content,css_click,content_click,icon,x,y,x_click,y_click) {
		var marker = new GMarker(point, icon);
		if ( content != '' ) {
			if ( bub != null ) {
				if ( content_click != '' ) {
					GEvent.addListener(marker, "click", function() {
						if ( bub.canHide ) {
							bub.oldCenter = map.getCenter();
						}
					
						bub.canHide = true;
						bub.openOnMarker(css_click,marker,x_click,y_click,content_click);
						bub.canHide = false;
						
						p = marker.getPoint();
						pix = map.fromLatLngToDivPixel(p);
						pix.y = pix.y -30;
						map.panTo(map.fromDivPixelToLatLng(pix));
					});
				}
				GEvent.addListener(marker, "mouseover", function() {
					bub.openOnMarker(css,marker,x,y,content);
				});
				GEvent.addListener(marker, "mouseout", function() {
					bub.hide();
				});
			}
		}
		return marker;
	}
	
	function createMarker(css,point,dtlLink,content,css_click,content_click,icon,x,y,id,x_click,y_click) {
		var marker = new GMarker(point, icon);
		if ( content != '' ) {
			if ( bub != null ) {
				GEvent.addListener(marker, "click", function() {
					if ( css_click == '' && content_click == '' ) {
						window.location.href = dtlLink;
					} else {
						if ( bub.canHide ) {
							bub.oldCenter = map.getCenter();
						}
					
						bub.canHide = true;
						bub.openOnMarker(css_click,marker,x_click,y_click,content_click);
						bub.canHide = false;
						
						p = marker.getPoint();
						pix = map.fromLatLngToDivPixel(p);
						pix.y = pix.y -30;
						map.panTo(map.fromDivPixelToLatLng(pix));
					}
				});
				GEvent.addListener(marker, "mouseover", function() {
					bub.openOnMarker(css,marker,x,y,content);
				});
				GEvent.addListener(marker, "mouseout", function() {
					bub.hide();
				});
			}
		}
		markers.push(marker);
		return marker;
	}
	
	function rebound() {
		center = bounds.getCenter();
		newZoom = map.getBoundsZoomLevel(bounds);
		if ( map.getZoom() != newZoom ){
			map.setCenter(center, newZoom);
		} else {
			map.panTo(center);
		}
	}
	
	function reboundMax() {
		center = bounds.getCenter();
		newZoom = map.getBoundsZoomLevel(bounds);
		if ( newZoom > 13 ) {
			newZoom = 13;
		}
		if ( map.getZoom() != newZoom ){
			map.setCenter(center, newZoom);
		} else {
			map.panTo(center);
		}
	}
	
	function recenter() {
		center = bounds.getCenter();
		map.setCenter(center);
	}
	
	function addMarker(css,latitude,longitude,dtlLink,content,css_click,content_click,icon,x,y,id,x_click,y_click) {
		point = new GLatLng(latitude, longitude);
		map.addOverlay( createMarker(css,point, dtlLink, content, css_click, content_click, icon,x,y,id,x_click,y_click) );
		bounds.extend(point);
	}
	
	function processLocs(arr) {
		if(markers.length>0){
			markers=[];
		}
		for(i = 0; i < arr.length; i++) {
			addMarker(
				arr[i][0],
				arr[i][1],
				arr[i][2],
				arr[i][3],
				arr[i][4],
				arr[i][5],
				arr[i][6],
				arr[i][7],
				arr[i][8],
				arr[i][9],
				arr[i][10],
				arr[i][11],
				arr[i][12]
			);
		}
	}
	
	function findNumId(arr,id) {
		for(i = 0; i < arr.length; i++) {
			if ( arr[i][10] == id ) {
				return i;
			}
		}
		return -1;
	}
	
	function displayListingInfoWindow(arr,id) {
		num = findNumId(arr,id);
		if ( num > -1 ) {
			if ( bub != null ) {
				bub.openOnMarker(arr[num][0],markers[num],arr[num][8],arr[num][9],arr[num][4]);
			} else {
				markers[num].openInfoWindowHtml(arr[num][3]);
			}
		}
	}
	
	function displayInfoWindow(arr,num) {
		if ( bub != null ) {
			bub.openOnMarker(arr[num][0],markers[num],arr[num][8],arr[num][9],arr[num][4]);
			p = markers[num].getPoint();
			if ( bub.canHide ) {
				bub.oldCenter = map.getCenter();
			}
			map.panTo(p);
		} else {
			markers[num].openInfoWindowHtml(arr[num][3]);
		}
		scroll(0,400);
	}
	
	function hideInfoWindow(arr,num) {
		if ( bub != null ) {
			bub.hide();
		} else {
			markers[num].closeInfoWindow();
		}
	}
	
	function standardDetailContent(address,cityStateZip,description,dtlLink,mls_id,office_name,num_photos,main_image,price,beds,baths,sqft,year) {
		retstr = [];
		retstr.push('<div class="click_results"><a href="javascript:bub.closeCenter()"><img class="close_click_results" src="');
		retstr.push(imgBase);
		retstr.push('close_click_results.gif" alt="close" /></a><div class="clear"></div><div class="click_results_details"><address>');
		retstr.push(address);
		retstr.push('<br /><span>');
		retstr.push(cityStateZip);
		retstr.push('</span></address><p>');
		retstr.push(description);
		retstr.push('<a href="');
		retstr.push(dtlLink);
		retstr.push('"><img class="more_click_results" src="');
		retstr.push(imgBase);
		retstr.push('more_click_results.gif" alt="More" /></a></p><div class="clear"></div><div class="click_mls">MLS# ');
		retstr.push(mls_id);
		retstr.push('</div><div class="click_broker">By: ');
		retstr.push(office_name);
		retstr.push('</div><a class="click_photo_num"><div class="click_photo_num_text">');
		retstr.push(num_photos);
		retstr.push('</div></a><a href="');
		retstr.push(dtlLink);
		retstr.push('"><img class="button_click_details" src="');
		retstr.push(imgBase);
		retstr.push('button_click_details.gif" alt="Full Details" /><img class="button_click_photos" src="');
		retstr.push(imgBase);
		retstr.push('button_click_photos.gif" alt="More Photos" /></a></div><div class="click_results_photo_price"><div class="click_results_photo"><a href="');
		retstr.push(dtlLink);
		retstr.push('"><img src="');
		retstr.push(main_image);
		retstr.push('" alt="" /><div class="frame_click_results_photo"></div></a><div class="click_results_price">$');
		retstr.push(price);
		retstr.push('</div></div></div><div class="click_results_stats"><div class="click_stats_beds">');
		retstr.push(beds);
		retstr.push('</div><div class="click_stats_baths">');
		retstr.push(baths);
		retstr.push('</div><div class="click_stats_sqft">');
		retstr.push(sqft);
		retstr.push('</div><div class="click_stats_year">');
		retstr.push(year);
		retstr.push('</div></div></div>');
		
		return retstr.join('');
	}
	
	function standardContent(imgSrc,address,broker,dtlLink,dtlImage,price,beds,baths,sqr_feet,lsize) {
		retstr = [];
		retstr.push('<address>');
		retstr.push(address);
		retstr.push('</address><div class="clear"></div><div class="over_broker">By: ');
		retstr.push(broker);
		retstr.push('</div><div class="clear"></div><div class="over_bed">');
		retstr.push(beds);
		retstr.push('</div><div class="over_sqft">');
		retstr.push(sqr_feet);
		retstr.push('</div><div class="over_bath">');
		retstr.push(baths);
		retstr.push('</div><div class="over_price">$');
		retstr.push(price);
		retstr.push('</div><div class="over_photo"><a href="');
		retstr.push(dtlLink);
		retstr.push('"><img src="');
		retstr.push(imgSrc);
		retstr.push('" /><div class="frame_over"></div></a></div>');
		return retstr.join('');
	}
	
	function standardNeighborhood(hood,city,state,homes,avg_home,link,dtlImage) {
		retstr = [];
		retstr.push('<h4>');
		retstr.push(hood);
		retstr.push('</h4><span>');
		retstr.push(city);
		retstr.push(', ');
		retstr.push(state);
		retstr.push('</span><div class="clear"></div><div class="over_tot_homes">');
		retstr.push(homes);
		retstr.push('</div><div class="over_avg_price">$');
		retstr.push(avg_home);
		retstr.push('</div><a class="button_view_neigh" href="');
		retstr.push(link);
		retstr.push('"><img src="');
		retstr.push(dtlImage);
		retstr.push('" alt="View Neighborhood" /></a>');
		return retstr.join('');
	}

	function mapInit(id) {
		map = new GMap2( document.getElementById(id) );
		bounds = new GLatLngBounds();
		GEvent.addListener(map, "click", function() {
			if ( bub != null ) {
				bub.hide();
			}
		});
	}
}


function mapLoading() {
	div_map = document.getElementById('map');
	d = document.createElement('div');
	d.id = 'page_overlay';
	d.style.display = 'none'
	d.innerHTML = '<div id="loadingBox"><img src="'+imgBase+'loading.gif" /><br />loading</div>';
	div_map.appendChild(d);
}

function mapSuggest() {
	div_map = document.getElementById('map');
	d = document.createElement('div');
	d.id = 'suggest_location';
	d.style.display = 'none'
	d.innerHTML = '<img id="suggest_title" src="'+imgBase+'suggest_title.jpg" /><a href="javascript:hideDiv(\'suggest_location\');"><img id="suggest_close" src="'+imgBase+'suggest_close.jpg" /></a><div style="clear: both;"></div><img id="suggest_head" src="'+imgBase+'suggest_head.jpg" /><div style="clear: both;"></div><div id="suggest_items"></div>';
	div_map.appendChild(d);
}

function suggestContent(content) {
	document.getElementById('suggest_items').innerHTML = content;
}

function pageOverlay(m_div_id,on) {
	div_map = document.getElementById(m_div_id);
	objOverlay = document.getElementById('page_overlay');

	if ( objOverlay && div_map ) {
		objOverlay.style.position = 'absolute';
		objOverlay.style.top = '0';
		objOverlay.style.left = '0';
		objOverlay.style.zIndex = '995';
	 	objOverlay.style.width = '100%';
	 	objOverlay.style.height = '100%';
		
		if ( ! on ) {
			objOverlay.style.display = 'none';
			objOverlay.style.visibility = 'hidden';
		} else if ( on ) {
			objOverlay.style.display = 'block';
			objOverlay.style.visibility = 'visible';
		}
		
	}
}

function messageOverlay(m_div_id,on) {
	div_map = document.getElementById(m_div_id);
	d = document.createElement('div');
	d.id = 'message_overlay';
	d.innerHTML = '';
	
	d.style.position = 'absolute';
	d.style.top = '0';
	d.style.left = '0';
	d.style.zIndex = '99999';
 	d.style.width = '100%';
 	
	div_map.appendChild(d);
}

var current_messages = new Array();
function displayMessageOverlay(m) {
	m_overlay = document.getElementById('message_overlay');
	if ( m_overlay ) {
		d = document.createElement('div');
		d.className = 'message_overlay_text';
		d.id = current_messages.length;
	}
}


function mBubble(map,map_div_id){this.map=map;this.map_div_id=map_div_id;this.visible=false;this.canHide=true;this.oldCenter=0;}
mBubble.prototype=new GOverlay();mBubble.prototype.initialize=function(map){var div=document.createElement("div");document.getElementById(this.map_div_id).appendChild(div);this.div=div;this.hide();}
mBubble.prototype.openOnMap=function(css,point,x,y,html){if(this.canHide){this.offsetX=x;this.offsetY=y;this.point=point;this.div.className=css;this.div.innerHTML=html;this.div.style.zIndex=999;this.show();this.redraw(true);}}
mBubble.prototype.openOnMarker=function(css,marker,x,y,html){if(this.canHide){this.openOnMap(css,marker.getPoint(),x,y,html);}}
mBubble.prototype.redraw=function(force){if(!this.visible){return;}
var cloc=this.map.fromContainerPixelToLatLng(new GPoint(0,0),true);var clocPixel=this.map.fromLatLngToDivPixel(cloc);var p=map.fromLatLngToDivPixel(this.point);this.div.style.left=(p.x-clocPixel.x+this.offsetX)+"px";this.div.style.top=(p.y-clocPixel.y+this.offsetY)+"px";}
mBubble.prototype.remove=function(){this.canHide=true;this.hide();this.div.parentNode.removeChild(this.div);}
mBubble.prototype.copy=function(){return new EWindow(this.map,this.css);}
mBubble.prototype.show=function(){this.div.style.display='';this.visible=true;}
mBubble.prototype.hide=function(){if(this.canHide){this.div.style.display='none';this.visible=false;}}
mBubble.prototype.close=function(){this.canHide=true;this.hide();}
mBubble.prototype.closeCenter=function(){this.close();map.panTo(this.oldCenter);}
mBubble.prototype.isHidden=function(){return!this.visible;}
mBubble.prototype.supportsHide=function(){return true;}

addEvent = function(o, e, f, s){
	var r = o[r = "_" + (e = "on" + e)] = o[r] || (o[e] ? [[o[e], o]] : []), a, c, d;
	r[r.length] = [f, s || o], o[e] = function(e){
		try{
			(e = e || event).preventDefault || (e.preventDefault = function(){e.returnValue = false;});
			e.stopPropagation || (e.stopPropagation = function(){e.cancelBubble = true;});
			e.target || (e.target = e.srcElement || null);
			e.key = (e.which + 1 || e.keyCode + 1) - 1 || 0;
		}catch(f){}
		for(d = 1, f = r.length; f; r[--f] && (a = r[f][0], o = r[f][1], a.call ? c = a.call(o, e) : (o._ = a, c = o._(e), o._ = null), d &= c !== false));
		return e = null, !!d;
    }
};

removeEvent = function(o, e, f, s){
	for(var i = (e = o["_on" + e] || []).length; i;)
		if(e[--i] && e[i][0] == f && (s || o) == e[i][1])
			return delete e[i];
	return false;
};

ToolTip = function(o, t, c, f){
	var i, $ = this;
	$.s = ($.o = document.createElement("div")).style;
	$.s.display = "none", $.s.position = "absolute", $.o.className = c, $.t = t, $.f = f;
	for(i in {mouseout: 0, mouseover: 0, mousemove: 0})
		addEvent(o, i, function(e){$[e.type](e);});
};
with({p: ToolTip.prototype}){
	p.update = function(e){
		var w = window, b = document.body;
		this.s.top = e.clientY + (w.scrollY || b.scrollTop || b.parentNode.scrollTop || 0) + "px",
		this.s.left = e.clientX + (w.scrollX || b.scrollLeft || b.parentNode.scrollLeft || 0) + "px";
	}
	p.mouseout = function(){
		this.s.display = "none";
	};
	p.mouseover = function(e){
		document.body.appendChild(this.o).innerHTML = this.t,
		e.stopPropagation(), this.update(e), this.s.display = "block";
	};
	p.mousemove = function(e){
		this.f && this.update(e);
	};
	p.setContent = function(s){
		this.o.innerHTML = this.t = s;
	};
}

function get_tool_tip(meEle, varImg)
{
	var t = new ToolTip(document.getElementById(meEle), "<img src='" + varImg + "'>", "toolTip", true);
}