namespace LinkBars {
    using System;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Collections;
    using System.ComponentModel;
    using System.Drawing;

    /// <summary>
    /// Creates a Yahoo! style navigation bar.
    /// </summary>
    /// <remarks>
    /// A navigation bar similar to that seen on Yahoo! (www.yahoo.com). The navigation bar can
    /// be used to structure the site in a hierarchial structure.  Users can use the
    /// navigation bar to jump to higher levels of the site.
    /// 
    /// If the control is passed an arraylist of hyperlink objects, then it will create
    /// the navigation bar will be created based on the arraylist.  Otherwise it will be created
    /// based on the directory/url structure of the site with the last element taken from the
    /// title of the current page.
    /// </remarks>
    /// <example>The following example shows how to use the NavBar Control to create 
    /// a navigation bar on a page based on a user supplied ArrayList of HyperLink objects.
    /// 
    /// <code>
    ///<%@ Register TagPrefix="NavBar" Namespace="LinkBars" %>
    ///
    ///<script language="C#" runat=server>
    ///    void Page_Load(Object o, EventArgs e) {
    ///        ArrayList navbar_urls = new ArrayList();
    ///        HyperLink hl;
    ///        hl = new HyperLink();
    ///        hl.Text = "Home";
    ///        hl.NavigateUrl = "mother";
    ///        navbar_urls.Add(hl);
    ///        
    ///        hl = new HyperLink();
    ///        hl.Text = "Tutorials";
    ///        hl.NavigateUrl = "mother";
    ///        navbar_urls.Add(hl);
    ///        
    ///        hl = new HyperLink();
    ///        hl.Text = "How to make a navigation control";
    ///        navbar_urls.Add(hl);
    ///        
    ///        bar.Urls = navbar;
    ///</script>
    ///    
    ///<html>
    ///<body>
    ///
    ///<NavBar:NavBar id=bar RPad=3 LPad=1 Seperator=">" width=100% 
    ///forecolor=yellow backcolor=darkred linkcolor=white runat=server />
    ///
    ///</body>
    ///</html>
    ///</code>
    ///
    ///Here is an example of how to use the navigation bar based on the URL of the page
    ///along with the page title.  Notice that the title tags must include a runat=server
    ///and it's id must also be set to "title".
    ///
    ///<code>
    ///<%@ Register TagPrefix="NavBar" Namespace="LinkBars" %>
    ///<html>
    ///<head>
    ///<title id="title" runat=server>This is the page title</title>
    ///</head>
    ///<body>
    ///
    ///<NavBar:NavBar id=bar RPad=3 LPad=1 Seperator=">" width=100% 
    ///forecolor=yellow backcolor=darkred linkcolor=white runat=server />
    ///
    ///</body>
    ///</html>
    ///</code>
    ///</example>
    public class NavBar : System.Web.UI.WebControls.WebControl {

        private ArrayList alTextUrlPairs = null;
        private string strSeperator = ">";
        private int iRPad = 2;
        private int iLPad = 1;
        private int iCutOff = 99999;
        private Color clrForeColor = Color.Black;
        private Color clrLinkColor = Color.Blue;
        private Color clrSeperatorColor = Color.Black;
        private string strCategoryLink = "", strCategoryName = "";
                        
    
        /// <value>An ArrayList containing text and url pairs to create
        /// the navigation bar. Each element of the ArrayList will be a HyperLink Object.
        /// Defaults to null.
        /// </value>
        public ArrayList Urls {
            get {
                return alTextUrlPairs;
            } 
            set {
                alTextUrlPairs = value;
            }
        }

        /// <value>A string value representing the seperator to be used between each link in the
        /// navigation bar. Defaults to the '>' character.
        /// </value>
        public string Seperator {
            get {
                return strSeperator;
            }
            set {
                strSeperator = value;
            }
        }

        /// <value>An int specifying the amount of white space padding on the left side
        /// of the <see cref="Seperator" />. Defaults to 1.
        /// </value>
        public int LPad {
            get {
                return iLPad;
            }
            set {
                //iLPad = value;
            }
        }

        /// <value>An int specifying the amount of white space padding on the right side 
        /// of the <see cref="Seperator" />. Defaults to 3.
        /// </value>
        public int RPad {
            get {
                return iRPad;
            }
            set {
                //iRPad = value;
            }
        }

