ImagePopup.init();
var ieBrowser = /undefined/.test(typeof(addEventListener));
var map = null;
var newRedIcon = null;
function load()
		{if (GBrowserIsCompatible())
				{map = new GMap2(document.getElementById("WorldMap"));
				map.addControl(new GLargeMapControl());
				map.addControl(new GMapTypeControl());
				map.addControl(new GOverviewMapControl());
				var center = new GLatLng(54.470038,14.238281)//GLatLng(43.64790, -79.56021);
				map.setCenter(center, 4);
				map.enableScrollWheelZoom(true)
				newRedIcon = new GIcon(); 
				newRedIcon.image = '/sharedimages/GoogleMaps/remax_balloon_new.png';
				newRedIcon.shadow = '/sharedimages/GoogleMaps/remax_balloon_shadow_new.png';
			newRedIcon.iconSize = new GSize(21, 29);
				newRedIcon.shadowSize = new GSize(25, 35);
				newRedIcon.iconAnchor = new GPoint(6, 20);
				newRedIcon.infoWindowAnchor = new GPoint(5, 1);

				GMarker.prototype.OverLayAdded = false;
				GMarker.prototype.RowElement = null;
				GMarker.prototype.PropertyListingInfoPopUp = function()
						{this.openInfoWindowHtml(this.RowElement.HTMLBubble());

						if (this.RowElement != this.RowElement.parentElement.ActiveRow)
								{this.RowElement.parentElement.setActiveRow(this.RowElement)
								}

						if (ScrollObject != null)
								ScrollObject.Destroy = true// IT IS CURRENTLY SCROLLING TO A LISTING
						ScrollObject = new ScrollObjectFNC(document.getElementById('ListingsDiv'))
						ScrollObject.EndPoint = this.RowElement.DOMRow.parentNode.offsetTop
						ScrollObject.Scroll()
						}

				}
		setTimeout('MapInitialized()',0)
		}
if (ieBrowser)
		{attachEvent("onload",function(){ListingsDisplayed();load();InitializeMapArray()})
		attachEvent("onunload",function(){GUnload()})
		}
else{addEventListener("load", function(){ListingsDisplayed();load();InitializeMapArray()}, true)
		addEventListener("onunload",function(){GUnload()}, true)
		}

function ScrollObjectFNC(ScrollDiv)
{this.ScrollDiv = ScrollDiv
this.SetDivTop = function(TopPos){this.ScrollDiv.scrollTop = TopPos}
this.EndPoint = null;
this.Pause = 40
this.Speed = .10
this.Destroy = false
this.Scroll = function()
		{this.InternalRef = this;
		if (this.InternalRef.Destroy)
				return;
		var ScrollSpeed = parseInt((this.EndPoint-this.ScrollDiv.scrollTop)*this.InternalRef.Speed)
		if (this.ScrollDiv.scrollTop > this.EndPoint)
				{if (this.ScrollDiv.scrollTop == 0)
						return;
				if (ScrollSpeed > -1)
						ScrollSpeed = -1
				if (this.ScrollDiv.scrollTop + ScrollSpeed <= this.EndPoint)
						{this.ScrollDiv.scrollTop = this.EndPoint;
						return;
						}
				}
		else
				{if (this.ScrollDiv.scrollTop + this.ScrollDiv.clientHeight == this.ScrollDiv.scrollHeight)
						return;
				if (ScrollSpeed < 1)
						ScrollSpeed = 1
				if (this.ScrollDiv.scrollTop + ScrollSpeed >= this.EndPoint)
						{this.ScrollDiv.scrollTop = this.EndPoint;
						return;
						}
				}
		this.ScrollDiv.scrollTop += ScrollSpeed;
		setTimeout(function(InternalRef)
										{return function()
																{if (InternalRef.Destroy)
																		return;
																return InternalRef.Scroll()
																}
										}(this.InternalRef)
								,this.InternalRef.Pause)
		}
}

var ScrollObject = null;

