// $Id: nice_menus.js,v 1.10.2.6 2008/08/04 23:45:51 add1sun Exp $

// We need to do some browser sniffing to weed out IE 6 only
// because only IE6 needs this hover hack.
if (document.all && !window.opera && (navigator.appVersion.search("MSIE 6.0") != -1) && $.browser.msie) {
  function IEHoverPseudo() {
      $("ul.nice-menu li.menuparent").hover(function(){
          $(this).addClass("over").find("> ul").show().addShim();
        },function(){
          $(this).removeClass("over").find("> ul").removeShim().hide();
        }
      );
      // Add a hover class to all li for CSS styling. Silly naming is done
      // so we don't break CSS compatibility for .over class already in use
      // and due to the fact that IE6 doesn't understand multiple selectors.
      $("ul.nice-menu li").hover(function(){
          $(this).addClass("ie-over");
        },function(){
          $(this).removeClass("ie-over");
        }
      );
    }

    // This is the jquery method of adding a function
    // to the BODY onload event.  (See jquery.com)
    $(document).ready(function(){ IEHoverPseudo() });
}

$.fn.addShim = function() {
  return this.each(function(){
	  if(document.all && $("select").size() > 0) {
	    var ifShim = document.createElement('iframe');
	    ifShim.src = "javascript:false";
			ifShim.style.width=$(this).width()+1+"px";
      ifShim.style.height=$(this).find("> li").size()*23+20+"px";
			ifShim.style.filter="progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)";
		  ifShim.style.zIndex="0";
    $(this).prepend(ifShim);
      $(this).css("zIndex","99");
		}
	});
};

$.fn.removeShim = function() {
  return this.each(function(){
	  if (document.all) $("iframe", this).remove();
	});
};
;
Drupal.behaviors.actionsShow = function (context) {
  var $ = jQuery;

  // Add a toggle to the action button to open and close the dropdown menu.
  $('a.actions').bind('click', function () {
    $(this).parent(".actions-wrapper").toggleClass("open");
  });

  // Add a click handler to catch when a user clicks outside the dropdown and
  // close the dropdown.
  $("body").bind('click', function (event) {
    var $target = $(event.target);
    var $actionWrapper = $target.parents('.actions-wrapper:first');
    // If the click happens outside the wrapper or if the click is one of the actions
    // in the wrapper, trigger the close dialog event.
    if ($actionWrapper.length === 0) {
      $(".actions-wrapper").trigger('closeDialog');
    }
  });
  
  $(".actions-wrapper").bind('closeDialog', function (event) {
      $(".actions-wrapper").removeClass("open");
  });

  // Add a class to the last option in the dropdown to remove the bottom border.
  $("body.gardens .view-sites-by-user td.views-field-actions .actions-wrapper li:last-child").addClass('last');

  // Add a click handler for the membership visibility toggle
  $('#membership-count').bind('click', Drupal.gardener_manager.membershipToggle);

  // Hide the membership list by default
  if ($.cookie("show_membership") != 1) {
    $('#site-membership').hide();
  } else {
    $('#membership-count').addClass('opened');
  }
};

Drupal.behaviors.resendVerificationEmail = function (context) {
  var $ = jQuery;
  var reminder = $("div.email-verification-reminder.messages").not('.gardens-manager-processed');
  if (reminder.length) {
    reminder.addClass("gardens-manager-processed");
    // Add a "Resend" link.
    var header = $("strong", reminder);
    var link = $("<a class='resend-link' href='javascript:void(0);'>Resend</a>");
    reminder.append(link);
    link.click(Drupal.behaviors.resendVerificationEmail.clickHandler);
  }
};

Drupal.behaviors.resendVerificationEmail.clickHandler = function (e) {
  var $ = jQuery;
  $.get('gardens-manager/resend-verification-email', Drupal.behaviors.resendVerificationEmail.success);
};

Drupal.behaviors.resendVerificationEmail.success = function () {
  var $ = jQuery;
  $("div.email-verification-reminder.messages a").replaceWith($("<span class='resend-link'>Email sent.</span>"));
};

Drupal.gardener_manager = {};

Drupal.gardener_manager.membershipToggle = function (event) {
  var $ = jQuery;
  $(event.target).toggleClass('opened');
  event.preventDefault();
  $('#site-membership').toggle();
  if ($.cookie("show_membership") != 1) {
    $.cookie("show_membership", 1, -1);
  } else {
    $.cookie("show_membership", 0, -1);
  }
  $('.clearfix').removeClass('clearfix').addClass('clearfix');
};

Drupal.gardener_manager.checkUsernameEmail = function (value, callback) {
  var $ = jQuery;

  $.ajax({type: 'POST',
    url: '/transfer-site/check-recipient',
    data: {
      recipient: value
    },
    dataType: 'json',
    success: callback
  });
};

