// TODO : ability to pass in href instead of relying on clicked anchor element...
function ajax_popup(event) {
  var el = Event.element(event)
  var xy = Position.cumulativeOffset(el);
  var clicked_link = parent_link(el);
  
  // this version returns click coordinates.  appears to measure relative to visible area of browser window
  //new Ajax.Request(clicked_link.href, {asynchronous:true, evalScripts:true, parameters:'xy='+event.clientX+','+event.clientY})
  
  // this version uses measured offset of clicked (image) element
  new Ajax.Request(clicked_link.href, {asynchronous:true, evalScripts:true, parameters:'xy='+xy[0]+','+xy[1]})
  Event.stop(event);
}
function parent_link(element) {
  if (element.tagName == "A")
    return element;
  else
    return parent_link(element.parentNode);
}
function arrow_toggle(link,toggled_element) {
  Element.toggle(toggled_element);
  if ($(toggled_element).style.display == 'none') {
    link.className = 'arrow_toggle_closed';
  } else {
    link.className = 'arrow_toggle_open'
  }
}
function lock() {
  new Ajax.Request('/account/lock_session');
}
function tick() {
  $('countdown').value = parseInt($F('countdown')) - 1;
  if ($F('countdown') <= 0) {
    lock();
  } else {
    setTimeout('tick()',1000);
  }
}
function reset_timer() {
  $('countdown').value = 300;
}
// CALENDAR
function get_calendar() {
  return $('body').getElementsByClassName('caccalendar').first();
}
function calendar_select_day(day_id) {
  $('selected_day').value = day_id;
  get_calendar().getElementsByClassName('selected').map(function(el){el.removeClassName('selected')});
  $(day_id).addClassName('selected');
}
function calendar_switch_view(to_type) {
  var selected_date_elements = $F('selected_day').match(/^day_(\d{4})(\d{2})(\d{2})$/);
  var year = RegExp.$1;
  var month = RegExp.$2;
  var day = RegExp.$3;
  var destURL = "";
                 
  switch(to_type) {
    case 'day':
      destURL = "/calendar/" + year + "/" + month + "/" + day;
      break;
    case 'month':
      destURL = "/calendar/" + year + "/" + month;
      break;
    case 'year':
      destURL = "/calendar/" + year
      break;
  }
  window.location.href = destURL;
}
// LIVE SEARCH
function search_state(state) {
  [ 'search_icon', 'search_spinner', 'search_reset' ].map(function(el){ Element.hide(el); });
  switch(state) {
    case 'normal':
      Element.show('search_icon');
      break;
    case 'searching':
      Element.show('search_spinner');
      break;
    case 'results':
      Element.show('search_reset');
      break;
  }
}
function search_results(html) {
  if ($('search_buffer').innerHTML == '') {
    $('search_buffer').innerHTML = $(search_target).innerHTML;
  }
  $(search_target).innerHTML = html;
}
function reset_search_results() {
  $(search_target).innerHTML = $('search_buffer').innerHTML;
  $('search_buffer').innerHTML = '';
  search_state('normal');
  search_watcher.setValue('');
}
// misc
Form.Element.DelayedObserver.prototype.setValue = function(v) {
  this.element.value = v;
  this.lastValue = v;
};
// INSERT PHOTO FROM GALLERY
var photos = {};
function register_photo(photo_id,size,data) {
  if (photos[photo_id] == undefined) {
    photos[photo_id] = {};
  }
  photos[photo_id][size] = data;
};
function set_src(size) {
  $('image_src').value = photos[$('photo_id').value][size]['path'];
  $('image_width').value = photos[$('photo_id').value][size]['width'];
  $('image_height').value = photos[$('photo_id').value][size]['height'];
};
function select_gallery_photo(photo_id) {
  $('photo_id').value = photo_id;
  set_src('thumb');
  new Effect.BlindUp('photo_list');
  new Effect.BlindDown('photo_preview');
  $('preview_image').src = photos[photo_id]['thumb']['path'];
}
// footer position reset hack for IE6
function resetFooterPosition() {
  $('footer').style.position = 'relative';
  $('footer').style.position = 'absolute';
  $('footer').style.bottom = '-22px';
}
function updateStreamingStatus() {
  new Ajax.Request('/services/next', {
    asynchronous:true,
    evalScripts:true,
    onLoading: function() { if ($('stream_spinner')) $('stream_spinner').show() },
    onComplete: function() { if ($('stream_spinner')) $('stream_spinner').hide() }
  });
}
