﻿
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
{

    /********/
    /* 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
        this.Cell1Shape = null;
        this.Cell2Shape = null;
        this.Cell3Shape = null;
        this.Cell4Shape = null;
        this.Cell5Shape = null;
        this.Cell6Shape = null;
        this.Cell7Shape = null;
        this.Cell8Shape = null;
        this.Cell9Shape = null;
        //ShowCirle TESTS: this.CircleShape = null;
        
        //If there is a tile layer shown, force opacity to 1
        this.ForceOpacityTo1 = forceOpacityTo1;

        //Site Congestion level
        this.SiteCongestionLevel = siteCongestionLevel;
        //Congestion Level CELL 1
        this.CongestionLevel1 = congestionLevelCell1;
        //Congestion Level CELL 3
        this.CongestionLevel3 = congestionLevelCell3;
        //Congestion Level CELL 2
        this.CongestionLevel2 = congestionLevelCell2;

        //COMMENTED OUT: The client doesn't want this anymore
        //Congestion level for all the cells is changed to the largest cell congestion
        /*if (siteCongestionLevel != -1)
        {
            // Congestion level for all the cells is changed to the largest cell congestion
            var LargestCellCongestion = congestionLevelCell1;
            if (congestionLevelCell2 > LargestCellCongestion)
                LargestCellCongestion = congestionLevelCell2;
            if (congestionLevelCell3 > LargestCellCongestion)
                LargestCellCongestion = congestionLevelCell3;
            if (siteCongestionLevel >= LargestCellCongestion)
            {
                //Congestion Level CELL 1
                this.CongestionLevel1 = siteCongestionLevel;
                //Congestion Level CELL 3
                this.CongestionLevel3 = siteCongestionLevel;
                //Congestion Level CELL 2
                this.CongestionLevel2 = siteCongestionLevel;
            }
            else
            {
                //Congestion Level CELL 1
                this.CongestionLevel1 = congestionLevelCell1;
                //Congestion Level CELL 3
                this.CongestionLevel3 = congestionLevelCell3;
                //Congestion Level CELL 2
                this.CongestionLevel2 = congestionLevelCell2;
            }
        }*/


        /**********/
        /* CELL 1 */
        /**********/

        //Opacity for the CELL 1
        this.Cell1ColR = cell1ColR;
        this.Cell1ColG = cell1ColG;
        this.Cell1ColB = cell1ColB;
        //Opacity for the CELL 1
        this.Cell1Opacity = cell1Opacity;
        //Agle for the CELL 1
        this.Cell1Angle = cell1Angle;
        //Radious for the CELL 1
        this.Cell1Radius = cell1Radius;
        //Is visible CELL 1
        this.VisibleCell1 = visibleCell1;

        /**********/
        /* CELL 2 */
        /**********/

        //Opacity for the CELL 2
        this.Cell2ColR = cell2ColR;
        this.Cell2ColG = cell2ColG;
        this.Cell2ColB = cell2ColB;
        //Opacity for the CELL 2
        this.Cell2Opacity = cell2Opacity;
        //Agle for the CELL 2
        this.Cell2Angle = cell2Angle;
        //Radious for the CELL 2
        this.Cell2Radius = cell2Radius;
        //Is visible CELL 2
        this.VisibleCell2 = visibleCell2;


        /**********/
        /* CELL 3 */
        /**********/

        //Opacity for the CELL3
        this.Cell3ColR = cell3ColR;
        this.Cell3ColG = cell3ColG;
        this.Cell3ColB = cell3ColB;
        //Opacity for the CELL 3
        this.Cell3Opacity = cell3Opacity;
        //Agle for the CELL 3
        this.Cell3Angle = cell3Angle;
        //Radious for the CELL 3
        this.Cell3Radius = cell3Radius;
        //Is visible CELL 3
        this.VisibleCell3 = visibleCell3;


        /**********/
        /* CELL 4 */
        /**********/

        //Opacity for the CELL4
        this.Cell4ColR = cell4ColR;
        this.Cell4ColG = cell4ColG;
        this.Cell4ColB = cell4ColB;
        //Opacity for the CELL 4
        this.Cell4Opacity = cell4Opacity;
        //Agle for the CELL 4
        this.Cell4Angle = cell4Angle;
        //Radious for the CELL 4
        this.Cell4Radius = cell4Radius;
        //Is visible CELL 4
        this.VisibleCell4 = visibleCell4;

        /**********/
        /* CELL 5 */
        /**********/

        //Opacity for the CELL5
        this.Cell5ColR = cell5ColR;
        this.Cell5ColG = cell5ColG;
        this.Cell5ColB = cell5ColB;
        //Opacity for the CELL 5
        this.Cell5Opacity = cell5Opacity;
        //Agle for the CELL 5
        this.Cell5Angle = cell5Angle;
        //Radious for the CELL 5
        this.Cell5Radius = cell5Radius;
        //Is visible CELL 5
        this.VisibleCell5 = visibleCell5;

        /**********/
        /* CELL 6 */
        /**********/

        //Opacity for the CELL6
        this.Cell6ColR = cell6ColR;
        this.Cell6ColG = cell6ColG;
        this.Cell6ColB = cell6ColB;
        //Opacity for the CELL 6
        this.Cell6Opacity = cell6Opacity;
        //Agle for the CELL 6
        this.Cell6Angle = cell6Angle;
        //Radious for the CELL 6
        this.Cell6Radius = cell6Radius;
        //Is visible CELL 6
        this.VisibleCell6 = visibleCell6;

        /**********/
        /* CELL 7 */
        /**********/

        //Opacity for the CELL7
        this.Cell7ColR = cell7ColR;
        this.Cell7ColG = cell7ColG;
        this.Cell7ColB = cell7ColB;
        //Opacity for the CELL 7
        this.Cell7Opacity = cell7Opacity;
        //Agle for the CELL 7
        this.Cell7Angle = cell7Angle;
        //Radious for the CELL 7
        this.Cell7Radius = cell7Radius;
        //Is visible CELL 7
        this.VisibleCell7 = visibleCell7;

        /**********/
        /* CELL 8 */
        /**********/

        //Opacity for the CELL8
        this.Cell8ColR = cell8ColR;
        this.Cell8ColG = cell8ColG;
        this.Cell8ColB = cell8ColB;
        //Opacity for the CELL 8
        this.Cell8Opacity = cell8Opacity;
        //Agle for the CELL 8
        this.Cell8Angle = cell8Angle;
        //Radious for the CELL 8
        this.Cell8Radius = cell8Radius;
        //Is visible CELL 8
        this.VisibleCell8 = visibleCell8;

        /**********/
        /* CELL 9 */
        /**********/

        //Opacity for the CELL9
        this.Cell9ColR = cell9ColR;
        this.Cell9ColG = cell9ColG;
        this.Cell9ColB = cell9ColB;
        //Opacity for the CELL 9
        this.Cell9Opacity = cell9Opacity;
        //Agle for the CELL 9
        this.Cell9Angle = cell9Angle;
        //Radious for the CELL 9
        this.Cell9Radius = cell9Radius;
        //Is visible CELL 9
        this.VisibleCell9 = visibleCell9;

        /******************/
        /* FORCE OPPACITY */
        /******************/
        if (this.ForceOpacityTo1)
        {
            this.Cell1Opacity = 1;
            this.Cell2Opacity = 1;
            this.Cell3Opacity = 1;
            this.Cell4Opacity = 1;
            this.Cell5Opacity = 1;
            this.Cell6Opacity = 1;
            this.Cell7Opacity = 1;
            this.Cell8Opacity = 1;
            this.Cell9Opacity = 1;
        }
    }
}

