/* ALL CODE COPYRIGHT SIMON SHEPHERD 2007 */

/*  begin /public_html/javascript/the6yardbox/the6yardbox.js
 *  common across all the6yardbox pages */

Prototip.Styles.the6yardbox = {
    className: 'the6yardbox',
    border: 3,
    borderColor: '#4100ad',
    radius: 3,
    stem: { height: 12, width: 15 },
    images: '../../../../../site_public/templates/the6yardbox/img/prototip'
  };

The6YardBox = {};

// call other functions on page load
document.observe("dom:loaded", function()
{
  // beta testing popup?
  if($('bug-report'))
  {
    $('bug-report').observe('click', function(e)
    {
      e.stop();
      
      document.fire('zehnMessage:created', $('report-popup').innerHTML);
      
      $('report-form').down('input[type=submit]').observe('click', function(e)
      {
        e.stop();
        var msg = $F($('report-form').down('textarea'));
        $('report-form').disable();
        new Ajax.Request('', {
          parameters: {
            'sessid' : sessid,
            'module' : 'The6YardBox',
            'func'   : 'send_feedback',
            'feedback'   : msg
          },
          onComplete: function(response)
          {
            $('report-form').enable();
            $('report-form').reset();
            Zehn.report.close_shown_message();
          }
        });
      });
    });
  }
  
  // add any help icon tooltips
  $$('img.help-img').each(function(helpImg)
  {
    new Tip(helpImg, helpImg.next('span.help-text').innerHTML, {
      fixed: true,
      stem: 'topLeft',
      hook: { target: 'bottomMiddle', tip: 'topLeft' },
      style: 'the6yardbox'
    });
  });
  
  // observe any support editors
  The6YardBox.add_support_tips();
  
  // add tooltips for the link teasers
  The6YardBox.add_link_teaser_tips();
  document.observe('moreContent:added', The6YardBox.add_link_teaser_tips);
});

// add tooltips for link teasers without one
The6YardBox.add_link_teaser_tips = function()
{
  $$('.link-teaser-header > a').each(function(teaserHeader)
  {
    if(typeof(teaserHeader.prototip) != 'undefined')
      return;
    var desc =
      teaserHeader.up('div.inner-teaser').down('div.link-description');
    if(desc.innerHTML == '')
      return;
    new Tip(teaserHeader, desc, {
      style: 'the6yardbox'
    });
  });
};

// add all unobserved tooltips
The6YardBox.add_support_tips = function()
{
  $$('a.edit-support').each(function(editLink)
  {
    if(!editLink.hasClassName('observed'))
      new The6YardBox.EditSupportTip(editLink);
  });
};

// class for a tooltip to edit whether you support another user
The6YardBox.EditSupportTip = Class.create({
  initialize: function(editLink)
  {
    this.link = $(editLink);
    this.link.observe('click', function(e)
    {
      $$('a.edit-support').each(function(item)
      {
        if(item.identify() != this.link.identify())
          item.prototip.hide();
      });
    }.bind(this));
    
    var content = $('edit-support-popup').clone(true);
    content.id = 'edit-support-popup-' + this.link.identify();
    new Tip(this.link, content.show(), {
      closeButton: true,
      fixed: true,
      stem: 'topMiddle',
      hook: { target: 'bottomMiddle', tip: 'topMiddle' },
      hideOn: false,
      showOn: 'click',
      style: 'the6yardbox'
    });
    content.down('a.supporting').observe('click',
      this.edit.bind(this));
    content.down('a.notsupporting').observe('click',
      this.edit.bind(this));
    //content.down('a.blocked').observe('click',
    //  this.edit.bind(this));
    
    this.link.observe('click', function(e) { e.stop(); });
    this.link.addClassName('observed');
  },
  
  // edit the supporting status
  edit: function(e)
  {
    e.stop();
    
    // is this user currently supported?
    if(this.link.down('span.supporting').visible())
      var supported = true;
    else
      var supported = false;
    
    var element = e.element();
    if(element.tagName != 'A' && element.tagName != 'a')
        element = element.up('a');
    
    var userId = this.link.id.substr(13);
    if(element.hasClassName('supporting'))
      var changeTo = 'supporting';
    else if(element.hasClassName('notsupporting'))
      var changeTo = 'notsupporting';
    else
      var changeTo = 'blocked';
    
    new Ajax.Request('', {
      parameters: {
        'sessid' : sessid,
        'module' : 'the6yardbox',
        'func'   : 'update_support_status',
        'user'   : userId,
        'status' : changeTo
      },
      onComplete: function(response)
      {
        if(response.status.toString()[0] != '2')
          return;
        
        if($('supported-count-' + userId))
        {
          if(changeTo == 'supporting')
          {
            if(!supported)
              $('supported-count-' +userId).update(
                parseInt($('supported-count-' + userId).innerHTML) + 1);
          }
          else
          {
            if(supported)
              $('supported-count-' +userId).update(
                parseInt($('supported-count-' + userId).innerHTML) - 1);
          }
        }
        
        this.link.select('span').each(function(item) { item.hide(); });
        this.link.down('span.' + changeTo).show();
        this.link.prototip.hide();
      }.bind(this)
    });
  }
});

