/*
* Created by Boris TS, 03/13/07 - ITG#270093 (Virtual Earth map Enhancement)
*
*JS functions for Virtual Earth map. All needed variables merged out on the form.
*
* Revisions:
* 03/16/07 BT Flash for CMA MAP report
* 03/26/07 BT Maps with multiple pins per Proprty Type
*/
// Meters per mile
var metersPerMile = 1609.3440006146921597227828997904; //meters 

// Array to hold meters/pixel at equator.
var metersArray = new Array(20)
metersArray[1] = 0; // not used.
metersArray[1] = 78271.52 ;
metersArray[2] = 39135.76;
metersArray[3] = 19567.88 ;
metersArray[4] = 9783.94  ;
metersArray[5] = 4891.97 ;
metersArray[6] = 2445.98 ;
metersArray[7] = 1222.99 ;
metersArray[8] = 611.50 ;
metersArray[9] = 305.75 ;
metersArray[10] = 152.87 ;
metersArray[11] = 76.44  ;
metersArray[12] = 38.22 ;
metersArray[13] = 19.11 ;
metersArray[14] = 9.55 ;
metersArray[15] = 4.78 ;
metersArray[16] = 2.39 ;
metersArray[17] = 1.19 ;
metersArray[18] = 0.60 ;
metersArray[19] = 0.30 ;

var mapLoadArray = new Array();

function MapObject(divID, centerLat, centerLon, zoomLevel, addDefaultPin, divPType)
{
	this.ID = divID;
	this.Latitude = centerLat;
	this.Longitude = centerLon;
	this.ZoomLevel = zoomLevel;
	this.AddDefaultPin = addDefaultPin;
	this.PropertyType = divPType;
}

function LoadMaps()
{
	for (mapcounter=0; mapcounter<mapLoadArray.length; mapcounter++ )
	{
		LoadVEMap(mapLoadArray[mapcounter].ID, mapLoadArray[mapcounter].Latitude, mapLoadArray[mapcounter].Longitude, mapLoadArray[mapcounter].ZoomLevel, mapLoadArray[mapcounter].AddDefaultPin, mapLoadArray[mapcounter].PropertyType);
	}
	//Provides for PDF printing w/ Rapattoni activeX control
	//NP	Rmoved this since it hides the bing logo on IE only.It renders fine in other browsers.
	//findCopyright();
}


function LoadVEMap(divID, centerLat, centerLon, zoomLevel, addDefaultPin, divPType)
{
	if(document.getElementById('imgMapDiv_' + divID)) 
	{
		document.getElementById('imgMapDiv_' + divID).style.display = "none";
		document.getElementById('imgNoMap_' + divID).style.display = "";
		document.getElementById('imgNoMap_' + divID).src = sRapMLSImages + 'p_nomap.jpg';

			if (addDefaultPin)
			{
				if(centerLat ==    0.000000)
				{
					document.getElementById('imgMapDiv_' + divID).style.display = "none";
				}
				else
				{
					document.getElementById('imgNoMap_' + divID).style.display = "none";
					document.getElementById('imgMapDiv_' + divID).style.display = "";
					var veMap = new VEMap('imgMapDiv_' + divID);
					
					if ((typeof(veToken) != 'undefined')&&(veToken != ''))
					{
					    veMap.SetClientToken(veToken); // veToken generated using merge tag.
					}
					
					veMap.LoadMap(new VELatLong(centerLat, centerLon), zoomLevel,'r' , true, VEMapMode.Mode2D, false);
                    var pin = new VEShape(VEShapeType.Pushpin, veMap.GetCenter());
                    pin.SetCustomIcon(mgMapPinFile);
                    veMap.AddShape(pin);
				}
			}
			else
			{
				if(divPType) // check Map with multiple pins by Proprty Type
				{
					var PinPType = divPType
					pinArray = eval('pinArray_' + PinPType);
				}
				else
				{
					pinArray = eval('pinArray');
				}
				
				for (iCounter=0; iCounter<pinArray.length; iCounter++ )
				{
					var pinId=iCounter + 1
					var point = pinArray[iCounter];
					var pinFile
					if (pinId == 1) // initialize the Map 
					{
						document.getElementById('imgNoMap_' + divID).style.display = "none";
						document.getElementById('imgMapDiv_' + divID).style.display = "";
						var veMap = new VEMap('imgMapDiv_' + divID);
						
						if ((typeof(veToken) != 'undefined')&&(veToken != ''))
						{
						    veMap.SetClientToken(veToken) // veToken generated using merge tag.
						}
						
						veMap.LoadMap(new VELatLong(centerLat, centerLon), zoomLevel,'r' , true, VEMapMode.Mode2D, false);
						//veMap.SetMapView(pinArray);
						if (pinArray.length > 1)
						{
							veMap.SetMapView(pinArray);
						}
					}
					if(pinId<=250) //display red push pins with numbers up to 250, for all listings above 250, just place a pin with no label.
					{
						pinFile=sRapMLSImages + '/VE_icons/' + pinId + '.gif';
					}
					else
					{
						pinFile=sRapMLSImages + '/VE_icons/'+'VEmarker.gif';
					}
                    var pin = new VEShape(VEShapeType.Pushpin, point);
                    pin.SetCustomIcon(pinFile);
                    veMap.AddShape(pin);
                    map.AttachEvent("onmouseover", map_MouseOver);

				}
			return veMap;
			//Provides for PDF printing w/ Rapattoni activeX control
			//NP	Rmoved this since it hides the bing logo on IE only.It renders fine in other browsers.
			//findCopyright();
		}
	}
}
function ResizeMap(newWidth, newHeight)
{

        map.Resize(newWidth, newHeight);
}


