/** jSlider
 * @author Alex Lim
 * @name jslider
 * @type jQuery
 * @param Hash o A set of key/value pairs to set as configuration properties.
 * @cat Plugins/jCarousel
 */
(function($) {
	//default settings
	var defaults = {
			li: 67,
			padding: 5,
			visible: 7,
			scroll: 7,
			index: 0,
			position:0,
			callback : null,
			slideLeft : false,
			slideRight : true,
			speed:500
			//prev : null,
			//next : null
	};

  //constructor.
	$.fn.slider = function(options) {
		slider = this;
	  this.sliderInit(options);
	  return this;
	  //this.deBug();
	};

	//helper methods
	$.fn.extend({
		deBug : function(obj) {
      console.log(obj);
    },

    getSliderWidth : function() {
			return (this.opts.li+this.opts.padding)*this.totalItems;
	  },

	  getTotalItems : function() {
	    return this.children('li').length;
	  },

	  getScrollWidth : function() {
	    return (this.opts.li*this.opts.visible)*this.opts.index;
	  },

	  getTotalPage : function() {
	  	return (this.totalItems/this.opts.scroll);
	  },

	  sliderInit : function(options) {
	  	this.opts = $.extend({}, defaults, options);
			this.totalItems = this.getTotalItems();
		  this.sliderWidth = this.getSliderWidth();
		  this.totalPage = this.getTotalPage();
		  $(this).css({width:this.sliderWidth+'px'});

		  //exec callback
		  this.opts.callback(this);
		  /* //bind events
		  $(this.opts.prev).bind('click', function() {
        $(slider).slidePrev();
        return false;
		  });
		  $(this.opts.next).bind('click', function() {
        $(slider).slideNext();
        return false;
		  }); */
	  },

	  slidePrev : function() {
	  	if (this.opts.index >0) {
	  		this.opts.slideLeft = true;
	  		this.opts.index-=1;
	  		this.scrollWidth = this.getScrollWidth();
		    $(this).animate({"right": this.scrollWidth+"px"}, this.opts.speed);
	  	}else {
	  	  this.opts.slideLeft = false;
	  	}
	  },

	  slideNext : function() {
	  	if (this.opts.index < (this.totalPage)-1) {
	  		this.opts.slideRight = true;
	  		this.opts.index+=1;
	  		this.scrollWidth = this.getScrollWidth();
  	  	$(this).animate({"right": this.scrollWidth+"px"}, this.opts.speed);
	  	}else {
	  	  this.opts.slideRight = false;
	  	}
	  },

      slideTo : function(idx) {

          // if the image idx we slide to is within bounds.
        if ( 0 <= idx && idx < this.totalPage) {

            // are we sliding to the left?
            if (idx < this.opts.index ) {
                this.opts.slideLeft  = true;
                this.opts.slideRight = false;
                this.opts.index = idx;
                $(this).animate({"right": this.getScrollWidth() +"px"}, this.opts.speed);

            // or are we sliding to the right?
            } else if ( idx > this.opts.index ) {
                this.opts.slideRight = true;
                this.opts.slideLeft  = false;                
                this.opts.index = idx;
                $(this).animate({"right": this.getScrollWidth()+"px"}, this.opts.speed);
            }


        }

      }
	});

})(jQuery);