﻿    //Main Objects
    var oGrid;
    var oPowerMap;
    var oPropertySearch;
    var oMapSlider;
    //Settings
    var iListPerPage = 100;
    var iLimitToShow;
    var PageNum = 1;
    var LPP = 10;
    var LoadingResult = false;
    var tDefaultLatLong = new Array();
    //var MapLatitude = (!DataFloat.Agora.Util.IsEmptyOrNull(DataFloat.Agora.Util.Request.QueryString["lat"])) ? parseFloat(DataFloat.Agora.Util.Request.QueryString["lat"]) : 30.685596;
    //var MapLongitude = (!DataFloat.Agora.Util.IsEmptyOrNull(DataFloat.Agora.Util.Request.QueryString["long"])) ? parseFloat(DataFloat.Agora.Util.Request.QueryString["long"]) : -88.146482;
    //tDefaultLatLong[320] = {Lat: MapLatitude, Long: MapLongitude};
    var fdlBoard = new Array();
    
    //Variables
    var bAdvacendSearchStatus = true;
    var iCurrentLoad = 0;
    var oCurrentDraw = null;
    var iCurrentImgPreview = 1;
    var oDataMapSource;
    var bDrawHasName = false;
    var allowToDraw = true;
    var iCurrentBoard = 200;
    var arrLoadedPops = new Array();    
    var mapAjax;
    var DrawAreas;
    var oComboManager;
    var arrProperties;

