﻿

var LastAddress = ""; // So we have it during callback;
var oDrivingDir = null;
var oVEMap = null;
var isPrinting = false;

function Initialize(){
	GoogleAnalytics();
	iPad_PageScroll($get('divDetailsPanel'));
	oDrivingDir = new DrivingDir();
	if (isFrameReady("parent.ExpandDetailFrame")) parent.ExpandDetailFrame();

		
	isPrinting = (0<$getval("hPrinting").length);
	
	// If this is a print window, do some reformatting
	if (isPrinting){
		var oWin = window.opener;
		if (oWin){	
			if (oWin.document.getElementById("bShowDir").checked){
				$setstyle("tblInfo2","display","none");		
				$get("divDirections").innerText = oWin.document.getElementById("divDirections").innerText;		
			} else {
				$setstyle("tblInfo2","display","none");	
			}
			
			if (oWin.document.getElementById("bShowMap").checked){
				$setstyle("tblMap").style.display = "none";		
				$get("divMap").innerText = oWin.document.getElementById("divMap").innerText;		
			} else {
				$setstyle("tblMap","display","none");	
			}

		} else {
			$setstyle("tblInfo2","display","none");	
			$setstyle("tblMap","display","none");	
		}
		
		Print2();
	}
	else
	{
		Resize();

		// Some users can't load the map.  Turn off the map option for them. 
		if ($get('divMap')) {
			try { oVEMap = new VEMap('divMap'); } catch (e) { }
			if ((!oVEMap) && $get("bShowMap")) $get("bShowMap").disabled = true;
		}
			
		if (0<$getval("hAgentID").length) return;
		setTimeout("ShowFirst()",100);		
	}
}

function ShowFirst(){
	if(!isFrameReady("parent.MenuFrame")) 
	{
		setTimeout("ShowFirst()",1000);
		return;
	}
	parent.MenuFrame.ShowFirst();	// Select first agent if no favorite agent
}

function Print() {
	window.print();
}


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);
	$setval("hLatitude", Lat);
	$setval("hLongitude", 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/Gadgets/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();
	var Width =  GetScreenWidth();
	var W = Math.min(Width - (127+35), 550);
		
	if ($getval("hSiteCode")=="COOP")
	{
		 W=Math.min(Width-30,600)
		 $setstyle("imgAgentLogo","visibility","hidden");
	}
	if ($getval("hSiteCode") == "SND") W = Math.min(Width - 30, 535);



	//var oMap = $get("divMap");
	$setstyle("divMap","width",(W-10)+"px"); //oMap.style.width = (W-25)+"px";


	var oDiv = $get("divDetailsPanel");
	$setstyle("divDetailsPanel","width",((Width-5)+"px")); // oDiv.style.width = (Width)+"px"; 
	$setstyle("divDetailsPanel", "height", ((Height-50) + "px")); //oDiv.style.height = (Height)+"px";
	$setstyle("DefaultLayout", "width", (Width + "px"));
	$setstyle("DefaultLayout", "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";
	if (oTbl2) oTbl2.style.width=W+"px";
}

function BackToMap(){
	if (isFrameReady("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);
}