function GetMap(iZoomLevel, fixedMap)
{
    // SBI 13851 TG 2/19/10 Added Param to show new Map controls
    // Default the fixedMap param is not supplied.
    fixedMap = typeof (fixedMap) != 'undefined' ? fixedMap : true;

	if(document.getElementById('imgMapDiv')) 
	{
		if(mgLongitude ==    0.000000 || typeof(mgLongitude) == 'undefined') 
		{
			document.getElementById('imgMapDiv').style.display = "none";
			document.getElementById('imgNoMap').style.display = "";
			document.getElementById('imgNoMap').src = sRapMLSImages + 'p_nomap.jpg';
			if(typeof(document.getElementById('MapTable')) != 'undefined' )
			{
				if(document.getElementById('MapTable') != null)
				{
					document.getElementById('MapTable').style.display = "none";
				}
			}
		}
		else
		{
			if (document.getElementById('imgMapDiv') != null)
			{
				map = new VEMap('imgMapDiv');
				
				if ((typeof(veToken) != 'undefined')&&(veToken != ''))
				{
				    map.SetClientToken(veToken) // veToken generated using merge tag.
				}
				
				if(document.getElementById('footer') != null)
				{
					document.getElementById('footer').id = 'temp_footer';	
				}
				// SBI 13851 TG 2/19/10 Added Param to show new Map controls
				map.LoadMap(new VELatLong(mgLatitude, mgLongitude ), iZoomLevel,'r' ,fixedMap, VEMapMode.Mode2D, false);
				if(document.getElementById('temp_footer') != null)
				{
					document.getElementById('temp_footer').id = 'footer';	
				}
				AddPin();
				//Provides for PDF printing w/ Rapattoni activeX control
				//NP	Rmoved this since it hides the bing logo on IE only.It renders fine in other browsers.
				//findCopyright();
			}
		}
    }

    return map;
}


function GetMapWithFlash(iZoomLevel)
{
	GetMap(iZoomLevel);
	LoadFlash();
}


