//<![CDATA[
var baseIcon = new GIcon();
var ixii = 0;
var j = 0;
var counter = 0;
var maximizeclickHandler = function() {};
var infowindowbeforecloseHandler = function() {};

resetUpImage = new Image();
resetUpImage.src = "/images/gm_icons/reset.gif";
resetDownImage = new Image();
resetDownImage.src = "/images/gm_icons/reset.gif";
resetOverImage = new Image();
resetOverImage.src = "/images/gm_icons/reset_on.gif";
searchUpImage = new Image();
searchUpImage.src = "/images/gm_icons/submit.gif";
searchDownImage = new Image();
searchDownImage.src = "/images/gm_icons/submit.gif";
searchOverImage = new Image();
searchOverImage.src = "/images/gm_icons/submit_on.gif";

baseIcon.iconSize = new GSize(24, 30);
baseIcon.shadowSize = new GSize(35, 20);
baseIcon.iconAnchor = new GPoint(11, 20);
baseIcon.infoWindowAnchor = new GPoint(11, 20);
baseIcon.infoShadowAnchor = new GPoint(11, 20);
//baseIcon.shadow = "/images/gm_icons/google_shadow.png";

function searchDown()
{
 document.images["search_button"].src = searchDownImage.src;
 removeMarkers();
 addMarkers();
 var quickcentre = document.getElementById('quickcentre').value;
 var recentre = document.getElementById('recentre').value;
 if (recentre != "Enter Full Address" && recentre != "") {
   var centreon = document.getElementById('recentre').value;
   showAddress(centreon);
 }
 if (quickcentre !="00") {
   var centreon = document.getElementById('quickcentre').value;
   showAddress(centreon);
 }
  poiMarkers();
 return true;
}

function searchUp()
{
 document.images["search_button"].src = searchUpImage.src;
 return true;
}

function resetDown() // Reset the searches and location of the map
{
 document.images["reset_button"].src = resetDownImage.src;
 document.forms['map_form'].reset();
 map.setCenter(new GLatLng(44.24913, -76.55274), 12);
 removeMarkers();
 addMarkers();
 return true;
}

function resetUp()
{
 document.images["reset_button"].src = resetUpImage.src;
 return true;
}

function createMarker(ixii, point, html_text, status) {
	baseIcon.image = "/images/gm_icons/google_all.png";
	if(status == 'Sat') {						
		baseIcon.image = "/images/gm_icons/google_sat.png";
	}
	if(status == 'Sun') {
		baseIcon.image = "/images/gm_icons/google_sun.png";
	}
	if(status == 'Mon') {
		baseIcon.image = "/images/gm_icons/google_mon.png";
	}
	if(status == 'Tue') {
		baseIcon.image = "/images/gm_icons/google_tue.png";
	}
	if(status == 'Wed') {
		baseIcon.image = "/images/gm_icons/google_wed.png";
	}
	if(status == 'Thu') {
		baseIcon.image = "/images/gm_icons/google_thu.png";
	}
	if(status == 'Fri') {
		baseIcon.image = "/images/gm_icons/google_fri.png";
	}
	var icon = new GIcon(baseIcon);		
	var marker = new GMarker(point, icon);	

	GEvent.addListener(marker, "click", function() {
		var smallNode = document.createElement('div');
		smallNode.style.width = '320px';
		smallNode.style.height = '240px';
		smallNode.id = 'pano';
		document.body.appendChild(smallNode);
		
		var tab1 = new GInfoWindowTab('Details', html_text);
		var tab2 = new GInfoWindowTab('Street View', smallNode);
					  
		marker.openInfoWindowTabsHtml([tab1,tab2]);
		makePanorama(smallNode, marker);
		
	  // Register event listeners to marker and info window.
	  GEvent.addListener(map.getInfoWindow(), "maximizeclick", function() {
		maximizeclickHandler();
	  });
	  GEvent.addListener(marker, "infowindowbeforeclose", function() {
		infowindowbeforecloseHandler();
	  });		
		
	});	
	return marker;
}

function makePanorama(smallNode, marker) {
		var panorama;
		panorama = panorama || new GStreetviewPanorama(smallNode);
		panorama.setContainer(smallNode);
		panorama.setLocationAndPOV(marker.getLatLng(), null);

// Update handlers for info window and marker event listeners.
  maximizeclickHandler = function() {
    panorama.remove();
  };
  infowindowbeforecloseHandler = function() {
    panorama.remove();
  };
}

