(function($) {
	$.fn.ContentSlider = function(options)
	{
		var defaults = {
			leftBtn : '/sites/all/themes/bioclipse/images/slider_left_button.png',
			rightBtn : '/sites/all/themes/bioclipse/images/slider_right_button.png',
			width : '856px',
			buttonEasing : 'swing',
			sliderEasing : 'swing',
			slideSpeed : '500',
			buttonSpeed : '500'
		}
		var o = $.extend(defaults, options); /* parse default options to a object o */
		var width = parseInt(o.width); /* width of slider */
		var n = this.children('#slider').children('#slider_content').children('.article').length; /* number of articles */
		var minLeftValue = -1 * (width * (n - 1)); /* smallest left value, presume at least 2(3?) articles... */
		var slideSpeed = parseInt(o.slideSpeed);
		var buttonSpeed = parseInt(o.buttonSpeed);
		var inuse = false; /* to make sure buttons don't stop working! */
		function moveSlider(direction, button){

			var leftOffset = parseInt(button.siblings('#slider').children('#slider_content').css('left'));
			
			
			var newLeft = (direction == 'left') ? leftOffset - width : leftOffset + width; /* move to leftOffset - width if we are going right, move to leftOffset + width if we are going left */
			
			if(newLeft > 0 || newLeft < minLeftValue ){
				inuse = false;
			}
			
			if( newLeft <= 0 && newLeft >= minLeftValue ){
				button
					.siblings('#slider')
						.children('#slider_content')
							.animate({'left':newLeft+'px'}, slideSpeed, o.sliderEasing, function(){
								inuse = false;
							});
				
				if(button.attr('class')=='cs_left_button') {	/* which button are we on now? */
					var thisBtn = $('#cs_wrapper .left_button_link');
					var otherBtn = $('#cs_wrapper .right_button_link');
				} else {
					var thisBtn = $('#cs_wrapper .right_button_link');
					var otherBtn = $('#cs_wrapper .left_button_link');
				}
				if(newLeft == 0 || newLeft == minLeftValue){	/* fade out buttons if we are at the end of the slider */
					thisBtn.animate({ opacity:0 }, buttonSpeed, o.buttonEasing, function() { thisBtn.hide(); });
				}
				if(otherBtn.css('opacity')=='0') {
					otherBtn.show().animate({ opacity:1 }, buttonSpeed, o.buttonEasing);
				}
			}
		}
		return this.each(function(){
			$(this) /* add the buttons */
				.prepend('<div class="cs_left_button"><a href="#" class="left_button_link"><img src="'+o.leftBtn+'" /></a></div>') 
				.append('<div class="cs_right_button"><a href="#" class="right_button_link"><img src="'+o.rightBtn+'" /></a></div>');
			$(this).find('.cs_left_button a')
					.css('opacity','0')	/* hide the left one */
					.hide()
					.end()
				.find('.cs_right_button a')
					.css('opacity','1')	/* show the right one */
					.end();
					
			var leftBtn = $(this).children('.cs_left_button');
			leftBtn.bind('click', function(){ /* bind buttons to click-event */
				if(inuse==false){	/* don't alow multiclicking */
					inuse = true;
					moveSlider('right', leftBtn);
				}
				return false;
			});
			
			var rightBtn = $(this).children('.cs_right_button');
			rightBtn.bind('click', function(){
				if(inuse==false){
					inuse = true;
					moveSlider('left', rightBtn);
				}
				return false;
			});
		});
	}
})(jQuery)

