(function(){
	
    var Event = YAHOO.util.Event, Dom = YAHOO.util.Dom;
	
	// start setup i18n
	var lang = Dom.getElementBy(function () {return true;},"html").getAttribute("lang");
	var buttonLabel = "Choose a date";
	if (lang === "de-DE") {
		buttonLabel = "Datum auswählen";
	}
	// end setup i18n
	
	Event.onContentReady("rt_messagefields", function () {
		var iCharLimit = 140 - Dom.get("smsfooterlength").value, iCharCount = 0;
		var oCounter = Dom.get("rt_charcount"), oTextArea = Dom.get("rt_messagetxa");
		
		oCounter.innerHTML = iCharLimit;
				
		Event.on("rt_messagetxa", "keyup", function () {
			oTextArea.value = oTextArea.value.substring(0, iCharLimit);
			iCharCount = oTextArea.value.length;
			oCounter.innerHTML = (iCharLimit - iCharCount);			
		});
	});

    Event.onContentReady("rt_datefields", function (){
		// Calendar instance based on YUI calendar button example
        var oCalendarMenu;
        var onButtonClick = function(){
        
            // Create a Calendar instance and render it into the body 
            // element of the Overlay.            
            var oCalendar = new YAHOO.widget.Calendar("buttoncalendar", oCalendarMenu.body.id);
			if (lang === "de-DE") {
				// Correct formats for Germany: dd.mm.yyyy, dd.mm, mm.yyyy
				oCalendar.cfg.setProperty("DATE_FIELD_DELIMITER", ".");

				oCalendar.cfg.setProperty("MDY_DAY_POSITION", 1);
				oCalendar.cfg.setProperty("MDY_MONTH_POSITION", 2);
				oCalendar.cfg.setProperty("MDY_YEAR_POSITION", 3);

				oCalendar.cfg.setProperty("MD_DAY_POSITION", 1);
				oCalendar.cfg.setProperty("MD_MONTH_POSITION", 2);

				// Date labels for German locale
				oCalendar.cfg.setProperty("MONTHS_SHORT",   ["Jan", "Feb", "M\u00E4r", "Apr", "Mai", "Jun", "Jul", "Aug", "Sep", "Okt", "Nov", "Dez"]);
				oCalendar.cfg.setProperty("MONTHS_LONG",    ["Januar", "Februar", "M\u00E4rz", "April", "Mai", "Juni", "Juli", "August", "September", "Oktober", "November", "Dezember"]);
				oCalendar.cfg.setProperty("WEEKDAYS_1CHAR", ["S", "M", "D", "M", "D", "F", "S"]);
				oCalendar.cfg.setProperty("WEEKDAYS_SHORT", ["So", "Mo", "Di", "Mi", "Do", "Fr", "Sa"]);
				oCalendar.cfg.setProperty("WEEKDAYS_MEDIUM",["Son", "Mon", "Die", "Mit", "Don", "Fre", "Sam"]);
				oCalendar.cfg.setProperty("WEEKDAYS_LONG",  ["Sonntag", "Montag", "Dienstag", "Mittwoch", "Donnerstag", "Freitag", "Samstag"]);		
			}
            oCalendar.render();
            
            // Subscribe to the Calendar instance's "select" event to 
            // update the Button instance's label when the user
            // selects a date.
            oCalendar.selectEvent.subscribe(function(p_sType, p_aArgs){
                var aDate, nMonth, nDay, nYear;
                if (p_aArgs) {
                    aDate = p_aArgs[0][0];
                    
                    nMonth = aDate[1];
                    nDay = aDate[2];
                    nYear = aDate[0];
                    
                    oButton.set("label", (nDay + "/" + nMonth + "/" + nYear));
                    
                    // Sync the Calendar instance's selected date with the date form fields
                    Dom.get("rt_month").selectedIndex = (nMonth - 1);
                    Dom.get("rt_day").selectedIndex = (nDay - 1);
                    Dom.get("rt_year").value = nYear;
                }
                oCalendarMenu.hide();
            });
            
            // Pressing the Esc key will hide the Calendar Menu and send focus back to 
            // its parent Button
            Event.on(oCalendarMenu.element, "keydown", function(p_oEvent){
                if (Event.getCharCode(p_oEvent) === 27) {
                    oCalendarMenu.hide();
                    this.focus();
                }
            }, null, this);
            
            var focusDay = function(){
                var oCalendarTBody = Dom.get("buttoncalendar").tBodies[0], aElements = oCalendarTBody.getElementsByTagName("a"), oAnchor;
                
                if (aElements.length > 0) {
                	Dom.batch(aElements, function(element){
                    	if (Dom.hasClass(element.parentNode, "today")) {
                            oAnchor = element;
                        }
                    });
                    
                    
                    if (!oAnchor) {
                        oAnchor = aElements[0];
	                    }
                          
                    // Focus the anchor element using a timer since Calendar will try 
                    // to set focus to its next button by default
                    YAHOO.lang.later(0, oAnchor, function(){
                        try {
                            oAnchor.focus();
                        } 
                        catch (e) {
                        }
                    });
                }
            };
            
            // Set focus to either the current day, or first day of the month in 
            // the Calendar	when it is made visible or the month changes
            oCalendarMenu.subscribe("show", focusDay);
            oCalendar.renderEvent.subscribe(focusDay, oCalendar, true);
            
            // Give the Calendar an initial focus
            focusDay.call(oCalendar);
            
            // Re-align the CalendarMenu to the Button to ensure that it is in the correct
            // position when it is initial made visible
            oCalendarMenu.align();
            
            // Unsubscribe from the "click" event so that this code is 
            // only executed once
            this.unsubscribe("click", onButtonClick);
        };
        
        var oDateFields = Dom.get("datefields");
        oDayField = Dom.get("rt_day"), oMonthField = Dom.get("rt_month"), oYearField = Dom.get("rt_year");
        
        // Hide the form fields used for the date so that they can be replaced by the 
        // calendar button.
        oMonthField.style.display = "none";
        oDayField.style.display = "none";
        oYearField.style.display = "none";
        
        // Create a Overlay instance to house the Calendar instance
        oCalendarMenu = new YAHOO.widget.Overlay("calendarmenu", {
            visible: false
        });
          
        // Create a Button instance of type "menu"
        var oButton = new YAHOO.widget.Button({
            type: "menu",
            id: "calendarpicker",
            label: buttonLabel,
            menu: oCalendarMenu,
            container: "rt_datefields"
        });
		if (Dom.get("rt_year").value) {
			oButton.set("label", (Dom.get("rt_day").value + "/" + Dom.get("rt_month").value + "/" + Dom.get("rt_year").value));
		}
        
        oButton.on("appendTo", function(){
        	// Create an empty body element for the Overlay instance in order 
            // to reserve space to render the Calendar instance into.
            oCalendarMenu.setBody("&#32;");
            oCalendarMenu.body.id = "calendarcontainer";
        });
        
        // Add a listener for the "click" event.  This listener will be
        // used to defer the creation the Calendar instance until the 
        // first time the Button's Overlay instance is requested to be displayed
        // by the user.
        oButton.on("click", onButtonClick);
    });
}());
