/**
 * Fusionbox 1.0 - jQuery Plug-in and misc prototyped functions
 *
 */

//|
//|  default fbmvc events, like fixing the max-width, convert external links, and auto-select inputs
//|
var fbmvc = {};
fbmvc.prepares = [];
fbmvc.preparePage = function()
{
  //|
  //|  convert external links - this is a javascript hack to allow xhtml validation
  //|
  $('a[rel=external]').each(function()
  {
    $(this).attr('target', '_blank');
  });
  
  $('a.confirmation').click(function()
  {
    var prompt = $(this).attr('rel');
    if ( ! prompt )  prompt = 'Are you sure?';
    var confirmation = confirm(prompt);
    if ( ! confirmation )  return false;
  }).removeClass('confirmation');
  
  fbmvc.current_focus = null;
  fbmvc.return_false = true;
  $("input[type=text], input[type=password], textarea").focus(function(e)
  {
    fbmvc.current_focus = this;
    this.select();
  });
  
  if ($.browser.safari)
  {
    //|
    //|  fixes the width of textareas (in Safari)
    //|
    $('textarea').each(function()
    {
      if ( ! $(this).css('max-width') || $(this).css('max-width')=='none')
      {
        $(this).css('max-width', $(this).css('width'));
      }
    });
    
    //|
    //|  fixes autoselecting in Safari, related to the mouseup event
    //|
    $('input[type=text], input[type=password], textarea').each(function()
    {
      $(this).blur(function()
      {
        if (fbmvc.current_focus===this)  fbmvc.return_false = true;
        else  fbmvc.return_false = false;
      });
      
      this.onmouseup = function()
      {
        if (fbmvc.return_false)
        {
          fbmvc.return_false = false;
          return false;
        }
      };
      this.onkeyup = function(e)
      {
        if (fbmvc.return_false && e.which==9)
        {
          fbmvc.return_false = false;
          this.select();
        }
      };
    });
  }
  
  if (fbmvc.prepares.length)
  {
    $(fbmvc.prepares).each(function(i, preparePage)
    {
      preparePage();
    })
  }
}

$(document).ready(fbmvc.preparePage);


jQuery.fn.changeCheckbox = function(handler)
{
  if (typeof handler === 'undefined')
  {
    return $(this).change();
  }
  
  $(this).keyup(function()
  {
    if (e.which==32)
    {
      $(this).attr('checked', ! $(this).attr('checked')).click();
      return false;
    }
  });
  
  if (jQuery.browser.msie)
  {
    $(this).click(handler);
    $(this).change(handler);
  }
  else
  {
    $(this).change(handler);
  }
};


jQuery.fn.swapClass = function(old_class, new_class)
{
  return $(this).removeClass(old_class).addClass(new_class);
};


$.fn.errorMessage = function(suffix)
{
  var id = '#' + $(this).attr('id') + (suffix ? '_' + suffix : '') + '_error_message';
  return $(id);
};


jQuery.scrollToTop = function()
{
  if ( jQuery.scrollTo )
  {
    jQuery.scrollTo({top: 0}, 0);
  }
  else
  {
    window.scroll(0,0);
  }
};


//|
//|  This method can be assigned in a focus() event, and it will move the cursor to the end of the <textarea> or <input type=text>
//|
jQuery.fn.moveCursorToEnd = function()
{
  var length = 0;
  if (this.val())
  {
    length = this.val().length;
  }
  
  if(this.get(0).setSelectionRange)
  {     /* DOM */
    var o = this.get(0);
    var callback = function()  {  o.setSelectionRange(length, length);  };
    setTimeout(callback, 2);
  }
  else if (this.get(0).createTextRange)
  {    /* IE */
    var r = this.get(0).createTextRange();
    r.moveStart('character', length);
    r.select();
  }
};


//used by jquery popup calendar plugin
Date.firstDayOfWeek = 7;

if ( typeof console === 'undefined' )
{
  console = {};
  console.log = function(log)
  {
    $('#fbmvc_javascript_development_log').html($('#fbmvc_javascript_development_log').html() + log + "<br />");
  };
}
