/*
 * jQuery formEffects
 * 
 * Copyright (c) 2008 Łukasz Świerżewski
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * @version 1.0.0
 * @author Łukasz Świerżewski
 * @mailto lukasz at zirpe dot com
 */

jQuery.fn.extend({
 formEffects: function(am_options) {
  return $('input:text, input:password, select, textarea', this).each(function() {
   new jQuery.FormEffects($(this), am_options);
  });
 }
});

jQuery.FormEffects = function($o_destenation, am_options) {
 var options = am_options || {};
 var s_squareName = $o_destenation.attr('name');
 var b_delayBlur = true;

 var b_selectbox = (s_squareName.match(/div\-selectbox\-/)) ? true : false;
 if (b_selectbox) {
  var s_selectboxName = s_squareName;
  var s_defaultValue = '';
  s_squareName = s_squareName.substr('div-selectbox-'.length);
 }

 if (options.infoBarOnFocus == true) {
  var s_title = (b_selectbox) ? $('select[@name='+s_squareName+']', $o_destenation.parent()).attr('title') : $o_destenation.attr('title');
  //var s_title = $o_destenation.attr('title');
  var $o_infoBar = $('div.infoBar div.content', $o_destenation.parents('div.content'));
  if ($o_infoBar.length == 0) options.infoBarOnFocus = false;
  else {
   var s_defaultTitle = $o_infoBar.html();
  }
 }

 var as_matchDate = s_squareName.match(/((create|modify)_date)_/);
 if (as_matchDate) s_squareName = s_squareName.substr(0, s_squareName.indexOf(as_matchDate[1]) + as_matchDate[1].length);

 $o_destenation
  .hover(function() { $(this).addClass('hover'); }, function() { $(this).removeClass('hover'); })
  .bind('focus', function(e) {
    $(this).addClass('focus');
    $('#square_'+s_squareName).addClass('active');
    if (options.infoBarOnFocus == true) $o_infoBar.html(s_title);
    if (b_selectbox) s_defaultValue = $o_destenation.val()
  }).bind('blur', function(e) {
   if (b_selectbox) {
    if (b_delayBlur) {
     b_delayBlur = false;
     setTimeout(function() { $o_destenation.trigger('blur'); }, 200);
     return;
    } else b_delayBlur = true;
   }

   var b_removeActive = true;
   if (!$(this).val()) {
    if (as_matchDate) {
     if ($('input[@name='+s_squareName+'_year]', $(this).parent()).val() || $('input[@name='+s_squareName+'_month]', $(this).parent()).val() || $('input[@name='+s_squareName+'_day]', $(this).parent()).val()) b_removeActive = false;
    }
   } else if (b_selectbox) {
    if ($('select[@name='+s_squareName+']', $o_destenation.parent()).fieldValue() != '') b_removeActive = false;
   }

   if (b_removeActive) $('#square_' + s_squareName).removeClass('active');
   if (options.infoBarOnFocus == true) $o_infoBar.html(s_defaultTitle);
   $(this).removeClass('focus');

   if (options.autoRefreshOnChange == true && b_selectbox) {
    if (s_defaultValue != $o_destenation.val()) ($o_destenation.parents('form')).submit();
   }
  });

  if (!$o_destenation.hasClass('selectbox')) {
   $o_destenation.bind('reset', function() {
    switch ($o_destenation.attr('type')) {
     case 'text': $(this).val(''); break;
     case 'checkbox': if (this.checked) this.checked = false; break;
    }
    $('#square_' + s_squareName).removeClass('active');
   });
  }
  /* .bind('keypress', function(e) { if (e.keyCode == 13) ($(this).parents('form')).submit(); }); */

 if (options.autoRefreshOnChange == true) {
  if (($o_destenation.attr('tagName')).toLowerCase() == 'select') $o_destenation.bind('change', function(e) { ($o_destenation.parents('form')).submit(); });
  if (($o_destenation.attr('tagName')).toLowerCase() == 'input') {
   if (!b_selectbox) $o_destenation.bind('keyup', function(e) { ($o_destenation.parents('form')).submit(); });
  }
 }

};
