/*
    Copyright 2005 Rolando Gonzalez (rolosworld@gmail.com)

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program; if not, write to the Free Software
    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

// Constants
var months = [ "Ocak",
	       "Şubat",
	       "Mart",
	       "Nisan",
	       "Mayıs",
	       "Haziran",
	       "Temmuz",
	       "Ağustos",
	       "Eylül",
	       "Ekim",
	       "Kasım",
	       "Aralık" ];

var wdays = [ "Pzr",
	      "Pzt",
	      "Salı",
	      "Çar",
	      "Per",
	      "Cum",
	      "Cmt" ];

// returns the last day of the month
function getLastDay(date)
{
  var start = new Date(date);
  start.setMonth(start.getMonth()+1);
  start.setDate(0);
  return start.getDate();
}

// compare month, date and year from 2 dates
function sameDates(date1, date2)
{
  if(date1.getMonth() == date2.getMonth() &&
     date1.getDate() == date2.getDate() &&
     date1.getFullYear() == date2.getFullYear())
    return true;
  return false;
}

// Calendar Object
function Calendar(fromYear, toYear)
{
  var me = this;

  this.from_year = fromYear;
  this.to_year = toYear;

  this.current_date = new Date();
  this.selected_date = new Date();
  this.browse_date = new Date();

  this.select_month = document.createElement("select");
  this.select_year = document.createElement("select");
  
  this.thead = document.createElement("thead");
  this.tbody = document.createElement("tbody");

  // callback called when day's are clicked,
  // called as function(date)
  this.callback = null;

  this.select_month.onchange = function()
  {
    me.browse_date.setMonth(me.select_month.value);
    me.body();
  };

  this.select_year.onchange = function()
  {
    me.browse_date.setFullYear(me.select_year.value);
    me.body();
  };

  this.newDay = function(d)
  {
    var c = "cd";
    var td = document.createElement("td");
    var date = new Date(me.browse_date);
    date.setDate(d);

    if(sameDates(date, me.selected_date))
      c = "csd";
    else if(sameDates(date, me.current_date))
      c = "ccd";

    td.setAttribute("className",c);
    td.setAttribute("class",c);
    td.day=d;

    td.onclick = function()
    {
      /*me.selected_date = new Date(date);
      me.body();
      me.callback(date);*/
    }
    td.onmouseover = function()
    {
      td.setAttribute("className","cod");
      td.setAttribute("class","cod");
    }
    td.onmouseout = function()
    {
      td.setAttribute("className",c);
      td.setAttribute("class",c);
    }
    td.appendChild(document.createTextNode(d));
    return td;
  };

  // sets the selected date, its displayed on different color
  this.setSelectedDate = function(date)
  {
    me.selected_date = new Date(date);
    me.browse_date = new Date(date);
  };

  //Draw head of calendar,  month , year,  week days.
  this.head = function()
  {
    var tr = document.createElement("tr");
    var td = document.createElement("td");
    var form = document.createElement("form");
    var option;// = document.createElement("option");

    td.setAttribute("className","cmy");
    td.setAttribute("class","cmy");
    td.setAttribute("colSpan","7");
      
    //draw months
    for( i = 0; i < months.length; i++ )
    {  
      option = document.createElement("option");
      option.setAttribute("value",i);
      option.appendChild(document.createTextNode(months[i]));
      me.select_month.appendChild(option);
    }

    // draw years
    for( i = me.from_year; i <= me.to_year; i++ )
    {
      option = document.createElement("option");
      option.setAttribute("value",i);
      option.appendChild(document.createTextNode(i));
      me.select_year.appendChild(option);
    }
      
    me.select_month = form.appendChild(me.select_month);
    me.select_year = form.appendChild(me.select_year);
    td.appendChild(form);
    tr.appendChild(td);
    me.thead.appendChild(tr);

    me.select_month.selectedIndex = me.browse_date.getMonth();    
    me.select_year.value = me.browse_date.getFullYear();    
  };

  // Draw days
  this.body = function()
  {
    while(me.tbody.firstChild) me.tbody.removeChild(me.tbody.firstChild);
    var tr = document.createElement("tr");
    var td = new Array();
    var th;
    var date = new Date(me.browse_date);
    var day;
    var i;

    date.setDate(1);

    // draw week days
    for(i = 0; i < wdays.length; i++ )
    {
      th = document.createElement("th");
      th.setAttribute("className","cwd");
      th.setAttribute("class","cwd");
      th.appendChild(document.createTextNode(wdays[i]));
      tr.appendChild(th);
    }
    me.tbody.appendChild(tr);

    // draw initial blank days
    for(i = 0; i < date.getDay(); i++ )
      td[i] = document.createElement("td");

    var last_day = getLastDay(date);
    // draw days
    for(day = 1; day < last_day+1; day++)
      td[i++] = me.newDay(day);

    // draw last blank days
    for(i; i < 42; i++ )
      td[i] = document.createElement("td");

    // insert days in rows
    for(i = 0; i < 6; i++)
    {
      tr = document.createElement("tr");
      for(var j = i*7; j < (i*7)+7; j++)
        tr.appendChild(td[j]);
      me.tbody.appendChild(tr);
    }
  };

  // returns calendar table element
  this.get = function()
  {
    me.head();
    me.body();
    var table = document.createElement("table");
    table.setAttribute("className","ct");
    table.setAttribute("class","ct");
    table.setAttribute("cellspacing","0");
    table.setAttribute("cellpadding","2");
    me.thead = table.appendChild(me.thead);
    me.tbody = table.appendChild(me.tbody);
    return table;
  };
}
