﻿function LocalMap(lmap)
{
    var _publicSchools = new VEShapeLayer();//Add this to show public schools
    var _privateSchools = new VEShapeLayer();
    var _worship = new VEShapeLayer();
    var _displayPublic = false;
    var _displayPrivate = false;
    var _displayWorship = false;
    var _requestLocal = null;
    var _localRequests = new Array();
    _localRequests[0] = null;//Public Schools
    _localRequests[1] = null;//Private Shools
    _localRequests[2] = null;//Churches
    
    this.Map = lmap;
    this.Distance = 10;
    this.MaxResults = 0;
    
    lmap.AddShapeLayer(_publicSchools);
    lmap.AddShapeLayer(_privateSchools);
    lmap.AddShapeLayer(_worship);
    _publicSchools.Hide();
    _privateSchools.Hide();
    _worship.Hide();
    
    this.get_DisplayPublic = function(){ return _displayPublic; };
    this.get_DisplayPrivate = function(){ return _displayPrivate; };
    this.get_DisplayWorship = function(){ return _displayWorship; };
    this.DisplayPublicSchools = function(display){
            _displayPublic = display;
            if(lmap)
            {
                if(display)
                    _publicSchools.Show();
                else
                    _publicSchools.Hide();
            }
        };
    this.DisplayPrivateSchools = function(display){
            _displayPrivate = display;
            if(lmap)
            {
                if(display)
                    _privateSchools.Show();
                else
                    _privateSchools.Hide();
            }
        };
    this.DisplayWorship = function(display){
            _displayWorship = display;
            if(lmap)
            {
                if(display)
                    _worship.Show();
                else
                    _worship.Hide();
            }
        };
    this.GetPublicSchools = function (){
            this.AbortLocal(_localRequests[0]);//Cancel Previous Call
            var center = lmap.GetCenter();
            $.ajax(
            {
                type: "POST",
                url: "../webservices/clientservices.asmx/GetPublicSchools",
                data: "{lat:"+center.Latitude+",lon:"+center.Longitude+",distance:"+this.Distance+",maxresults:"+this.MaxResults+"}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: this.PublicCallback,
                error: this.PublicFailed
            });
           /* _localRequests[0] = ClientServices._staticInstance.GetPublicSchools(lmap.GetCenter(),
                this.Distance, this.MaxResults, this.PublicCallback, this.PublicFailed);*/
        };
    this.GetPublicSchoolsRect = function(){};
    this.GetPublicSchoolsPoly = function(points){};
    this.GetPrivateSchools = function (){
            this.AbortLocal(_localRequests[1]);//Cancel Previous Call
            var center = lmap.GetCenter();
            $.ajax(
            {
                type: "POST",
                url: "../webservices/clientservices.asmx/GetPrivateSchools",
                data: "{lat:"+center.Latitude+",lon:"+center.Longitude+",distance:"+this.Distance+",maxresults:"+this.MaxResults+"}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: this.PrivateCallback,
                error: this.PrivateFailed
            });
            /*_localRequests[1] = ClientServices._staticInstance.GetPrivateSchools(lmap.GetCenter(),
                this.Distance, this.MaxResults, this.PrivateCallback, this.PrivateFailed);*/
        };
    this.GetPrivateSchoolsRect = function(){};
    this.GetPrivateSchoolsPoly = function(points){};
    this.GetWorship = function (){
            this.AbortLocal(_localRequests[2]);//Cancel Previous Call
            var center = lmap.GetCenter();
            $.ajax(
            {
                type: "POST",
                url: "../webservices/clientservices.asmx/GetChurches",
                data: "{lat:"+center.Latitude+",lon:"+center.Longitude+",distance:"+this.Distance+",maxresults:"+this.MaxResults+"}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: this.WorshipCallback,
                error: this.WorshipFailed
            });
            /*_localRequests[2] = ClientServices._staticInstance.GetChurches(lmap.GetCenter(),
                this.Distance, this.MaxResults, this.WorshipCallback, this.WorshipFailed);*/
        };
    this.GetWorshipRect = function(){};
    this.GetWorshipPoly = function(points){};
    this.PublicCallback = function(schools){
            _publicSchools.DeleteAllShapes();
            if(!schools) return;
            for(i = 0; i < schools.length; i++)
	        {
	            pin = new VEShape( VEShapeType.Pushpin, new VELatLong(schools[i].Location.Latitude, schools[i].Location.Longitude));
	            pin.SetCustomIcon('<div class="PublicSchool"></div>');
                pin.SetTitle('<b>' + schools[i].Name + '</b>');
                pin.SetDescription(BuildSchoolTable(schools[i]));
                _publicSchools.AddShape(pin);
	        }
        };
    this.PrivateCallback = function(schools){
            _privateSchools.DeleteAllShapes();
            if(!schools) return;
            for(i = 0; i < schools.length; i++)
	        {
	            pin = new VEShape( VEShapeType.Pushpin, new VELatLong(schools[i].Location.Latitude, schools[i].Location.Longitude) );
	            pin.SetCustomIcon('<div class="PrivateSchool"></div>');
                pin.SetTitle('<b>' + schools[i].Name + '</b>');
                pin.SetDescription(BuildSchoolTable(schools[i]));
                _privateSchools.AddShape(pin);
	        }
        }
    this.WorshipCallback = function(churches){
            _worship.DeleteAllShapes();
            if(!churches) return;
            for(i = 0; i < churches.length; i++)
	        {
	            pin = new VEShape( VEShapeType.Pushpin, new VELatLong(churches[i].Location.Latitude, churches[i].Location.Longitude) );
	            pin.SetCustomIcon('<div class="Worship"></div>');
                pin.SetTitle('<b>' + churches[i].Name + '</b>');
                pin.SetDescription(BuildChurchTable(churches[i]));
                _worship.AddShape(pin);
	        }
        }
    this.PublicFailed = function SearchFailed(e){
            _publicSchools.DeleteAllShapes();
            if (_localRequests[0] && !_localRequests[0].get_executor().get_aborted())
                alert('Public Schools Failed!\nMessage:\t\t' + e._message + 
                    '\nStack Trace:\t' + e._stackTrace);
        };
    this.PrivateFailed = function SearchFailed(e){
            _privateSchools.DeleteAllShapes();
            if (_localRequests[1] && !_localRequests[1].get_executor().get_aborted())
                alert('Private Schools Failed!\nMessage:\t\t' + e._message + 
                    '\nStack Trace:\t' + e._stackTrace);
        };
    this.WorshipFailed = function SearchFailed(e){
            _worship.DeleteAllShapes();
            if (_localRequests[2] && !_localRequests[2].get_executor().get_aborted())
                alert('Worship Failed!\nMessage:\t\t' + e._message + 
                    '\nStack Trace:\t' + e._stackTrace);
        };
    this.AbortLocal = function(req){if(!req) return;
            var executor = req.get_executor();
            if (executor.get_started())
                executor.abort();
        };
}
function BuildSchoolTable(school)
{
    return '<div style="height:70px;overflow:auto;"><table width="100%"><tr align="left"><td>Type</td><td>' + school.Type + '</td></tr><tr align="left"><td>Address</td><td>' + school.Address + '</td></tr><tr align="left"><td>City, State Zip</td><td>' + school.CityStateZip + '</td></tr>'+(school.Students > 0?'<tr align="left"><td>Students</td><td>' + school.Students + '</td></tr>':'')+'<tr align="left"><td>Low Grade</td><td>' + school.LowGrade + '</td></tr><tr align="left"><td>High Grade</td><td>' + school.HighGrade + '</td></tr><tr align="left"><td>Phone</td><td>' + school.Phone + '</td></tr>'+(school.Affiliations > 0?'<tr align="left"><td>Affiliations</td><td>' + school.Affiliations + '</td></tr>':'')+(school.Mascot?'<tr align="left"><td>Mascot</td><td>' + school.Mascot + '</td></tr>':'')+(school.Colors?'<tr align="left"><td>Primary Color</td><td>' + school.PrimaryColor + '</td></tr>':'')+'</table></div>';
}
function BuildChurchTable(church)
{
       return '<div style="height:70px;overflow:auto;"><table width="100%"><tr align="left"><td>Type</td><td>' + church.Type + '</td></tr><tr align="left"><td>Address</td><td>' + church.Address + '</td></tr><tr align="left"><td>City, State Zip</td><td>' + church.City + ', ' + church.State + ' ' + church.ZipCode + '</td></tr><tr align="left"><td>Phone</td><td>' + church.Phone + '</td></tr></table></div>';
};
