/** 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
 * 
 *example usage:
 * var mainSlider = jQuery('.photogallery .gal-img').slider({
		callback : mainSlider_cb,
		scroll: 1, 
		visible:1,
		li:605
	});
	
  function mainSlider_cb(obj) {
  	
    jQuery('.gal-img-controls #prev a').click(function() {
    	obj.slidePrev();
    	if (obj.opts.slideLeft) {
      	//make next active
        if (obj.opts.index < (obj.totalPage)-1) {
        	 if (jQuery('.gal-img-controls #next a.disabled-btn').length >=1) {
        		 jQuery('.gal-img-controls #next a.disabled-btn').removeClass('disabled-btn');
        	 }
        }
        //make prev inactive
        if (obj.opts.index <=0) {
        	jQuery('.gal-img-controls #prev a').addClass('disabled-btn');
        }
    	}
      return false;
    });
    jQuery('.gal-img-controls #next a').click(function() {
    	obj.slideNext();
    	if (obj.opts.slideRight) {
    		 //make prev active
  	    if (obj.opts.index >0) {
  	    	if (jQuery('.gal-img-controls #prev a.disabled-btn').length >=1) {
        		 jQuery('.gal-img-controls #prev a.disabled-btn').removeClass('disabled-btn');
        	 }
  	    }
  	    //make next inactive
  	    if (obj.opts.index >= (obj.totalPage)-1) {
  	    	jQuery('.gal-img-controls #next a').addClass('disabled-btn');
  	    }
    	}
      return false;
    });
  	
  }
 */
(function($) {
	//default settings
	var defaults = {
			li: 67,
			padding: 5,
			visible: 7,
			scroll: 7,
			index: 0,
			position:0,
			callback : null,
			slideLeft : false,
			slideRight : true
			//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);
	  },
	  
	  setItemsHeight : function() {
	    //get items height and set all of the items to have same height
		  //to avoid extra white spaces
		  var itemsHeight = this.children('li').eq(this.opts.index).children('img').height();
		  this.children('li').height( (itemsHeight+6));
	  },
	  
	  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() {
	    obj = this;
	  	if (this.opts.index >0) {
	  		this.opts.slideLeft = true;
	  		this.opts.index-=1;
	  		this.scrollWidth = this.getScrollWidth();
		    $(this).animate({"right": this.scrollWidth+"px"}, 500,'', function(){
		      //obj.setItemsHeight();
		    });
	  	}else {
	  	  this.opts.slideLeft = false;
	  	}
	  },
	  
	  slideNext : function() {
	   obj = this;
	  	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"}, 500,'', function(){
          //obj.setItemsHeight();
        });
	  	}else {
	  	  this.opts.slideRight = false;
	  	}
	  }
	  	  
	});
	
})(jQuery);