/**
 * CoffeeCup Software: Forums Javascript Library
 *
 * This file contains javascript library for the forums section of www.coffeecup.com
 *
 * @author     Jeff Welch <jw@coffeecup.com>
 * @category   Empire
 * @package    Public
 * @copyright  Copyright (c) 2006-2007 CoffeeCup Software, Inc. (http://www.coffeecup.com/)
 * @version    $Id$
 */

$(document).ready(function() {

    // Setup how bbcode links are converted to bbcode
    var allowed_tags = {
       allowed_tags_link:      {startTag: '[url]',        endTag: '[/url]'},
       allowed_tags_image:     {startTag: '[img]',        endTag: '[/img]'},
       allowed_tags_bold:      {startTag: '[b]',          endTag: '[/b]'},
       allowed_tags_italic:    {startTag: '[i]',          endTag: '[/i]'},
       allowed_tags_underline: {startTag: '[u]',          endTag: '[/u]'},
       allowed_tags_quote:     {startTag: '[quote]',      endTag: '[/quote]'},
       allowed_tags_code:      {startTag: '[code]',       endTag: '[/code]'},
       allowed_tags_list:      {startTag: "[list]\n",     endTag: "\n[/list]"},
       allowed_tags_listitem:  {startTag: '[*] ',         endTag: ''}
    };

    // Initialize the number of attachments
    var attachment_count = 0;

    // Setup the bbcode shortcuts
    $('#quicktags').show();
    for(key in allowed_tags)
    {
       $('#' + key).click(function(){
          forum_replyfield_replace(allowed_tags[$(this).attr('id')].startTag +
             $('#text').getSelection().text + allowed_tags[$(this).attr('id')].endTag);
       });
    }

    // Setup the smilies shortcuts
    $('#smiley_legend img').click(function(){
       forum_replyfield_replace($(this).attr('alt'));
    });

    // Setup smilies hiding
    $('#smiley_legend ul').css('height','24px');
    if($('#hide_smilies input').attr('checked')){$('#smiley_legend li').hide()}
    $('#hide_smilies input').change(function(){
       $(this).attr('checked') ? $('#smiley_legend li').fadeOut('slow') : $('#smiley_legend li').fadeIn('slow');
    })

    // Setup the attachments adder
    $('#attachment_1, #attachment_2, #attachment_3').remove();
    $('#add_attachment').click(function(){
       if($('#attachmentcontainer').find('input[type=file]').length < 3)
       {
          $('#attachmentcontainer').append('<div class="file_attachment" id="attachment_' + ++attachment_count + '"><input type="file" name="attachment_' + attachment_count + '" /> <img id="cancel_attachment_' + attachment_count + '" alt="Cancel" title="Cancel" src="/images/icons/delete.png" /></div>');
          $('#attachment_' + attachment_count).hide().fadeIn('slow');
          $('#cancel_attachment_' + attachment_count).click(function(){
             $(this).parent().fadeOut('slow', function(){$(this).remove(); checkAttachmentAdder();})
          });
          checkAttachmentAdder();
       }
    }).css('cursor','pointer');

    function checkAttachmentAdder()
    {
       if($('#attachmentcontainer').find('input[type=file]').length == 3)
       {
          $('#add_attachment').hide();
       }
       else if($('#attachmentcontainer').find('input[type=file]').length > 0)
       {
          $('#add_attachment').show();
          $('#add_attachment span:first').text('Attach Another File');
       }
       else
       {
          $('#add_attachment').show();
          $('#add_attachment span:first').text('Attach a File');
       }
       
    }

    // Setup the global permission checkers
    $("input[id^='global_']").change(function(){
       var permission = $(this).attr('id').substring(7);
       var global_checked = this.checked;
       $("." + permission).each(function(){
          this.checked = global_checked;
       });
    });

    // Setup the individual permission checkers
    $("input.can_read, input.can_write, input.can_create").change(function(){

       // The name of the class we are dealing with
       var input_name = $(this).attr('class');

       // If the checkbox is being unchecked, uncheck the global check
       if(!this.checked)
       {
          $("input#global_" + input_name).get(0).checked = false;
       }
       // Otherwise run over all the similar elements.  If they are all checked,
       // check the global one.
       else
       {
          var checked = true;
          $('input.' + input_name).each(function(){
             if(!this.checked)
             {
                checked = false;
                return;
             }
          });
          if(checked == true)
          {
             $("input#global_" + input_name).get(0).checked = true;
          }
       }
    });

    // Setup the uploading message
    if($('#post_message, #addreply').length > 0)
    {
       // Have to preload it or firefox won't display it unless it's cached.
       var preload = new Image();
       preload.src = '/images/blue-loader.gif';

       $('#post_message, #addreply').click(function(){
          var uploading_file = false;
          $('div.file_attachment input').each(function(){
             if($(this).attr('value'))
             {
                uploading_file = true;
             }
          });
          if(uploading_file)
          {
   	       $(this).hide(250);
             $(this).after("<p id=\"patience_message\"><em><strong>Please be patient while your attachments are uploaded.  This could take a few minutes depending on your connection....</strong></em>");
             $("p#patience_message").hide().fadeIn(500);
          }
       });
    }

});

// Replaces an element in a textarea
function forum_replyfield_replace(text, context)
{
   var selection = $('#text',context).getSelection();
   var top = $('#text',context).get(0).scrollTop;
   var left = $('#text',context).get(0).scrollLeft;
   var position = selection.end + text.length - selection.length;

   $('#text',context).replaceSelection(text, true)
   $('#text',context).setCaretTo(position);

   $('#text',context).get(0).scrollTop = top;
   $('#text',context).get(0).scrollLeft = left;
}