function createPoiMarker(j, point, poi_html, name, status) {
	baseIcon.image = "/images/gm_icons/poi.png";
	if(status == 'Education') {
		baseIcon.image = "/images/gm_icons/google_edu.png";
	}
	if(status == 'Shopping') {
		baseIcon.image = "/images/gm_icons/google_shop.png";
	}
	if(status == 'Government') {
		baseIcon.image = "/images/gm_icons/google_gov.png";
	}
	if(status == 'Religious') {
		baseIcon.image = "/images/gm_icons/google_rel.png";
	}
	if(status == 'Busses') {
		baseIcon.image = "/images/gm_icons/google_bus.png";
	}
	if(status == 'Movies') {
		baseIcon.image = "/images/gm_icons/google_mov.png";
	}
	if(status == 'Train') {
		baseIcon.image = "/images/gm_icons/google_trans.png";
	}
	if(status == 'Hospital') {
		baseIcon.image = "/images/gm_icons/google_hosp.png";
	}
	if(status == 'Restaurant') {
		baseIcon.image = "/images/gm_icons/google_restaurant.png";
	}
	if(status == 'Lodging') {
		baseIcon.image = "/images/gm_icons/google_lodge.png";
	}
	if(status == 'Arena') {
		baseIcon.image = "/images/gm_icons/google_arena.png";
	}
	if(status == 'LCBO') {
		baseIcon.image = "/images/gm_icons/google_lcbo.png";
	}
	if(status == 'Beer Store') {
		baseIcon.image = "/images/gm_icons/google_beer.png";
	}
	if(status == 'Bank') {
		baseIcon.image = "/images/gm_icons/google_bank.png";
	}
	if(status == 'Provincial Park') {
		baseIcon.image = "/images/gm_icons/google_park.png";
	}
	if(status == 'Golf Course') {
		baseIcon.image = "/images/gm_icons/google_golf.png";
	}
	if(status == 'Pharmacy') {
		baseIcon.image = "/images/gm_icons/google_rx.png";
	}
	if(status == 'Coffee') {
		baseIcon.image = "/images/gm_icons/google_coffee.png";
	}
	if(name == 'Tim Hortons') {
		baseIcon.image = "/images/gm_icons/google_tims.png";
	}
	if(status == 'Police') {
		baseIcon.image = "/images/gm_icons/google_poli.png";
	}
	if(status == 'Fire') {
		baseIcon.image = "/images/gm_icons/google_fire.png";
	}
	if(status == 'Eatery') {
		baseIcon.image = "/images/gm_icons/google_eatery.png";
	}
	if(status == 'Grocery') {
		baseIcon.image = "/images/gm_icons/google_grocery.png";
	}
	if(status == 'Gas Bar') {
		baseIcon.image = "/images/gm_icons/google_gas.png";
	}
	if(name == 'Canadian Tire') {
		baseIcon.image = "/images/gm_icons/google_ct.png";
	}
	if(name == 'McDonald\'s') {
		baseIcon.image = "/images/gm_icons/google_mc.png";
	}
	var icon = new GIcon(baseIcon);
	var marker = new GMarker(point, icon);
	GEvent.addListener(marker, "click", function() {
				marker.openInfoWindowHtml(poi_html);
	});	
	return marker;
}

function getPopupHtml(ixii, img_src, beds, address, price, baths, relocation_string) {
	var html = 	'<table width="320" border="0"  cellpadding="2" cellspacing="0">' +
					'<tr>' +
						'<td align="left"><img src="' + img_src + '" alt="" width="160" height="120" \/><\/td>' +
						'<td align="right">' +	price + '<br \/>' +	beds + '<br \/>' + baths + '<\/td>' +
					'<\/tr>' +
					'<tr>' +						
						'<td colspan="2" align="center">' +
						'<b><nobr>' +
							address	+				
						'<\/nobr><\/b><br \/>' +
						'<a href="' + relocation_string + '" ><b><nobr>' +
							'More Pictures and Full Listing' +
						'<\/nobr><\/b><\/a>' +
						'<\/td>' +
					'<\/tr>' +
				'<\/table>';
	return html;
}

function getStreetView() {
	var street_view = 	'<div name="pano" id="pano" style="width: 320px; height: 240px">COMING VERY SOON!</div>';
	return street_view;
}