function LoadFlash() {

    ua = navigator.userAgent.toLowerCase();
    var ifIE = (ua.indexOf('msie') != -1 && !this.isOpera && (ua.indexOf('webtv') == -1));

    if (!ifIE) {
        window.flashObj_Map = document.forms[0].flashObj_Map;
    }
   
	if(map != null)
	{
	    flash = document.getElementById('flashObj_Map');
	    flash.INIT('imgMapDiv', MediaURL + 'rapmlsimages/map/flash4maps/', map.GetZoomLevel(), GetPixelsPerMile(), 8,'A');
    	flash.HideButtonBar('imgMapDiv');
	}//if(map != null)
}
function GetPixelsPerMile()
{
	var CenterPoint = new VELatLong(mgLatitude, mgLongitude);
	var pixelsPerMile = 0;
	pixelsPerMile = (metersPerMile /(metersArray[map.GetZoomLevel()] * Math.cos(CenterPoint.Latitude * Math.PI/180)));
	return pixelsPerMile;
}
function hideMouseoverImage(x) 
{
	switch(x) 
	{
		case "n":
			document.all.ibtnPanN.src = "/mq/graphics/pan_n1.gif";
			document.all.ibtnPanNW.src = "/mq/graphics/pan_nw1.gif";
			document.all.ibtnPanNE.src = "/mq/graphics/pan_ne1.gif";
			break;
		case "s":
			document.all.ibtnPanS.src = "/mq/graphics/pan_s1.gif";
			document.all.ibtnPanSW.src = "/mq/graphics/pan_sw1.gif";
			document.all.ibtnPanSE.src = "/mq/graphics/pan_se1.gif";
			break;
		case "e":
			document.all.ibtnPanE.src = "/mq/graphics/pan_e1.gif";
			document.all.ibtnPanSE.src = "/mq/graphics/pan_se1.gif";
			document.all.ibtnPanNE.src = "/mq/graphics/pan_ne1.gif";
			break;
		case "w":
			document.all.ibtnPanW.src = "/mq/graphics/pan_w1.gif";
			document.all.ibtnPanNW.src = "/mq/graphics/pan_nw1.gif";
			document.all.ibtnPanSW.src = "/mq/graphics/pan_sw1.gif";
			break;
		case "nw":
			document.all.ibtnPanNW.src = "/mq/graphics/pan_nw1.gif";
			break;
		case "sw":
			document.all.ibtnPanSW.src = "/mq/graphics/pan_sw1.gif";
			break;
		case "ne":
			document.all.ibtnPanNE.src = "/mq/graphics/pan_ne1.gif";
			break;
		case "se":
			document.all.ibtnPanSE.src = "/mq/graphics/pan_se1.gif";
			break;
	}
}
function showMouseoverImage(x) 
{
	switch(x) 
	{
		case "n":
			document.all.ibtnPanN.src = "/mq/graphics/pan_n2.gif";
			document.all.ibtnPanNW.src = "/mq/graphics/pan_nw3.gif";
			document.all.ibtnPanNE.src = "/mq/graphics/pan_ne3.gif";
			break;
		case "s":
			document.all.ibtnPanS.src = "/mq/graphics/pan_s2.gif";
			document.all.ibtnPanSW.src = "/mq/graphics/pan_sw3.gif";
			document.all.ibtnPanSE.src = "/mq/graphics/pan_se3.gif";
			break;
		case "e":
			document.all.ibtnPanE.src = "/mq/graphics/pan_e2.gif";
			document.all.ibtnPanNE.src = "/mq/graphics/pan_en3.gif";
			document.all.ibtnPanSE.src = "/mq/graphics/pan_es3.gif";
			break;
		case "w":
			document.all.ibtnPanW.src = "/mq/graphics/pan_w2.gif";
			document.all.ibtnPanNW.src = "/mq/graphics/pan_wn3.gif";
			document.all.ibtnPanSW.src = "/mq/graphics/pan_ws3.gif";
			break;
		case "nw":
			document.all.ibtnPanNW.src = "/mq/graphics/pan_nw2.gif";
			break;
		case "sw":
			document.all.ibtnPanSW.src = "/mq/graphics/pan_sw2.gif";
			break;
		case "ne":
			document.all.ibtnPanNE.src = "/mq/graphics/pan_ne2.gif";
			break;
		case "se":
			document.all.ibtnPanSE.src = "/mq/graphics/pan_se2.gif";
			break;
	}
}
function SetZoomLevel(id, level)
{
	map.SetZoomLevel(level);
}
function PanMap(x,y)
{
	flash.ClearShape();
	zoomLevel = map.GetZoomLevel();
	zoomLevel = 20 - zoomLevel;
	
	map.Pan(x*zoomLevel,y*zoomLevel);
	
}
// Method called after rectangle is drawn on map.
function SetArea(controlID, topX, topY, bottomX, bottomY) 
{
	// Convert Pixels to LatLong.
	var topPoint = map.PixelToLatLong(new VEPixel(topX, topY));
	var bottomPoint = map.PixelToLatLong(new VEPixel(bottomX, bottomY));

	pointsArray = new Array();
	pointsArray[0] = topPoint;
	pointsArray[1] = bottomPoint;
	map.SetMapView(pointsArray);
	
	var topPixels = map.LatLongToPixel(topPoint);
	var bottomPixels = map.LatLongToPixel(bottomPoint);
	flash.DrawArea(topPixels.x, topPixels.y, bottomPixels.x, bottomPixels.y);
	
	
}


