function displayNotice(message)
{
  jQuery("div#notification .notice").html(message);

  jQuery("div#notification .notice").slideDown();
  setTimeout(function(){
   jQuery("div#notification .notice").slideUp("slow", function () {
     jQuery("div#notification .notice").hide();
   });
  }, 5000);
}

function displayError(message)
{
  jQuery("div#notification .error").html(message);

  jQuery("div#notification .error").slideDown();
  setTimeout(function(){
   jQuery("div#notification .error").slideUp("slow", function () {
     jQuery("div#notification .error").hide();
   });
  }, 5000);
}

/**
 * Replace submit buttons by links
 */
function replaceButtons()
{
  // Hide submits buttons where there is a replacement link
  jQuery('a.submit, a.ajax-submit').parent().children('input[type=submit]').hide();

  // Actions for the submit replacement link
  jQuery('a.submit').click(function(e)
  {
    e.preventDefault();

    jQuery(this).parents('form').submit();
    jQuery(this).parent().children('input[type=submit]').hide();

  });
}

jQuery(document).ready(function() 
{
  Cufon.replace('.cufon-aurebesh');

  // Color form inputs in error
  jQuery('form ul.error_list').each(function(e) 
  {
    var li = jQuery(this).parents('li');
    
    li.addClass('error');
  });

  // Slide of notification bar
  if(jQuery("div#notification .notice").html().length > 0)
  {
    displayNotice(jQuery("div#notification .notice").html());
  }
  if(jQuery("div#notification .error").html().length > 0)
  {
    displayError(jQuery("div#notification .error").html());
  }

  
  replaceButtons();

  // In-column div scroller
  if(jQuery("#scroller-container").html())
  {
    jQuery(window).scroll(function()
    {
      if (jQuery(window).scrollTop() > 200)
      {
        jQuery("#scroller-container").css("position", "fixed");
        if((jQuery("#scroller-container").position().top -180) + jQuery("#scroller-container").height() > jQuery("#page").height())
        {
          jQuery("#scroller-container").css("position", "absolute");
          jQuery("#scroller-container").css("bottom", "0px");
          jQuery("#scroller-container").css("top", "");
        }
        else
        {
          jQuery("#scroller-container").css("top", "10px");
          jQuery("#scroller-container").css("bottom", "");
        }
      }
      if(jQuery(window).scrollTop() <= 200)
      {
        jQuery("#scroller-container").css("position", "static");
        jQuery("#scroller-container").css("top", "200");
        jQuery("#scroller-container").css("bottom", "");
      }
    });
  }

  // Calendar scroll logic
  jQuery.localScroll();

  // Action links toggler
  jQuery("div.actions > a").live('click', function(e)
  {
    var divActions = jQuery(this).parent('div').children('ul');
    divActions.slideToggle();
    e.preventDefault();
  });

  

  var colorboxConfirmationStatus = false;
  
  jQuery('a.colorbox').live('click', function(e)
  {
    e.preventDefault();
    var link = jQuery(this);
    jQuery(this).colorbox({
      open: true,
      href:jQuery(this).attr('href'),
      onClosed: function() {
        if(link.hasClass('delete'))
        {
          /// we may have to hide a deleted element
          if(colorboxConfirmationStatus)
          {
            link.closest('.hide-on-delete').hide();
          }
          else
          {
            link.closest('.hide-on-delete').show();
          }
        }
      }
    });
  });

  // Form image widget display partial
  jQuery('form ul li.image-preview a.edit').click(function(e)
  {
    e.preventDefault();
    jQuery(this).next().show();
    jQuery(this).hide();
  });

  
  // Close link action on colorbox
  jQuery('#colorbox a.close').live('click', function(e)
  {
    e.preventDefault();
    jQuery.fn.colorbox.close();
  });

  // Handle delete popup action
  jQuery('#colorbox #deleteBox a.delete').live('click', function(e)
  {
    e.preventDefault();
    var link = jQuery(this);
    var url = jQuery(this).attr('href');

    jQuery.ajax
    ({
      url: url,
      success: function(e) {
        displayNotice('L\'élement a été supprimé.');
        colorboxConfirmationStatus = true;
        
      },
      error:  function(e) {
        displayNotice('Une erreur s\'est produite. L\'élement n\'a pas été supprimé.');
        colorboxConfirmationStatus = false;
      }
    });

    jQuery.fn.colorbox.close();
  });

  // Init jQuery bbcode
  // Test dom existance to avoid weird plugin bug
  if(jQuery('textarea.bbcode').length > 0)
  {
    jQuery('textarea.bbcode').bbcodeeditor(
    {
      bold:$('.bbcode-btn.bold'),italic:$('.bbcode-btn.italic'),underline:$('.bbcode-btn.underline'),link:$('.bbcode-btn.link'),quote:$('.bbcode-btn.quote'),code:$('.bbcode-btn.code'),image:$('.bbcode-btn.image'),
      usize:$('.bbcode-btn.usize'),dsize:$('.bbcode-btn.dsize'),nlist:$('.bbcode-btn.nlist'),blist:$('.bbcode-btn.blist'),litem:$('.bbcode-btn.item'),
      back:$('.bbcode-btn.back'),forward:$('.bbcode-btn.forward'),back_disable:'bbcode-btn back_disable',forward_disable:'bbcode-btn forward_disable',
      exit_warning:false,preview:jQuery('.bbcode-preview-container')
    });
  }

  jQuery('.bbcode-preview a.show').click(function(e)
  {
    e.preventDefault();
    jQuery(this).parents('.bbcode-preview').children('.bbcode-preview-container').slideDown();
    jQuery(this).parents('.bbcode-preview').children('a.hide').show();
    jQuery(this).hide();
  });

  jQuery('.bbcode-preview a.hide').click(function(e)
  {
    e.preventDefault();
    jQuery(this).parents('.bbcode-preview').children('.bbcode-preview-container').hide();
    jQuery(this).parents('.bbcode-preview').children('a.show').show();
    jQuery(this).hide();
  });

  // Common ajax tips
  jQuery("img.qtiped").live('mouseover', function()
  {
    var content = jQuery(this).attr('alt');
    var target = jQuery(this);
    if (target.data('qtip')) { return false; }

    target.qtip({
       content: content,
       position: {
          corner: {
             target: 'bottomMiddle',
             tooltip: 'topMiddle'
          }
       },
       show: {
         delay: 0,
         effect: { type: 'slide' }
       },
       style: {
          width: 200,
          padding: 0,
          background: '#222',
          color: '#CCC',
          textAlign: 'center',
          border: {
             width: 5,
             radius: 5,
             color: '#222'
          },
          tip: 'topMiddle',
          name: 'dark' // Inherit the rest of the attributes from the preset dark style
       }
    });

    target.trigger('mouseover');


    
    
  });

});
