﻿//namespace PhysicianApp.PhysicianApp.Components
if (PhysicianApp == null || typeof (PhysicianApp) == "undefined")
    var PhysicianApp = function () { }

if (PhysicianApp.PhysicianApp == null || typeof (PhysicianApp.PhysicianApp) == "undefined")
    PhysicianApp.PhysicianApp = function () { }

if (PhysicianApp.PhysicianApp.Components == null || typeof (PhysicianApp.PhysicianApp.Components) == "undefined")
    PhysicianApp.PhysicianApp.Components = function () { }

//
// Class: RadiusInput
//
// Client side wrapper for a RadiusInput user control
//
PhysicianApp.PhysicianApp.Components.RadiusInput = function (id, addressId, cityId, stateId, zipId, distanceId) {
    this.addressId = addressId;
    this.cityId = cityId;
    this.stateId = stateId;
    this.zipId = zipId;
    this.distanceId = distanceId;
    this.id = id;
}

PhysicianApp.PhysicianApp.Components.RadiusInput.prototype.id = null;

//
// The id to the address RadTextBox
//
PhysicianApp.PhysicianApp.Components.RadiusInput.prototype.addressId = null;

//
// The id to the city RadTextBox
//
PhysicianApp.PhysicianApp.Components.RadiusInput.prototype.cityId = null;

//
// The id to the state RadComboBox
//
PhysicianApp.PhysicianApp.Components.RadiusInput.prototype.stateId = null;

//
// The id to the zip RadTextBox
//
PhysicianApp.PhysicianApp.Components.RadiusInput.prototype.zipId = null;

//
// The id to the distance RadComboBox
//
PhysicianApp.PhysicianApp.Components.RadiusInput.prototype.distanceId = null;

//
// An array of instances of the RadiusInput class that are created when a RadiusInput user control renders
//
PhysicianApp.PhysicianApp.Components.RadiusInput.Controls = new Array();

//
// Try to find an instance of this class based on the id of a RadiusInput user control
//
PhysicianApp.PhysicianApp.Components.RadiusInput.GetInstance = function (id) {
    for (var i = 0; i < PhysicianApp.PhysicianApp.Components.RadiusInput.Controls.length; i++) {
        var ref = PhysicianApp.PhysicianApp.Components.RadiusInput.Controls[i];

        if (ref.get_Id() == id)
            return ref;
    }

    return null;
}

PhysicianApp.PhysicianApp.Components.RadiusInput.prototype.get_Id = function () {
    return this.id;
}

PhysicianApp.PhysicianApp.Components.RadiusInput.prototype.get_Address = function () {
    var addressCtrl = $find(this.addressId);

    if (addressCtrl != null) {
        return addressCtrl.get_value();
    }

    return null;
}

PhysicianApp.PhysicianApp.Components.RadiusInput.prototype.get_City = function () {
    var cityCtrl = $find(this.cityId);

    if (cityCtrl != null) {
        return cityCtrl.get_value();
    }

    return null;
}

PhysicianApp.PhysicianApp.Components.RadiusInput.prototype.get_State = function () {
    var stateCtrl = $find(this.stateId);

    if (stateCtrl != null) {
        return stateCtrl.get_value();
    }

    return null;
}

PhysicianApp.PhysicianApp.Components.RadiusInput.prototype.get_Zip = function () {
    var zipCtrl = $find(this.zipId);

    if (zipCtrl != null) {
        return zipCtrl.get_value();
    }

    return null;
}

PhysicianApp.PhysicianApp.Components.RadiusInput.prototype.get_Distance = function () {
    var distanceCtrl = $find(this.distanceId);

    if (distanceCtrl != null) {
        var distance = distanceCtrl.get_value();

        if (distance == null || typeof(distance) == "undefined" || distance == "")
            return 0;

        return parseInt(distance);
    }

    return null;
}

//
// This function assumes all nessessary script includes for google maps are in place on the page that is
// calling this function.
//
// Parameters:
//   callback: function(latitude [decimal], longitude[decimal], success [bool]) - The function that is called when the address has been geocoded
//
// Returns (object): { latitude: the latitude coordinate of the address, longitude: the longitude coordinate of the address }
//   NULL: the address could not be geocoded
//
PhysicianApp.PhysicianApp.Components.RadiusInput.prototype.geocode = function (callback) {
    var geocoder = new google.maps.ClientGeocoder();
    var city = this.get_City();
    var state = this.get_State();
    var zip = this.get_Zip();
    var address = this.get_Address();
    var hasAddress = address != null && address != "";
    var hasCity = city != null && city != "";
    var hasState = state != null && state != "";
    var hasZip = zip != null && zip != "";

    var geoCodeAddress = "";

    if (hasAddress)
        geoCodeAddress += address;

    if (hasCity) {
        if (hasAddress)
            geoCodeAddress += ", ";

        geoCodeAddress += city;
    }

    if (hasState) {
        if (hasAddress || hasCity)
            geoCodeAddress += ", ";

        geoCodeAddress += state;
    }

    if (hasZip) {
        if (hasAddress || hasCity || hasState)
            geoCodeAddress += ", ";

        geoCodeAddress += zip;
    }

    if (geoCodeAddress == "") {
        if (callback != null) {
            callback(0, 0, false);
            return;
        }
    }

    geocoder.getLatLng(geoCodeAddress,
        function (point) {
            if (point) {
                if (callback != null)
                    callback(point.lat(), point.lng(), true);
            }
            else {
                if (callback != null)
                    callback(0, 0, false);
            }
        }
    );
}
