//http://groups.google.com/group/jquery-en/browse_thread/thread/f4ac0893083fda8

$.editable.addInputType('datetimepicker', {
        element: function(settings, original) {
                var dayselect = $('<select id="day_">');
                var monthselect = $('<select id="month_">');
                var yearselect = $('<select id="year_">');
                var hourselect = $('<select id="hour_">');
                var minselect  = $('<select id="minute_">');
                var secselect  = $('<select id="second_">');
                
                /* Day loop */
                for(var day=1; day <=31; day++) {
                    if(day < 10) {
                    	day = '0' + day;
                    }
                    var option = $('<option>').val(day).append(day);
                    dayselect.append(option);
                }
                $(this).append(dayselect);

                /* Month loop */
                for(var month=1; month <= 12; month++) {
                        var monthname = [];
                        monthname[1] = "January";
                        monthname[2] = "February";
                        monthname[3] = "March";
                        monthname[4] = "April";
                        monthname[5] = "May";
                        monthname[6] = "June";
                        monthname[7] = "July";
                        monthname[8] = "August";
                        monthname[9] = "September";
                        monthname[10] = "October";
                        monthname[11] = "November";
                        monthname[12] = "December";
                        if(month < 10) {
                        	monthnum = '0' + month;
                        } else {
                            monthnum = month;
                        }
                        var option = $('<option>').val(monthnum).append(monthname[month]);
                        monthselect.append(option);
                }
                $(this).append(monthselect);

                /* Year loop */
                var d = new Date();
                for(var year=parseInt(d.getFullYear()); year >= parseInt(d.getFullYear())-70; year--) {
                        var option = $('<option>').val(year).append(year);
                        yearselect.append(option);
                }
                $(this).append(yearselect);

                
                /* Hour loop */
                for (var hour=1; hour <= 24; hour++) {
                    if (hour < 10) {
                        hour = '0' + hour;
                    }
                    var option = $('<option>').val(hour).append(hour);
                    hourselect.append(option);
                }
                $(this).append(hourselect);

                /* Minutes loop */
                //for (var min=0; min <= 45; min = parseInt(min)+15) {
                for (var min=0; min <= 60; min++) {
                    if (min < 10) {
                        min = '0' + min;
                    }
                    var option = $('<option>').val(min).append(min);
                    minselect.append(option);
                }
                $(this).append(minselect);

                /* Seconds loop */
                //for (var sec=0; sec <= 45; sec = parseInt(sec)+15) {
                for (var sec=0; sec <= 60; sec++) {
                    if (sec < 10) {
                        sec = '0' + sec;
                    }
                    var option = $('<option>').val(sec).append(sec);
                    secselect.append(option);
                }
                $(this).append(secselect);
                
                
                /* Hidden input to store value which is submitted to server. */
                var hidden = $('<input type="hidden">');
                $(this).append(hidden);
                return(hidden);
        },
        content: function(string, settings, original) {
                //preselect the selects with values out of database
        		var day = parseInt(string.substr(0,2));
                var month = parseInt(string.substr(3,2));
                var year = parseInt(string.substr(6,4));
                
                var hour = parseInt(string.substr(11,2));
                var minute = parseInt(string.substr(14,2));
                var second = parseInt(string.substr(17,2));

                $("#day_", this).children().each(function() {
                    if(day == $(this).val()) {
                        $(this).attr('selected', 'selected');
                    }
                });

                $("#month_", this).children().each(function() {
                    if(month == $(this).val()) {
                        $(this).attr('selected', 'selected');
                    }
                });

                $("#year_", this).children().each(function() {
                    if(year == $(this).val()) {
                        $(this).attr('selected', 'selected');
                    }
                });
                
                $("#hour_", this).children().each(function() {
                    if(hour == $(this).val()) {
                        $(this).attr('selected', 'selected');
                    }
                });
                $("#minute_", this).children().each(function() {
                    if(minute == $(this).val()) {
                        $(this).attr('selected', 'selected');
                    }
                });
                $("#second_", this).children().each(function() {
                    if(second == $(this).val()) {
                        $(this).attr('selected', 'selected');
                    }
                });
        },
        submit: function(settings, original) {
                var value = $("#day_").val() + "." + $("#month_").val() + "." + $("#year_").val() + " " + $("#hour_").val() + ":" + $("#minute_").val() + ":" + $("#second_").val();
                $("input", this).val(value);
        	
        		//returns timestamp
        		//var d = new Date($("#year_").val(), $("#month_").val(), $("#day_").val(), $("#hour_").val(), $("#minute_").val(), $("#second_").val());
                //$("input", this).val(d.getTime());
        }
}); 
