Drupal.behaviors.ch_gift_catalog = function(context) {
  $('#ch-gift-catalog-filters input:radio').click(function() {
    $('#ch-gift-catalog-filters').submit();
  });
  
  $('#ch-ecard-filters input:radio').click(function() {
    $('#ch-ecard-filters').submit();
  });
  
  $('.ecard-sample a.preview').click( function() {
    var data = {
      sender_name: $('.attributes .attribute-1 input').value(),
      sender_email: $('.attributes .attribute-2 input').value(),
      recipient_name: $('.attributes .attribute-3 input').value(),
      recipient_email: $('.attributes .attribute-4 input').value(),
      message: $('.attributes .attribute-5 input').value()
    };
    return false;
  });
  
  $('.ecard-item a[rel="lightmodal"]').click( function() {
    Drupal.behaviors.date_popup('#modalContainer');
  });
  
  $('.ecard-item a[rel="lightmodal"]').each( function() {
    //var row = $(this).parents('tr.ecard-item');
    //$(this).attr('href', Drupal.settings.ch_ecard.path[0]+' #'+row.attr('id')+' .add-to-cart');
    $(this).attr('href', $(this).attr('href')+'/ajax');
    $(this).attr('rel', 'lightmodal[|width:650px;]');
  });
  
  $('.attribute-5 .textarea-processed').keydown(function() {
    limitChars($(this), 215, $('.attribute-5 .description p'));
  })
  $('.attribute-5 .textarea-processed').keyup(function() {
    limitChars($(this), 215, $('.attribute-5 .description p'));
  })
  
  if($('.attribute-5 .textarea-processed').length != 0) {
    limitChars($('.attribute-5 .textarea-processed'), 215, $('.attribute-5 .description p'));  
  }    
}

Drupal.behaviors.ch_ecard_lightmodal = function(context) {
  if (Lightbox.disableCloseClick) {
    // close the lightbox when clicking the overlay
    $('#lightbox').click(function() { Lightbox.end('forceClose'); } );
    
    $('#outerImageContainer :submit').click(function() {
      // submit the form
      $('#outerImageContainer, #lightbox').unbind('click');
      return true;
    } );
    
    // don't close the lightbox when clicking in the box
    $('#outerImageContainer, #imageDataContainer').click(function() { return false; } );
  }
  
  $('#modalContainer #edit-attributes-5').keydown(function() {
    limitChars($(this), 215, $('#modalContainer .description p'));
  })
  $('#modalContainer #edit-attributes-5').keyup(function() {
    limitChars($(this), 215, $('#modalContainer .description p'));
  })    
}

/**
 * @author      Richard Castera
 * @date        6/14/2010
 * @comments    Limit and display the amount of characters left for the ecard message.
 **/       
function limitChars(textarea, limit, infodiv) {
    var text = textarea.val(); 
    var textlength = text.length;
    var info = infodiv;
    
    if(textlength > limit) {
        textarea.val(text.substr(0,limit));
        return false;
    } 
    else {
        info.text((limit - textlength) +' characters left for your E-Card message.');
        return true;
    }
}