using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.Globalization;
using System.IO;
using System.Collections;

namespace iTripLog.Map.Controler
{
    public class ImageButton : Control
    {
        public ImageButton(Image e)
        {
            backgroundImage = e;
            imageSize = new Point(e.Width, e.Height);
            if (imageSize.X >= imageSize.Y)
            {
                this.Size = new Size(36*460/240, 27*640/290);
            }
            else
            {
                this.Size = new Size(27 * 460 / 240, 36 * 640 / 290);
            }
        }
        bool big = false;
        Image backgroundImage;
        bool pressed = false;
        Point lastLocation = new Point(0, 0);
        Point imageSize;

        // Property for the background image to be drawn behind the button text.
        public Image BackgroundImage
        {
            get
            {
                return this.backgroundImage;
            }
            set
            {
                this.backgroundImage = value;
            }
        }

        // When the mouse button is pressed, set the "pressed" flag to true 
        // and invalidate the form to cause a repaint.  The .NET Compact Framework 
        // sets the mouse capture automatically.
        protected override void OnMouseDown(MouseEventArgs e)
        {
            this.pressed = true;
            this.BringToFront();
            if (big)
            {
                if (imageSize.X >= imageSize.Y)
                {
                    this.Size = new Size(36 * 460 / 240, 27*640/290);
                }
                else
                {
                    this.Size = new Size(27 * 460 / 240, 36 * 640 / 290);
                }
                this.Location = lastLocation;
            }
            else
            {
                lastLocation = this.Location;
                if (imageSize.X< imageSize.Y)
                {
                    this.Location = new Point( 0,0);
                    this.Size = new Size(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height-30);
                }else{
                    this.Location = new Point(0, (640 - (int)((640 - 10 * 640 / 290) * 0.75)) / 2 - 10 * 640 / 290);
                    this.Size = new Size(460, (int)((460 - 10 * 460 / 240) * 0.75));
                }
                
            }
            big = changeBig();
            base.OnMouseDown(e);
        }

        bool changeBig()
        {
            if (big)
            {
                return false;
            }
            return true;
        }


        protected override void OnPaint(PaintEventArgs e)
        {
            e.Graphics.FillRectangle(new SolidBrush(SystemColors.ActiveCaption), e.ClipRectangle);

            if (this.backgroundImage != null)
            {
                System.Drawing.Imaging.ImageAttributes attr = new System.Drawing.Imaging.ImageAttributes();
                attr.SetColorKey(Color.Magenta, Color.Magenta);

                if (this.pressed)
                    e.Graphics.DrawImage(this.backgroundImage, this.ClientRectangle, 0, 0, this.backgroundImage.Width, this.backgroundImage.Height, GraphicsUnit.Pixel, attr);
                else
                    e.Graphics.DrawImage(this.backgroundImage, this.ClientRectangle, 0, 0, this.backgroundImage.Width, this.backgroundImage.Height, GraphicsUnit.Pixel, attr);

            }

            base.OnPaint(e);
        }
    }
}