// the tag content suggester
The6YardBox.NavSearchSuggester = Class.create({
  initialize: function(input, suggestions, func)
  {
    this.suggestions = $(suggestions);
    this.input = $(input);
    this.func = func;
    this.defaultVal = $F(this.input);
    this.add_observers();
  },
  
  // add required observers
  add_observers: function()
  {
    this.input.observe('focus', function()
    {
      if($F(this.input) == this.defaultVal)
        this.input.writeAttribute('value', '');
    }.bind(this));
    this.input.observe('blur', function()
    {
      if($F(this.input) == '')
        this.input.writeAttribute('value', this.defaultVal);
    }.bind(this));
    this.input.observe('keyup', this.process_key_press.bind(this));
  },
  
  // do something or start timer
  process_key_press: function(e)
  {
    this.start_timer();
  },
  
  // show the selected index
  show_selected_index: function()
  {
    this.suggestions.select('li').each(function(li)
    {
      li.removeClassName('selected');
    });
    this.suggestions.select('li')[this.index].addClassName('selected');
  },
  
  // start the timer to ajax query if no more text typed
  start_timer: function()
  {
    if($F(this.input) == '')
    {
      this.suggestions.hide();
      return;
    }
    if($F(this.input) == this.previousValue)
      return;
    this.previousValue = $F(this.input);
    var previous = $F(this.input);
    this.end_timer.bind(this).delay(0.4, previous);
  },
  
  // query if no more text added
  end_timer: function(previous)
  {
    if($F(this.input) == previous)
    {
      this.show_suggestions();
    }
  },
  
  // get some suggestions for autocompleting and show them
  show_suggestions: function()
  {
    this.previousValue = '';
    this.index = -1;
    
    new Ajax.Request('', {
      parameters: {
        sessid : sessid,
        module : 'The6YardBox',
        func   : this.func,
        filter : $F(this.input),
        TO_WEB : TO_WEB
      },
      onComplete: function(response)
      {
        if(response.status.toString()[0] != '2')
          return;
        
        this.suggestions.update(response.responseText);
        if(this.suggestions.select('li').size() > 0)
        {
          this.suggestions.show();
        }
      }.bind(this)
    });
  }
});

// title editor (auto expands, hides/shows click here text
The6YardBox.TitleEditor = Class.create({
  initialize: function(textarea)
  {
    this.textarea = $(textarea);
    this.autoexpand = new Zehn.AutoExpandTextarea(this.textarea, 0, 12, true);
    this.textarea.observe('focus', this.focused.bind(this));
    this.textarea.observe('blur', this.blurred.bind(this));
    this.blurred();
  },
  blurred: function()
  {
    this.re = new RegExp('[A-Za-z0-9]');
    if(!$F(this.textarea).match(this.re))
    {
      this.textarea.addClassName('title-empty');
      this.textarea.setValue('Click Here To Edit The Title');
    }
  },
  focused: function()
  {
    this.textarea.removeClassName('title-empty');
    if($F(this.textarea) == 'Click Here To Edit The Title')
    {
      this.textarea.setValue('');
    }
  }
});
