
(function($) {
  $.fn.ContentSlider = function(options)
  {
    var defaults = {
      leftBtn : 'images/cs_leftImg.jpg',
      rightBtn : 'images/cs_rightImg.jpg',
      width : '900px',
      height : '400px',
      speed : 400,
      easing : 'easeOutQuad',
      textResize : false,
      IE_h2 : '26px',
      IE_p : '11px'
    }
    var defaultWidth = defaults.width;
    var o = $.extend(defaults, options);
    var w = parseInt(o.width);
    var n = this.children('.cs_wrapper').children('.cs_slider').children('.cs_article').length;
    var thisInstance = this.attr('id');
    var wa = $('#'+thisInstance+' .cs_article:first').width();

	var x=-1*wa*n+wa; // Minimum left value

    var p = parseInt(o.width)/parseInt(defaultWidth);
    var inuse = false; // Prevents colliding animations

    function moveSlider(d, b)
    {
      var l = parseInt(b.siblings('.cs_wrapper').children('.cs_slider').css('left'));
      if(isNaN(l)) {
        var l = 0;
      }
      var m = (d=='left') ? l-w : l+w;

      if(1) {
        b
          .siblings('.cs_wrapper')
            .children('.cs_slider')
              .animate({ 'left':m+'px' }, o.speed, o.easing, function() {
                inuse=false;
				
				//Switch slider position to start or to end for cycling
				 if (m<=2*x+w-wa&&d=='left') {
		   
					b
				     .siblings('.cs_wrapper')
				      .children('.cs_slider').css({'left':m-x+wa+'px'});
		
				}

				if (m>=x-w+wa&&d=='right') {
		 
					b
				     .siblings('.cs_wrapper')
				      .children('.cs_slider').css({'left':m+x-wa+'px'});
		
				}
				
              });
			  
	

        if(b.attr('class')=='cs_leftBtn') {
          var thisBtn = $('#'+thisInstance+' .cs_leftBtn');
          var otherBtn = $('#'+thisInstance+' .cs_rightBtn');
        } else {
          var thisBtn = $('#'+thisInstance+' .cs_rightBtn');
          var otherBtn = $('#'+thisInstance+' .cs_leftBtn');
        }
		
    
      }
	  
	  	
    }

    return this.each(function() {

if (-x>=w) {

		    $(this)
		        
		        // Add the buttons to move left and right
		        .prepend('<a href="#" class="cs_leftBtn"><img src="'+o.leftBtn+'" /></a>')
		        .append('<a href="#" class="cs_rightBtn"><img src="'+o.rightBtn+'" /></a>')
		        // Dig down to the article div elements
		        .find('.cs_article')

		          .end();
			
			//Add frame copies before and after for seamless cycling
			 var a=$(this).find('.cs_article');
			 a.clone().insertAfter("#"+thisInstance+' .cs_article:last');
			 a.clone().insertBefore("#"+thisInstance+' .cs_article:first');
			 if (o.left_preset!=null) {
				$(this).find('.cs_slider').css({'left':o.left_preset});
			} else  {
				$(this).find('.cs_slider').css({'left':x-wa+'px'});
			}
	      // 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 = $(this).children('.cs_leftBtn');
	      leftBtn.bind('click', function() {
	        if(inuse===false) {
	          inuse = true;
	          moveSlider('right', leftBtn);
	        }
	        return false; // Keep the link from firing
	      });

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

	}
    });
	
  }
})(jQuery)