/// <summary>
        /// Most of the work happens here for generating the hook up script code
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            // MS AJAX aware script management
            ClientScriptProxy scriptProxy = ClientScriptProxy.Current;

            // Register resources
            RegisterResources(scriptProxy);

            string dateFormat = DateFormat;

            if (string.IsNullOrEmpty(dateFormat) || dateFormat == "Auto")
            {
                // Try to create a data format string from culture settings
                // this code will fail if culture can't be mapped on server hence the empty try/catch
                try
                {
                    dateFormat = CultureInfo.CurrentCulture.DateTimeFormat.ShortDatePattern;
                }
                catch { }
            }

            dateFormat = dateFormat.ToLower().Replace("yyyy", "yy");

            // Capture and map the various option parameters
            StringBuilder sbOptions = new StringBuilder(512);

            sbOptions.Append("{");

            string onSelect = OnClientSelect;

            if (DisplayMode == DatePickerDisplayModes.Button)
            {
                sbOptions.Append("showOn: 'button',");
            }
            else if (DisplayMode == DatePickerDisplayModes.ImageButton)
            {
                string img = ButtonImage;
                if (img == "WebResource")
                {
                    img = scriptProxy.GetWebResourceUrl(this, typeof(ControlResources), ControlResources.CALENDAR_ICON_RESOURCE);
                }
                else
                {
                    img = ResolveUrl(ButtonImage);
                }

                sbOptions.Append("showOn: 'button', buttonImageOnly: true, buttonImage: '" + img + "',buttonText: 'Select date',");
            }
            else if (DisplayMode == DatePickerDisplayModes.Inline)
            {
                // need to store selection in the page somehow for inline since it's
                // not tied to a textbox
                scriptProxy.RegisterHiddenField(this, ClientID, Text);
                onSelect = ClientID + "OnSelect";
            }

            if (!string.IsNullOrEmpty(onSelect))
            {
                sbOptions.Append("onSelect: " + onSelect + ",");
            }

            if (DisplayMode != DatePickerDisplayModes.Inline)
            {
                if (!string.IsNullOrEmpty(OnClientBeforeShow))
                {
                    sbOptions.Append("beforeShow: function(y,z) { $('#ui-datepicker-div').maxZIndex(); " +
                                     OnClientBeforeShow + "(y,z); },");
                }
                else
                {
                    sbOptions.Append("beforeShow: function() { $('#ui-datepicker-div').maxZIndex(); },");
                }
            }

            if (MaxDate.HasValue)
            {
                sbOptions.Append("maxDate: " + UrlUtils.EncodeJsDate(MaxDate.Value) + ",");
            }

            if (MinDate.HasValue)
            {
                sbOptions.Append("minDate: " + UrlUtils.EncodeJsDate(MinDate.Value) + ",");
            }

            if (ShowButtonPanel)
            {
                sbOptions.Append("showButtonPanel: true,");
            }

            sbOptions.Append("dateFormat: '" + dateFormat + "'}");


            // Write out initilization code for calendar
            StringBuilder sbStartupScript = new StringBuilder(400);

            sbStartupScript.AppendLine("$( function() {");


            if (DisplayMode != DatePickerDisplayModes.Inline)
            {
                scriptProxy.RegisterClientScriptBlock(Page,
                                                      typeof(ControlResources),
                                                      "__attachDatePickerInputKeys",
                                                      AttachDatePickerKeysScript, true);

                sbStartupScript.AppendFormat("var cal = jQuery('#{0}').datepicker({1}).attachDatepickerInputKeys();\r\n",
                                             ClientID, sbOptions);
            }
            else
            {
                sbStartupScript.AppendLine("var cal = jQuery('#" + ClientID + "Div').datepicker(" + sbOptions.ToString() + ")");

                if (SelectedDate.HasValue && SelectedDate.Value > new DateTime(1900, 1, 1, 0, 0, 0, DateTimeKind.Utc))
                {
                    CustomJsonSerializer ser = new CustomJsonSerializer();
                    ser.DateSerializationMode = JsonDateEncodingModes.NewDateExpression;
                    string jsDate = ser.Serialize(SelectedDate);

                    sbStartupScript.AppendLine("cal.datepicker('setDate'," + jsDate + ");");
                }
                else
                {
                    sbStartupScript.AppendLine("cal.datepicker('setDate',new Date());");
                }

                // Assign value to hidden form var on selection
                scriptProxy.RegisterStartupScript(this, typeof(ControlResources), UniqueID + "OnSelect",
                                                  "function  " + ClientID + "OnSelect(dateStr) {\r\n" +
                                                  ((!string.IsNullOrEmpty(OnClientSelect)) ? OnClientSelect + "(dateStr);\r\n" : "") +
                                                  "jQuery('#" + ClientID + "')[0].value = dateStr;\r\n}\r\n", true);
            }

            sbStartupScript.AppendLine("} );");
            scriptProxy.RegisterStartupScript(Page, typeof(ControlResources), "_cal" + UniqueID,
                                              sbStartupScript.ToString(), true);
        }