Drupal.gardener_manager.initSiteTransfer = function() {
  var activeSubmit = new Acquia.ActiveSubmitButton('#edit-transfer');

  // Create interactive validator instances for each field.
  var recipient = new Acquia.InteractiveFieldValidator(activeSubmit, '#edit-recipient', Drupal.gardener_manager.checkUsernameEmail, '#recipient-status').forceLowerCase();
  
  activeSubmit.statusChanged();
};

Drupal.behaviors.siteTransfer = function (context) {
  var $ = jQuery;
  if ($('#gardens-manager-transfer-site', context)) {
    Drupal.gardener_manager.initSiteTransfer();
  }
};;
/**
 * Cookie plugin
 *
 * Copyright (c) 2006 Klaus Hartl (stilbuero.de)
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 *
 */

/**
 * Create a cookie with the given name and value and other optional parameters.
 *
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Set the value of a cookie.
 * @example $.cookie('the_cookie', 'the_value', { expires: 7, path: '/', domain: 'jquery.com', secure: true });
 * @desc Create a cookie with all available options.
 * @example $.cookie('the_cookie', 'the_value');
 * @desc Create a session cookie.
 * @example $.cookie('the_cookie', null);
 * @desc Delete a cookie by passing null as value. Keep in mind that you have to use the same path and domain
 *       used when the cookie was set.
 *
 * @param String name The name of the cookie.
 * @param String value The value of the cookie.
 * @param Object options An object literal containing key/value pairs to provide optional cookie attributes.
 * @option Number|Date expires Either an integer specifying the expiration date from now on in days or a Date object.
 *                             If a negative value is specified (e.g. a date in the past), the cookie will be deleted.
 *                             If set to null or omitted, the cookie will be a session cookie and will not be retained
 *                             when the the browser exits.
 * @option String path The value of the path atribute of the cookie (default: path of page that created the cookie).
 * @option String domain The value of the domain attribute of the cookie (default: domain of page that created the cookie).
 * @option Boolean secure If true, the secure attribute of the cookie will be set and the cookie transmission will
 *                        require a secure protocol (like HTTPS).
 * @type undefined
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */

/**
 * Get the value of a cookie with the given name.
 *
 * @example $.cookie('the_cookie');
 * @desc Get the value of a cookie.
 *
 * @param String name The name of the cookie.
 * @return The value of the cookie.
 * @type String
 *
 * @name $.cookie
 * @cat Plugins/Cookie
 * @author Klaus Hartl/klaus.hartl@stilbuero.de
 */
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};;
(function($) {

  Drupal.behaviors.jsShow = function (context) {
    $('.js-show', context).show();
  };

  /**
   * Custom Mollom image/audio toggle links.
   *
   * @see gardener_site_pre_render_mollom()
   */
  Drupal.behaviors.alterMollomSignup = function (context) {
    // Implement toggling for the custom links added by
    // gardener_site_pre_render_mollom().
    $('a.mollom-audio-captcha', context).bind('click', function () {
      $('.mollom-captcha-text-switch-to-image').show();
      $('.mollom-captcha-text-switch-to-audio').hide();
    });
    $('a.mollom-image-captcha', context).bind('click', function () {
      $('.mollom-captcha-text-switch-to-image').hide();
      $('.mollom-captcha-text-switch-to-audio').show();
    });
    // Fix the default links added by mollom.module and hidden by the Flight
    // theme's CSS clipping to not display within the clip when they receive
    // focus via tabbing in some browsers.
    var mollom = $("#edit-mollom-captcha-wrapper .field-prefix", context);
    var img = $(".mollom-captcha-content", mollom);
    if (mollom.length && img.length) {
      mollom.html("");
      img.appendTo(mollom);
    }
  };

  /**
   * Add a confirmation dialog for links to features that require an upgraded subscription.
   *
   * @see gardener_site_paid_feature_link()
   */
  Drupal.behaviors.upgradeDialog = function (context) {
    if (Drupal.settings.upgradeDialog) {
      for (var id in Drupal.settings.upgradeDialog) {
        $('#' + id, context).bind('click', function (event) {
          event.preventDefault();
          var id = this.id;
          // @todo Replace with jQuery dialog that allows for theming of title
          //   and content.
          var message = Drupal.settings.upgradeDialog[id].title + "\n\n" + Drupal.settings.upgradeDialog[id].content;
          if (confirm(message)) {
            window.location.href = Drupal.settings.upgradeDialog[id].upgrade_url;
          }
        });
      }
    }
  };
  
  Drupal.behaviors.pleaseWaitSubmit = function (context) {
    // Disable the submit button when submitting a Zuora form.
    $('.acquia-zuora-form').bind('submit', Drupal.pleaseWait);
  };
  
  Drupal.pleaseWait = function (event) {
    var textHolder = $('#edit-submit span span', this).length ? $('#edit-submit span span', this) : $('#edit-submit span', this);
    textHolder.text(Drupal.t('Please wait...'));
  };

})(jQuery);
;

