(function($) {
  $.fn.ContentSlider = function(options)
  {
    var defaults = {
      leftBtn : '/images/pro_btn_calloutBackOn.gif',
      rightBtn : '/images/pro_btn_calloutForwardOn.gif',
      width : '280px',
      height : '100px',
      speed : 400,
      easing : 'easeOutQuad',
      textResize : false,
      IE_h2 : '26px',
      IE_p : '11px',
      IsConsumer : false,
      IsProHome : false
    }
    var defaultWidth = defaults.width;
    var o = $.extend(defaults, options);
    var w = parseInt(o.width);
    var siblingElement;
    if (o.IsConsumer)
      siblingElement = this.children('.case_studies_widget').children('.cs_wrapper');
    else
      siblingElement = this.children('.cs_wrapper');
    var x = siblingElement.children('.auto_scroll');
    var autoScroll = x.length == 1;
    var autoLeft = false;
    var n = siblingElement.children('.cs_slider').children('.cs_article').length;
    var x = -1*w*n+w; // Minimum left value
    var p = parseInt(o.width)/parseInt(defaultWidth);
    var inuse = false; // Prevents colliding animations
    var leftBtnClass = ".cs_leftBtn";
    var rightBtnClass = ".cs_rightBtn";
    var userClick = false;

    if (o.IsConsumer) {
      leftBtnClass  = ".left";
      rightBtnClass  = ".right";
    }

    if (o.IsProHome) {
      leftBtnClass = ".left_home";
      rightBtnClass  = ".right_home";
    }

    var leftBtn = siblingElement.find(leftBtnClass);
    var rightBtn = siblingElement.find(rightBtnClass);
    if (o.IsConsumer)
    {
      leftBtn.each(function (i){ if(i==0) 
        $(this).css('opacity','0'); 
	}); 
      var lastRight;
      rightBtn.each(function (i){ lastRight = $(this); });
      lastRight.css('opacity','0');
    }

    function moveSlider(d, b)
    {
      if (b.css('opacity')=='0'){
        inuse=false;
        return false;
      }
      var l = parseInt(siblingElement.children('.cs_slider').css('left'));
      if(isNaN(l)) {
        var l = 0;
      }
      var m = (d=='left') ? l-w : l+w;
      if(m<=0&&m>=x) {
        siblingElement 
            .children('.cs_slider')
              .animate({ 'left':m+'px' }, o.speed, o.easing, function() {
                inuse=false;
              });

        if (!o.IsConsumer){
        var thisBtn = b;
        if (d=='left'){
          var otherBtn = b.parent().contents(leftBtnClass);
        }else{
          var otherBtn = b.parent().contents(rightBtnClass);
        }
			  
        if(m==0||m==x) {
          thisBtn.animate({ 'opacity':'0' }, o.speed, o.easing, function() { thisBtn.hideHome(); });
        }
        if(otherBtn.css('opacity')=='0') {
          otherBtn.show().animate({ 'opacity':'1' }, { duration:o.speed, easing:o.easing });
        }
	}
        return true;
      }
      return false;
    }

    function vCenterBtns(b)
    {
      // Safari and IE don't seem to like the CSS used to vertically center
      // the buttons, so we'll force it with this function
      var mid = parseInt(o.height)/2;
      b
        .find('.cs_leftBtn img').css({ 'top':mid+'px', 'padding':0 }).end()
        .find('.cs_rightBtn img').css({ 'top':mid+'px', 'padding':0 });
    }

    function ShowNextSlide(obj, left, right) {
      if (userClick)
        return;
      if (!inuse){
        inuse = true;
        var result;
        if (autoLeft) {
          result = moveSlider('right', left);
        } else {
          result = moveSlider('left', right);
        }
        if (!result) {
          inuse = false;
          autoLeft = !autoLeft;
        }
      }
      window.setInterval(function(){obj.ShowNextSlide(obj, left, right);}, 5000);
    }

    return this.each(function() {

      $(this)
        // Set the width and height of the div to the defined size
        .css({
          width:o.width,
          height:o.height
        })
        // Add the buttons to move left and right
        .prepend(o.IsConsumer || o.IsProHome ? '' : '<a href="#" class="cs_leftBtn"><img src="'+o.leftBtn+'" /></a>')
        .append(o.IsConsumer || o.IsProHome ? '' : '<a href="#" class="cs_rightBtn"><img src="'+o.rightBtn+'" /></a>')
        // Dig down to the article div elements

        .find('.cs_article')
          // Set the width and height of the div to the defined size
          .css({
            width:o.width,
            height:o.height
          })
          .end();
        
       if (!(o.IsConsumer || o.IsProHome)){
         leftBtn = $(this).find('.cs_leftBtn');
         rightBtn= $(this).find('.cs_rightBtn');
       }
       var _this = $(this);
       //window.setInterval(function(){ShowNextSlide(_this, leftBtn, rightBtn);}, 5000);
       var buttonContainer = o.IsProHome ? $(this).next().next() : $(this);

       if (!o.IsConsumer){
        // Animate the entrance of the buttons
        buttonContainer 
         .find(leftBtnClass)
          .css('opacity','0')
          .hideHome()
          .end()
        .find(rightBtnClass)
          .hideHome()
          .animate({ 'width':'show' });
       }


      // Resize the font to match the bounding box
      if(o.textResize===true) {
        var h2FontSize = $(this).find('h2').css('font-size');
        var pFontSize = $(this).find('p').css('font-size');
        $.each(jQuery.browser, function(i) {
          if($.browser.msie) {
             h2FontSize = o.IE_h2;
             pFontSize = o.IE_p;
          }
        });
        $(this).find('h2').css({ 'font-size' : parseFloat(h2FontSize)*p+'px', 'margin-left' : '66%' });
        $(this).find('p').css({ 'font-size' : parseFloat(pFontSize)*p+'px', 'margin-left' : '66%' });
        $(this).find('.readmore').css({ 'font-size' : parseFloat(pFontSize)*p+'px', 'margin-left' : '66%' });
      }

      // Store a copy of the button in a variable to pass to moveSlider()
      var leftBtn = buttonContainer.find(leftBtnClass);
      leftBtn.each(function (i){
        $(this).click(function(){
          if(inuse===false) {
            userClick = true;
            inuse = true;
            moveSlider('right', $(this));
          }
          return false; // Keep the link from firing
        });
      });

      // Store a copy of the button in a variable to pass to moveSlider()
      var rightBtn = buttonContainer.find(rightBtnClass);
      rightBtn.each(function (i){
        $(this).click(function(){
          if(inuse===false) {
            userClick = true;
            inuse=true;
            moveSlider('left', $(this));
          }
          return false; // Keep the link from firing
        });
      });

      vCenterBtns($(this)); // This is a CSS fix function.
    });
  }
})(jQuery)