Object.extend(Date, {
  months: $w("January February March April May June July August September October November December"),
  weekdays: $w("Sunday Monday Tuesday Wednesday Thursday Friday Saturday"),
  daysInMonth: function(date) {
    try {
      date.getFullYear();
    } catch(e) {
      var today = new Date();
      date = new Date(date.year || today.getFullYear(), date.month || date.month === 0 ? date.month : today.getMonth(), 1);
    }
    
    var month = date.getMonth();
    if (month === 1) {
      return date.isLeapYear() ? 29 : 28;
    } else if (month < 7) {
      return (month + 1) % 2 === 0 ? 30 : 31;
    } else {
      return (month + 1) % 2 === 0 ? 31 : 30;
    }
  },
  isLeapYear: function(date) {
    if ((typeof date == 'string') || (typeof date == 'number')) { date = new Date(date, 1, 1); }
    return date.getFullYear() % 4 === 0 && (date.getFullYear() % 100 !== 0 || date.getFullYear() % 400 === 0);
  }
});

Object.extend(Date.prototype, {
  toJSON: function() {
    return '"' + this.getUTCFullYear() + '-' +
      (this.getUTCMonth() + 1).toPaddedString(2) + '-' +
      this.getUTCDate().toPaddedString(2) + 'T' +
      this.getUTCHours().toPaddedString(2) + ':' +
      this.getUTCMinutes().toPaddedString(2) + ':' +
      this.getUTCSeconds().toPaddedString(2) + 'Z"';
  },
  dayOfChangedMonth: function(options) {
    return this.getDate() > Date.daysInMonth({month: options.month, year: options.year || this.getFullYear()}) ? Date.daysInMonth({month: options.month, year: options.year || this.getFullYear()}) : this.getDate();
  },
  advanceMonth: function(months) {
    var agnosticDay = new Date(this.getFullYear(), this.getMonth() + months, 1);
    var currentDayInNewMonth = this.dayOfChangedMonth({year: agnosticDay.getFullYear(), month: agnosticDay.getMonth()});
    var date = this.clone();
    date.setDate(currentDayInNewMonth);
    date.setMonth(this.getMonth() + months);
    return date;
  },
  change: function(options) {
    var year          = options && options.year ? options.year : this.getFullYear();
    var month         = options && (options.month || options.month === 0) ? options.month : this.getMonth();
    var date          = options && options.date ? options.date : this.dayOfChangedMonth({year: year, month: month});
    var hour          = options && (options.hours || options.hours === 0) ? options.hours : this.getHours();
    var minutes       = options && (options.minutes || options.minutes === 0) ? options.minutes : (options.hours ? 0 : this.getMinutes());
    var seconds       = options && (options.seconds || options.seconds === 0) ? options.seconds : (options.hours || options.minutes ? 0 : this.getSeconds());
    var milliseconds  = options && (options.milliseconds || options.milliseconds === 0) ? options.milliseconds : (options.hours || options.minutes || options.seconds ? 0 : this.getMilliseconds());
    return new Date(year, month, date, hour, minutes, seconds, milliseconds);
  },
  clone: function() {
    return this.change({date: this.getDate()});
  },
  advance: function(options) {
    var date = this.clone();
    date.setFullYear(this.getFullYear() + (options.years || 0));
    var lambdaMonth = options.months || 0;
    date.setDate(1);
    date = date.advanceMonth(lambdaMonth);
    date.setDate(this.dayOfChangedMonth({year: date.getFullYear(), month: date.getMonth()}) + (options.days || 0));
    return this.change({year: date.getFullYear(), month: date.getMonth(), date: date.getDate()});    
  },
  daysInMonth: function(options){ return (options ? Date.daysInMonth(options) : Date.daysInMonth(this)); },
  stripTime:        function()      { return new Date(this.getFullYear(), this.getMonth(), this.getDate()); },
  yearsAgo:         function(count) { return this.change({year: this.getFullYear() - count}); },
  monthsAgo:        function(count) { return this.advanceMonth(-count); },
  daysAgo:          function(count) { var newDate = this.clone(); newDate.setDate(this.getDate() - count); return newDate; },
  yearsSince:       function(count) { return this.yearsAgo(-count); },
  monthsSince:      function(count) { return this.monthsAgo(-count); },
  daysSince:        function(count) { return this.daysAgo(-count); },
  lastYear:         function()      { return this.yearsAgo(1); },
  nextYear:         function()      { return this.yearsAgo(-1); },
  lastMonth:        function()      { return this.monthsAgo(1); },
  nextMonth:        function()      { return this.monthsAgo(-1); },
  midnight:         function()      { return this.stripTime(); },
  beginningOfDay:   function()      { return this.stripTime(); },
  beginningOfMonth: function()      { return this.change({date: 1}).midnight(); },
  endOfMonth:       function()      { return this.change({date: this.daysInMonth()}).midnight(); },
  beginningOfYear:  function()      { return this.change({date: 1, month: 0}).midnight(); },
  beginningOfWeek:  function() {
    var daysToMonday = this.getDay() !== 0 ? this.getDay() - 1 : 6;
    return this.advance({days: -daysToMonday}).midnight();
  },
  isToday: function() {
    var today = new Date();
    return today.getFullYear() == this.getFullYear() && today.getMonth() == this.getMonth() && today.getDate() == this.getDate();
  },
  isYesterday: function() {
    var yesterday = new Date().daysAgo(1);
    return yesterday.getFullYear() == this.getFullYear() && yesterday.getMonth() == this.getMonth() && yesterday.getDate() == this.getDate();    
  },
  //used right now in order to be used in sites not running newest version of prototype
  isLeapYear: function() {
    return Date.isLeapYear(this);
  }
});

if (Function.prototype.methodize) {
  $w("isLeapYear").each(function(method) {
    Date.prototype[method] = Date[method].methodize();
  });
}

var rules = {
  // Check and uncheck all marks for a category
	'input.mark-category-check' : function(el){
		el.onclick = function(){
		  // Find the containing <dt>
		  dt = el.parentNode;
		  while (dt.tagName != 'DT') {
		    dt = dt.parentNode;
		  }
		  
		  // Find the next <dd> after the <dt>
			dd = dt.nextSibling;
			while (dd.tagName != 'DD') {
			  dd = dd.nextSibling;
			}
			
			// Find all inputs in the <dd>
			checkboxes = dd.getElementsByTagName('INPUT');
			
			// Toggle them to the value of the overall category checbox
			for (i = 0; i < checkboxes.length; i++) {
			  checkboxes[i].checked = el.checked;
			}
		}
	},
	
    '.marks-check-all' : function(el){
    	el.onclick = function(){
    	  // Find the containing <dt>
    	  dl = document.getElementById('artwork_list');

    		// Find all inputs in the <dd>
    		checkboxes = dl.getElementsByTagName('INPUT');
    		// Toggle them to the value of the overall category checbox
    		for (i = 0; i < checkboxes.length; i++) {
    		  checkboxes[i].checked = true;
    		}
    		return false;
    	}

  	},
  	
  	
  	 '.marks-check-none' : function(el){
      	el.onclick = function(){
      	  // Find the containing <dt>
      	  dl = document.getElementById('artwork_list');

      		// Find all inputs in the <dd>
      		checkboxes = dl.getElementsByTagName('INPUT');
      		// Toggle them to the value of the overall category checbox
      		for (i = 0; i < checkboxes.length; i++) {
      		  checkboxes[i].checked = false;
      		}
          return false;
      	}
    	}
	
	
};





Behaviour.register(rules);