        /// <value>
        /// A color specifying the color of the text in the navigation bar.  Use 
        /// <see cref="LinkColor" /> to specify the color of the Links. Use 
        /// <see cref="SeperatorColor" /> to specify the color of the seperators.
        /// Defaults to Black.
        /// </value>
        public override Color ForeColor {
            get {
                return clrForeColor;
            }
            set {
                clrForeColor = value;
            }
        }

        /// <value>
        /// A color specifying the color of the links in the navigation bar. Use
        /// <see cref="ForeColor" /> to specify the color of the Text.
        /// Defaults to Blue.
        /// </value>
        public Color LinkColor {
            get {
                return clrLinkColor;
            }
            set {
                clrLinkColor = value;
            }
        }

        /// <value>
        /// A color specifying the color of the seperators in the navigation bar. Use
        /// <see cref="ForeColor" /> to specify the color of the Text.
        /// Defaults to Black.
        /// </value>
        public Color SeperatorColor {
            get {
                return clrSeperatorColor;
            }
            set {
                clrSeperatorColor = value;
            }
        }

        /// <value>
        /// A string specifying the category of the page.  Used to build up the link bar.
        /// </value>
        public string CategoryName {
                get {
                    return strCategoryName;
                }
                set {
                    strCategoryName = value;
                }
        }

        /// <value>
        /// An string specifying the category url of the page.  Used to build up the link bar.
        /// </value>
        public string CategoryLink {
            get {
                return strCategoryLink;
            }
            set {
                strCategoryLink = value;
            }
        }

        public string strRPad {
            get {
                //build up the Right Padding string.
                string strRPad = String.Empty;
                for (int i = 0; i < iRPad; i++) 
                {
                    strRPad += " ";
                }
                return strRPad;
            }
        }

        public string strLPad {
            get {
                //build up the Left Padding string.
                string strLPad = String.Empty;
                for (int i = 0; i < iLPad; i++) 
                {
                    strLPad += " ";
                }
                return strLPad;
            }
        }

        public Label lblSeperator {
            get {
                Label lblSeperator;
                lblSeperator = new Label();
                lblSeperator.Text = strLPad + strSeperator + strRPad;
                lblSeperator.ForeColor = Color.Black;
                lblSeperator.Font.Name = "Verdana";
                lblSeperator.Font.Size = FontUnit.Point(8);
                return lblSeperator;
            }
        }


