//
// Jala Project [http://opensvn.csie.org/traccgi/jala]
//
// Copyright 2004 ORF Online und Teletext GmbH
//
// Licensed under the Apache License, Version 2.0 (the ``License'');
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//    http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an ``AS IS'' BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// $Revision$
// $LastChangedBy$
// $LastChangedDate$
// $HeadURL$
//
/**
 * @fileoverview Fields and methods of the jala.Date class.
 */
// Define the global namespace for Jala modules
if (!global.jala) {
   global.jala = {};
}
/**
 * HelmaLib dependencies
 */
app.addRepository("modules/core/Date.js");
app.addRepository("modules/helma/Html.js");
/**
 * Constructs a new Renderings object.
 * @class This class provides various convenience
 * methods for rendering purposes.
 * @constructor
 */
jala.Date = function() {
   return this;
};
/**
 * Renders a timestamp as set of DropDown boxes, following the
 * format passed as argument. Every <select>
 * item is prefixed with a string so that it can be retrieved
 * easily from the values of a submitted POST request.
 * @param {String} prefix The prefix to use for all dropdown boxes, eg. "postdate"
 * @param {Date} date A Date object to use as preselection (optional)
 * @param {Object} fmt Array containing one parameter object for every single
 * select box that should be rendered, with the following properties set:
 * 
 * - pattern - The date format pattern that should be rendered. Valid
 * patterns are: "dd", "MM", "yyyy", "HH", "ss".*
- firstOption - The string to use as first option, eg.: "choose a day"*
*/
jala.Date.prototype.renderEditor = function(prefix, date, fmt) {
   /**
    * rendering method
    * @private
    */
   var render = function(param, date) {
      switch (param.pattern) {
         case "dd":
         param.offset = 1;
         param.max = 31;
         param.selected = (date ? date.getDate() : null);
         break;
         case "MM":
         param.offset = 1;
         param.max = 12;
         param.selected = (date ? date.getMonth() +1 : null);
         break;
         case "yyyy":
         param.offset = 2002;
         param.max = 20;
         param.selected = (date ? date.getFullYear() : null);
         break;
         case "HH":
         param.offset = 0;
         param.max = 24;
         param.selected = (date ? date.getHours() : null);
         break;
         case "mm":
         param.offset = 0;
         param.max = 60;
         param.selected = (date ? date.getMinutes() : null);
         break;
         case "ss":
         param.offset = 0;
         param.max = 60;
         param.selected = (date ? date.getSeconds() : null);
         break;
      }
      var key = prefix + ":" + param.pattern;
      if (req.data[key])
         param.selected = req.data[key];
      var options = [];
      var opt;
      for (var i=0;i days) {
            renderer.renderDay(null);
         } else {
            date.setDate(daycnt);
            if ((dayObj = collection.get(date.format(accessNameFormat))) != null) {
               idx = collection.contains(dayObj);
               if (idx > -1) {
                  if  (idx > firstDayIndex) {
                     firstDayIndex = idx;
                  }
                  if (idx < lastDayIndex) {
                     lastDayIndex = idx;
                  }
               }
            }
            selected = (today != null) ? date.equals(today) : false;
            renderer.renderDay(date, dayObj != null, selected);
            daycnt++;
         }
      }
      renderer.renderRow(res.pop());
   }
   var prevMonth = prevNextMonth("prev", firstDayIndex) || null;
   var nextMonth = prevNextMonth("next", lastDayIndex) || null;
   renderer.renderCalendar(date, res.pop(), prevMonth, nextMonth);
   return;
};
/**
 * Returns a rendered calendar
 * @see #renderCalendar
 * @type String
 */
jala.Date.Calendar.prototype.getCalendar = function(today) {
   res.push();
   this.render(today);
   return res.pop();
};
/**
 * Returns a new instance of the default calendar renderer.
 * @class A default renderer to use in conjunction with jala.Date.Calendar
 * @param {jala.Date.Calendar} calendar The calendar utilizing this renderer
 * @returns A newly created instance of jala.Date.Calendar.Renderer
 * @constructor
 */
