/******************************************************************************
* jquery.ctg.slides.js 
*
* @description a function allows showing a number of items at once,
*   and sliding between them
*
* @param {Number} numToShow the number of elements to show at once
* @param {String} countLblQ a query to access the count label
* @param {String} totalLblQ a query to access the total label
* @param {String} buttonLeftQ a query to access the left button
* @param {String} buttonRightQ a query to access the right button
* @param {Object} [settings] a custom settings opbject
* @return {Object} a jquery object
******************************************************************************/
(function($) {
		
	$.fn.slides = function(numToShow, countLblQ, totalLblQ, buttonLeftQ, buttonRightQ,settings) {
 
        //Defaults
		settings = jQuery.extend({
			hideIfLess: true,
			container: $(this).parent()
		}, settings);

		var eArray = this; 				//Array of elements to apply to
		var len = eArray.length;
		var count = len;

		//$(this).parent("ul").css("width",$(eArray[0]).width()*len*2)
		
		//If less than or equal than to show, just show them, why bother paginating?
        if(len <= numToShow && settings.hideIfLess) {
            settings.container.hide();   
        }
		
		//If count is less the the number of slots, set the buttons
		//to show the count ex 2 of 2 instead of 3 of 2
		$(countLblQ).text((count < numToShow ? count : numToShow));
		$(totalLblQ).text(count);

        //Init buttons state to disabled
        $(buttonLeftQ).addClass("disabled");
	    if (count <= numToShow) { $(buttonRightQ).addClass("disabled"); }
	    
	    $(eArray).hide();
	    $(eArray[0]).show();

        //Left button click - move to previous element
		$(buttonLeftQ).click(function(e) {
			e.preventDefault();
			
			if(count < len) {
				var firstE = eArray[len - count];
				$(firstE).hide();
				
				count++;
				var firstE = eArray[len - count];
				$(firstE).fadeIn("fast");
				$(countLblQ).text(len - count + parseInt(numToShow));
                $(buttonRightQ).removeClass("disabled");    //Enable right button
			}
			if (count == len) { $(buttonLeftQ).addClass("disabled"); }      //If at begining, disable button
		});		

        //Right button click - move to next element
		$(buttonRightQ).click(function(e) {
			e.preventDefault();

			if(count > numToShow) {
				var firstE = eArray[len - count];
				$(firstE).hide();
				
				count--;
				
				var nextE = eArray[len - count]
				$(nextE).fadeIn("fast");
				
				$(countLblQ).text(len - count + parseInt(numToShow));
                $(buttonLeftQ).removeClass("disabled");     //Enable left button
			}
			if (count == numToShow) { $(buttonRightQ).addClass("disabled"); }   //If at end, disable button
		});		
    	return this;
	};//Close function
	
})(jQuery);
