PopUp = Behavior.create({
  initialize : function() {
    var dimensions = this.element.className.split(' ').first().split('x');
    // this.window_settings = 'resizable=yes,status=0,toolbar=0,width=' + (dimensions[0] - 0 + 30) + ',height=' + (dimensions[1] - 0 + 125);
    this.window_settings = 'resizable=yes,status=0,toolbar=0,width=900,height=650';
  },
  onclick: function(e) {
    window.open(this.element.href, 'picture', this.window_settings);
  }
});

function mdy(date) {
  var month = (date.getMonth() + 1).toString();
  if (month.length == 1)
    month = '0' + month;
  var day = date.getDate().toString();
  if (day.length == 1)
    day = '0' + day;
  return month + '/' + day + '/' + date.getFullYear();
}

today = new Date();
year_ago = new Date();
month_ago = new Date();
week_ago = new Date();
year_ago.setDate(year_ago.getDate() - 365);
month_ago.setDate(month_ago.getDate() - 30);
week_ago.setDate(week_ago.getDate() - 7);

date_ranges = {
	all : [mdy(new Date(1995,0,1)), mdy(today)],
  year : [mdy(year_ago), mdy(today)],
  month : [mdy(month_ago), mdy(today)],
  week : [mdy(week_ago), mdy(today)]
}

Event.addBehavior({
  '.picturePopUp' : PopUp,
  '.opener' : PopOpen,
  '.date_field' : DateSelector({ minYear: 1995, position: 'bottom' }),
  '#advanced_search input[type=radio]' : RadioActivator('custom', 'date_boxes'),
  '#date_boxes input' : RadioButtonPusher('date_range_custom', 'date_boxes'),
  '#date_boxes .date_field_icon' : RadioButtonPusher('date_range_custom', 'date_boxes'),
  '.toggle' : Toggle,
  '#comment_first_name' : HintedField('First '),
  '#comment_last_name' : HintedField('Last '),
  '#advanced_search input[type=radio]' : DateFiller(date_ranges, ['#start_date', '#end_date'])
});

document.observe("dom:loaded", function() {
  if ($('zeitgeist_type')) new Control.Tabs('zeitgeist_type');
  $$('.noscript').invoke('hide');
  $$('.script').invoke('show');

  //Set the search params to earliest possible date and today if "all" is selected under advanced search
	Event.observe(window, "load", function(){
		$('start_date').value = mdy(new Date(1995,0,1));
		$('end_date').value = mdy(new Date()); //a new date with no params automatically gets set to the current date
		$('advanced_search').hide();
	});

	// Remove the name and email address fields when a user wishes to post
	// a comment anonymously.
  Event.observe('comment_anonymous', 'click', function(){
		$('name').toggle();
	});
});