function Proceed(controlID, message)
{
	return true;
}

// Clears the drawn figure.
function Clear(controlID)
{
}

// Returns current zoom level.
function GetZoomLevel(controlID)
{
	return map.GetZoomLevel();
}
function  AddPin()
{
	if (typeof(pinArray) != 'undefined' && pinArray.length != 0 || typeof(PinPType) != 'undefined')
	{
		//alert ('Multiple Pins')
		AddMultiplePins()
		if (pinArray.length > 1)
		{
			map.SetMapView(pinArray);
		}
	}
	else
	{
		//alert ('Single Pin')
		AddSinglePin();
	}
}

function AddMultiplePins()
{
	if(typeof(PinPType) != 'undefined') // check Map with multiple pins by Proprty Type
	{
		pinArray = eval('pinArray_' + PinPType);
	}
	else
	{
		pinArray = eval('pinArray');
	}
	
	for (iCounter=0; iCounter<pinArray.length; iCounter++ )
	{
		var pinId=iCounter + 1
		var point = pinArray[iCounter];
		var pinFile
		if(pinId<=250) //display red push pins with numbers up to 250, for all listings above 250, just place a pin with no label.
		{
			pinFile=sRapMLSImages + '/VE_icons/' + pinId + '.gif';
		}
		else
		{
			pinFile=sRapMLSImages + '/VE_icons/'+'VEmarker.gif';
		}
        var pin = new VEShape(VEShapeType.Pushpin, point);
        pin.SetCustomIcon(pinFile);
        map.AddShape(pin);
        map.AttachEvent("onmouseover", map_MouseOver);

	}
}

//ITG #500956, BT, 09/09/08 - changed name of mgMLNumber to mgMLNumberDisplay
function AddSinglePin()
{
	var title = ''
	if (typeof(mgMLNumberDisplay) != 'undefined' && mgMLNumberDisplay.length > 1) { title = 'Listing #' + mgMLNumberDisplay}
    var pin = new VEShape(VEShapeType.Pushpin, map.GetCenter());
    pin.SetCustomIcon(mgMapPinFile);
    pin.SetTitle(title);
    map.AddShape(pin);
    map.AttachEvent("onmouseover", map_MouseOver);

}

function AddMyControl()
{
	var el = document.createElement("div");
	el.id = "myControl";
	el.style.top = "155px";
	el.style.left = "160px";
	el.style.border = "2px solid darkblue";
	el.style.background = "White";
	el.innerHTML = "# 30001066";
	el.style.fontSize='8pt';
	el.style.fontfamily = 'arial';
	 map.AddControl(el);
	//we can't get this to work correctly so commented out for now
	//addShim(el);
}

function addShim(el)
{
	var shim = document.createElement("iframe");
	shim.id = "myShim";
	shim.frameBorder = "0";
	shim.style.position = "relative";
	shim.style.zIndex = "1";
	shim.style.top  = el.offsetTop;
	shim.style.left = el.offsetLeft;
	shim.width  = el.offsetWidth;
	shim.height = el.offsetHeight;
	el.shimElement = shim;
	document.getElementById('MapTD').parentNode.insertBefore(shim, el);
}

var collChildren;

function findCopyright()
{
	var coll=document.getElementsByTagName('div');

	var copyrightDiv;
	
	for(i = 0; i < coll.length; i++)
	{
		if(coll[i].innerHTML.indexOf('PoweredByLogo') != -1)
		{
			for(n = 0; n < coll[i].childNodes.length; n++)
			{
				if(coll[i].childNodes[n].className.indexOf('PoweredByLogo') != -1)
				{
				    coll[i].childNodes[n].style.cssText = "";

				    //RNI# 090317-000332, BTs, 12/23/2009 - set display to "none"
				    //alert(coll[i].childNodes[n]);
				    //coll[i].childNodes[n].style.display = "none";
				}
			}
		}
	}

}


function map_MouseOver(e)
{
    if (e.elementID)
    {
	    return true;
    }
}

