/**
Javascript functions to use gmap.
Requires Prototype and GMap scripts to be included already.
*/

var map;
var geocoder;
var tooltip;
var highlight;


function Communities(el) {
	this.empty();
	this.selected_id=0;
	this.selected_id_class='';
	this.el = $(el);
}
Communities.prototype = {
	place: false,
	add_place_marker: function(place) {
		this.place = place;
		new MarkerPlace(place);
	},
	empty: function() {
		this.communities = new Object();
		if (map) map.clearOverlays();
	},
	on_click: function(community) {
		if (!siteLoad(community.data.href))
			document.location.href = community.data.href;
	},
	add_communities: function(incoming_communities) {
		var cl_even = 'even';
		var cl_odd = 'odd';
		var cl = cl_odd;
		
		this.empty();
		//this.el.innerHTML = '';
		this.el.update();
		for(var i=0; i<incoming_communities.length; i++) {
			this.communities[incoming_communities[i].id] = new Community(incoming_communities[i], cl, this.on_click);
			this.communities[incoming_communities[i].id].add_to_citylist(this.el);
			cl = (cl == cl_odd)? cl_even: cl_odd;
		}
	}
}

function Community(data, cl, on_click_callback) {
	this.initilize(data, cl, on_click_callback);
}
Community.prototype = {
	initilize: function(data, cl, on_click_callback) {
		this.id = data.id;
		this._class = cl;
		this.data = data;
		this.on_click_callback = on_click_callback;
		if (map) this.build_marker();
	},
	add_to_citylist: function(el) {
		var this_community = this;
		var mapped = '';
		if (this.data.has_marker==0) {
			mapped = '<span>not mapped</span>';
		}
		
		if (!$('comm'+this.id))
			new Insertion.Bottom(el, "<tr id=\"comm"+this.id+"\" class=\""+this._class+" hover\"><td><a href=\""+this.data.href+"\">"+this.data.name+"</a></td><td>"+this.data.forsale+"</td><td>"+this.data.starting+"</td></tr>");
		this.citylist = $('comm'+this.id);
		this.citylist.onmouseover = function(){ this_community.mouse_over(); }
		this.citylist.onmouseout = function(){ this_community.mouse_out(); }
		this.citylist.onclick = function(){ this_community.on_click(); }
	},
	build_marker: function() {
		if (this.data.has_marker != 0) {
			this.marker = new MarkerPlace(this.data, this);
		} else {
			this.marker = new NoMarker(this);
		}
	},
	on_map: function() {
		if (this.has_marker() && onMap({x:this.data.longitude, y:this.data.latitude})) {
			return true;
		}
		return false;
	},
	has_marker: function () {
		return this.data.has_marker != 0;
	},
	mouse_over: function() {
		if (this.citylist) {
			this.citylist.addClassName('selected');
		}
		if (map) {
			this.marker.mouse_over();
			if (!this.on_map()) map.panTo(this.marker.marker().getPoint());
		}
	},
	mouse_out: function() {
		if (this.citylist) {
			this.citylist.removeClassName('selected');
		}
		if (map) this.marker.mouse_out();
	},
	on_click: function() {
		if (this.on_click_callback) this.on_click_callback(this);
		else document.location.href = this.data.href;
	}
};

function NoMarker(community) {
	this.community = community;
}
NoMarker.prototype = {
	mouse_over: function(val){},
	mouse_out: function(val){},
	rebuild: function(){}
}

function MarkerPlace(data, community) {
	this.community = community;
	this.tooltip = "<div class='markerTooltip'>"+community.data.name+"</div>";
	this.make_marker();
	this.initialize(data, community);
}
MarkerPlace.prototype = {
	initialize: function(data, community) {
		this.has_highlight = true;
		this.community = community;
		//GEvent.addListener(this.marker, "infowindowclose", function (){onInfoClose(this_lot);});
	},
	add_listeners: function() {
		var community = this.community;
		GEvent.addListener(this.marker(), "click", function (){ community.on_click(); scrollToCoordinates($('comm'+community.id).parentNode); });
		GEvent.addListener(this.marker(), "mouseover", function (){ community.mouse_over(); });
		GEvent.addListener(this.marker(), "mouseout", function (){ community.mouse_out(); });
	},
	rebuild: function() {
		map.removeOverlay(this.marker());
		this.make_marker();
	},
	mouse_over: function() {
		this.showTooltip();
	},
	mouse_out: function(){
		tooltip.style.visibility = "hidden";
		highlight.style.visibility = "hidden";
	},
	make_marker: function() {
		if (this.community.data.saved) {
			this._marker = new GMarker(new GLatLng(this.communtiy.data.latitude, this.community.data.longitude), icon_saved);
			
		} else {
			this._marker = new GMarker(new GLatLng(this.community.data.latitude, this.community.data.longitude), icon);
		}
		this.add_listeners();
		map.addOverlay(this.marker());
	},
	marker: function() { return this._marker }
}

var MarkerTooltip = {
	showTooltip: function() {
		marker = this.marker();
		tooltip.innerHTML = this.tooltip;
		var point = map.getCurrentMapType().getProjection().fromLatLngToPixel(map.fromDivPixelToLatLng(new GPoint(0,0),true),map.getZoom());
		var offset = map.getCurrentMapType().getProjection().fromLatLngToPixel(marker.getPoint(),map.getZoom());
		var anchor = marker.getIcon().iconAnchor;
		var width = marker.getIcon().iconSize.width;
		var height = tooltip.clientHeight;
		var pos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(offset.x - point.x - anchor.x + width, offset.y - point.y -anchor.y -height));
		var hpos = new GControlPosition(G_ANCHOR_TOP_LEFT, new GSize(offset.x - point.x - anchor.x - 20, offset.y - point.y -anchor.y - 20));
		
		pos.apply(tooltip);
		hpos.apply(highlight);
		tooltip.style.visibility = 'visible';
		if (this.has_highlight) {
			highlight.style.visibility = 'visible';
		}
	}
}