function handleNoFlash(errorCode) {
  if (errorCode == FLASH_UNAVAILABLE) {
	alert("Error: Flash doesn't appear to be supported by your browser");
	return;
  }
}  

function getPoiPopupHtml(j, img_src, name, address, description, lat, long) {
	var poi_html = 	'<table width="220" border="0"  cellpadding="2" cellspacing="0">' +
						'<tr>' +
							'<td align="left"><img src="' + img_src + '" alt="" width="100" height="75" \/><\/td>' +
							'<td align="right">' +	name + '<\/td>' +
						'<\/tr>' +
						'<tr>' +						
							'<td colspan="2"><p class="justify">' +
							description + '<br />' + lat + ', ' + long + '<\/p>' +
							'<p class="centered"><b><nobr>' +
								address	+				
							'<\/nobr><\/b><\/p>' +
							'<\/td>' +						
						'<\/tr>' +	
				'<\/table>';
	return poi_html;
}

function isInRange(value, min, max) {
	var temp_val = parseInt(value);
	var temp_min = parseInt(min);
	var temp_max = parseInt(max);		
	if(temp_val <= temp_max && temp_val >= temp_min) { 
		return true;
	}
	else {
		return false;
	}			
}

function dayIsGood(actual_value, from_form) {
	var temp_act_val = actual_value;
	var temp_form_value = from_form;
	if(temp_form_value == 'All')
		return true;
	else if(temp_act_val == temp_form_value)
		return true;
	else if(temp_form_value == '') {
		if(temp_act_val == 'Mon')
			return true;
		if(temp_act_val == 'Tue')
			return true;
		if(temp_act_val == 'Wed')
			return true;
		if(temp_act_val == 'Thu')
			return true;
		if(temp_act_val == 'Fri')
			return true;
		if(temp_act_val == 'Sat')
			return true;
		if(temp_act_val == 'Sun')
			return true;			
	}
	else
		return false;
}

function addMarkers() {
	if(document.getElementById('loaddiv').style.visibility == 'hidden')
		document.getElementById('loaddiv').style.visibility = 'visible';
	if ( ixii < num_props ) {
		var max = num_props;
		// Add properties
		while ( ixii < max) {
			//grab all the variables from the map form
			//property type needs to be worked on
			var for_day = document.getElementById('for_day').value;
			var price_min = document.getElementById('price_min').value;
			var price_max = document.getElementById('price_max').value;
			var bedrooms_min = document.getElementById('bedrooms_min').value;
			var baths_min = document.getElementById('baths_min').value;
			//if no price is known be careful not to show the property when a search is done
			if(dayIsGood(props[ixii][10], for_day)) {
			var tempPrice = props[ixii][9];
			if(tempPrice == '')
				tempPrice = 0;
			if(isInRange(tempPrice,price_min,price_max)) {
				//check the bedrooms							
				//if no beds are known be careful not to show the property when a search is done
				var tempBeds = props[ixii][3];
				if(tempBeds == '')
					tempBeds = 0;
				if(parseInt(tempBeds) >= parseInt(bedrooms_min)) {
					//if no baths are known be careful not to show the property when a search is done
					var tempBaths = props[ixii][4];
					if(tempBaths == '')
						tempBaths = 0;								
					//check the baths
					if(parseInt(tempBaths) >= parseInt(baths_min)) {
						//WE PASSED ALL THE SEARCH REQUIREMENTS	
						//0: listingsdb_id, 1:lon, 2:lat, 3:beds, 4:baths, 5:price, 6:image_url, 7:sq_feet, 8:address 9:price_number 10:Open House Date
						var img_src = '';
						//image source for popup
						if (props[ixii][6].length>0){
							img_src = props[ixii][6];
						} 
						else {
							img_src = "/images/nophoto.gif";
						}
						//beds
						var beds = "";
						var baths = "";
						if(props[ixii][3] == 1)
							beds = props[ixii][3] + " bed";
						else
							beds = props[ixii][3] + " beds";
						if(props[ixii][3] == '')
							beds = '';
						if (props[ixii][4] == 1) {
							baths = props[ixii][4] + " bath";
						}
						else {
							baths = props[ixii][4] + " baths";
						}
						if(props[ixii][4] == '')
							baths = '';
						//address 
						var address = props[ixii][8];
						//price
						var price = props[ixii][5];
						if(props[ixii][5] == '$0')
							price = '';
						//preload all popup images so they load fast/correctly
						//problems with firefox if this is not done
						arImages[ixii] = new Image();
						arImages[ixii].src = img_src;
						var prop_id = props[ixii][0];	
						var relocation_string = "/index.php?action=listingview&amp;listingID=" + prop_id + "&PageID=97";
						//get different text for houses/condos, land, and commercial
						var html_text = "";
						street_view = getStreetView();
						html_text = getPopupHtml(ixii, img_src, beds, address, price, baths, relocation_string);
						map.addOverlay(createMarker(ixii, new GLatLng(props[ixii][1], props[ixii][2]), html_text, props[ixii][10]), ixii);	
						counter++;
						//update the loading message
						if(ixii == 1) {
							document.getElementById('loaddiv').innerHTML = "Loading " + max + " of " + num_props;
						}
						else {
							document.getElementById('loaddiv').innerHTML = "Found " + counter + " out of " + max + " searched";	
						}
						//GLog.write("After addOverlay call " + i);
					}//Check baths
				}//Check beds
			}//Check price
			}
		ixii++;
		}//add all the properties that satisfy the tests
		//recursive call with delay
		window.setTimeout(addMarkers,5);
	}//if i < num_props
	else {
		document.getElementById('loaddiv').style.visibility='hidden';
		ixii = 0;	
		counter = 0;
	}
	counter = 0;
}//function addMarkers

