﻿
Type.registerNamespace("ThreeIreland");

//ThreeIreland.SiteShape = function(pinsClustered, //Number of pins in the cluster
//                                  clusterNumColor,     //Color for the number of pins in the cluster   
//                                  forceOpacityTo1,
//                                  zoom,    //Map's current zoom
//                                  lat, lon,     //Latitude & longitude for the central point
//                                  centralImgPin, //Pin image (coloured circle) for the center of the site
//                                  cell1Radius, cell2Radius, cell3Radius, //Radius for the cells 1-3
//                                  cell4Radius, cell5Radius, cell6Radius, //Radius for the cells 3-6
//                                  cell7Radius, cell8Radius, cell9Radius, //Radius for the cells 6-9
//                                  cell1ColR, cell1ColG, cell1ColB, cell1Opacity, //Cell1 Color
//                                  cell2ColR, cell2ColG, cell2ColB, cell2Opacity, //Cell2 Color
//                                  cell3ColR, cell3ColG, cell3ColB, cell3Opacity, //Cell3 Color
//                                  cell4ColR, cell4ColG, cell4ColB, cell4Opacity, //Cell4 Color
//                                  cell5ColR, cell5ColG, cell5ColB, cell5Opacity, //Cell5 Color
//                                  cell6ColR, cell6ColG, cell6ColB, cell6Opacity, //Cell6 Color
//                                  cell7ColR, cell7ColG, cell7ColB, cell7Opacity, //Cell7 Color
//                                  cell8ColR, cell8ColG, cell8ColB, cell8Opacity, //Cell8 Color
//                                  cell9ColR, cell9ColG, cell9ColB, cell9Opacity, //Cell9 Color
//                                  cell1Angle, cell2Angle, cell3Angle, //Angles for the three cells 1-3
//                                  cell4Angle, cell5Angle, cell6Angle, //Angles for the three cells 3-6
//                                  cell7Angle, cell8Angle, cell9Angle, //Angles for the three cells 6-9
//                                  siteCongestionLevel, //Site congestionlevel
//                                  congestionLevelCell1, congestionLevelCell2, congestionLevelCell3, //Congestion levels  1-3
//                                  visibleCell1, visibleCell2, visibleCell3, //Visible (true/false) cells 1-3
//                                  visibleCell4, visibleCell5, visibleCell6, //Visible (true/false) cells 3-6
//                                  visibleCell7, visibleCell8, visibleCell9, //Visible (true/false) cells 6-9
//                                  siteDistance,  //Radius and angular distance to calculate cells
//                                  availabilityCell1, availabilityCell2, availabilityCell3
//                                  )


ThreeIreland.SiteShape = function(pinsClustered, //Number of pins in the cluster
                                  clusterNumColor,     //Color for the number of pins in the cluster   
                                  forceOpacityTo1,
                                  zoom,    //Map's current zoom
                                  lat, lon,     //Latitude & longitude for the central point
                                  centralImgPin, //Pin image (coloured circle) for the center of the site
                                  azimuths,
                                  siteCongestionLevel, //SITE CONGESTION LEVEL
                                  cellCongestions, //CONGESTION LEVELS
                                  siteAngularDistance,
                                  cellAvailabilities,
                                  siteDistance  //Radius and angular distance to calculate cells
                                  ) {

    /********/
    /* SITE */
    /********/

    //Shape objects
    this.CentreShape = null;

    //Centre point latitude and longitude
    this.Lat = lat;
    this.Lon = lon;

    //Current zoom
    this.MapZoom = zoom;


    /**********/
    /* CENTRE */
    /**********/

    //Image for the centre
    this.CentralImgPin = centralImgPin;


    /****************************/
    /*    CLUSTERED OR NOT??    */
    /****************************/

    if (pinsClustered != "") {
        //Cluster
        this.PinsClustered = pinsClustered;
        this.ClusterTxtColor = clusterNumColor;
    }
    else {
        this.AngularDistance = siteDistance; //angular distance covered on earth's surface

        //Cells

        //If there is a tile layer shown, force opacity to 1
        this.ForceOpacityTo1 = forceOpacityTo1;

        this.Azimuths = azimuths;
        this.CellCongestions = cellCongestions;
        this.SiteAngularDistance = siteAngularDistance;
        this.CellAvailabilities = cellAvailabilities;
        //##
        this.CellOpacity = 0.6;
        

 
        /******************/
        /* FORCE OPPACITY */
        /******************/
        if (this.ForceOpacityTo1) {
            this.CellOpacity = 0.6;
        }
    }
}

