/*
Date Input 1.1.5
Requires jQuery version: 1.2
Requires plugins:
  * Dimensions - http://plugins.jquery.com/files/dimensions_1.2.zip

Copyright (c) 2007-2008 Jonathan Leighton & Torchbox Ltd

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/

DateInput = (function($) { // Localise the $ function
var today = new Date();

function DateInput(el, opts) {
  if (typeof(opts) != "object") opts = {};
  $.extend(this, DateInput.DEFAULT_OPTS, opts);
  
  this.input = $(el);
  this.bindMethodsToObj("show", "hide", "hideIfClickOutside", "selectDate", "prevMonth", "nextMonth");
  
  this.build();
  this.selectDate();
  this.hide();
};
DateInput.DEFAULT_OPTS = {
  month_names: ["Januari", "Februari", "Mars", "April", "Maj", "Juni", "Juli", "Augusti", "September", "Oktober", "November", "December"],
  short_month_names: ["jan", "feb", "mar", "apr", "maj", "jun", "jul", "aug", "sep", "okt", "nov", "dec"],
  short_day_names: ["s&ouml;n", "m&aring;n", "tis", "ons", "tor", "fre", "l&ouml;r"],
  start_of_week: 1,
  validate: function(diff) {}
};
DateInput.prototype = {
  build: function() {
    this.monthNameSpan = $('<span class="month_name"></span>');
    var monthNav = $('<p class="month_nav"></p>').append(
      $('<a href="#" class="prev">&laquo;</a>').click(this.prevMonth),
      " ", this.monthNameSpan, " ",
      $('<a href="#" class="next">&raquo;</a>').click(this.nextMonth)
    );
    
    var tableShell = "<table><thead><tr>";
    $(this.adjustDays(this.short_day_names)).each(function() {
      tableShell += "<th>" + this + "</th>";
    });
    tableShell += "</tr></thead><tbody></tbody></table>";
    
    this.dateSelector = this.rootLayers = $('<div class="date_selector"></div>').append(monthNav, tableShell).appendTo(document.body);
    
    if ($.browser.msie && $.browser.version < 7) {
      this.ieframe = $('<iframe class="date_selector_ieframe" frameborder="0" src="#"></iframe>').insertBefore(this.dateSelector);
      this.rootLayers = this.rootLayers.add(this.ieframe);
    };
    
    this.tbody = $("tbody", this.dateSelector);
    
    // The anon function ensures the event is discarded
    this.input.keyup(this.bindToObj(function() { if(!isNaN(this.value)) this.selectDate(); }));
  	this._buildMonth();
  },
  _allDays: null,
  
  _buildMonth: function()
  {
  	var _html = [];
  	for(var i=1; i<=42; ++i)
  	{
  		var day = i%7;
  		
  		if(this.isFirstDayOfWeek(day)) _html.push("<tr>");
  		_html.push("<td><a href='#'></a></td>");
  		if(this.isLastDayOfWeek(day)) _html.push("</tr>");
  	}
  	this.tbody.empty().append(_html.join(""));
  	this._allDays = $("td", this.tbody);
  	
  	$("a", this.tbody).click(this.bindToObj(function(event)
	{
		var diff = $(event.target).parent().attr("diff");
		var date = new Date();
		date.setDate(date.getDate() + parseInt(diff));
		this.selectDate(date);
		this.hide();
		return false;
	}));
  },
  
  _displayedMonth: null,
  selectMonth: function(date) 
  {
  	if(isNaN(date.valueOf())) date = new Date();
  	var currentMonth = new Date(date.getFullYear(), date.getMonth(), 1);
  	if(this._displayedMonth && currentMonth.valueOf()==this._displayedMonth) return;
  	this._displayedMonth = currentMonth.valueOf();
  	this.currentMonth = currentMonth;
  	var month = date.getMonth();
  	var rangeDate = this.rangeStart(date);
  	
  	var today = new Date();
  	var todayDay = today.getDate();
  	var todayMonth = today.getMonth();
  	var todayYear = today.getYear();
  	
  	for(var i=0, td; td = this._allDays[i]; i++)
  	{
  		var dateDay = rangeDate.getDate();
  		var dateMonth = rangeDate.getMonth();
  		var dateYear = rangeDate.getYear();
  		
  		$("a", td).html(dateDay);
  		td.className = "";
  		//td.setAttribute("date", "D" + rangeDate.valueOf());
  		td.setAttribute("diff", this.daysBetween(today, rangeDate));
  		if(dateMonth!=month) td.className = "unselected_month";
  		if(dateDay==todayDay && dateMonth==todayMonth && dateYear==todayYear) td.className += " today";
  		
  		rangeDate.setDate(dateDay+1);
  	}
    this.monthNameSpan.html(this.monthName(date) + " " + date.getFullYear());
    this.setPosition();
  },
  
  selectDate: function(date) {
  	
    if (typeof(date) == "undefined") {
      date = this.stringToDate(this.input.val()||"");
    };
    
    if (date) {
      this.selectedDate = date;
      this.selectMonth(date);
      var diff = this.daysBetween(today, date);
      $("td", this.tbody).each(function()
      {
      	var em = $(this);
      	if(em.attr("diff")==diff) em.addClass("selected");
      	else em.removeClass("selected");
      });
      
      if (this.input.val() != diff) {
      	this.input.val((this.validate?this.validate(diff):diff)||"").change();
      };
    } else {
      this.selectMonth(today);
    };
  },
  
  show: function() {
  	this.selectDate();
    this.rootLayers.css("display", "block");
    this.setPosition();
    this.input.unbind("focus", this.show);
    $([window, document.body]).click(this.hideIfClickOutside);
  },
  
  hide: function() {
  	var self = this;
    this.rootLayers.css("display", "none");
    $([window, document.body]).unbind("click", this.hideIfClickOutside);
    this.input.focus(this.show);
  },
  
  hideIfClickOutside: function(event) {
    if (event.target != this.input[0] && !this.insideSelector(event)) {
      this.hide();
    };
  },
  
  stringToDate: function(string) {
  	if(!(string||"0").search(/^\d+$/)>-1)
  	{
  		var d = new Date();
  		d.setDate(d.getDate() + Math.min(9999, parseInt(string||0)));
  		return new Date(d.getFullYear(), d.getMonth(), d.getDate());
  	}
  	return null;
  },
  
  dateToString: function(date) {
  	return parseInt(this.daysBetween(today, date));
  },
  
  setPosition: function() {
    var offset = this.input.offset();
    this.rootLayers.css({
    //  top: offset.top + this.input.outerHeight(),
      top: offset.top - (this.rootLayers.outerHeight()),
      left: offset.left
    });
    
    if (this.ieframe) {
      this.ieframe.css({
        width: this.dateSelector.outerWidth(),
        height: this.dateSelector.outerHeight()
      });
    };
  },
  
  moveMonthBy: function(amount) {
    this.selectMonth(new Date(this.currentMonth.setMonth(this.currentMonth.getMonth() + amount)));
  },
  
  prevMonth: function() {
    this.moveMonthBy(-1);
    return false;
  },
  
  nextMonth: function() {
    this.moveMonthBy(1);
    return false;
  },
  
  monthName: function(date) {
    return this.month_names[date.getMonth()];
  },
  
  insideSelector: function(event) {
    var offset = this.dateSelector.offset();
    offset.right = offset.left + this.dateSelector.outerWidth();
    offset.bottom = offset.top + this.dateSelector.outerHeight();
    
    return event.pageY < offset.bottom &&
           event.pageY > offset.top &&
           event.pageX < offset.right &&
           event.pageX > offset.left;
  },
  
  bindToObj: function(fn) {
    var self = this;
    return function() { return fn.apply(self, arguments) };
  },
  
  bindMethodsToObj: function() {
    for (var i = 0; i < arguments.length; i++) {
      this[arguments[i]] = this.bindToObj(this[arguments[i]]);
    };
  },
  
  indexFor: function(array, value) {
    for (var i = 0; i < array.length; i++) {
      if (value == array[i]) return i;
    };
  },
  
  monthNum: function(month_name) {
    return this.indexFor(this.month_names, month_name);
  },
  
  shortMonthNum: function(month_name) {
    return this.indexFor(this.short_month_names, month_name);
  },
  
  shortDayNum: function(day_name) {
    return this.indexFor(this.short_day_names, day_name);
  },
  
  daysBetween: function(start, end) {
    start = Date.UTC(start.getFullYear(), start.getMonth(), start.getDate());
    end = Date.UTC(end.getFullYear(), end.getMonth(), end.getDate());
    return (end - start) / 86400000;
  },
  
  changeDayTo: function(to, date, direction) {
    var difference = direction * (Math.abs(date.getDay() - to - (direction * 7)) % 7);
    return new Date(date.getFullYear(), date.getMonth(), date.getDate() + difference);
  },
  
  rangeStart: function(date) {
    return this.changeDayTo(this.start_of_week, new Date(date.getFullYear(), date.getMonth()), -1);
  },
  
  rangeEnd: function(date) {
    return this.changeDayTo((this.start_of_week - 1) % 7, new Date(date.getFullYear(), date.getMonth() + 1, 0), 1);
  },
  
  isFirstDayOfWeek: function(day) {
    return day == this.start_of_week;
  },
  
  isLastDayOfWeek: function(day) {
    return day == (this.start_of_week - 1) % 7;
  },
  
  adjustDays: function(days) {
    var newDays = [];
    for (var i = 0; i < days.length; i++) {
      newDays[i] = days[(i + this.start_of_week) % 7];
    };
    return newDays;
  }
};

$.fn.date_input = function(opts) {
  return this.each(function() { new DateInput(this, opts); });
};
$.date_input = { initialize: function(opts) {
  $("input.date_input").date_input(opts);
} };

return DateInput;
})(jQuery); // End localisation of the $ function
