//Draws the controls, only gets called for the first drawing of the page, not for each postback
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            try
            {
                _xml = new XmlDocument();
                _xml.LoadXml(_data.Value.ToString());
            }
            catch
            {
                _xml = createBaseXmlDocument();
            }

            _listboxLinks        = new ListBox();
            _listboxLinks.ID     = "links" + base.ID;
            _listboxLinks.Width  = 400;
            _listboxLinks.Height = 140;
            foreach (XmlNode node in _xml.DocumentElement.ChildNodes)
            {
                string text  = node.Attributes["title"].Value.ToString();
                string value = (node.Attributes["type"].Value.ToString().Equals("internal") ? "i" : "e")
                               + (node.Attributes["newwindow"].Value.ToString().Equals("1") ? "n" : "o")
                               + node.Attributes["link"].Value.ToString();
                _listboxLinks.Items.Add(new ListItem(text, value));
            }

            _buttonUp        = new Button();
            _buttonUp.ID     = "btnUp" + base.ID;
            _buttonUp.Text   = umbraco.ui.GetText("relatedlinks", "modeUp");
            _buttonUp.Width  = 80;
            _buttonUp.Click += new EventHandler(this.buttonUp_Click);


            _buttonDown    = new Button();
            _buttonDown.ID = "btnDown" + base.ID;
            _buttonDown.Attributes.Add("style", "margin-top: 5px;");
            _buttonDown.Text   = umbraco.ui.GetText("relatedlinks", "modeDown");
            _buttonDown.Width  = 80;
            _buttonDown.Click += new EventHandler(this.buttonDown_Click);

            _buttonDelete        = new Button();
            _buttonDelete.ID     = "btnDel" + base.ID;
            _buttonDelete.Text   = umbraco.ui.GetText("relatedlinks", "removeLink");
            _buttonDelete.Width  = 80;
            _buttonDelete.Click += new EventHandler(this.buttonDel_Click);

            _textboxLinkTitle       = new TextBox();
            _textboxLinkTitle.Width = 400;
            _textboxLinkTitle.ID    = "linktitle" + base.ID;

            _checkNewWindow         = new CheckBox();
            _checkNewWindow.ID      = "checkNewWindow" + base.ID;
            _checkNewWindow.Checked = false;
            _checkNewWindow.Text    = umbraco.ui.GetText("relatedlinks", "newWindow");

            _textBoxExtUrl       = new TextBox();
            _textBoxExtUrl.Width = 400;
            _textBoxExtUrl.ID    = "exturl" + base.ID;

            _buttonAddExtUrl        = new Button();
            _buttonAddExtUrl.ID     = "btnAddExtUrl" + base.ID;
            _buttonAddExtUrl.Text   = umbraco.ui.GetText("relatedlinks", "addlink");
            _buttonAddExtUrl.Width  = 80;
            _buttonAddExtUrl.Click += new EventHandler(this.buttonAddExt_Click);

            _buttonAddIntUrlCP        = new Button();
            _buttonAddIntUrlCP.ID     = "btnAddIntUrl" + base.ID;
            _buttonAddIntUrlCP.Text   = umbraco.ui.GetText("relatedlinks", "addlink");
            _buttonAddIntUrlCP.Width  = 80;
            _buttonAddIntUrlCP.Click += new EventHandler(this.buttonAddIntCP_Click);

            _pagePickerExtractor = new PagePickerDataExtractor();
            _pagePicker          = new pagePicker(_pagePickerExtractor);
            _pagePicker.ID       = "pagePicker" + base.ID;

            ContentTemplateContainer.Controls.Add(new LiteralControl("<div class=\"relatedlinksdatatype\" style=\"text-align: left;  padding: 5px;\"><table><tr><td rowspan=\"2\">"));
            ContentTemplateContainer.Controls.Add(_listboxLinks);
            ContentTemplateContainer.Controls.Add(new LiteralControl("</td><td style=\"vertical-align: top\">"));
            ContentTemplateContainer.Controls.Add(_buttonUp);
            ContentTemplateContainer.Controls.Add(new LiteralControl("<br />"));
            ContentTemplateContainer.Controls.Add(_buttonDown);
            ContentTemplateContainer.Controls.Add(new LiteralControl("</td></tr><tr><td style=\"vertical-align: bottom\">"));
            ContentTemplateContainer.Controls.Add(_buttonDelete);
            ContentTemplateContainer.Controls.Add(new LiteralControl("<br />"));
            ContentTemplateContainer.Controls.Add(new LiteralControl("</td></tr></table>"));

            // Add related links container
            ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format("<a href=\"javascript:;\" onClick=\"document.getElementById('{0}_addExternalLinkPanel').style.display='none';document.getElementById('{0}_addExternalLinkButton').style.display='none';document.getElementById('{0}_addLinkContainer').style.display='block';document.getElementById('{0}_addInternalLinkPanel').style.display='block';document.getElementById('{0}_addInternalLinkButton').style.display='block';\"><strong>{1}</strong></a>", ClientID, umbraco.ui.GetText("relatedlinks", "addInternal"))));
            ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format(" | <a href=\"javascript:;\" onClick=\"document.getElementById('{0}_addInternalLinkPanel').style.display='none';document.getElementById('{0}_addInternalLinkButton').style.display='none';document.getElementById('{0}_addLinkContainer').style.display='block';document.getElementById('{0}_addExternalLinkPanel').style.display='block';document.getElementById('{0}_addExternalLinkButton').style.display='block';\"><strong>{1}</strong></a>", ClientID, umbraco.ui.GetText("relatedlinks", "addExternal"))));

            // All urls
            ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format("<div id=\"{0}_addLinkContainer\" style=\"display: none; padding: 4px; border: 1px solid #ccc; margin-top: 5px;margin-right:10px;\">", ClientID)));
            ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format("<a href=\"javascript:;\" onClick=\"document.getElementById('{0}_addLinkContainer').style.display='none';\" style=\"border: none;\"><img src=\"{1}/images/close.png\" style=\"float: right\" /></a>", ClientID, this.Page.ResolveUrl(SystemDirectories.Umbraco))));
            ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format("{0}:<br />", umbraco.ui.GetText("relatedlinks", "caption"))));
            ContentTemplateContainer.Controls.Add(_textboxLinkTitle);
            ContentTemplateContainer.Controls.Add(new LiteralControl("<br />"));
            ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format("<div id=\"{0}_addExternalLinkPanel\" style=\"display: none; margin: 3px 0\">", ClientID)));
            ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format("{0}:<br />", umbraco.ui.GetText("relatedlinks", "linkurl"))));
            ContentTemplateContainer.Controls.Add(_textBoxExtUrl);
            ContentTemplateContainer.Controls.Add(new LiteralControl("</div>"));

            ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format("<div id=\"{0}_addInternalLinkPanel\" style=\"display: none; margin: 3px 0\">", ClientID)));
            ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format("{0}:<br />", umbraco.ui.GetText("relatedlinks", "internalPage"))));
            ContentTemplateContainer.Controls.Add(_pagePicker);
            ContentTemplateContainer.Controls.Add(new LiteralControl("</div>"));

            ContentTemplateContainer.Controls.Add(new LiteralControl("<div style=\"margin: 5px 0\">"));
            ContentTemplateContainer.Controls.Add(_checkNewWindow);
            ContentTemplateContainer.Controls.Add(new LiteralControl("</div>"));

            ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format("<div id=\"{0}_addInternalLinkButton\" style=\"display: none;\">", ClientID)));
            ContentTemplateContainer.Controls.Add(_buttonAddIntUrlCP);
            ContentTemplateContainer.Controls.Add(new LiteralControl("</div>"));

            ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format("<div id=\"{0}_addExternalLinkButton\" style=\"display: none;\">", ClientID)));
            ContentTemplateContainer.Controls.Add(_buttonAddExtUrl);
            ContentTemplateContainer.Controls.Add(new LiteralControl("</div>"));

            ContentTemplateContainer.Controls.Add(new LiteralControl("</div>"));

            ContentTemplateContainer.Controls.Add(new LiteralControl("</div>"));

            resetInputMedia();
        }
        //Draws the controls, only gets called for the first drawing of the page, not for each postback
        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);

            try
            {
                _xml = new XmlDocument();
                _xml.LoadXml(_data.Value.ToString());

            }
            catch
            {
                _xml = createBaseXmlDocument();
            }

            _listboxLinks = new ListBox();
            _listboxLinks.ID = "links" + base.ID;
            _listboxLinks.Width = 400;
            _listboxLinks.Height = 140;
            foreach (XmlNode node in _xml.DocumentElement.ChildNodes)
            {
                string text = node.Attributes["title"].Value.ToString();
                string value = (node.Attributes["type"].Value.ToString().Equals("internal") ? "i" : "e")
                    + (node.Attributes["newwindow"].Value.ToString().Equals("1") ? "n" : "o")
                    + node.Attributes["link"].Value.ToString();
                _listboxLinks.Items.Add(new ListItem(text, value));
            }

            _buttonUp = new Button();
            _buttonUp.ID = "btnUp" + base.ID;
            _buttonUp.Text = umbraco.ui.GetText("relatedlinks", "modeUp");
            _buttonUp.Width = 80;
            _buttonUp.Click += new EventHandler(this.buttonUp_Click);

           
            _buttonDown = new Button();
            _buttonDown.ID = "btnDown" + base.ID;
            _buttonDown.Attributes.Add("style", "margin-top: 5px;");
            _buttonDown.Text = umbraco.ui.GetText("relatedlinks", "modeDown");
            _buttonDown.Width = 80;
            _buttonDown.Click += new EventHandler(this.buttonDown_Click);

            _buttonDelete = new Button();
            _buttonDelete.ID = "btnDel" + base.ID;
            _buttonDelete.Text = umbraco.ui.GetText("relatedlinks", "removeLink");
            _buttonDelete.Width = 80;
            _buttonDelete.Click += new EventHandler(this.buttonDel_Click);

            _textboxLinkTitle = new TextBox();
            _textboxLinkTitle.Width = 400;
            _textboxLinkTitle.ID = "linktitle" + base.ID;

            _checkNewWindow = new CheckBox();
            _checkNewWindow.ID = "checkNewWindow" + base.ID;
            _checkNewWindow.Checked = false;
            _checkNewWindow.Text = umbraco.ui.GetText("relatedlinks", "newWindow");

            _textBoxExtUrl = new TextBox();
            _textBoxExtUrl.Width = 400;
            _textBoxExtUrl.ID = "exturl" + base.ID;

            _buttonAddExtUrl = new Button();
            _buttonAddExtUrl.ID = "btnAddExtUrl" + base.ID;
            _buttonAddExtUrl.Text = umbraco.ui.GetText("relatedlinks", "addlink");
            _buttonAddExtUrl.Width = 80;
            _buttonAddExtUrl.Click += new EventHandler(this.buttonAddExt_Click);

            _buttonAddIntUrlCP = new Button();
            _buttonAddIntUrlCP.ID = "btnAddIntUrl" + base.ID;
            _buttonAddIntUrlCP.Text = umbraco.ui.GetText("relatedlinks", "addlink");
            _buttonAddIntUrlCP.Width = 80;
            _buttonAddIntUrlCP.Click += new EventHandler(this.buttonAddIntCP_Click);

            _pagePickerExtractor = new PagePickerDataExtractor();
            _pagePicker = new pagePicker(_pagePickerExtractor);
            _pagePicker.ID = "pagePicker" + base.ID;
            
            ContentTemplateContainer.Controls.Add(new LiteralControl("<div class=\"relatedlinksdatatype\" style=\"text-align: left;  padding: 5px;\"><table><tr><td rowspan=\"2\">"));
            ContentTemplateContainer.Controls.Add(_listboxLinks);
            ContentTemplateContainer.Controls.Add(new LiteralControl("</td><td style=\"vertical-align: top\">"));
            ContentTemplateContainer.Controls.Add(_buttonUp);
            ContentTemplateContainer.Controls.Add(new LiteralControl("<br />"));
            ContentTemplateContainer.Controls.Add(_buttonDown);
            ContentTemplateContainer.Controls.Add(new LiteralControl("</td></tr><tr><td style=\"vertical-align: bottom\">"));
            ContentTemplateContainer.Controls.Add(_buttonDelete);
            ContentTemplateContainer.Controls.Add(new LiteralControl("<br />"));
            ContentTemplateContainer.Controls.Add(new LiteralControl("</td></tr></table>"));

            // Add related links container
            ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format("<a href=\"javascript:;\" onClick=\"document.getElementById('{0}_addExternalLinkPanel').style.display='none';document.getElementById('{0}_addExternalLinkButton').style.display='none';document.getElementById('{0}_addLinkContainer').style.display='block';document.getElementById('{0}_addInternalLinkPanel').style.display='block';document.getElementById('{0}_addInternalLinkButton').style.display='block';\"><strong>{1}</strong></a>", ClientID, umbraco.ui.GetText("relatedlinks", "addInternal"))));
            ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format(" | <a href=\"javascript:;\" onClick=\"document.getElementById('{0}_addInternalLinkPanel').style.display='none';document.getElementById('{0}_addInternalLinkButton').style.display='none';document.getElementById('{0}_addLinkContainer').style.display='block';document.getElementById('{0}_addExternalLinkPanel').style.display='block';document.getElementById('{0}_addExternalLinkButton').style.display='block';\"><strong>{1}</strong></a>", ClientID, umbraco.ui.GetText("relatedlinks", "addExternal"))));

            // All urls
            ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format("<div id=\"{0}_addLinkContainer\" style=\"display: none; padding: 4px; border: 1px solid #ccc; margin-top: 5px;margin-right:10px;\">", ClientID)));
            ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format("<a href=\"javascript:;\" onClick=\"document.getElementById('{0}_addLinkContainer').style.display='none';\" style=\"border: none;\"><img src=\"{1}/images/close.png\" style=\"float: right\" /></a>", ClientID,  this.Page.ResolveUrl(SystemDirectories.Umbraco))));
            ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format("{0}:<br />", umbraco.ui.GetText("relatedlinks", "caption"))));
            ContentTemplateContainer.Controls.Add(_textboxLinkTitle);
            ContentTemplateContainer.Controls.Add(new LiteralControl("<br />"));
            ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format("<div id=\"{0}_addExternalLinkPanel\" style=\"display: none; margin: 3px 0\">", ClientID)));
            ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format("{0}:<br />", umbraco.ui.GetText("relatedlinks", "linkurl"))));
            ContentTemplateContainer.Controls.Add(_textBoxExtUrl);
            ContentTemplateContainer.Controls.Add(new LiteralControl("</div>"));

            ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format("<div id=\"{0}_addInternalLinkPanel\" style=\"display: none; margin: 3px 0\">", ClientID)));
            ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format("{0}:<br />", umbraco.ui.GetText("relatedlinks", "internalPage"))));
            ContentTemplateContainer.Controls.Add(_pagePicker);
            ContentTemplateContainer.Controls.Add(new LiteralControl("</div>"));

            ContentTemplateContainer.Controls.Add(new LiteralControl("<div style=\"margin: 5px 0\">"));
            ContentTemplateContainer.Controls.Add(_checkNewWindow);
            ContentTemplateContainer.Controls.Add(new LiteralControl("</div>"));

            ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format("<div id=\"{0}_addInternalLinkButton\" style=\"display: none;\">", ClientID)));
            ContentTemplateContainer.Controls.Add(_buttonAddIntUrlCP);
            ContentTemplateContainer.Controls.Add(new LiteralControl("</div>"));

            ContentTemplateContainer.Controls.Add(new LiteralControl(String.Format("<div id=\"{0}_addExternalLinkButton\" style=\"display: none;\">", ClientID)));
            ContentTemplateContainer.Controls.Add(_buttonAddExtUrl);
            ContentTemplateContainer.Controls.Add(new LiteralControl("</div>"));

            ContentTemplateContainer.Controls.Add(new LiteralControl("</div>"));

            ContentTemplateContainer.Controls.Add(new LiteralControl("</div>"));

            resetInputMedia();
        }