//<%STEMCELL:PerDirectoryNoticeText%>
//ALL RIGHTS RESERVED:
//====================
//The contents of this file, and associated files in this directory, are 
//Copyright (C) ThinkGeo,LLC , all rights reserved, 2003-2008.
//
//All software Source Code, Images, Database-Design and code, Graphics Design 
//and source files, and related content (collectively referred to as SOURCE) 
//are Copyright (c) ThinkGeo,LLC, 2003-2008, All Rights Reserved.
//ThinkGeo,LLC is a USA corporation at 1617 St. Andrews Dr.Suite 201,Lawrence, 
//KS 66047.
//http://ThinkGeo.com
//
//MapSuite is a Registered Trademark of ThinkGeo,LLC.
//</%STEMCELL:PerDirectoryNoticeText%>
using System;
using System.Drawing;
using System.Configuration;
using System.Drawing.Drawing2D;

using MapSuite;
using MapSuite.Geometry;
using MapSuite.WebEdition;

namespace ThinkgeoWebSample
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                // Set property of map control
                Map1.MapUnit = MapLengthUnits.DecimalDegrees;
                Map1.Mode = Map.ModeType.Pan;
                Rectangle Rect = new Rectangle(0, 0, (int)Map1.Width.Value, (int)Map1.Height.Value);
                Map1.CanvasGeoBrush = new GeoLinearGradientBrush(Rect, GeoColor.GeographicColors.Ocean1, GeoColor.GeographicColors.Ocean2, GeoLinearGradientMode.ForwardDiagonal);
                Map1.AjaxEnabled = true;

                // Load world countries layer
                Layer cntryLayer = new Layer(this.Server.MapPath("") + @"\SampleData\world\cntry02.shp", true);
                //Color Countries with labels for all zoom level 14 and below
                cntryLayer.ZoomLevel01.GeoStyle = GeoAreaStyles.GetHueFamilyStyle(6, GeoColor.KnownColors.Red, GeoColor.KnownColors.Gray);
                cntryLayer.ZoomLevel01.GeoTextStyle = GeoTextStyles.Country1("CNTRY_NAME");
                cntryLayer.ZoomLevel01.ApplyUntilZoomLevel = ApplyUntilZoomLevel.ZoomLevel14;
                //Color Countries with no labels to higest zoom levels
                cntryLayer.ZoomLevel14.GeoStyle = GeoAreaStyles.GetHueFamilyStyle(6, GeoColor.KnownColors.Red, GeoColor.KnownColors.Gray);
                cntryLayer.ZoomLevel14.ApplyUntilZoomLevel = ApplyUntilZoomLevel.ZoomLevel18;
                Map1.Layers.Add(cntryLayer);

                // Save some values in session
                Session.Add("FullScale", Map1.CurrentScale);
            }
        }
        /// <summary>
        /// Deal with ajax event post back by map control
        /// </summary>        
        #region Postback Event

        protected void Map1_AjaxPostback(object sender, AjaxPostbackEventArgs e)
        {   
            switch (e.EventName)
            {
                case "ClickLevel1":
                    Map1.CurrentScale = GetScaleFromZoomlevel(Map1.Layers[0].ZoomLevel01);
                    break;
                case "ClickLevel2":
                    Map1.CurrentScale = GetScaleFromZoomlevel(Map1.Layers[0].ZoomLevel02);
                    break;
                case "ClickLevel3":
                    Map1.CurrentScale = GetScaleFromZoomlevel(Map1.Layers[0].ZoomLevel03);
                    break;
                case "ClickLevel4":
                    Map1.CurrentScale = GetScaleFromZoomlevel(Map1.Layers[0].ZoomLevel04);
                    break;
                case "ClickLevel5":
                    Map1.CurrentScale = GetScaleFromZoomlevel(Map1.Layers[0].ZoomLevel05);
                    break;
                case "ClickLevel6":
                    Map1.CurrentScale = GetScaleFromZoomlevel(Map1.Layers[0].ZoomLevel06);
                    break;
                case "ClickLevel7":
                    Map1.CurrentScale = GetScaleFromZoomlevel(Map1.Layers[0].ZoomLevel07);
                    break;
                case "ClickLevel8":
                    Map1.CurrentScale = GetScaleFromZoomlevel(Map1.Layers[0].ZoomLevel08);
                    break;
                case "ClickLevel9":
                    Map1.CurrentScale = GetScaleFromZoomlevel(Map1.Layers[0].ZoomLevel09);
                    break;
                case "ClickLevel10":
                    Map1.CurrentScale = GetScaleFromZoomlevel(Map1.Layers[0].ZoomLevel10);
                    break;
                case "ClickLevel11":
                    Map1.CurrentScale = GetScaleFromZoomlevel(Map1.Layers[0].ZoomLevel11);
                    break;
                case "ClickLevel12":
                    Map1.CurrentScale = GetScaleFromZoomlevel(Map1.Layers[0].ZoomLevel12);
                    break;
                case "ClickLevel13":
                    Map1.CurrentScale = GetScaleFromZoomlevel(Map1.Layers[0].ZoomLevel13);
                    break;
                case "ClickLevel14":
                    Map1.CurrentScale = GetScaleFromZoomlevel(Map1.Layers[0].ZoomLevel14);
                    break;
                case "ClickLevel15":
                    Map1.CurrentScale = GetScaleFromZoomlevel(Map1.Layers[0].ZoomLevel15);
                    break;
                case "ClickLevel16":
                    Map1.CurrentScale = GetScaleFromZoomlevel(Map1.Layers[0].ZoomLevel16);
                    break;
                case "ClickLevel17":
                    Map1.CurrentScale = GetScaleFromZoomlevel(Map1.Layers[0].ZoomLevel17);
                    break;
                case "ClickLevel18":
                    Map1.CurrentScale = (Double)Session["FullScale"];                     
                    break;
                case "ZoomIn":
                    Map1.ZoomIn(20);
                    break;
                case "ZoomOut":
                    Map1.ZoomOut(20);
                    break;
                case "TrackZoom":
                    Map1.Mode = Map.ModeType.TrackZoomIn;
                    break;
                case "PreviousExtent":
                    Map1.GetPreviousExtent();
                    break;
                case "ToggleExtent":
                    Map1.ToggleMapExtents();
                    break;
                case "FullExtent":
                    Map1.CurrentExtent = Map1.Layers[0].Extent;
                    break;
                case "PanLeft":
                    Map1.Pan(PanDirection.Left, 20);
                    break;
                case "PanRight":
                    Map1.Pan(PanDirection.Right, 20);
                    break;
                case "PanUp":
                    Map1.Pan(PanDirection.Up, 20);
                    break;
                case "PanDown":
                    Map1.Pan(PanDirection.Down, 20);
                    break;
                case "PanNorthEast":
                    Map1.Pan(45, 20);
                    break;
                case "PanNorthWest":
                    Map1.Pan(315, 20);
                    break;
                case "PanSouthEast":
                    Map1.Pan(135, 20);
                    break;
                case "PanSouthWest":
                    Map1.Pan(225, 20);
                    break;
                // Click zoom level button in zoom bar
                default:
                    break; 
            }

            // Sent value back and we will deal with it in client by "CustomCallback" function 
            Map1.AjaxReturnArgs = "Level" + GetLevelNumber(Map1.CurrentScale).ToString();
        }

        /// <summary>
        /// Get zoom level, here we get the average value between upper and lower
        /// </summary>
        /// <param name="presetZoomLevel"></param>
        /// <returns></returns>
        private double GetScaleFromZoomlevel(PresetZoomLevel presetZoomLevel)
        {
            return (presetZoomLevel.UpperExtent + presetZoomLevel.LowerExtent) / 2;
        }

        /// <summary>
        /// Get level number by scale
        /// </summary>
        /// <param name="scale"></param>
        /// <returns></returns>
        public int GetLevelNumber(double scale)
        {
            int result;
            if (scale < 0)
            {
                throw new Exception("The Width of your Map should be more than 0");
            }
            else if (scale < Map1.Layers[0].ZoomLevel01.UpperExtent)
            {
                result = 1;
            }
            else if (scale < Map1.Layers[0].ZoomLevel02.UpperExtent)
            {
                result = 2;
            }
            else if (scale < Map1.Layers[0].ZoomLevel03.UpperExtent)
            {
                result = 3;
            }
            else if (scale < Map1.Layers[0].ZoomLevel04.UpperExtent)
            {
                result = 4;
            }
            else if (scale < Map1.Layers[0].ZoomLevel05.UpperExtent)
            {
                result = 5;
            }
            else if (scale < Map1.Layers[0].ZoomLevel06.UpperExtent)
            {
                result = 6;
            }
            else if (scale < Map1.Layers[0].ZoomLevel07.UpperExtent)
            {
                result = 7;
            }
            else if (scale < Map1.Layers[0].ZoomLevel08.UpperExtent)
            {
                result = 8;
            }
            else if (scale < Map1.Layers[0].ZoomLevel09.UpperExtent)
            {
                result = 9;
            }
            else if (scale < Map1.Layers[0].ZoomLevel10.UpperExtent)
            {
                result = 10;
            }
            else if (scale < Map1.Layers[0].ZoomLevel11.UpperExtent)
            {
                result = 11;
            }
            else if (scale < Map1.Layers[0].ZoomLevel12.UpperExtent)
            {
                result = 12;
            }
            else if (scale < Map1.Layers[0].ZoomLevel13.UpperExtent)
            {
                result = 13;
            }
            else if (scale < Map1.Layers[0].ZoomLevel14.UpperExtent)
            {
                result = 14;
            }
            else if (scale < Map1.Layers[0].ZoomLevel15.UpperExtent)
            {
                result = 15;
            }
            else if (scale < Map1.Layers[0].ZoomLevel16.UpperExtent)
            {
                result = 16;
            }
            else if (scale < Map1.Layers[0].ZoomLevel17.UpperExtent)
            {
                result = 17;
            }
            else
            {
                result = 18;
            }
            return result;
        }

        #endregion
    }
}