$(document).ready(function() {
    /*iLimitToShow = 200;
    oPowerMap = new DataFloat.Agora.RealMap("divMapMs");
   
   //New Map
    mapAjax = new DataFloat.Agora.AjaxManager();
    mapAjax.MapManager = oPowerMap;
    mapAjax.MapManager.OnMapChangeCenter = false;
    mapAjax.MapManager.LoadMap();
    mapAjax.MapManager.VirtualMap.SetCenterAndZoom(new VELatLong(tDefaultLatLong[320].Lat, tDefaultLatLong[320].Long), 12)
        
	oMapSearch_OnLoad(OfficeJSon);*/
})
    
    function SetMapLocation(BoardId){
        iCurrentBoard = 320;
        mapAjax.MapManager.VirtualMap.SetCenterAndZoom(new VELatLong(tDefaultLatLong[320].Lat, tDefaultLatLong[320].Long), 10)
    }
	
	function DoSearch(pinObject){
        var oPinClick = null; 
	    if (pinObject != undefined)
	        oPinClick = oPowerMap.VirtualMap.GetShapeByID(pinObject.elementID);
   
   	    if (oPinClick == null){
	        mapAjax.StopAllRequest();
            var VELatLongRectangle = oPowerMap.VirtualMap.GetMapView();
	    
		    //New Map Search
		    
		    var drawParam;
		    var arrCheckArea = $('.cbxmyareas');
		    var arrCheckAreaToSearch = new Array();
		    var arrAllDraw = oPowerMap.GetAllDraw();
		    if (arrCheckArea.length > 0){
		        var arrDrawAreasToSearch = new Array();
		        for (iCkA=0;iCkA<arrCheckArea.length;iCkA++){
		            if (arrCheckArea[iCkA].checked)
		                arrCheckAreaToSearch.push(arrAllDraw[iCkA]);
		        }
		    }
		    drawParam = (arrCheckAreaToSearch.toAgoraAreaSearch());
	        
		    var searchLatMin, searchLatMax, searchLongMin, searchLongMax;
		    if (drawParam == ''){
		        searchLatMin = VELatLongRectangle.BottomRightLatLong.Latitude;
		        searchLatMax = VELatLongRectangle.TopLeftLatLong.Latitude;
		        searchLongMin = VELatLongRectangle.TopLeftLatLong.Longitude;
		        searchLongMax = VELatLongRectangle.BottomRightLatLong.Longitude;
            }else{
                var arrSplitLat = drawParam.split('|');
                var pointA = arrSplitLat[0].split(',');
                var pointB = arrSplitLat[1].split(',');
                var pointC = arrSplitLat[2].split(',');
                                
                searchLatMin = pointB[0];
		        searchLatMax = pointA[0];
		        searchLongMin = pointA[1];
		        searchLongMax = pointC[1];
                if (searchLatMin > searchLatMax)
                {
                    var searchLatTemp = searchLatMin;
                    searchLatMin = searchLatMax;
                    searchLatMax = searchLatTemp;
                }
                if (searchLongMin < searchLongMax)
                {
                    var searchLongTemp = searchLongMin;
                    searchLongMin = searchLongMax;
                    searchLongMax = searchLongTemp;            
                }
            }		    
		    var mapParam = "Agora__ItemPerPage=101&dec_LatMin=" + searchLatMin + "&dec_LatMax=" + searchLatMax + "&dec_LongMin=" + searchLongMin + "&dec_LongMax=" + searchLongMax + "&" + serializeForm('frmMapSearch');;
		    mapAjax.ItemPerPage = 101;
		    mapAjax.FormParams = mapParam;
            mapAjax.NewSystemRequest(DataFloat.Agora.Services.ServiceMapSearch, null, oMapSearch_OnLoad);
        }else{
            MlsNumber = oPinClick.MlsNumber; 
            oPowerMap_PinClick(null, MlsNumber);
        }
	}
	
	function oMapSearch_OnLoad(result){
	        //AdvancedSearchTab_OnClick();

	        arrProperties = JSON.parse(result);
	        
	        mapAjax.MapManager.ClearAll();
	  	    var arrPin = new Array();
	  	    var arrMls = new Array();
	  	    PinClass = null;
            for (var i = 0; i < arrProperties.length; i++) {
                iMLSNum = arrProperties[i]["mls"];
                //iName = arrProperties[i]["name"];
                iName =  "";
                iLatitude = arrProperties[i]["lat"];
                iLongitude = arrProperties[i]["long"];
                iKmls = arrProperties[i]["kmls"];
                iListDate = arrProperties[i]["listdate"];
                arrMls.push(arrProperties[i]["mls"]);
                if (iLatitude && iLongitude)
                {
				    var Html = "<b><a href='OfficeDetail.aspx?RecordType=2&n_k="+ arrProperties[i]["OfficeKey"]+ "'>"+arrProperties[i]["name"]+"</a></b><br/>"+arrProperties[i]["OfficeAddress"]+"<br/>"+arrProperties[i]["OfficeCity"]+"&#160;"+arrProperties[i]["OfficeState"]+"&#160;"+arrProperties[i]["OfficeZip"]+"<br/>"+arrProperties[i]["OfficePhone"];
					arrPin[arrPin.length] = new mapAjax.MapManager.PinObject(i, iName, iLatitude, iLongitude, iMLSNum, PinClass, Html , iKmls);					    
                    tDefaultLatLong[320] = {Lat: iLatitude, Long: iLongitude};					
                    mapAjax.MapManager.VirtualMap.SetCenterAndZoom(new VELatLong(tDefaultLatLong[320].Lat, tDefaultLatLong[320].Long), 10)					
			    }
            }
            mapAjax.MapManager.NewItem(arrPin);
            /*StartListView();*/
	}

	
	function NavGrid(direction, page){
	    page = String.IsEmptyOrNull(direction) ? page : oGrid.CurrentPage + direction;
	    oGrid.LoadContent(page);oGrid.CreateNavigation('NavGrid');
	}
	
	
	/* GRID */

	/* MAP */
	function oPowerMap_PinClick(e, MlsNumber, index){
	    if (!oPowerMap.DrawMode){
	        oPowerMap.RemoveAllHigligth();
	        if (e != null && e != undefined){ oPinClick = oPowerMap.VirtualMap.GetShapeByID(e.elementID); if (oPinClick!=null) { MlsNumber = oPinClick.MlsNumber; index = oPinClick.Position+1;}}
	        if ((MlsNumber != null && MlsNumber != undefined)){
	            oPowerMap.HigligthPin(index);
	            iCurrentImgPreview = 1;
	            /*var oPropertySearchPreview = new DataFloat.Agora.AjaxManager();    
	            oPropertySearchPreview.FormParams = "RecordType=" + iCurrentBoard + "&0110_exc=" + MlsNumber + "&HtmlInterface=NewMapPreview";
	            oPropertySearchPreview.NewRequest(DataFloat.Agora.Services.ServiceGetHtmlContent, 1, null, "PropertyPreview", true);*/
	        }else{
	            //DoSearch();
	        }
        }
    }
    
	
	function GoPhoto(Direction, ImgName, IndexName, Count, ImgSrc){
	        iCurrentImgPreview = $("#" + IndexName).html();
		    iCurrentImgPreview =  parseInt(iCurrentImgPreview) + (1*Direction);
		    if (iCurrentImgPreview <= 1)
		    {
		        $('.Previous').hide();
		        iCurrentImgPreview=1;
		    }else $('.Previous').show();
		    if (iCurrentImgPreview >= Count)
		    {
		        $('.Next').hide();
		        iCurrentImgPreview=Count;
		    }else $('.Next').show();
		    	    		    
		    $('#' + IndexName).html(iCurrentImgPreview);
		    $("#" + ImgName).attr('src', $('#ImageIndex_' + iCurrentImgPreview).val());
		    
		}   
		
		
        /*DataFloat.Agora.RealMap.prototype.OnMouseOverPin = function(e){
        
	        if(e.elementID)
	        {
		        var pin = mapAjax.MapManager.VirtualMap.GetShapeByID(e.elementID);
        		ShowMapPopup(pin);
	        }
        }*/	
		
        function ShowMapPopup(pin){
            var k = pin.MlsNumber;
        		if (arrLoadedPops[k] == undefined){
		            arrLoadedPops[k] = true;

		            if (!IsNumeric(k)) return;
 		            pin.SetDescription("<div id='PropertyInfo_"+pin.kmls+"' style='width:450px' ><img src='/images/ran_bg_loading.gif'/> Loading...</div>"); 
            		
		            mapAjax.MapManager.getPopInfo(pin.kmls,"PropertyInfo_"+pin.kmls, pin);
		            
                }
        }		
	
        /*DataFloat.Agora.RealMap.prototype.getPopInfo = function(k, divID, pinObejct){
            var VEPopUpRequest = new DataFloat.Agora.AjaxManager("RecordType=320&HtmlInterface=MapPopUpInfo_320&n_k="+k);
            VEPopUpRequest.NewSystemRequest(DataFloat.Agora.Services.ServiceContentManagerMultipleHtml, 1, function(data){
                $("#"+divID).html(data);
                pinObejct.SetDescription(data);
            });
        }*/			
		
		var PropertyLatitude = null;
		var PropertyLongitude = null;
		var PropertyAddress = null;
		
        function GetPropertyDirection(Latitude, Longitude, PFullAddress)
        {
            PropertyLatitude = Latitude;
            PropertyLongitude = Longitude;
            PropertyAddress = escape(PFullAddress);
            $("#StartingLocation").focus();
        }
        
        function GetMapDirection(StartLocationID)
        {
            var FromLocation = $("#"+StartLocationID).val();
            if (FromLocation=="") return;
            
            DataFloat.Agora.Util.NewPop('/POP/PropertyDetailMapDirections.aspx?Latitude=' + PropertyLatitude + '&Longitude=' + PropertyLongitude + '&AddressTo=' + PropertyAddress + "&AddressFrom=" + FromLocation, 1000, 500);
        }
        
        
	    function Directions_OnLoad(route){
	        // Unroll route
               var legs     = route.RouteLegs;
               var turns    = "Total distance: " + route.Distance.toFixed(1) + " mi\n";
               var numTurns = 0;
               var leg      = null;
               directionLoaded = true;
               dataSourceDirections = new Array();

                for(var i = 0; i < legs.length; i++)
                {

                   leg = legs[i];
                   var turn = null;
                      
                   for(var j = 0; j < leg.Itinerary.Items.length; j ++)
                   {
                      
                      turn = leg.Itinerary.Items[j];
                      dataSourceDirections.push({Step: turn.Text, Distance: turn.Distance.toFixed(1)});
                      numTurns++;
                   }
                }
                $('#DrawingBoxContainer').hide();
	    }        
    	    
	    function Directions_StillWorking(){
	        $('#DrawingBox').html('<img src="/Css/MapSearch/agora_map_box_ico_alert.gif"/>We are working on your request.<br/>Please wait...');
	    }
	    
	    function Directions_CheckTimeout(){
	        if (!directionLoaded){
	            $('#DrawingBox').html("We didn't get to process your request.<br/><br/><a href='javascript:StartDrivingDirections()'>Try Again</a> &nbsp;&nbsp;&nbsp;<a href='javascript:$(\"#DrawingBoxContainer\").hide();'>Cancel</a>");
                directionLoaded = true;
	        }
	    }
	    
	    function ClearDirection(){
	        mapAjax.MapManager.VirtualMap.DeleteRoute();
	        document.getElementById("IntineraryOrder").length=0;
	    }	    
	    
	    var directionLoaded = true;
	    function ShowDirections(){
    	    
	        var AddressStart = $('#StreetAddress').val();
    	    
	        if (AddressStart != ''){
	            $('#DrawingBoxContainer').show();
	            $('#DrawingBox').html("Processing your request.<br/>Please wait...");
	            var options = new VERouteOptions();
                options.RouteCallback = Directions_OnLoad;
                
	            var arrPoints = new Array();
	            var arrAddress = oComboManager.TextToArray();
                arrPoints.push(AddressStart);
	            for (iA=0;iA<arrAddress.length;iA++)
	                arrPoints.push(arrAddress[iA]);
    	            
	            directionLoaded = false;
	            mapAjax.MapManager.VirtualMap.GetDirections(arrPoints, options);
	            setTimeout("Directions_StillWorking()", 5000);
	            setTimeout("Directions_CheckTimeout()", 10000);
    	    
	        }
	    }
	    
	    function GetIndex(MlsNumber){
	        for (iDs = 0; iDs < arrProperties.length; iDs++){
	            if (arrProperties[iDs].kmls == MlsNumber)
	                return iDs;
	        }
	    }
    	
	    
	
	var MapX;
	var MapY;
	function OnEventCatchPosition(e)
	{
	    MapX = e.mapX;
	    MapY = e.mapY;
	}
	

function FindAgentAlphabetic(pString){
    document.location.href = 'AgentResult.aspx?0040=1&0520_right=' + pString;
}