function poiMarkers() {
	var show_poi = document.getElementById('show_poi').value;
	if ( j < num_pois ) {
		var max = num_pois;
		// Add properties
		while ( j < max) {
			if (show_poi == pois[j][6] || show_poi == "All") {
			//0: poi_id, 1:longitude, 2:latitude, 3:name, 4:address, 5:description, 6:type
			//image source for popup
			if (pois[j][0].length>0){
				var img_src = "/images/poi/" + pois[j][0] + ".gif";
			} 
			else {
				var img_src = "/images/poi/nophoto.gif";
			}
			//name
			var name = pois[j][3];
			//address 
			var address = pois[j][4];
			//description
			var description = pois[j][5];
			//preload all popup images so they load fast/correctly
			//problems with firefox if this is not done
			arImages[j] = new Image();
			arImages[j].src = img_src;
			var prop_id = pois[j][0];
			//get different text for houses/condos, land, and commercial
			var poi_html = "";
			poi_html = getPoiPopupHtml(j, img_src, name, address, description, pois[j][2], pois[j][1]);
			map.addOverlay(createPoiMarker(j, new GLatLng(pois[j][2], pois[j][1]), poi_html, name, pois[j][6]), j);	
			counter++;
			//update the loading message
			if(j == 1) {
				document.getElementById('loaddiv').innerHTML = "Loading " + max + " of " + num_pois;
			}
			else {
				document.getElementById('loaddiv').innerHTML = "Found " + counter + " out of " + max + " searched";	
			}
			}
			//GLog.write("After addOverlay call " + i);
		j++;
		}//add all the properties that satisfy the tests
		//recursive call with delay
		window.setTimeout(poiMarkers,5);
	}//if i < num_props
	else {
		document.getElementById('loaddiv').style.visibility='hidden';
		j = 0;	
		counter = 0;
	}
	counter = 0;
}//function poiMarkers

function clearDefault(el) {
if (el.defaultValue==el.value) el.value = ""
}

function showAddress(centreon) {
	if (centreon != "Enter Full Address") {
    var geocoder = null;
	geocoder = new GClientGeocoder();
  if (geocoder) {
	geocoder.getLatLng(
	  centreon,
	  function(point) {
		if (!point) {
		  alert(centreon + " not found");
		} else {
		  map.setCenter(point);
		}
	  }
	);
  }
	}
}

function createMap(){
	map = new GMap2(document.getElementById("map"));
	map.setCenter(new GLatLng(44.24913, -76.55274), 12);
	map.setUIToDefault();
	map.enableGoogleBar();
	addMarkers();
}//end function createMap()

function removeMarkers() {
	map.clearOverlays();
}

function loader(){
	createMap();
}
function unloader(){
	GUnload();	
}

window.onload = loader;
window.onunload = unloader;

function currentMapTypeNumber(map){
	var type=-1;
	for(var ix=0; ix<map.getMapTypes().length; ix++){
		if(map.getMapTypes()[ix]==map.getCurrentMapType())
		type=ix;
	}
	return type;
}//end function currentMapTypeNumber(map)
//]]>