/*!
 * @copyright Advanced Care Solutions, Inc.
 * @author Christopher Rahauiser <crahauiser@acs-web.com>
 * @depends jquery.ui.widget.js
 * @depends jquery.acs.slideshow.js
 */
(function($) {
  $.widget('acs.horizontalslideshow', $.acs.slideshow, {
    options: $.extend({}, $.acs.slideshow.prototype.options, {
      rightToLeft: true
    }),
    // default options are based on element width. Override this method to setup
    // animation options
    _createWidget: function(options, element) {
      this.element = $(element);
      var widgetWidth = this.element.width(),
          options = options || {}, // in case no options were passed in
          nonDefaults = $.extend({}, this._getCreateOptions(), options),
          RtoL = nonDefaults.rightToLeft || this.options.rightToLeft,
          posWidth = widgetWidth + 'px',
          negWidth = '-' + posWidth;
      if (! RtoL) {
        var tmp = posWidth;
        posWidth = negWidth;
        negWidth = tmp;
      }
      options = this._setupCustomOptions(options, nonDefaults, posWidth, negWidth);
      $.acs.slideshow.prototype._createWidget.apply(this, [options, element]);
    },
    _setupCustomOptions: function(options, nonDefaults, posWidth, negWidth) {
      // We leave options alone if they have been passed in or taken from metadata
      if (! nonDefaults.animateInNextProperties) {
        this.options.animateInNextProperties = {}; // slideshow's opacity: toggle override
        options.animateInNextProperties = { left: 0 };
      }
      if (! nonDefaults.animateInPreviousProperties) {
        this.options.animateInPreviousProperties = {}; // slideshow's opacity: toggle override
        options.animateInPreviousProperties = { left: 0 };
      }
      if (! nonDefaults.animateOutNextProperties) {
        this.options.animateOutNextProperties = {}; // slideshow's opacity: toggle override
        options.animateOutNextProperties = { left: negWidth };
      }
      if (! nonDefaults.animateOutPreviousProperties) {
        this.options.animateOutPreviousProperties = {}; // slideshow's opacity: toggle override
        options.animateOutPreviousProperties = { left: posWidth };
      }
      if (! nonDefaults.prepareNext) {
        options.prepareNext = function(animateInObj, animateOutObj) {
          animateInObj.css('left', posWidth);
        };
      }
      if (! nonDefaults.preparePrevious) {
        options.preparePrevious = function(animateInObj, animateOutObj) {
          animateInObj.css('left', negWidth);
        };
      }
      if (! nonDefaults.animateOutPreviousCallback) {
        options.animateOutPreviousCallback = function() {
          $(this).css({
            left: negWidth,
            zIndex: 1
          });
        };
      }
      if (! nonDefaults.childrenSetup) {
        options.childrenSetup = function(children) {
          children.eq(0).css('zIndex', 2);
          children
            .not(':eq(0)')
              .css('left', negWidth)
              .show();
        };
      }
      return options;
    }
  });
})(jQuery);