jala.Date.Calendar.Renderer = function(calendar) {
   /**
    * An instance of helma.Html used for rendering the calendar
    * @type helma.Html
    */
   this.html = new helma.Html();
   /**
    * The calendar utilizing this renderer instance
    * @type jala.Date.Calendar
    */
   this.calendar = calendar;
   return this;
};
/** @ignore */
jala.Date.Calendar.Renderer.prototype.toString = function() {
   return "[Jala Calendar Default Renderer]";
};
/**
 * Renders a single cell in the calendar day header row directly to response.
 * @param {String} text The text to display in the header field.
 */
jala.Date.Calendar.Renderer.prototype.renderDayHeader = function(text) {
   this.html.element("th", text);
   return;
};
/**
 * Renders a single calendar row directly to response.
 * @param {String} row The body of the calendar row.
 */
jala.Date.Calendar.Renderer.prototype.renderRow = function(row) {
   this.html.element("tr", row);
   return;
};
/**
 * Renders a single day within the calendar directly to response.
 * @param {Date} date A date instance representing the day within the calendar.
 * @param {Boolean} isExisting True if there is a child object in the calendar's
 * collection to which the date cell should link to
 * @param {Boolean} isSelected True if this calendar day should be rendered
 * as selected day.
 */
jala.Date.Calendar.Renderer.prototype.renderDay = function(date, isExisting, isSelected) {
   var attr = {"class": "jala-calendar-day day"};
   if (isSelected === true) {
      attr["class"] += " jala-calendar-selected selected";
   }
   this.html.openTag("td", attr);
   if (date != null) {
      var text = date.getDate();
      if (isExisting === true) {
         attr = {"href": this.calendar.getCollection().href() +
                         date.format(this.calendar.getHrefFormat())};
         this.html.link(attr, text);
      } else {
         res.write(text);
      }
   }
   this.html.closeTag("td");
   return;
};
/**
 * Renders a link to the previous or next month's calendar directly to response.
 * @param {Date} date A date object set to the previous or next available
 * month. This can be null in case there is no previous or next month.
 */
jala.Date.Calendar.Renderer.prototype.renderPrevNextLink = function(date) {
   if (date != null) {
      var attr = {"href": this.calendar.getCollection().href() +
                          date.format(this.calendar.getHrefFormat())};
      this.html.link(attr, date.format("MMMM", this.calendar.getLocale()));
   }
   return;
};
/**
 * Renders the calendar directly to response.
 * @param {Date} date A date object representing this calendar's month and year.
 * Please mind that the day will be set to the last date in this
 * month.
 * @param {String} body The rendered calendar weeks including the day header
 * (basically the whole kernel of the table).
 * @param {Date} prevMonth A date object set to the last available date of
 * the previous month. This can be used to render a navigation link to
 * the previous month.
 * @param {Date} nextMonth A date object set to the first available date
 * of the next month. This can be used to render a navigation link to
 * the next month.
 */
jala.Date.Calendar.Renderer.prototype.renderCalendar = function(date, body, prevMonth, nextMonth) {
   var locale = this.calendar.getLocale();
   this.html.openTag("table", {"class": "jala-calendar calendar"});
   this.html.openTag("thead");
   this.html.openTag("tr");
   this.html.openTag("th", {"colspan": 7});
   res.write(date.format("MMMM", locale));
   res.write(' ');
   res.write(date.format("yyyy", locale));
   this.html.closeTag("th");
   this.html.closeTag("tr");
   this.html.closeTag("thead");
   this.html.element("tbody", body);
   this.html.openTag("tfoot");
   this.html.openTag("tr");
   this.html.openTag("td", {"class": "jala-calendar-left left", "colspan": 3});
   this.renderPrevNextLink(prevMonth);
   this.html.closeTag("td");
   this.html.openTag("td");
   this.html.closeTag("td");
   this.html.openTag("td", {"class": "jala-calendar-right right", "colspan": 3});
   this.renderPrevNextLink(nextMonth);
   this.html.closeTag("td");
   this.html.closeTag("tr");
   this.html.closeTag("tfoot");
   this.html.closeTag("table");
   return;
};
/**
 * Default date class instance.
 * @type jala.Date
 * @final
 */
jala.date = new jala.Date();