/*
 * 
 * ImageScroller - a Image Horizental Scroll Viewer 
 * Version 0.1
 * @requires jQuery v1.2.1
 * 
 * Copyright (c) 2007 Luan
 * Email verycss-ok@yahoo.com.cn 
 * 
 * Dual licensed under the MIT and GPL licenses:
 * http://www.opensource.org/licenses/mit-license.php
 * http://www.gnu.org/licenses/gpl.html
 * 
 * Example:
 *   #viewer {height:100px; width:300px; clear:both; overflow:hidden; border:3px solid #e5e5e5;}
 *   #viewerFrame {width:505px; clear:both; padding:0;}
 *   #viewer img {width:90px; height:90px; margin:5px; display:inline; border:0;}
 *   #viewer a {display:block; float:left; width:100px; height:100px;}
 *   <script type="text/javascript">
 *   $(function(){
 *   	$("#viewer").imageScroller({
 *   	next:"btn1",
 *   	prev:"btn2",
 *   	frame:"viewerFrame",
 *   	width:100, 
 *   	child:"a",
 *   	auto:true
 *   	});	 
 *   });
 *   </script>
 *   <div id="viewer"><div id="viewerFrame">
 *   <a href=""><img src="pre1.jpg"></a>
 *   <a href=""><img src="pre2.jpg"></a>
 *   <a href=""><img src="pre3.jpg"></a>
 *   <a href=""><img src="pre4.jpg"></a>
 *   <a href=""><img src="pre5.jpg"></a>
 *   </div></div>
 *   <span id="btn1">prev</span><br/><span id="btn2">next</span>   
*/

jQuery.fn.imageScroller = function(params){
	var p = params || {
		next:"next",
		prev:"prev",
		frame:"viewerFrame",
		//width:100,
		child:".slide",
		auto:true
	}; 
	var _btnNext = $(".next", this);
	var _btnPrev = $(".prev", this);
	var _controls = $(".controls", this);
	var _imgFrame = this;
	var _width = p.width;
	var _child = p.child;
	var _childs = $(_child,_imgFrame);
	var _auto = p.auto;
	var _itv;
	var _firstchild = _imgFrame.find(_child+":first");
	_width = _firstchild.width();
	_firstchild.appendTo( _imgFrame );
	
	var turnLeft = function(){
		_btnPrev.unbind("click",turnLeft);
		_firstchild = _imgFrame.find(_child+":first");
		_lastchild = _imgFrame.find(_child+":last");
		_width = _firstchild.width();
		_firstchild.css('marginLeft',_width);
		_firstchild.appendTo( _imgFrame );
		if(_auto) autoStop();
			
		_firstchild.animate({
			  marginLeft: parseInt(_firstchild.css('marginLeft'),10) == 0 ?
				-_firstchild.outerWidth() :
				0
			}, function(){
				_btnPrev.bind("click",turnLeft);
				if(_auto) autoPlay();
		});
		_lastchild.animate({
			  marginLeft: parseInt(_lastchild.css('marginLeft'),10) == 0 ?
				-_lastchild.outerWidth() :
				0
			}, function(){
				_lastchild.css("marginLeft",0);
		});
	};
	var turnRight = function(){
		_btnNext.unbind("click",turnRight);
		if(_auto) autoStop();
		_childs = $(_child,_imgFrame);
		if (_childs.length > 2){
			var _firstchild = _childs.eq(_childs.length-2);
		} else {
			var _firstchild = _imgFrame.find(_child+":first");
		}
		_lastchild = _imgFrame.find(_child+":last");
		_firstchild.css('marginLeft',-_width);
		
		_firstchild.animate({
			  marginLeft: parseInt(_firstchild.css('marginLeft'),10) == 0 ?
				_firstchild.outerWidth() :
				0
			}, function(){
				_btnNext.bind("click",turnRight);
				if(_auto) autoPlay();
		});
		
		_lastchild.animate({
			  marginLeft: parseInt(_lastchild.css('marginLeft'),10) == 0 ?
				_lastchild.outerWidth() :
				0
			}, function(){
				_lastchild.prependTo( _imgFrame );
				_lastchild.css("marginLeft",0);
		});
		
	};
	_btnNext.css("cursor","hand").click( turnRight );
	_btnPrev.css("cursor","hand").click( turnLeft );
	if (_childs.length > 1){
		var autoPlay = function(){
		  _itv = window.setInterval(turnLeft, 8000);
		};
		var autoStop = function(){
			window.clearInterval(_itv);
		};
		if(_auto)	autoPlay();
	} else {
		_controls.hide();
	}
};

