
function focus_defaulted() {
  if (this.defaulted == undefined) {
    this.defaulted = this.value;
  }
  if (this.value == this.defaulted) {
    this.value ='';
  }
}

function blur_defaulted() {
  if (this.value == '') {
    this.value = this.defaulted;
  }
}

function PostSharer() {
  this.box = null;
  this.link = null;
  this.content = null;

  this.popup = function popup(link) {
    if (this.link == link && this.box.visible()) {
      this.hide();
    } else {
      this.link = link;
      this.show();
    }
  }
  
  this.hide = function hide() {
    this.link = null;
    this.box.hide();
  }
  this.show = function show() {
    if (this.box == null) {
      this.box = new Element('div', { 'id': 'post_sharer'});
      this.box.hide();
      $$('body').first().insert(this.box);

      close_link = new Element('a',
        { 'class': 'sharer_closer', 'href': '#' }
      ).update('close');
      close_link.onclick = function() { post_sharer.hide(); return false; }
      this.box.insert(close_link);
      this.content = new Element('div', {'id': 'post_sharer_content'});
      this.box.insert(this.content);
    }
    
    this.box.removeClassName('share_email');
    
    clone_offset = this.box.getHeight() * -1 - 8;
    if (Element.viewportOffset(this.link).top < this.box.getHeight() + 10) {
      clone_offset = this.link.getHeight() + 4;
    }

    // Can't use clonePosition due to IE shitballs
    this.box.setStyle( { top:this.link.cumulativeOffset().top + clone_offset + 'px',
      left:this.link.cumulativeOffset().left + "px"});

    this.content.innerHTML = '';
    new Ajax.Updater(this.content, this.link.href, { method: 'get' });
    this.box.show();   
  }
}

var post_sharer = new PostSharer();

function share_post(e) {
  Event.stop(e);  
  post_sharer.popup(Event.element(e));
}

function share_email(link) {
  new Ajax.Updater('post_sharer', link.href, { method: 'get' } );
  $('post_sharer').addClassName('share_email');
  return false;
}

function ftg_view_thumb(e) {
  Event.stop(e);
  $$('div.ftg_viewer>img')[0].src = Event.element(e).up().href;
  $('ftg_thumbs').scrollTo();
}

document.observe('dom:loaded', function() {
  $$('a.ftg_thumb').each(function(e) {
    e.observe('click', ftg_view_thumb);
  });
  $$('.defaulted').each(function(e) {
    e.observe('focus', focus_defaulted);
    e.observe('blur', blur_defaulted);
  });
  $$('a.share_post').each(function(e) {
    e.observe('click', share_post);
  });
});

function validate_guest_post_form() {
  if ($F('guest_username') ==	'') { 
    alert('Please give your name or nickname to comment');
    new Effect.Highlight('guest_username');
    return false;
  };
  if ($F('forum_post_body') == '') {
     alert("You've not written a comment!");
     new Effect.Highlight('forum_post_body');
     return false;
     }
  return true;
}