        protected override void CreateChildControls() {
            
            //iterate through the arraylist of hyperlinks and place the seperators and padding
            //string in between the hyperlinks.
            HyperLink hl;
            hl = new HyperLink();
            hl.Text    = "Home";
            hl.NavigateUrl = "/";
            //hl.ForeColor = Color.FromString("#0505C0");
            hl.ForeColor = Color.FromArgb(5,5,192);
            hl.Font.Name = "Verdana";
            hl.Font.Size = FontUnit.Point(8);
            Controls.Add(hl);

                        
                        string[] aPath = Page.Request.Url.AbsolutePath.Split(new Char[] {'/'});
            int cntPath = aPath.Length;
                        int iSchoolID = -1;

            //now check if a school_id exists. if so then this is a school specific
            //page and we'll need to reflect that in the navbar.
            //<BUGBUG>i don't like this method as it makes the navbar inconsistent with the
            //directory structure!!</BUGBUG>
            if (HttpContext.Current.Request.Params["school_id"] != null) 
            {
                Controls.Add(lblSeperator);
                hl = new HyperLink();
                hl.Text = "Universities";
                //hl.NavigateUrl = "/school-state.aspx";
                hl.ForeColor = Color.FromArgb(5,5,192);
                hl.Font.Name = "Verdana";
                hl.Font.Size = FontUnit.Point(8);
                Controls.Add(hl);

                Controls.Add(lblSeperator);
                hl = new HyperLink();
                hl.Text = DH_Schools.DH_SchoolsDB.GetSchoolNameFromID(Convert.ToInt32(HttpContext.Current.Request.Params["school_id"]));
                hl.NavigateUrl = DH_Schools.DH_SchoolsDB.GetSchoolUrlFromID(Convert.ToInt32(HttpContext.Current.Request.Params["school_id"]));
                hl.ForeColor = Color.FromArgb(5,5,192);
                hl.Font.Name = "Verdana";
                hl.Font.Size = FontUnit.Point(8);
                Controls.Add(hl);
            }
                        //HACK FOR THE SCHOOL MESSAGE FORUMS
                        else if (aPath[cntPath-1] == "school_categories.aspx")
            {
                Controls.Add(lblSeperator);
                hl = new HyperLink();
                hl.Text = "Universities";
                //hl.NavigateUrl = "/school-state.aspx";
                hl.ForeColor = Color.FromArgb(5,5,192);
                hl.Font.Name = "Verdana";
                hl.Font.Size = FontUnit.Point(8);
                Controls.Add(hl);

                Controls.Add(lblSeperator);
                hl = new HyperLink();
                                iSchoolID = DH_Users.DH_UserDB.GetSchoolIDFromUserID(HttpContext.Current.User.Identity.Name);
                hl.Text = DH_Schools.DH_SchoolsDB.GetSchoolNameFromID(iSchoolID);
                hl.NavigateUrl = DH_Schools.DH_SchoolsDB.GetSchoolUrlFromID(iSchoolID);
                hl.ForeColor = Color.FromArgb(5,5,192);
                hl.Font.Name = "Verdana";
                hl.Font.Size = FontUnit.Point(8);
                Controls.Add(hl);
            }
                        
            //start the iteratation at i=1 because we want to disregard the first element
            //of the array after the Split because it will be an empty string. We also
            //want to disregard the last element of the array because that will just be
            //the file name, which will be replaced with the title of the page.
                        string strLinkText = "";
            string strCompleteUrl = "";
            for (int i=1;i < cntPath-1; i++) {
                Controls.Add(lblSeperator);
                                        
                hl = new HyperLink();
                strLinkText = aPath[i];
                hl.Text = Char.ToUpper(strLinkText[0]) + strLinkText.Substring(1).ToLower();

                strCompleteUrl += "/"+aPath[i].ToString();
                                hl.NavigateUrl = strCompleteUrl;                                                                

                                //HACK FOR THE SCHOOL MESSAGE FORUMS!
                                if ((HttpContext.Current.Request.Params["school_id"] != null ||
                                     aPath[cntPath-1] == "school_categories.aspx") &&
                                    hl.Text == "Messages") {
                                  hl.NavigateUrl = "/messages/school_categories.aspx";
                                }
                                  
                //hl.ForeColor = Color.FromString("#0505C0");
                hl.ForeColor = Color.FromArgb(5,5,192);
                hl.Font.Name = "Verdana";
                hl.Font.Size = FontUnit.Point(8);
            
                Controls.Add(hl);
            }

            //put in the category name if it exists
            if (strCategoryName != String.Empty) {
                Controls.Add(lblSeperator);

                hl = new HyperLink();
                hl.Text = strCategoryName;
                hl.NavigateUrl = strCategoryLink;
                //hl.ForeColor = Color.FromString("#0505C0");
                hl.ForeColor = Color.FromArgb(5,5,192);
                hl.Font.Name = "Verdana";
                hl.Font.Size = FontUnit.Point(8);

                Controls.Add(hl);
            }            
                
            //create the last element from the title of the page.
            Controls.Add(lblSeperator);

            Label lblTitle = new Label();

            //if a title exists in the page, then use that, otherwise use the file name.
            if (Convert.ToString(Page.FindControl("page_title")) != String.Empty) {
                lblTitle.Text = ((HtmlGenericControl)Page.FindControl("page_title")).InnerText;
            }
            else {
                lblTitle.Text = aPath[cntPath-1].ToString();
            }
            
            if (lblTitle.Text.Length > iCutOff) {
                lblTitle.Text = lblTitle.Text.Substring(0, iCutOff) + "...";
            }

            lblTitle.ForeColor = Color.Black;
            lblTitle.Font.Name = "Verdana";
            lblTitle.Font.Size = FontUnit.Point(8);
            lblTitle.Font.Bold = true;
            Controls.Add(lblTitle);
        }
    }
}