var MapSignal = {StartIndex:0,
								EndIndex:0,
								TranserSize:10,
								TranserPause:100,
								Rows:new Array(),
								ActiveRow:null,
								setActiveRow:function(MapRow)
														{parentElement = MapRow.parentElement
														if (parentElement.ActiveRow != null)
																{parentElement.ActiveRow.DOMRow.setAttribute("PrevColorVal","");
																parentElement.ActiveRow.DOMRow.className = "gm_list";
																}
														parentElement.ActiveRow = MapRow;
														parentElement.ActiveRow.DOMRow.setAttribute("PrevColorVal","gm_list_on");
														parentElement.ActiveRow.DOMRow.className = parentElement.ActiveRow.DOMRow.getAttribute("PrevColorVal");
														}
								}

function InitializeMapArray()
{MapProxyLoaded = true;
InitializeObjects();
}

var MarkerMngr = null;
var AllMarkers = null;

function BroadcastStart()
{if (MapSignal.Rows.length == 0)
		return;
var ReInit = false;
MapSignal.EndIndex = MapSignal.StartIndex + MapSignal.TranserSize

if (MapSignal.EndIndex > MapSignal.Rows.length)
		{ReInit = true;
		MapSignal.EndIndex = MapSignal.Rows.length;
		}

if (MapSignal.StartIndex == 0)
		{map.clearOverlays();
		//MarkerMngr = new MarkerManager(map);
		AllMarkers = new Array()
		}

var BufLat = parseFloat(MapSignal.Rows[MapSignal.StartIndex].Latitude)
var BufLong = parseFloat(MapSignal.Rows[MapSignal.StartIndex].Longitude)
var ListingCo_ords = new GLatLng(BufLat < -90 || BufLat > 90?0:BufLat, BufLong < -90 || BufLong > 90?0:BufLong);
if (MapSignal.StartIndex == 0)
		{InitPoints = BufLat + "," + BufLong
		map.setCenter(ListingCo_ords, 10);
		}

var marker = new GMarker(ListingCo_ords, newRedIcon);  

marker.RowElement = MapSignal.Rows[MapSignal.StartIndex];
GEvent.addListener(marker, "click", marker.PropertyListingInfoPopUp);

AllMarkers[AllMarkers.length] = marker;
MapSignal.Rows[MapSignal.StartIndex].Marker = marker;
for (var x = MapSignal.StartIndex +  1;x<MapSignal.EndIndex;x++)
		{BufLat = parseFloat(MapSignal.Rows[x].Latitude)
		if (BufLat < -90 || BufLong > 90)
				continue;
		BufLong = parseFloat(MapSignal.Rows[x].Longitude)    
		if (BufLong < -90 || BufLong > 90)
				continue;
		var ListingCo_ords2 = new GLatLng(BufLat, BufLong);
		var marker2 = new GMarker(ListingCo_ords2, newRedIcon);
		marker2.RowElement = MapSignal.Rows[x];
		GEvent.addListener(marker2, "click", marker2.PropertyListingInfoPopUp);
		MapSignal.Rows[x].Marker = marker2;
		AllMarkers[AllMarkers.length] = marker2;
		}

if (MapSignal.EndIndex < MapSignal.Rows.length)
		{document.getElementById("MapLoad").innerHTML = "Map Initialization:"+parseInt((MapSignal.EndIndex/MapSignal.Rows.length) * 100)+"%"
		MapSignal.StartIndex = MapSignal.EndIndex;
		setTimeout('BroadcastStart()',MapSignal.TranserPause)
		}
else{MapSignal.StartIndex = 0;
		GEvent.addListener(map, 'moveend', ShowMarkers)
		ShowMarkers();
		document.getElementById("MapLoad").innerHTML = " "
		}
}

function ShowMarkers()
{var Bounds = map.getBounds();
var NorthEast = Bounds.getNorthEast();
var SouthWest = Bounds.getSouthWest();
//var TotalOverLays = new Array();
//var BufMarkerMngr = new MarkerManager(map);
for (var x = 0; x < AllMarkers.length; x++)
		{var Xpos = AllMarkers[x].getPoint().x;
		var Ypos = AllMarkers[x].getPoint().y;
		if (Xpos > SouthWest.lng() && Xpos < NorthEast.lng() &&
				Ypos > SouthWest.lat() && Ypos < NorthEast.lat())
				{if (!AllMarkers[x].OverLayAdded)
						{AllMarkers[x].OverLayAdded = true;
						//TotalOverLays[x] = AllMarkers[x];
						map.addOverlay(AllMarkers[x]);
						}
				}
		else{AllMarkers[x].OverLayAdded = false;
				map.removeOverlay(AllMarkers[x]);
				}
		}
}