function onMap(point) {
    var bounds = mapBounds();
    if (bounds.minX <= point.x &&
        bounds.maxX >= point.x &&
        bounds.minY <= point.y && bounds.maxY >= point.y) {
        return true;
    }
    return false;
}
function mapBounds() {
    var bounds = map.getBounds();
    min_x = bounds.getSouthWest();
    min_x = min_x.lng();
    min_y = bounds.getSouthWest();
    min_y = min_y.lat();
    max_x = bounds.getNorthEast();
    max_x = max_x.lng();
    max_y = bounds.getNorthEast();
    max_y = max_y.lat();
    return {minX:min_x, maxX:max_x, minY:min_y, maxY:max_y};
}

Object.extend(MarkerPlace.prototype, MarkerTooltip);

if (typeof(GIcon) != 'undefined') {
	var icon = new GIcon();
	icon.image = u_url+'/modules/mls-communities/images/house.png';
	icon.shadow = u_url+'/modules/mls-communities/images/shadow.png';
	icon.iconSize = new GSize(20, 30);
	icon.shadowSize = new GSize(33, 30);
	icon.iconAnchor = new GPoint(10, 30);
	icon.infoWindowAnchor = new GPoint(13, 6);
	
	var icon_saved = new GIcon();
	icon_saved.image = u_url+'/modules/mls-communities/images/house_saved.png';
	icon_saved.shadow = u_url+'/modules/mls-communities/images/shadow_saved.png';
	icon_saved.iconSize = new GSize(27, 32);
	icon_saved.shadowSize = new GSize(33, 32);
	icon_saved.iconAnchor = new GPoint(10, 32);
	icon_saved.infoWindowAnchor = new GPoint(13, 15);
	
	var arrow = new GIcon();
	arrow.image = u_url+'/modules/mls-communities/images/arrow.png';
	arrow.shadow = u_url+'/modules/mls-communities/images/arrow_shadow.png';
	arrow.iconSize = new GSize(39, 34);
	arrow.shadowSize = new GSize(39, 34);
	arrow.iconAnchor = new GPoint(12, 34);
	arrow.infoWindowAnchor = new GPoint(12, 20);
}

function showGeoPoint(address) {
	geocoder.getLatLng(
		address,
		function(point) {
			if (point) {
				/*
				var baseIcon = new GIcon();
				baseIcon.image = "/img/orange.png";
				baseIcon.shadow = "http://www.google.com/mapfiles/shadow50.png";
				baseIcon.iconSize = new GSize(20, 34);
				baseIcon.shadowSize = new GSize(37, 34);
				baseIcon.iconAnchor = new GPoint(9, 34);
				baseIcon.infoWindowAnchor = new GPoint(9, 2);
				baseIcon.infoShadowAnchor = new GPoint(18, 25);
				baseIcon.transparent = "http://www.google.com/intl/en_ALL/mapfiles/markerTransparent.png";
				var icon = new GIcon(baseIcon);
				*/
				//var marker = new GMarker(point, baseIcon);
				var marker = new GMarker(point);
				map.panTo(point);
				map.clearOverlays();
				map.addOverlay(marker);
				//marker.openInfoWindowHtml(point);
			}
		}
	);
}

function showPoint(point) {
	if (map) {
		var marker = new GMarker(point);
		map.panTo(point);
		map.clearOverlays();
		map.addOverlay(marker);
	}
}

function scrollToCoordinates(child) {
	if (child) {
		var results_area = Math.round(Element.getHeight($(child.parentNode)) / 2);
		var result = Element.getHeight($(child));
		var offset;
		if (child.offsetParent == child.parentNode) {
			offset = child.offsetTop - results_area + result;
		} else {
			offset = child.offsetTop - child.parentNode.offsetTop - results_area + result;
		}
		child.parentNode.scrollTop = offset;
	}
}

Event.observe(window, 'unload', function() { if (typeof(GUnload) != 'undefined') GUnload(); }, false);
//gmap_setup = function() {
Event.observe(window, 'load', function() {
	//setTimeout(function() {
		if (typeof(GBrowserIsCompatible) != 'undefined') {
			if (GBrowserIsCompatible()) {
				geocoder = new GClientGeocoder();
				map = new GMap2($('map'));
				//map.addControl(new StatsOnMap());
				map.addControl(new GLargeMapControl());
				map.addControl(new GMapTypeControl());
				//map.addControl(new GScaleControl());
				map.enableScrollWheelZoom();
				
				tooltip = document.createElement("div");
				map.getPane(G_MAP_FLOAT_PANE).appendChild(tooltip);
				tooltip.style.visibility="hidden";
				highlight = document.createElement("div");
				map.getPane(G_MAP_MARKER_SHADOW_PANE).appendChild(highlight);
				highlight.style.visibility="hidden";
				//highlight.innerHTML='<img src="/?get&f=glow-trans.png" class="trans" style="behavior: url("+site_url+"/iepngfix.php);" width="60" height="60" />';
				if (Prototype.Browser.IE) highlight.innerHTML='<img src="'+u_url+'/modules/mls-communities/images/glow-trans.gif" width="60" height="60" />';
				else highlight.innerHTML='<img src="'+u_url+'/modules/mls-communities/images/glow-trans.png" width="60" height="60" />';
			}
		}
		
		if (window.gmap_loaded)
			gmap_loaded();
		
	//}, 1000);
}, false);
//}
