$(function($){
/* SwitchBacks v0.01a
 * @author ZavuloN
 * @param Object	options		hash with options
 * 								images			- Array список картинок для смены
 * 								shiftingBlocks	- jQuery Object Блоки для смены внутри основного
 * 								wait			- задержка перед сменой
 *								animationSpeed	- время анимации
 *								
 */
	
	
	$.fn.switchBacks = function(options) {
		if ($(this).length > 1) {
			try {console.log('SwitchBacks: только один элемент в коллекции')} catch (e) {}
		}
		else {
			var api = new switchBacks(this, options);
		}
		return api
	}
	
	var switchBacks = function(el, options){
		
		var $this = this;
		this.el = el;
		this.current_index = 0;
		this.status = 'move';
		this.wait_load = false;
		var defaults = {
			wait:5000,
			animationSpeed: 700
		}
		
		var img;
		
		$this.options = $.extend({}, defaults, options)
		$this.options.shiftingBlocks.hide().eq(0).show();

	    $(this).css('background-image', 'url(' + $this.options.images[0] + ')')
		
		this.change = function(){
	        if (!$this.wait_load) {
	            if ($this.options.images[++$this.current_index] == undefined) 
	                $this.current_index = 0
	            $this.waitLoadImage($this.options.images[$this.current_index])
	        }
	    }
		
		
		this.pause = function(){
			this.status = 'stop';	
			clearTimeout($this.timeout);
		};
		
		this.start = function(init){
			this.status = 'move';
			if (init) {
				$this.timeout = setTimeout($this.change, $this.options.wait)
			}
			else {
				$this.change();
			}
		}
		
		this.prev = function(){
			if($this.wait_load && img){
				$(img).unbind('load');
			}
			clearTimeout($this.timeout);
            if (--$this.current_index < 0) {
				$this.current_index = $this.options.images.length - 1; 
//				alert($this.current_index + '-' + $this.options.images[$this.current_index])
			}
			
            $this.waitLoadImage($this.options.images[$this.current_index])
		}
		
		this.next = function(){
			
			if($this.wait_load && img){
				$(img).unbind('load');
			}
			clearTimeout($this.timeout);
			
            if ($this.options.images[++$this.current_index] == undefined) 
                $this.current_index = 0
            $this.waitLoadImage($this.options.images[$this.current_index])
		} 
				
	    this.waitLoadImage = function(link){
	        $this.wait_load = true;
	        img = new Image();
	        $(img).load(function(){
	            $($this.el).fadeOut($this.options.animationSpeed, function(){
	                $($this.el).css('background-image', 'url(' + link + ')');
					$this.options.shiftingBlocks.hide().eq($this.current_index).show();
	                $($this.el).fadeIn($this.options.animationSpeed, function(){})
                    $this.wait_load = false;
					if ($this.status == 'move') {
						clearTimeout($this.timeout);
						$this.timeout = setTimeout($this.change, $this.options.wait);
					}
	            })
				img = null;
	        }).error(function(){
	            if ($this.options.images[++$this.current_index] == undefined) 
	                $this.current_index = 0
	            $this.waitLoadImage($this.options.images[$this.current_index])
	        }).attr('src', link);
	        
	    }
		
		this.start(true);	
		
		return this;
		
	}
    
}(jQuery));