ThreeIreland.SiteShape.prototype =
{
    //ADD SHAPE: Adds the shape to the layer received as parameter
    //----------
    AddToShapeLayer: function(veShapeLayer)
    {
        //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 (this.MapZoom > 10)
            {
                //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
                if (this.VisibleCell1)
                {
                    //Define colour according to the CONGESTION
                    var color = null;
                    if (this.CongestionLevel1 == 1)
                        color = new VEColor(0, 128, 0, this.Cell1Opacity);
                    else if (this.CongestionLevel1 == 2)
                        color = new VEColor(80, 80, 80, this.Cell1Opacity);
                    else if (this.CongestionLevel1 == 3)
                        color = new VEColor(255, 165, 0, this.Cell1Opacity);
                    else if (this.CongestionLevel1 == 4)
                        color = new VEColor(255, 0, 0, this.Cell1Opacity);
                    else
                        color = new VEColor(this.Cell1ColR, this.Cell1ColG, this.Cell1ColB, this.Cell1Opacity)
                    //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
                    var color = null;
                    if (this.CongestionLevel2 == 1)
                        color = new VEColor(0, 128, 0, this.Cell1Opacity);
                    else if (this.CongestionLevel2 == 2)
                        color = new VEColor(80, 80, 80, this.Cell1Opacity);
                    else if (this.CongestionLevel2 == 3)
                        color = new VEColor(255, 165, 0, this.Cell1Opacity);
                    else if (this.CongestionLevel2 == 4)
                        color = new VEColor(255, 0, 0, this.Cell1Opacity);
                    else
                        color = new VEColor(this.Cell2ColR, this.Cell2ColG, this.Cell2ColB, this.Cell2Opacity);
                    //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
                    var color = null;
                    if (this.CongestionLevel3 == 1)
                        color = new VEColor(0, 128, 0, this.Cell1Opacity);
                    else if (this.CongestionLevel3 == 2)
                        color = new VEColor(80, 80, 80, this.Cell1Opacity);
                    else if (this.CongestionLevel3 == 3)
                        color = new VEColor(255, 165, 0, this.Cell1Opacity);
                    else if (this.CongestionLevel3 == 4)
                        color = new VEColor(255, 0, 0, this.Cell1Opacity);
                    else
                        color = new VEColor(this.Cell3ColR, this.Cell3ColG, this.Cell3ColB, this.Cell3Opacity)
                    //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);
                }
            }

            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);
    },
    //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();

