public RailRoad loadXML(string path, ContentManager content)
        {
            XmlDocument doc = new XmlDocument();

            //doc.Load(@"C:\Users\LasseSoerensen\Documents\Visual Studio 2010\Projects\RailRoadXML\RailRoadXML\RailRoad.xml");
            doc.Load(path);

            XmlNodeList nodeList = doc.SelectNodes("RailRoad/Track");

            RailRoad rr = new RailRoad();

            foreach (XmlNode n in nodeList)
            {
                string type = n.Attributes.GetNamedItem("Type").Value.ToString();

                Track t = new StraightTrack();
                t = new StraightTrack();
                t.gfx = content.Load<Texture2D>("StraightNB");
                t.direction = Convert.ToBoolean(n.Attributes.GetNamedItem("Direction").Value.ToString());
                if (type.Equals("CornerTrack"))
                {
                    t = new CornerTrack();
                    t.direction = Convert.ToBoolean(n.Attributes.GetNamedItem("Direction").Value.ToString());
                    //if (t.direction)
                    //{
                    t.gfx = content.Load<Texture2D>("rightTurnNB");
                    //}
                    //else
                    //{
                    //    t.gfx = content.Load<Texture2D>("leftTurnNB");
                    //}
                }

                if (type.Equals("SwitchRight"))
                {
                    t = new SwitchRight();
                    t.direction = Convert.ToBoolean(n.Attributes.GetNamedItem("Direction").Value.ToString());
                    t.turn = Convert.ToBoolean(n.Attributes.GetNamedItem("Switch").Value.ToString());

                    t.gfx = content.Load<Texture2D>("switchRight");
                }

                if (type.Equals("SwitchLeft"))
                {
                    t = new SwitchLeft();
                    t.direction = Convert.ToBoolean(n.Attributes.GetNamedItem("Direction").Value.ToString());
                    t.turn = Convert.ToBoolean(n.Attributes.GetNamedItem("Switch").Value.ToString());

                    t.gfx = content.Load<Texture2D>("switchLeft");
                }

                t.rotation = MathHelper.ToRadians(Convert.ToInt32(n.Attributes.GetNamedItem("Rotation").Value));

                t.position = new Vector2(Convert.ToInt32(n.Attributes.GetNamedItem("X").Value), Convert.ToInt32(n.Attributes.GetNamedItem("Y").Value));
                t.id = Convert.ToInt32(n.Attributes.GetNamedItem("ID").Value);

                //foreach (XmlNode nn in n.ChildNodes)
                //{
                //if (nn.Name == "Signal")
                //{
                //    Signal s = new Signal(Convert.ToInt32(nn.Attributes.GetNamedItem("ID").Value), content);
                //    s.position = new Vector2(Convert.ToInt32(nn.Attributes.GetNamedItem("X").Value), Convert.ToInt32(nn.Attributes.GetNamedItem("Y").Value));
                //    switch (nn.Attributes.GetNamedItem("InitialState").Value)
                //    {
                //        case "Stop": s.state = Signal.State.Stop; break;
                //        case "Go": s.state = Signal.State.Go; break;
                //        case "Off": s.state = Signal.State.Off; break;
                //    }

                //    t.signal = s;
                //    System.Diagnostics.Debug.WriteLine("Signal added");
                //}
                //else
                //if (nn.Name == "Sensor")
                //{
                //    Sensor s = new Sensor();
                //    s.id = Convert.ToInt32(nn.Attributes.GetNamedItem("ID").Value);
                //    s.mySignal = rr.findSignal(Convert.ToInt32(nn.Attributes.GetNamedItem("SignalID").Value));

                //    switch (nn.Attributes.GetNamedItem("InitialState").Value)
                //    {
                //        case "On": s.state = Sensor.State.On; break;
                //        case "Off": s.state = Sensor.State.Off; break;
                //    }
                //    t.sensor = s;
                //}

                //}

                rr.tracks.Add(t);
            }
            foreach (XmlNode xml in nodeList)
            {
                int id = Convert.ToInt32(xml.Attributes.GetNamedItem("ID").Value);
                foreach (Track t in rr.tracks)
                {
                    if (t.id == id)
                    {
                        t.prevTrack = rr.findTrack(Convert.ToInt32(xml.Attributes.GetNamedItem("PreviousTrack").Value));
                        t.nextTrack = rr.findTrack(Convert.ToInt32(xml.Attributes.GetNamedItem("NextTrack").Value));

                        if (t is SwitchLeft || t is SwitchRight)
                        {
                            t.switchTrack = rr.findTrack(Convert.ToInt32(xml.Attributes.GetNamedItem("SwitchTrackID").Value));
                        }
                        foreach (XmlNode nn in xml.ChildNodes)
                        {
                            if (nn.Name == "Signal")
                            {
                                Signal s = new Signal(Convert.ToInt32(nn.Attributes.GetNamedItem("ID").Value), content);
                                s.position = new Vector2(Convert.ToInt32(nn.Attributes.GetNamedItem("X").Value), Convert.ToInt32(nn.Attributes.GetNamedItem("Y").Value));
                                switch (nn.Attributes.GetNamedItem("InitialState").Value)
                                {
                                    case "Stop": s.state = Signal.State.Stop; break;
                                    case "Go": s.state = Signal.State.Go; break;
                                    case "Off": s.state = Signal.State.Off; break;
                                }

                                t.signal = s;
                                System.Diagnostics.Debug.WriteLine("Signal added");
                            }
                        }
                    }
                }
            }

            foreach (XmlNode xml in nodeList)
            {
                foreach (Track t in rr.tracks)
                {
                    if (t.id == Convert.ToInt32(xml.Attributes.GetNamedItem("ID").Value))
                    {
                        foreach (XmlNode nn in xml.ChildNodes)
                        {
                            if (nn.Name == "Sensor")
                            {
                                Sensor s = new Sensor();
                                s.id = Convert.ToInt32(nn.Attributes.GetNamedItem("ID").Value);
                                s.mySignal = rr.findSignal(Convert.ToInt32(nn.Attributes.GetNamedItem("SignalID").Value));

                                switch (nn.Attributes.GetNamedItem("InitialState").Value)
                                {
                                    case "On": s.state = Sensor.State.On; break;
                                    case "Off": s.state = Sensor.State.Off; break;
                                }
                                t.sensor = s;
                            }
                        }
                    }
                }
            }

            return rr;
        }
        private void pbCorner_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Left)
            {
                Track t = new CornerTrack();
                Track oldT = game.rr.tracks[game.rr.tracks.Count() - 1];
                t.gfx = content.Load<Texture2D>("rightTurnNB");
                t.rotation = MathHelper.ToRadians(cornerFlip);
                if (direction == Direction.Right)
                {
                    if (isCorner)
                    {
                        if (cornerFlip == 0)
                        {
                            if (MathHelper.ToDegrees(oldT.rotation) == 180)
                            {
                                t.position = new Vector2(oldT.position.X, oldT.position.Y - 18);
                                direction = Direction.Down;
                                pbStraight.Image.RotateFlip(RotateFlipType.Rotate90FlipY);
                                pbStraight.Refresh();
                            }
                            else if (MathHelper.ToDegrees(oldT.rotation) == 270)
                            {
                                t.position = new Vector2(oldT.position.X + 59, oldT.position.Y - 59);
                                direction = Direction.Down;
                                pbStraight.Image.RotateFlip(RotateFlipType.Rotate90FlipY);
                                pbStraight.Refresh();
                            }
                        }
                        else if (cornerFlip == 90)
                        {
                            if (MathHelper.ToDegrees(oldT.rotation) == 180)
                            {
                                t.position = new Vector2(oldT.position.X + 59, oldT.position.Y - 59);
                                t.direction = true;
                                direction = Direction.Up;
                                pbStraight.Image.RotateFlip(RotateFlipType.Rotate90FlipY);
                                pbStraight.Refresh();
                            }
                            else if (MathHelper.ToDegrees(oldT.rotation) == 270)
                            {
                                t.position = new Vector2(oldT.position.X + 118, oldT.position.Y - 100);
                                t.direction = true;
                                direction = Direction.Up;
                                pbStraight.Image.RotateFlip(RotateFlipType.Rotate90FlipY);
                                pbStraight.Refresh();
                            }
                        }
                    }
                    else
                    {
                        if (cornerFlip == 0)
                        {
                            t.position = new Vector2(oldT.position.X + 50, oldT.position.Y);
                            direction = Direction.Down;
                            pbStraight.Image.RotateFlip(RotateFlipType.Rotate90FlipY);
                            pbStraight.Refresh();
                        }
                        else if (cornerFlip == 90)
                        {
                            t.position = new Vector2(oldT.position.X + 109, oldT.position.Y - 41);
                            t.direction = true;
                            direction = Direction.Up;
                            pbStraight.Image.RotateFlip(RotateFlipType.Rotate90FlipY);
                            pbStraight.Refresh();
                        }
                    }
                }
                if (direction == Direction.Down)
                {
                    if (isCorner)
                    {
                        if (cornerFlip == 90)
                        {
                            if (MathHelper.ToDegrees(oldT.rotation) == 0)
                            {
                                t.position = new Vector2(oldT.position.X + 50, oldT.position.Y + 50);
                                direction = Direction.Left;
                                pbStraight.Image.RotateFlip(RotateFlipType.Rotate90FlipY);
                                pbStraight.Refresh();
                            }
                            else if (MathHelper.ToDegrees(oldT.rotation) == 270)
                            {
                                t.position = new Vector2(oldT.position.X + 18, oldT.position.Y);
                                direction = Direction.Left;
                                pbStraight.Image.RotateFlip(RotateFlipType.Rotate90FlipY);
                                pbStraight.Refresh();
                            }
                        }
                        else if (cornerFlip == 180)
                        {
                            if (MathHelper.ToDegrees(oldT.rotation) == 0)
                            {
                                t.position = new Vector2(oldT.position.X + 100, oldT.position.Y + 118);
                                t.direction = true;
                                direction = Direction.Right;
                                pbStraight.Image.RotateFlip(RotateFlipType.Rotate90FlipY);
                                pbStraight.Refresh();
                            }
                            else if (MathHelper.ToDegrees(oldT.rotation) == 270)
                            {
                                t.position = new Vector2(oldT.position.X + 50, oldT.position.Y + 50);
                                t.direction = true;
                                direction = Direction.Right;
                                pbStraight.Image.RotateFlip(RotateFlipType.Rotate90FlipY);
                                pbStraight.Refresh();
                            }
                        }
                    }
                    else
                    {
                        if (cornerFlip == 90)
                        {
                            t.position = new Vector2(oldT.position.X, oldT.position.Y + 50);
                            direction = Direction.Left;
                            pbStraight.Image.RotateFlip(RotateFlipType.Rotate90FlipY);
                            pbStraight.Refresh();
                        }
                        else if (cornerFlip == 180)
                        {
                            t.position = new Vector2(oldT.position.X + 32, oldT.position.Y + 100);
                            t.direction = true;
                            direction = Direction.Right;
                            pbStraight.Image.RotateFlip(RotateFlipType.Rotate90FlipY);
                            pbStraight.Refresh();
                        }
                    }
                }
                if (direction == Direction.Left)
                {
                    if (isCorner)
                    {
                        if (cornerFlip == 180)
                        {
                            if (MathHelper.ToDegrees(oldT.rotation) == 90)
                            {
                                t.position = new Vector2(oldT.position.X - 50, oldT.position.Y + 50);
                                direction = Direction.Up;
                                pbStraight.Image.RotateFlip(RotateFlipType.Rotate90FlipY);
                                pbStraight.Refresh();
                            }
                            else if (MathHelper.ToDegrees(oldT.rotation) == 0)
                            {
                                t.position = new Vector2(oldT.position.X, oldT.position.Y + 18);
                                direction = Direction.Up;
                                pbStraight.Image.RotateFlip(RotateFlipType.Rotate90FlipY);
                                pbStraight.Refresh();
                            }
                        }
                        else if (cornerFlip == 270)
                        {
                            if (MathHelper.ToDegrees(oldT.rotation) == 90)
                            {
                                t.position = new Vector2(oldT.position.X - 100, oldT.position.Y + 82);
                                t.direction = true;
                                direction = Direction.Down;
                                pbStraight.Image.RotateFlip(RotateFlipType.Rotate90FlipY);
                                pbStraight.Refresh();
                            }
                            else if (MathHelper.ToDegrees(oldT.rotation) == 0)
                            {
                                t.position = new Vector2(oldT.position.X - 59 , oldT.position.Y + 59);
                                t.direction = true;
                                direction = Direction.Down;
                                pbStraight.Image.RotateFlip(RotateFlipType.Rotate90FlipY);
                                pbStraight.Refresh();
                            }
                        }
                    }
                    else
                    {
                        if (cornerFlip == 180)
                        {
                            t.position = new Vector2(oldT.position.X, oldT.position.Y + 18);
                            direction = Direction.Up;
                            pbStraight.Image.RotateFlip(RotateFlipType.Rotate90FlipY);
                            pbStraight.Refresh();
                        }
                        else if (cornerFlip == 270)
                        {
                            t.position = new Vector2(oldT.position.X - 59, oldT.position.Y + 59);
                            t.direction = true;
                            direction = Direction.Down;
                            pbStraight.Image.RotateFlip(RotateFlipType.Rotate90FlipY);
                            pbStraight.Refresh();
                        }
                    }
                }

                if (direction == Direction.Up)
                {
                    if (isCorner)
                    {
                        if (cornerFlip == 0)
                        {
                            if (MathHelper.ToDegrees(oldT.rotation) == 90)
                            {
                                t.position = new Vector2(oldT.position.X - 50, oldT.position.Y - 50);
                                t.direction = true;
                                direction = Direction.Left;
                                pbStraight.Image.RotateFlip(RotateFlipType.Rotate90FlipY);
                                pbStraight.Refresh();
                            }
                            else if (MathHelper.ToDegrees(oldT.rotation) == 180)
                            {
                                t.position = new Vector2(oldT.position.X-100, oldT.position.Y - 118);
                                t.direction = true;
                                direction = Direction.Left;
                                pbStraight.Image.RotateFlip(RotateFlipType.Rotate90FlipY);
                                pbStraight.Refresh();
                            }
                        }
                        else if (cornerFlip == 270)
                        {
                            if (MathHelper.ToDegrees(oldT.rotation) == 90)
                            {
                                t.position = new Vector2(oldT.position.X - 18, oldT.position.Y);
                                direction = Direction.Right;
                                pbStraight.Image.RotateFlip(RotateFlipType.Rotate90FlipY);
                                pbStraight.Refresh();
                            }
                            else if (MathHelper.ToDegrees(oldT.rotation) == 180)
                            {
                                t.position = new Vector2(oldT.position.X - 59, oldT.position.Y- 59);
                                direction = Direction.Right;
                                pbStraight.Image.RotateFlip(RotateFlipType.Rotate90FlipY);
                                pbStraight.Refresh();
                            }
                        }
                    }
                    else
                    {
                        if (cornerFlip == 0)
                        {
                            t.position = new Vector2(oldT.position.X - 50 , oldT.position.Y - 50);
                            t.direction = true;
                            direction = Direction.Left;
                            pbStraight.Image.RotateFlip(RotateFlipType.Rotate90FlipY);
                            pbStraight.Refresh();
                        }
                        else if (cornerFlip == 270)
                        {
                            t.position = new Vector2(oldT.position.X - 18, oldT.position.Y);

                            direction = Direction.Right;
                            pbStraight.Image.RotateFlip(RotateFlipType.Rotate90FlipY);
                            pbStraight.Refresh();
                        }
                    }
                }
                t.prevTrack = game.rr.tracks[game.rr.tracks.Count() - 1];
                t.id = oldT.id + 1;
                game.rr.tracks[game.rr.tracks.Count() - 1].nextTrack = t;
                game.addTrack(t);
                isCorner = true;
            }
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                pbCorner.Image.RotateFlip(RotateFlipType.Rotate270FlipXY);
                pbCorner.Refresh();
                cornerFlip = cornerFlip + 90;
                if (cornerFlip > 270)
                {
                    cornerFlip = 0;
                }
            }
        }