﻿
var LastAddress = ""; // So we have it during callback;
var oDrivingDir = null;
var oVEMap = null;
var isPrinting = false;

function Initialize(){
	GoogleAnalytics()
	oDrivingDir = new DrivingDir();
	if (parent.ExpandDetailFrame) parent.ExpandDetailFrame();
	
	oVEMap = new VEMap('divMap');
	//oVEMap.LoadMap(null,19,"s",false,1,true);
	//oVEMap.Find("", $get("hAddress").value , null, null, 0, 20, true, true, true, null, AddAddrPushPinCallBack);
	isPrinting = (0<$get("hPrinting").value.length);
	
	// If this is a print window, do some reformatting
	if (isPrinting){
		var oWin = window.opener;
		if (oWin){	
			if (oWin.document.getElementById("bShowDir").checked){
				$get("tblInfo2").style.display = "none";		
				$get("divDirections").innerText = oWin.document.getElementById("divDirections").innerText;		
			} else {
				$get("tblInfo2").style.display = "none";	
			}
			
			if (oWin.document.getElementById("bShowMap").checked){
				$get("tblMap").style.display = "none";		
				$get("divMap").innerText = oWin.document.getElementById("divMap").innerText;		
			} else {
				$get("tblMap").style.display = "none";	
			}

		} else {
			$get("tblInfo2").style.display = "none";	
			$get("tblMap").style.display = "none";	
		}
		
		Print2();
	}
	else
	{
		Resize();
		
		if (0<$get("hAgentID").value.length) return;
		setTimeout("ShowFirst()",100);		
	}
}
function ShowFirst(){
	if(!parent.MenuFrame) 
	{
		setTimeout("ShowFirst()",1000);
		return;
	}
	parent.MenuFrame.ShowFirst();	// Select first agent if no favorite agent
}

function Print(){
	var whnd = window.open(self.location.href+"&prt=1","printwindow" ,"width=800,height=500;");
}

function Print2(){
	//print now that we are in the popup print window	
	
	window.print();
	setTimeout("window.close()",500);
}

function ShowMap(){
	var isMapShown = $get("bShowMap").checked 
	$get("divMap").style.display=(isMapShown) ? "block": "none";
	if (isMapShown){
		oVEMap.LoadMap(null,19,"s",false,1,true);
		var Addr = $get("hAddress").value 
		if (Addr.length>0) 	oVEMap.Find("", Addr, null, null, 0, 20, true, true, true, null, AddAddrPushPinCallBack);
	}
}

function AddAddrPushPinCallBack(oVEShapeLayer, oVEFindResult, oVEPlace, bHasMore, e){
	if(null==oVEPlace) return;
	if (0==oVEPlace.length) return;
			
	var Lat = oVEPlace[0].LatLong.Latitude;
	var Lon = oVEPlace[0].LatLong.Longitude;
	var Point = new VELatLong(Lat,Lon);
	$get("hLatitude").value = Lat;
	$get("hLongitude").value = Lon;
				
	var shape = new VEShape(VEShapeType.Pushpin, Point)
	shape.SetCustomIcon("<img src='../Icons/Pin_Red_Small.gif' />");
	var Addr=oVEPlace[0].Name; 
	shape.SetTitle("<div><B>" + Addr + "</B></div>");	
	
	
	Img = "<iframe src='" + FullURL("SiteContent/BirdsEyeImage.aspx?Lat=" + Lat + "&Lon=" + Lon) + "' width='200' height='200' scrolling='no'></iframe>";
	shape.SetDescription("<div>" + Img + "</div>")
	
	
	
	oVEMap.AddShape(shape);
	oVEMap.SetCenter(Point);	
}

function FindAddress(Addr){
	//parent.MapAddr = Addr;
	//parent.MapZoom = 10;
}


function Resize(){
	// If the user resizes the window this will resize all the layers

	var Height = GetScreenHeight()-45;
	var Width =  GetScreenWidth()-5;
	var W = Math.min(Width-250,600);
	if ($get("hSiteCode").value=="COOP")
	{
		 W=Math.min(Width-30,600)
		 $get("imgAgentLogo").style.visibility="hidden";
	}

	var oMap = $get("divMap");
	oMap.style.width = (W-25)+"px";


	var oDiv = $get("divDetailsPanel");
	oDiv.style.width = (Width)+"px"; 
	oDiv.style.height = (Height)+"px";

	var oInfo = $get("tblNav");
	var oTbl = $get("tblInfo");
	var oTbl2 = $get("tblInfo2");
	var oImg = $get("imgAgentLogo");
	if (!oImg) oImg = $get("imgAgentLogo2");
	oImg.style.width=127+"px";
	oInfo.style.width = Width+"px";
	
	oTbl.style.width=W+"px";
	oTbl2.style.width=W+"px";
}

function BackToMap(){
	if (parent.ContractDetailFrame) parent.ContractDetailFrame();
}

function ClearFavAgent(){
	MyAjaxService.RememberSetting("MyFavoriteRealtor",0,$get("hEncryptedVisitorID").value)
	document.location.reload(true);
}

function SetFavAgent(){
	var AgentID = $get("hAgentID").value;
	//if(""==$get("haveVisitorEmail").value){
	
	//var EmailAddr = prompt("To choose an agent please enter your email address:","")
	//if (!EmailAddr) return; // None entered
	
	//var RegEx = new RegEx("^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$");
	//if (!EmailAddr.match(RegEx)) return;
		
	//alert("Error");
	//return
	//}
	
	MyAjaxService.RememberSetting("MyFavoriteRealtor",AgentID,$get("hEncryptedVisitorID").value)
	document.location.reload(true);
}