/*
 * jquery图片循环滚动插件，用法：			
 * $(document).ready(function(){
 * 		$.imgslide({
 * 			total: 6,
 *			containerId: "facePhoto"				
 * 		});
 * });
 */
(function ($) {

	$.imgslide = function (options) {
		return $.imgslide.impl.init(options);
	};
	
	$.imgslide.auto = function(){
		$.imgslide.impl.auto();
	};
	
	$.imgslide.stop = function(){
		$.imgslide.impl.stop();
	};
 	
	$.imgslide.impl = {
		opts: null,		
		index: 0,
		timer: null,
		init: function(options){
			this.opts = $.extend({}, $.imgslide.defaults, options);
			this.auto();
			return this;
		},
		auto: function(){
			if(this.opts.debug){
				debug("auto play");
			}
			if(this.opts.autoplay && this.opts.total > 1){
				this.timer = setInterval("$.imgslide.impl.play($.imgslide.impl.index)", $.imgslide.impl.opts.playtime);
			}			
		},
		stop: function(){
			if(this.opts.debug){
				debug("stop play");
			}
			if(this.timer){
				clearInterval(this.timer);
			}			
		},
		play: function(idx){
			this.index = idx;
			this.index++;
			if (this.index >= this.opts.total) {
				this.index = 0;				
			}
			if(this.opts.debug){
				debug("play index: "+this.index);
			}
			for (var i = 0; i < this.opts.total; i++) {							
				if ( i == this.index) {
					$("#"+this.opts.containerId + i).show();						
				} else {
					$("#"+this.opts.containerId + i).hide();					
				}
			}
		}
	};
	
	/*
	 * 选项默认值
	 */
	$.imgslide.defaults = {
		total: 0, 	
		containerId: "",	
		playtime: 2000,
		autoplay: true,
		debug: false
	};
	
 	function debug(msg) {
	    if (window.console && window.console.log){
	    	window.console.log('debug: ' + msg);
	    }else{
	    	window.alert('debug: ' + msg);
	    }     
  	};
})(jQuery);
