// 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.execute = function() 
{
	var cnt = this.pending.length;
	
    for( var i=0; i<cnt; i++ ) 
        this.pending[i](); 
} 



function map_load()
{
	search_box_toggle(); // Prevents the double click on start-up
	
	if (GBrowserIsCompatible())
	{
		map = new GMap2(document.getElementById("map"));
		map.setCenter(new GLatLng(43.411564, -80.392261), 11); // 43.382914, -80.320308
		
		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 search_box_toggle()
{
	var search_box = document.getElementById('search_fields');
	var property_nav = document.getElementById('property_nav');

	if(search_box.style.display == "none")
	{
		search_box.style.display = "block";
		property_nav.style.height = "255px";
	}
	else
	{
		search_box.style.display = "none";
		property_nav.style.height = "430px";
	}
}

// page click event 
function page_click(page, show)
{
	document.pagination.ListingPage.value = page;	
	document.pagination.submit();
}

// show on change event
function change_show(show)
{
	document.pagination.ListingShow.value = show;
	document.pagination.submit();
}

//]]>