function InitializePoints()
{MarkerMngr.addMarkers(AllMarkers,13,17)
MarkerMngr.refresh();
}

var MapProxyLoaded = false;
var MapLoaded = false;
var ListingsLoaded = false;

function MapInitialized()
{MapLoaded = true;
InitializeObjects();
}

function RecursiveFind(StartOBJ, OBJ_ID)
{if (StartOBJ.getAttribute("id") == OBJ_ID)
		return StartOBJ;
var cLength = StartOBJ.children.length;
var OBJFound = null;
if (StartOBJ.children[x].children.length > 0)
		{return RecursiveFind(StartOBJ.children[x], OBJ_ID);
		}
StartOBJ = StartOBJ.nextSibling
while(StartOBJ)
		{if (StartOBJ.getAttribute("id") == OBJ_ID)
				return StartOBJ;

		if (StartOBJ.children[x].getAttribute("id") == OBJ_ID)
				{return StartOBJ.children[x];
				}
		StartOBJ = StartOBJ.nextSibling
		}

return null;
//OBJ_ID
}

function ListingsDisplayed()
{
  var BufDivs = document.getElementsByName("ssg");  
  var DivLength = BufDivs.length
  
  for (var x = 0;x<DivLength;x++)
  {
    var CurDiv = BufDivs[x].nextSibling;
    var BufCurRow = CurDiv.parentNode;

    while(BufCurRow != null && BufCurRow.getAttribute("RT") == null)
    {
      BufCurRow = BufCurRow.parentNode;
    }

    if (ieBrowser)
    {
      BufCurRow.onmouseover = function(){ListingRowOver(this);}
      BufCurRow.onmouseout = function(){ListingRowOut(this);}
      BufCurRow.onclick = function(){ListingRowClicked(this);}
    }
    else
    {
      BufCurRow.removeAttribute("onmouseover");
      BufCurRow.removeAttribute("onmouseout");
      BufCurRow.removeAttribute("onclick");
      BufCurRow.className = "";
      BufCurRow.addEventListener("mouseover",function(){ListingRowOver(this);}, true);
      BufCurRow.addEventListener("mouseout",function(){ListingRowOut(this);}, true);
      BufCurRow.addEventListener("click",function(){ListingRowClicked(this);}, true);
    }

    var MapSignalIndex = MapSignal.Rows.length;
    MapSignal.Rows[MapSignalIndex] =
																	  {
																	    parentElement:MapSignal,
																	    DOMRow:BufCurRow,
																	    GoogleRow:CurDiv,
																	    HTMLBubble: function()
																	    {
																	      var AllSubSpans = this.GoogleRow.getElementsByTagName("span");
																	      var BufType = null;
																	      var SpanLength = AllSubSpans.length;
																	      var BufOBJ = null;
																	      for (var y = 0;y<SpanLength;y++)
																	      {
																	        BufType = AllSubSpans[y].getAttribute("MapRowIndex");
                                          if (BufType != null && BufType.length > 0)
                                          {
                                            BufOBJ = document.getElementById(CurDiv.getAttribute(BufType));
                                            if (BufOBJ != null)
                                            {
                                              AllSubSpans[y].innerHTML = BufOBJ.innerHTML;
                                              if (AllSubSpans[y].className == "")
                                              {
                                                AllSubSpans[y].className = BufOBJ.className;
                                              }
                                            }
                                          }
                                        }
																		    this.HTMLBubbleSet = true;
																		    return this.GoogleRow.innerHTML
																	    },
		                                  HTMLBubbleSet:false,                                        
		                                  ListingKey:CurDiv.getAttribute("ListingKey"),
		                                  Longitude:CurDiv.getAttribute("Longitude"),
		                                  Latitude:CurDiv.getAttribute("Latitude"),
		                                  Marker:null
		                                }
	  MapSignal.Rows[MapSignalIndex].HTMLBubble();
	  MapSignal.Rows[MapSignalIndex].HTMLBubble = function()
    {
      return this.GoogleRow.innerHTML;
    }
	  BufCurRow.setAttribute("MapRowIndex", MapSignal.Rows.length - 1);
	  BufCurRow.setAttribute("MouseIsOver", false);
	  BufCurRow.setAttribute("MouseIsOut", false);
	  BufCurRow.setAttribute("className", "gm_list");
  }

  document.getElementById("MapLoad").innerHTML = "Map Initialization:0%"
  ListingsLoaded = true;
  InitializeObjects();
}

