/*
 * 
 * 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>   
*/
/*
================================================================================================
================================================================================================
================================================================================================
================================================================================================
- Totally changed by Reza JP @ Geeks Ltd
- jafari@geeks.ltd.uk

=================================================================================================
================================================================================================
================================================================================================
*/
jQuery.fn.imageScroller = function(params) {
    var p = params || {
        next: "buttonNext",
        prev: "buttonPrev",
        frame: "viewerFrame",
        width: 50,
        child: "a",
        auto: true,
        lastItemId: "lastItem",
        firstItemId: "firstItem"
    };
    var _btnNext = $("#" + p.next);
    var _btnPrev = $("#" + p.prev);
    var _imgFrame = $("#" + p.frame);
    var _width = p.width;
    var _child = p.child;
   
    var _itv;
    var lastItem = $("#" + p.lastItemId);
    var firstItem = $("#" + p.firstItemId);
    var container = $(this);
    var turnLeft = function() {
        if (firstItem.position().left >= 0)
            return;
        var newMargin =Math.round( parseInt( _imgFrame.css("marginLeft").replace("px", "")) + _width);
       
        _imgFrame.animate({ marginLeft: newMargin }, 'fast', '', function() {
            _btnPrev.bind("click", turnLeft);
           
        });
    };

    var turnRight = function() {
        var lastItemRight = lastItem.position().left + lastItem.outerWidth();
        var containerRight = container.position().left + container.outerWidth();
        if (lastItem.offset().top <= container.offset().top && lastItemRight <= containerRight)
            return;
        var newMargin = Math.round(parseInt( _imgFrame.css("marginLeft").replace("px", "")) - _width);
        _imgFrame.animate({ marginLeft: newMargin + "px" }, 'fast', '', function() {
            _btnNext.bind("click", turnRight);
            
        });
    };

    _btnNext.css("cursor", "hand").click(turnRight);
    _btnPrev.css("cursor", "hand").click(turnLeft);

    
};

