(function($) {
  $.fn.ContentSlider = function(options)
  {
    var defaults = {
      leftBtn : 'images/cs_leftImg.jpg',
      rightBtn : 'images/cs_rightImg.jpg',
      width : '610px',
      speed : 600,
      easing : 'easeOutQuad',
	  imagesPaths : 'application/views/images/cat/'
    }
    var defaultWidth = defaults.width;
    var o = $.extend(defaults, options);
    var w = 225;
    var n = this.children('.cs_wrapper').children('.cs_slider').children('.cs_article').length;
    var x = -1*w*n+w; // Minimum left value

    var imagesPaths = defaults.imagesPaths;;
    var inuse = false; // Prevents colliding animations

    return this.each(function() {
      $(this)
        // Set the width and height of the div to the defined size
        .css({
         width:o.width,
		 height: o.height
        })
		.find('.cs_slider')
		  // Set the width and height of the div to the defined size
		.css({
			width:o.sli_width,
			height:o.sli_height
		  })
		.find('.cs_article')
		  // Set the width and height of the div to the defined size
		.css({
			width:o.width,
			height:o.height
		  });

		 // set initial position
    	 $('.cs_slider').css({ 'left':'0px', 'top':'0px'});  
	 });
  }

})(jQuery)
$(document).ready( function() {
    function moveSlider(thisInstance, d, b,cat_id, pages)
    {
	   if (d != 'up' && d != 'down') {
	  
	  var n = parseInt($('#'+thisInstance+' .cs_wrapper').children('.cs_slider').children('.cs_article').length);
	  var l = parseInt($('#'+thisInstance+' .cs_wrapper').children('.cs_slider').css('left'));
	  var w = parseInt($('#'+thisInstance+' .cs_wrapper').children('.cs_slider').attr('exWidth'));
	  var x = -1*w*n+w;
	  
	  if(isNaN(l)) { var l = 0;}

	  var m = (d=='left') ? l-w : l+w;
      
	  if(m<=0&&m>=x) {
	 
       $('#'+thisInstance+' .cs_wrapper').children('.cs_slider').animate({ 'left':m+'px' });
      } 
    } 
	else 
	{  // up condition
		var slider_h = parseInt($('#'+thisInstance+' .cs_wrapper').children('.cs_slider').css('height'));
		var slider_top = parseInt($('#'+thisInstance+' .cs_wrapper').children('.cs_slider').css('top'));
		var article_h = parseInt($('#'+thisInstance+' .cs_wrapper').children('.cs_slider').children('.cs_article').css('height'));
		var article_c = parseInt($('#'+thisInstance+' .cs_wrapper').children('.cs_slider').children('.cs_article').length);
		
		var max_height = 0-(article_c*article_h);
		var h = slider_top-article_h;
			cond = max_height<h;
		if (d == 'down'){
			h = slider_top+article_h;
			cond = slider_top<0; 
		}
		 if( cond ) {
	       $('#'+thisInstance+' .cs_wrapper').children('.cs_slider').animate({ 'top': h+'px' });
		 } 
	}
	};

	  $('.cs_rightBtn').bind('click', function() {
			thisInstance = this.id.substr(1);
			moveSlider(thisInstance, 'left', thisInstance);
		return false; // Keep the link from firing
	  });  
      
	  $('.cs_leftBtn').bind('click', function() {
		thisInstance = this.id.substr(1);
          moveSlider(thisInstance, 'right', thisInstance);
        return false; // Keep the link from firing
      });
	  
      $('.cs_upBtn').bind('click', function() {
			thisInstance = this.id.substr(1);
			moveSlider(thisInstance, 'up', thisInstance);
            return false;
		   });      
		   
      $('.cs_downBtn').bind('click', function() {
			thisInstance = this.id.substr(1);
			moveSlider(thisInstance, 'down', thisInstance);
            return false;
		   });
});