function InitializeObjects()
{if (MapLoaded && MapProxyLoaded && ListingsLoaded)
		{BroadcastStart();
		}
}

function ListingRowOver(Row)
{Row.setAttribute("MouseIsOver", true)
Row.setAttribute("MouseIsOut", false)
Row.setAttribute("PrevColorVal",Row.className)
Row.className = "gm_list_on";
}

function ListingRowOut(Row)
{Row.setAttribute("MouseIsOver", false)
Row.setAttribute("MouseIsOut", true)
Row.className = Row.getAttribute("PrevColorVal");
}

function ListingRowClicked(Row)
{MapRow = MapSignal.Rows[Row.getAttribute("MapRowIndex")];
if (!MapRow.Marker.OverLayAdded)
		{MapRow.Marker.OverLayAdded = true;
		map.addOverlay(MapRow.Marker);
		}
MapRow.Marker.PropertyListingInfoPopUp();
MapRow.parentElement.setActiveRow(MapRow);
}

function SetMinMaxPriceRange(obj)
		{	 
		//	alert(obj.value);
			//alert(ddlMinPrice.selectedIndex);
			
			//if (obj.value == 'For Rent/Lease' || obj.value == 'Holiday/Short Term Rental')
			if (obj.value == 260 || obj.value == 262)
			{
					if (document.all[divPrice] != null)
					{
						document.all[divPrice].style.visibility = "hidden";
						document.all[divPrice].style.display = "none";
					}
					if (document.all[divRentalPrice] != null)
					{
						document.all[divRentalPrice].style.visibility = "visible";
						document.all[divRentalPrice].style.display = "";  
					}
					ddlMinPrice.selectedIndex = 0;
					ddlMaxPrice.selectedIndex = 0;
			}   
			else
			{
					if (document.all[divPrice] != null)
					{
						document.all[divPrice].style.visibility = "visible";
						document.all[divPrice].style.display = "";
					}
					if (document.all[divRentalPrice] != null)
					{
						document.all[divRentalPrice].style.visibility = "hidden"; 
						document.all[divRentalPrice].style.display = "none";
					}
					ddlRentalMinPrice.selectedIndex = 0;
					ddlRentalMaxPrice.selectedIndex = 0;
			}
		}
		
	function AddRemoveFavourite(obj, Add)
	{
		if (Add)
		{
			SmartModuleMyRemaxRegistration.SmartModuleMyRemaxFavourites.AddPropertyToFavourites(obj, maxFavs, cookieExp, langMF, encLangMF, formatRegionId);
		}
		else
		{
			SmartModuleMyRemaxRegistration.SmartModuleMyRemaxFavourites.RemovePropertyFromFavourites(obj, cookieExp);
		}
		ToggleFavourite(obj, Add);
		// Call function from MyRemaxFavourites module, if it exists.
		if (typeof(UpdateMyFavouritesDatagrid) != 'undefined') UpdateMyFavouritesDatagrid();
	}
	
	function ToggleFavourite(obj, Remove)
	{
		if (Remove)
		{
			if(document.getElementById("divFavAdd_" + obj))document.getElementById("divFavAdd_" + obj).style.display = "none";
			if(document.getElementById("divFavRem_" + obj))document.getElementById("divFavRem_" + obj).style.display = "";
		}
		else
		{
			if(document.getElementById("divFavAdd_" + obj))document.getElementById("divFavAdd_" + obj).style.display = "";
			if(document.getElementById("divFavRem_" + obj))document.getElementById("divFavRem_" + obj).style.display = "none";
		}
		return false;
	}
