// JavaScript Document
//<![CDATA[
var map = null;
var fsIcon = null;
var flIcon = null;
var bounds;
var buffer = [];
var listings = [];
var selected_listing = null;
var buffer = new Fnarray();


function Fnarray()
{
	this.pending = [];	
}

Fnarray.prototype.add = function(f)
{
    if(typeof f!= "function") 
        f = new function(f){};

    this.pending[this.pending.length] = f; 	
}

Fnarray.prototype.clear = function(f)
{
	this.pending.length = 0;
}

Fnarray.prototype.count = function()
{
	return this.pending.length;
}

Fnarray.prototype.execute = function() 
{
	var cnt = this.pending.length;
	
    	for( var i=0; i<cnt; i++ ) 
       	this.pending[i](); 
} 



function map_load()
{
	toggle_search_box(); // Prevents the double click on start-up
	
	if (GBrowserIsCompatible())
	{
		map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(43.382914, -80.320308), 13);
		
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GScaleControl());
		map.enableDoubleClickZoom();
		
		bounds = new GLatLngBounds();
		init_icons();
		
		GEvent.addDomListener(document.getElementById("map"), "mousewheel",     wheelZoom); // IE			
		GEvent.addDomListener(document.getElementById("map"), "DOMMouseScroll", wheelZoom); // Firefox
		
		buffer.execute();
		
		map.setZoom(map.getBoundsZoomLevel(bounds));
	
		 // ===== determine the centre from the bounds ======
		var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2;
		var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2;
		map.setCenter(new GLatLng(clat,clng));
	}
	else
	{
		alert('Internet browser is not compatible with Google Maps.');	
	}
	
}

function init_icons()
{
	fsIcon = new GIcon();
	fsIcon.image = "http://cdn1.mahalomarketing.ca/common/img/prud_sign.png";
	fsIcon.shadow = "";
	fsIcon.iconSize = new GSize(38, 50);
	fsIcon.shadowSize = new GSize(63, 28);
	fsIcon.iconAnchor = new GPoint(18, 15);
	fsIcon.infoWindowAnchor = new GPoint(45, 2);
	
	flIcon = new GIcon();
	flIcon.image = "http://cdn1.mahalomarketing.ca/common/img/prud_sign.png";
	flIcon.shadow = "";
	fsIcon.iconSize = new GSize(38, 50);
	flIcon.shadowSize = new GSize(63, 28);
	flIcon.iconAnchor = new GPoint(18, 15);
	flIcon.infoWindowAnchor = new GPoint(45, 2);	
}

// Use the mouse wheel for zooming and zooming out
function wheelZoom(a)
{
	if(a.detail) // Firefox
	{
		if(a.detail < 0)
			map.zoomIn();
		else if(a.detail > 0)
			map.zoomOut();
	}
	else if(a.wheelDelta) // Internet Explorer
	{
		if(a.wheelDelta > 0)
			map.zoomIn();
		else if(a.wheelDelta < 0)
			map.zoomOut();
	}
}

// Create the listing marker
function create_listing(index, latitude, longitude, listing_id, listing_status)
{
	var point 	= new GLatLng(latitude, longitude);
	var marker 	= null;
	
	if(listing_status == 'FL')
		marker = new GMarker(point, flIcon);
	else
		marker = new GMarker(point, fsIcon);
	
	listings[index] = marker;
	GEvent.addListener(marker, "click", function(){ listing_click(index, listing_id); });
	
	map.addOverlay(marker);
	bounds.extend(point);
	
	return marker;
}

// Listing click event
function listing_click(index, listing_id)
{
	selected_listing = listings[index];
			 
	var pars = '';
	var myAJAX = new Ajax.Request('/listings/ballon/' + listing_id, 
		{
			method: 'get',
			parameters: pars,
			onComplete: show_ballon
		});	
}

// Show listing ballon
function show_ballon(html)
{
	selected_listing.openInfoWindowHtml(html.responseText);	
}


function hide_div(div_id)
{
	var el = document.getElementById(div_id);

	if(el.style.display == "none")
		el.style.display = "block";
	else
		el.style.display = "none";
}

function toggle_search_box()
{
	var search_box = document.getElementById('fields');

	if(search_box.style.display == "none")
	{
		search_box.style.display = "block";
		$('properties_list').style.height = "260px";
	}
	else
	{
		search_box.style.display = "none";
		$('properties_list').style.height = "450px";
	}
}


function show_listing_progress()
{
	$('properties_table').style.display = 'none';
	$('properties_progress').style.display = 'block';
}

function show_ballon_progress()
{
	$('ballon_content').style.display = 'none';
	$('ballon_progress').style.display = 'block';
}

function page_click(page)
{
	$('current_page').value = page;
	map_update();
}

function limit_change(limit)
{
	$('display_limit').value = limit;
	map_update();	
}

function map_update()
{
	map = null;
	buffer.clear();

	var myAJAX = new Ajax.Updater('map_main','/listings/index',
				 {
				 	asynchronous: true,
				 	evalScripts: true,
					parameters: Form.serialize('PaginationForm'),
					requestHeaders: ['X-Update', 'map_main'],
					onLoading: show_listing_progress
				 });


}

function map_refresh()
{
	if (GBrowserIsCompatible())
	{
		map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(43.382914, -80.320308), 13);
		
		map.addControl(new GLargeMapControl());
		map.addControl(new GMapTypeControl());
		map.addControl(new GScaleControl());
		map.enableDoubleClickZoom();
		
		bounds = new GLatLngBounds();
		init_icons();
		
		GEvent.addDomListener(document.getElementById("map"), "mousewheel",     wheelZoom); // IE			
		GEvent.addDomListener(document.getElementById("map"), "DOMMouseScroll", wheelZoom); // Firefox
		
		buffer.execute();
		
		map.setZoom(map.getBoundsZoomLevel(bounds));
	
		 // ===== determine the centre from the bounds ======
		var clat = (bounds.getNorthEast().lat() + bounds.getSouthWest().lat()) /2;
		var clng = (bounds.getNorthEast().lng() + bounds.getSouthWest().lng()) /2;
		map.setCenter(new GLatLng(clat,clng));
	}
	else
	{
		alert('Internet browser is not compatible with Google Maps.');	
	}

}

//]]>