Drupal.behaviors.expandText = function(context) {
  $('.node .expand-content', context).bind('click', function(){
    if ($(this).is('.expanded')) {
      $(this).children('.expand-text').hide().siblings('.limit-text').show();
    }
    else {
      $(this).children('.expand-text').show().siblings('.limit-text').hide();
    }
    $(this).toggleClass('expanded').siblings('.group-footer').toggleClass('hidden');
  });
};

Drupal.behaviors.showjCarousel = function(context) {
  $('#header-region div.view-feat-crsl div.item-list li div.grid-grouped-item', context).show();
}

Drupal.ltvGroupJoinRedirectAfterSubmit = function(data, options, element) {
  // tell the user they have joined
  Popups.message('You have joined this group', 'Redirecting to the content creation form.');
  // redirect to creation form
  var location = $(element).attr('href').split('join?destination=')[1];
  //location = location.split('=')[1];
  window.location.href = Drupal.settings.basePath + location;
  return false;
};

// if a user selects a photo without hitting the upload button, there is a bad form response.
Drupal.behaviors.createPhotoUpload = function(context) {
  $('#popups input#edit-field-photo-0-upload-btn').attr('disabled', 'disabled');
  $('#popups input#edit-field-photo-0-upload').mousedown(function() {
    // enable the upload button
    $('#popups input#edit-field-photo-0-upload-btn').removeAttr('disabled');
  });
  $('#popups input#edit-field-photo-0-upload-btn').mousedown(function() {
    // make sure that there is a value in the file field
    if ($('input#edit-field-photo-0-upload').val() != '') {
      $('div#popups form#node-form div.buttons').css('display', 'block');
    }
  });
};

/**
 * Because IE6 doesn't support :hover we add this in to make mystuff menu work.
 */
Drupal.behaviors.menuShow = function(context) {
  if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 7) {  
    $("ul.links li", context).hover(function() {
      $(this).find("ul").show().css({ visibility: 'visible' });
    }, function() {
      $(this).find("ul").hide().css({ visibility: 'hidden' });;
    });
  }
};

Drupal.groupCategorySelector = function() {};
Drupal.groupCategorySelector.attach = function(context, selector) {
  // get the argument and make sure that the correct option is selected
  $elm = $(selector, context).val(Drupal.settings.groupCategorySelector.selected);
  
  // attach an action that will navigate to the appropriate url upon selection
  $elm.change(function() {
    var arg = $elm.val();
    location.href = arg == 0 ? Drupal.settings.basePath + 'groups' : Drupal.settings.basePath + 'groups/' + Drupal.settings.groupCategorySelector.options[$elm.val()];
  });
};

// The main lifetime.com header menu doesn't set the width properly 
// because it is not included in the HTML so we adjust this automatically as JS runs.
Drupal.behaviors.fixLtvMenu = function() {
  $("#ltv-topnav-container li.toplevel ul.columns").each(function(index) {
    $t = $(this);
    // add a class like columns-4 or columns-3 to each parent UL to fix width issues on menus
    $t.addClass('columns-'+ $t.find(">li").length);
    // make the last menu elements open right instead of left
    if (index > 9) {
      $t.addClass('columns-right');
    }
  });
};

Drupal.behaviors.ltvMessageUserCompose = function(context) {
  $('#popups-body a.reply').bind('click', function() {
    Popups.close();
    var options = {href:$(this).attr('href'), afterSubmit:'Drupal.ltvMessageUserComposeAfterSubmit', width: '666px'};
    Popups.openPath(this, options);
    return false;
  });
  if ($('#popups-body form').is('#messaging-users-compose')) {
    $('#popups-body form#messaging-users-compose input#edit-preview').remove();
  }
};

Drupal.ltvMessageUserComposeAfterSubmit = function(data, options, element) {
  Popups.message('Message sent');
  window.location.href = Drupal.settings.basePath + Drupal.settings.popups.originalPath;
  return false;
};

Drupal.behaviors.breakUsernames = function() {
  $('div.profile-summary a.user-picture, div.group-owner a.user-picture').breakWords();
};

Drupal.behaviors.messagingAccessMessage = function() {
  var $elm = $('#block-ltv-user_profile div.left a.button');
  // check to see if this is set, and don't rebind if it is.
  if (!$elm.is('.clicked')) {
    $elm.bind('click', function() {
      // we set this the first time so that we don't bind more then once,
      // resulting in multiple alerts.
      $elm.addClass('clicked');
      if ($(this).is('.popups-processed')) {
        alert('You must first be friends with this user.');
      }
    });
  }
};

/**
 * Adjust height of next/prev navigation buttons for IE6.
 */ 
Drupal.behaviors.adjustNavButtons = function() {
  if (jQuery.browser.msie && parseInt(jQuery.browser.version, 10) < 7) {
    $enclosingBox = $('div.node-nodepager');
    buttonHeight = $('div.node-nodepager ul.nodepager li.pager-previous').height();
    buttonTop = Math.floor( ($enclosingBox.height())/2 . buttonHeight/2);
    $('div.node-nodepager ul.nodepager li.pager-previous,div.node-nodepager ul.nodepager li.pager-next').css(
      'top',buttonTop);
  }
};