ThreeIreland.SiteShape.prototype =
{
    //ADD SHAPE: Adds the shape to the layer received as parameter
    //----------

    AddToShapeLayer: function(veShapeLayer, drawCells) {
        if (drawCells == null) {
            if (this.MapZoom > 10) drawCells = true;
            else drawCells = false;
        }
        //Get VELatLon object
        var loc = new VELatLong(this.Lat, this.Lon);
        //Create the CENTRE - Images 30x30 because if they have another dimensions they aren't drawn properly
        var centre = new VEShape(VEShapeType.Pushpin, loc);

        if (this.PinsClustered > 0)//If it is clustered
        {
            if (Sys.Browser.agent == Sys.Browser.InternetExplorer && Sys.Browser.version == 6)
                centre.SetCustomIcon("<div style=\"filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../../Images/" + this.CentralImgPin +
                                 ".png', sizingMethod='image'); height:30px; width:30px; color:" + this.ClusterTxtColor +
                                 ";min-width:30px; min-height:30px; text-align: center;\"><div style=\"width:30px; height:30px;\" class=\"clusterPinsNum\">" + this.PinsClustered +
                                 "</div></div>");
            else
                centre.SetCustomIcon("<div style=\"background-image: url('../../Images/" + this.CentralImgPin +
                                 ".png'); background-repeat: no-repeat; color:" + this.ClusterTxtColor +
                                 ";min-width:30px; min-height:30px; text-align: center;\"><div style=\"width:30px; height:30px;\" class=\"clusterPinsNum\">" + this.PinsClustered +
                                 "</div></div>");
        }
        else {
            //Draw the CELLS if zoom > 10
            if (drawCells == true) {
                //CALCULATE THE RADIUS DEPENDING ON THE ZOOM
                /*radius = this.CalculateRadiusByZoom(this.Cell1Radius);//we work with the same radius for all the cells in the site
                var R = 6371; // earth's mean radius in km
                var d = parseFloat(radius) / R;  // d = angular distance covered on earth's surface*/

                var lat = (loc.Latitude * Math.PI) / 180; //rad
                var lon = (loc.Longitude * Math.PI) / 180; //rad
                //ShowCirle TESTS: var circle = this.AddCircle(loc, new VEColor(255, 255, 255, this.Cell1Opacity), this.Cell1Angle, lat, lon);
                //ShowCirle TESTS: this.CircleShape = circle;
                //ShowCirle TESTS: veShapeLayer.AddShape(circle);
                //CELL 1
                //##


                var color = null;
                var cellAvailability;
                var cell;
                for (var i = 0; i < this.Azimuths.length; i++) {

                    availability = this.CellAvailabilities[i];
                    if (availability != null && availability != "Live" && availability != "") {
                        if (availability == "Unavailable") color = new VEColor(255, 0, 0, this.CellOpacity); //Red
                        else if (availability == "RecentlyUnavailable") color = new VEColor(255, 165, 0, this.CellOpacity); //Amber
                        else color = new VEColor(255, 255, 0, this.CellOpacity); //Yellow - unknown type
                    }
                    else {
                        switch (this.CellCongestions[i]) {
                            case 1: color = new VEColor(0, 165, 0, this.CellOpacity); break; //Dark green
                            case 3: color = new VEColor(178, 0, 255, this.CellOpacity); break; //Purple
                            case 4: color = new VEColor(255, 0, 0, this.CellOpacity); break; //Red
                            case 2: color = new VEColor(80, 80, 80, this.CellOpacity); break; //Grey
                            default: case 0: new VEColor(255, 255, 0, this.CellOpacity); break; //Yellow - unknown type
                        }
                    }

                        //Get the shape and add it
                        cell = this.AddCircleCells(loc,
                                                    color,
                                                    this.Azimuths[i],
                                                    lat,
                                                    lon);

                        veShapeLayer.AddShape(cell);
                    
                } //end for
            } //end if (drawCells == true) 

            if (Sys.Browser.agent == Sys.Browser.InternetExplorer && Sys.Browser.version == 6)
                centre.SetCustomIcon("<div style=\"filter:progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../../Images/" + this.CentralImgPin +
                                 ".png', sizingMethod='image'); height:30px; width:30px;\" class=\"pinItem\"></div>");
            else
                centre.SetCustomIcon("<img src='../../Images/" + this.CentralImgPin + ".png' class=\"pinItem\"/>");
        }
        this.CentreShape = centre;
        veShapeLayer.AddShape(centre);
    },

    //                if (this.VisibleCell1) {

    //                    var color = null;
    //                    if (this.AvailabilityCell1 != null && this.AvailabilityCell1 != "Live" && this.AvailabilityCell1 != "") {
    //                        if (this.AvailabilityCell1 == "Unavailable") color = new VEColor(255, 0, 0, this.CellOpacity); //Red
    //                        else if (this.AvailabilityCell1 == "RecentlyUnavailable") color = new VEColor(255, 165, 0, this.CellOpacity); //Amber
    //                        else color = new VEColor(255, 255, 0, this.CellOpacity); //Yellow - unknown type
    //                    }
    //                    else {
    //                        switch (this.CongestionLevel1) {
    //                            case 1: color = new VEColor(0, 165, 0, this.CellOpacity); break; //Dark green
    //                            case 2: color = new VEColor(80, 80, 80, this.CellOpacity); break; //Grey
    //                            case 3: color = new VEColor(178, 0, 255, this.CellOpacity); break; //Purple
    //                            case 4: color = new VEColor(255, 0, 0, this.CellOpacity); break; //Red
    //                            default: case 0: color = new VEColor(this.Cell1ColR, this.Cell1ColG, this.Cell1ColB, this.CellOpacity); break;
    //                        }

    //                    }

    //                    //Get the shape and add it
    //                    var cell1 = this.AddCircleCells(loc,
    //                                                    color,
    //                                                    this.Cell1Angle,
    //                                                    lat,
    //                                                    lon);

    //                    this.Cell1Shape = cell1;
    //                    veShapeLayer.AddShape(cell1);
    //                }
    //                //CELL 2
    //                if (this.VisibleCell2) {
    //                    //Define colour according to the CONGESTION
    //                    if (this.AvailabilityCell2 != null && this.AvailabilityCell2 != "Live" && this.AvailabilityCell2 != "") {
    //                        if (this.AvailabilityCell2 == "Unavailable") color = new VEColor(255, 0, 0, this.Cell2Opacity); //Red
    //                        else if (this.AvailabilityCell2 == "RecentlyUnavailable") color = new VEColor(255, 165, 0, this.Cell2Opacity); //Amber
    //                        else color = new VEColor(255, 255, 0, this.Cell2Opacity); //Yellow - unknown type
    //                    }
    //                    else {
    //                        switch (this.CongestionLevel2) {
    //                            case 1: color = new VEColor(0, 165, 0, this.Cell2Opacity); break; //Dark green
    //                            case 2: color = new VEColor(80, 80, 80, this.Cell2Opacity); break; //Grey
    //                            case 3: color = new VEColor(178, 0, 255, this.Cell2Opacity); break; //Purple
    //                            case 4: color = new VEColor(255, 0, 0, this.Cell2Opacity); break; //Red
    //                            default: case 0: color = new VEColor(this.Cell2ColR, this.Cell2ColG, this.Cell2ColB, this.Cell2Opacity); break;
    //                        }
    //                    }



    //                    //Get the shape and add it
    //                    var cell2 = this.AddCircleCells(loc,
    //                                                    color,
    //                                                    this.Cell2Angle,
    //                                                    lat,
    //                                                    lon);

    //                    this.Cell2Shape = cell2;
    //                    veShapeLayer.AddShape(cell2);
    //                }
    //                //CELL 3
    //                if (this.VisibleCell3) {
    //                    //Define colour according to the CONGESTION
    //                    if (this.AvailabilityCell3 != null && this.AvailabilityCell3 != "Live" && this.AvailabilityCell3 != "") {
    //                        if (this.AvailabilityCell3 == "Unavailable") color = new VEColor(255, 0, 0, this.Cell3Opacity); //Red
    //                        else if (this.AvailabilityCell3 == "RecentlyUnavailable") color = new VEColor(255, 165, 0, this.Cell3Opacity); //Amber
    //                        else color = new VEColor(255, 255, 0, this.Cell3Opacity); //Yellow - unknown type
    //                    }
    //                    else {
    //                        switch (this.CongestionLevel3) {
    //                            case 1: color = new VEColor(0, 165, 0, this.Cell3Opacity); break; //Dark green
    //                            case 2: color = new VEColor(80, 80, 80, this.Cell3Opacity); break; //Grey
    //                            case 3: color = new VEColor(178, 0, 255, this.Cell3Opacity); break; //Purple
    //                            case 4: color = new VEColor(255, 0, 0, this.Cell3Opacity); break; //Red
    //                            default: case 0: color = new VEColor(this.Cell3ColR, this.Cell3ColG, this.Cell3ColB, this.Cell3Opacity); break;
    //                        }
    //                    }

    //                    //Get the shape and add it
    //                    var cell3 = this.AddCircleCells(loc,
    //                                                    color,
    //                                                    this.Cell3Angle,
    //                                                    lat,
    //                                                    lon);
    //                    this.Cell3Shape = cell3;
    //                    veShapeLayer.AddShape(cell3);
    //                }
    //                //CELL 4
    //                if (this.VisibleCell4) {
    //                    alert("NOT IMPLEMENTED: Code for congestion level color. See SitheShape.js, line 385.");
    //                    var cell4 = this.AddCircleCells(loc,
    //                                        new VEColor(this.Cell4ColR, this.Cell4ColG, this.Cell4ColB, this.Cell4Opacity),
    //                                        this.Cell4Angle,
    //                                        lat,
    //                                        lon);

    //                    this.Cell4Shape = cell4;
    //                    veShapeLayer.AddShape(cell4);
    //                }
    //                //CELL 5
    //                if (this.VisibleCell5) {
    //                    alert("NOT IMPLEMENTED: Code for congestion level color. See SitheShape.js, line 396.");
    //                    var cell5 = this.AddCircleCells(loc,
    //                                        new VEColor(this.Cell5ColR, this.Cell5ColG, this.Cell5ColB, this.Cell5Opacity),
    //                                        this.Cell5Angle,
    //                                        lat,
    //                                        lon);

    //                    this.Cell5Shape = cell5;
    //                    veShapeLayer.AddShape(cell5);
    //                }
    //                //CELL 6
    //                if (this.VisibleCell6) {
    //                    alert("NOT IMPLEMENTED: Code for congestion level color. See SitheShape.js, line 407.");
    //                    var cell6 = this.AddCircleCells(loc,
    //                                        new VEColor(this.Cell6ColR, this.Cell6ColG, this.Cell6ColB, this.Cell6Opacity),
    //                                        this.Cell6Angle,
    //                                        lat,
    //                                        lon);

    //                    this.Cell6Shape = cell6;
    //                    veShapeLayer.AddShape(cell6);
    //                }
    //                //CELL 7
    //                if (this.VisibleCell7) {
    //                    alert("NOT IMPLEMENTED: Code for congestion level color. See SitheShape.js, line 418.");
    //                    var cell7 = this.AddCircleCells(loc,
    //                                        new VEColor(this.Cell7ColR, this.Cell7ColG, this.Cell7ColB, this.Cell7Opacity),
    //                                        this.Cell7Angle,
    //                                        lat,
    //                                        lon);

    //                    this.Cell7Shape = cell7;
    //                    veShapeLayer.AddShape(cell7);
    //                }
    //                //CELL 8
    //                if (this.VisibleCell8) {
    //                    alert("NOT IMPLEMENTED: Code for congestion level color. See SitheShape.js, line 429.");
    //                    var cell8 = this.AddCircleCells(loc,
    //                                        new VEColor(this.Cell8ColR, this.Cell8ColG, this.Cell8ColB, this.Cell8Opacity),
    //                                        this.Cell8Angle,
    //                                        lat,
    //                                        lon);

    //                    this.Cell8Shape = cell8;
    //                    veShapeLayer.AddShape(cell8);
    //                }
    //                //CELL 9
    //                if (this.VisibleCell9) {
    //                    alert("NOT IMPLEMENTED: Code for congestion level color. See SitheShape.js, line 440.");
    //                    var cell9 = this.AddCircleCells(loc,
    //                                        new VEColor(this.Cell9ColR, this.Cell9ColG, this.Cell9ColB, this.Cell9Opacity),
    //                                        this.Cell9Angle,
    //                                        lat,
    //                                        lon);

    //                    this.Cell9Shape = cell9;
    //                    veShapeLayer.AddShape(cell9);
    //                }


    //ADD THE CELLS: Also a circle in which we only draw the area we want
    //-------------- according to the angle received.
    //-------------- Based on: http://viavirtualearth.com/Wiki/Draw+a+circle.ashx
    AddCircleCells: function(loc, color, angle, lat, lon) {
        /* NOTE: 
        *  Azimuth angles increase anti-clockwise starting on the north (N)
        * 
        *              (N)0°
        *           \   |   /                For each, angle we draw the points in the circle found in the area between angle-30 and angle+30
        *            \  |  /                 Angles between 30 and 130 aren't a problem, but for those that contain the 0, we will need to 
        *             \ | /                    make two loops, one from 0 till the maxium value smaller than 30 and a second one from
        *              \|/                     the minium value bigger than 330 to 360
        *(W)270°--------+-------- 90°(E)
        *               |
        *               |
        *               |
        *               |  
        *              (S)180°
        */


        var d = this.AngularDistance;
        //DRAW THE CELL
        var locs = new Array();
        var initialPoint = 0;
        var finalPoint = 0;
        var centreAdded = false;
        if ((angle >= 30) && (angle <= 330))  //Standard auxiliar angles (between 30° & 330°)
        {
            locs.push(loc);
            initialPoint = angle - 30;
            finalPoint = angle + 30;
            for (x = initialPoint; x <= finalPoint; x += 10) {
                var p2 = new VELatLong(0, 0)
                brng = this.LatLonToRadians(x); //rad
                p2.Latitude = Math.asin(Math.sin(lat) * Math.cos(d) + Math.cos(lat) * Math.sin(d) * Math.cos(brng));
                p2.Longitude = ((lon + Math.atan2(Math.sin(brng) * Math.sin(d) * Math.cos(lat), Math.cos(d) - Math.sin(lat) * Math.sin(p2.Latitude))) * 180) / Math.PI;
                p2.Latitude = (p2.Latitude * 180) / Math.PI;
                locs.push(p2);
            }
        }
        else if (angle == 0) //Auxiliar angles (between 0° & 30°)
        {
            //Points from 330(MIN) to 360
            initialPoint = 360 - 30 + angle;
            finalPoint = 360;
            for (x = initialPoint; x < finalPoint; x += 10) {
                var p1 = new VELatLong(0, 0)
                brng = this.LatLonToRadians(x); //rad
                p1.Latitude = Math.asin(Math.sin(lat) * Math.cos(d) + Math.cos(lat) * Math.sin(d) * Math.cos(brng));
                p1.Longitude = ((lon + Math.atan2(Math.sin(brng) * Math.sin(d) * Math.cos(lat), Math.cos(d) - Math.sin(lat) * Math.sin(p1.Latitude))) * 180) / Math.PI;
                p1.Latitude = (p1.Latitude * 180) / Math.PI;
                locs.push(p1);
            }
            //Points from 0 to 30(MAX)
            initialPoint = 0;
            finalPoint = angle + 30;
            for (x = initialPoint; x < finalPoint; x += 10) {
                var p2 = new VELatLong(0, 0)
                brng = this.LatLonToRadians(x); //rad
                p2.Latitude = Math.asin(Math.sin(lat) * Math.cos(d) + Math.cos(lat) * Math.sin(d) * Math.cos(brng));
                p2.Longitude = ((lon + Math.atan2(Math.sin(brng) * Math.sin(d) * Math.cos(lat), Math.cos(d) - Math.sin(lat) * Math.sin(p2.Latitude))) * 180) / Math.PI;
                p2.Latitude = (p2.Latitude * 180) / Math.PI;
                locs.push(p2);
            }
            //Last point smaller than 30
            var pFinal = new VELatLong(0, 0)
            brng = this.LatLonToRadians(finalPoint); //rad
            pFinal.Latitude = Math.asin(Math.sin(lat) * Math.cos(d) + Math.cos(lat) * Math.sin(d) * Math.cos(brng));
            pFinal.Longitude = ((lon + Math.atan2(Math.sin(brng) * Math.sin(d) * Math.cos(lat), Math.cos(d) - Math.sin(lat) * Math.sin(pFinal.Latitude))) * 180) / Math.PI;
            pFinal.Latitude = (pFinal.Latitude * 180) / Math.PI;
            locs.push(pFinal);
            //Central point
            locs.push(loc);

        }
        else if ((angle > 0) && (angle < 30)) //Auxiliar angles (between 0° & 30°)
        {
            //In this angles we increment +32 or -32 to the angle because of some problems
            //found drawing the cells if we use 30 and  x += 5 or x += 1. It seems to be a 
            //VE bug, therefore we cannot solve it.

            //Points from 330(MIN) to 360
            initialPoint = 360 - 32 + angle;
            finalPoint = 360;
            for (x = initialPoint; x < finalPoint; x += 10) {
                var p1 = new VELatLong(0, 0)
                brng = this.LatLonToRadians(x); //rad
                p1.Latitude = Math.asin(Math.sin(lat) * Math.cos(d) + Math.cos(lat) * Math.sin(d) * Math.cos(brng));
                p1.Longitude = ((lon + Math.atan2(Math.sin(brng) * Math.sin(d) * Math.cos(lat), Math.cos(d) - Math.sin(lat) * Math.sin(p1.Latitude))) * 180) / Math.PI;
                p1.Latitude = (p1.Latitude * 180) / Math.PI;
                locs.push(p1);
            }
            //Points from 0 to 30(MAX)
            initialPoint = 0;
            finalPoint = angle + 32;
            for (x = initialPoint; x < finalPoint; x += 10) {
                var p2 = new VELatLong(0, 0)
                brng = this.LatLonToRadians(x); //rad
                p2.Latitude = Math.asin(Math.sin(lat) * Math.cos(d) + Math.cos(lat) * Math.sin(d) * Math.cos(brng));
                p2.Longitude = ((lon + Math.atan2(Math.sin(brng) * Math.sin(d) * Math.cos(lat), Math.cos(d) - Math.sin(lat) * Math.sin(p2.Latitude))) * 180) / Math.PI;
                p2.Latitude = (p2.Latitude * 180) / Math.PI;
                locs.push(p2);
            }
            //Last point smaller than 30
            var pFinal = new VELatLong(0, 0)
            brng = this.LatLonToRadians(finalPoint); //rad
            pFinal.Latitude = Math.asin(Math.sin(lat) * Math.cos(d) + Math.cos(lat) * Math.sin(d) * Math.cos(brng));
            pFinal.Longitude = ((lon + Math.atan2(Math.sin(brng) * Math.sin(d) * Math.cos(lat), Math.cos(d) - Math.sin(lat) * Math.sin(pFinal.Latitude))) * 180) / Math.PI;
            pFinal.Latitude = (pFinal.Latitude * 180) / Math.PI;
            locs.push(pFinal);
            //Central point
            locs.push(loc);

        }
        else //Auxiliar angles (between 330° & 360°)
        {
            //Points from 0 to 30(MAX)
            initialPoint = 0;
            finalPoint = angle - 360 + 30;
            for (x = initialPoint; x < finalPoint; x += 10) {
                var p2 = new VELatLong(0, 0)
                brng = this.LatLonToRadians(x); //rad
                p2.Latitude = Math.asin(Math.sin(lat) * Math.cos(d) + Math.cos(lat) * Math.sin(d) * Math.cos(brng));
                p2.Longitude = ((lon + Math.atan2(Math.sin(brng) * Math.sin(d) * Math.cos(lat), Math.cos(d) - Math.sin(lat) * Math.sin(p2.Latitude))) * 180) / Math.PI;
                p2.Latitude = (p2.Latitude * 180) / Math.PI;
                locs.push(p2);
            }
            //Last point smaller than 30
            var p2 = new VELatLong(0, 0)
            brng = this.LatLonToRadians(finalPoint); //rad
            p2.Latitude = Math.asin(Math.sin(lat) * Math.cos(d) + Math.cos(lat) * Math.sin(d) * Math.cos(brng));
            p2.Longitude = ((lon + Math.atan2(Math.sin(brng) * Math.sin(d) * Math.cos(lat), Math.cos(d) - Math.sin(lat) * Math.sin(p2.Latitude))) * 180) / Math.PI;
            p2.Latitude = (p2.Latitude * 180) / Math.PI;
            locs.push(p2);
            //Central point
            locs.push(loc);
            //Points from 330(MIN) to 360
            initialPoint = angle - 30;
            finalPoint = 360;
            for (x = initialPoint; x <= finalPoint; x += 10) {
                var p2 = new VELatLong(0, 0)
                brng = this.LatLonToRadians(x); //rad
                p2.Latitude = Math.asin(Math.sin(lat) * Math.cos(d) + Math.cos(lat) * Math.sin(d) * Math.cos(brng));
                p2.Longitude = ((lon + Math.atan2(Math.sin(brng) * Math.sin(d) * Math.cos(lat), Math.cos(d) - Math.sin(lat) * Math.sin(p2.Latitude))) * 180) / Math.PI;
                p2.Latitude = (p2.Latitude * 180) / Math.PI;
                locs.push(p2);
            }
        }
        //CREATE THE POLYGON SHAPE OBJECT
        var cell = new VEShape(VEShapeType.Polygon, locs);
        cell.HideIcon();
        cell.SetLineColor(new VEColor(0, 0, 255, 1));
        cell.SetFillColor(color);
        cell.SetLineWidth(1);
        return cell;
    },
    //ShowCirle TESTS: Useful for testings, draws a circle that contains the cells
    AddCircle: function(loc, color, angle, lat, lon) {
        var d = this.AngularDistance;
        //DRAW THE CELL
        var locs = new Array();
        var initialPoint = 0;
        var finalPoint = 0;
        var centreAdded = false;
        initialPoint = 0;
        finalPoint = 360;
        for (x = initialPoint; x <= finalPoint; x += 10) {
            var p2 = new VELatLong(0, 0)
            brng = this.LatLonToRadians(x); //rad
            p2.Latitude = Math.asin(Math.sin(lat) * Math.cos(d) + Math.cos(lat) * Math.sin(d) * Math.cos(brng));
            p2.Longitude = ((lon + Math.atan2(Math.sin(brng) * Math.sin(d) * Math.cos(lat), Math.cos(d) - Math.sin(lat) * Math.sin(p2.Latitude))) * 180) / Math.PI;
            p2.Latitude = (p2.Latitude * 180) / Math.PI;
            locs.push(p2);
        }
        //CREATE THE POLYGON SHAPE OBJECT
        var cell = new VEShape(VEShapeType.Polygon, locs);
        cell.HideIcon();
        cell.SetLineColor(new VEColor(255, 0, 0, 1));
        cell.SetFillColor(color);
        cell.SetLineWidth(1);
        return cell;
    },
    //CONVERT ANGLE: We need to convert our angle because the circle is drawn
    //-------------- clockwise and angles increment anticlockwise.
    //               Retuns an auxiliar angle. F.e. the real 0° will be converted to 90°
    ConvertAngle: function(angle) {
        var auxAng = angle;
        //FORMULA: (MaxValueInTheRange - RealAngle) + (FullRightAnglesToPass * 90)
        if ((angle >= 0) && (angle < 90))
            auxAng = 90 - angle;
        else if ((angle >= 90) && (angle < 180))
            auxAng = (180 - angle) + 270;
        else if ((angle >= 180) && (angle < 270))
            auxAng = (270 - angle) + 180;
        else if ((angle >= 270) && (angle <= 360))
            auxAng = (360 - angle) + 90;
        return auxAng;
    },
    //CONVERT LatLon: convert lat/long in degrees to radians
    //--------------- http://viavirtualearth.com/Wiki/distance.ashx
    LatLonToRadians: function(point) {
        return point * Math.PI / 180;
    },
    //DELETE SHAPE: Deletes the shape from the layer received as parameter
    //-------------
    DeleteFromShapeLayer: function(veShapeLayer) {
        veShapeLayer.DeleteShape(this.CentreShape);
        if (this.Cell1Shape != null) {
            if (this.VisibleCell1)
                veShapeLayer.DeleteShape(this.Cell1Shape);
            if (this.VisibleCell2)
                veShapeLayer.DeleteShape(this.Cell2Shape);
            if (this.VisibleCell3)
                veShapeLayer.DeleteShape(this.Cell3Shape);
            if (this.VisibleCell4)
                veShapeLayer.DeleteShape(this.Cell4Shape);
            if (this.VisibleCell5)
                veShapeLayer.DeleteShape(this.Cell5Shape);
            if (this.VisibleCell6)
                veShapeLayer.DeleteShape(this.Cell6Shape);
            if (this.VisibleCell7)
                veShapeLayer.DeleteShape(this.Cell7Shape);
            if (this.VisibleCell8)
                veShapeLayer.DeleteShape(this.Cell8Shape);
            if (this.VisibleCell9)
                veShapeLayer.DeleteShape(this.Cell9Shape);
            //ShowCirle TESTS: veShapeLayer.DeleteShape(this.CircleShape);
        }
    }
}

ThreeIreland.SiteShape.registerClass('ThreeIreland.SiteShape');

if (typeof (Sys) !== "undefined") Sys.Application.notifyScriptLoaded();


