﻿// ------------------------------------------------------
// Interface: All objects need to be able to mark as modified will need to implement this interface
// ------------------------------------------------------
var IModified = Class.create({
    Modified: false,
    initialize: function() {
        this.Modified = false;
    },

    IsModified: function() {
        return this.Modified;
    },

    MarkModified: function() {
        this.Modified = true;
    }
});

// ------------------------------------------------------
// Class: Pin colour of location map
// ------------------------------------------------------
var PinColour = Class.create({
    Text: null,
    Value: null,

    initialize: function(text, value) {
        this.Text = text;
        this.Value = value;
    }
});

// ------------------------------------------------------
// Class: Location Map
// ------------------------------------------------------

var LocationMap = Class.create(IModified,{

    // Properties
    MapImageUrl: null,
    LocationTypes: null,

    // Constructors
    initialize: function() {
        //private variable
        this.MapImageUrl = new String();
        this.LocationTypes = new Array();
    },

    // Public Methods
    AddLocationType: function(locType) {
        if (locType && locType instanceof LocationType) {
            if (this.LocationTypes && this.LocationTypes instanceof Array) {
                this.LocationTypes.push(locType);
                this.LocationTypes = this.SortLocationTypesByName();
                this.MarkModified();
            }
        }
    },

    RemoveLocationType: function(locType) {
        var result = this.IndexOfLocationTypeByName(locType);
        if (result >= 0) {
            this.LocationTypes.splice(result, 1);
            this.MarkModified();
        }

        return result;
    },

    IndexOfLocationTypeByName: function(typeName) {
        var result = -1;

        if (this.LocationTypes) {
            this.LocationTypes.each(function(item, index) {
                if (item && item.Name.toLowerCase() == typeName.toLowerCase()) {
                    result = index;
                }
            });
        }

        return result;
    },

    FindLocationTypeByName: function(typeName) {
        return this.LocationTypes.find(function(item) {
            return item.Name.toLowerCase() == typeName.toLowerCase();
        });
    },

    SortLocationTypesByName: function() {
        if (this.LocationTypes && this.LocationTypes instanceof Array) {
            return this.LocationTypes.sortBy(function(item) {
                return item.Name;
            });
        }
    }
});

// ------------------------------------------------------
// Class: Location Type
// ------------------------------------------------------

var LocationType = Class.create(IModified, {

    // Properties
    Name: null,
    Description: null,
    Locations: null,
    PinColour: null,

    // Constructors
    initialize: function(name)
    {
        this.Name = name;
        this.Description = "";
        this.PinColour = new PinColour("Black", "black.gif");
        this.Locations = new Array();
    },

    // Public Methods
    AddLocation: function(loc)
    {
        if (loc && loc instanceof Location)
        {
            if (this.Locations && this.Locations instanceof Array)
            {
                this.Locations.push(loc);
                this.Locations = this.SortLocationsByName();
                this.MarkModified();
            }
        }
    },

    SetPinColour: function(text, value)
    {
        if (this.PinColour == null)
        {
            this.PinColour = new PinColour(text, value);
        }
        else
        {
            this.PinColour.Text = text;
            this.PinColour.Value = value;
        }
    },

    FindLocationByName: function(name)
    {
        return this.Locations.find(function(item)
        {
            return item.Name.toLowerCase() == name.toLowerCase();
        });
    },

    RemoveLocation: function(name)
    {
        var result = this.IndexOfLocationByName(name);
        if (result >= 0)
        {
            this.Locations.splice(result, 1);
            this.MarkModified();
        }

        return result;
    },

    IndexOfLocationByName: function(name)
    {
        var result = -1;

        if (this.Locations)
        {
            this.Locations.each(function(item, index)
            {
                if (item && item.Name.toLowerCase() == name.toLowerCase())
                {
                    result = index;
                }
            });
        }

        return result;
    },

    SortLocationsByName: function()
    {
        if (this.Locations && this.Locations instanceof Array)
        {
            return this.Locations.sortBy(function(item)
            {
                return item.Name;
            });
        }
    }
});

// ------------------------------------------------------
// Class: Location
// ------------------------------------------------------

var Location = Class.create(IModified,{

    // Properties
    Name: null,
    Top: null,
    Left: null,
    Manager: null,
    Telephone: null,
    Switchboard: null,
    AddressLine1: null,
    AddressLine2: null,
    AddressLine3: null,
    County: null,
    Postcode: null,

    // Constructors
    initialize: function(name) {
        this.Name = name;
        this.Top = 0;
        this.Left = 0;
        this.Manager = new String();
        this.Telephone = new String();
        this.Switchboard = new String();
        this.AddressLine1 = new String();
        this.AddressLine2 = new String();
        this.AddressLine3 = new String();
        this.County = new String();
        this.Postcode = new String();
    }
});
