/* 
Requires jQuery 1.3

Usage:

<script type="text/javascript">
$(document).ready(function() {
    $('.photos-container').each(function(i, el) {
        $(el).scrollery();
    });
});
</script>

Example template:

<div class="photos-container">
<div class="previous"><a href="#prev">&nbsp;</a></div>
<div class="photos">
<div class="photos-inner">
{% for p in album.all_photos %}
    <div class="photo {% ifequal p photo %}selected{% endifequal %}">
        {% if settings.crop_thumbnails %}
            {% get_image_preview square 55 for p as preview_path %}
        {% else %}
            {% get_image_preview height 55 for p as preview_path %}
        {% endif %}            
        <div class="image">
            <a href="{{p.permalink}}"><img src="{{preview_path}}" alt="{{p.alt}}" title="{{p.title}}" /></a>
        </div>
    </div> <!-- .photo -->
{% endfor %}    
</div>
</div> <!-- .photos -->
<div class="next"><a href="#next">&nbsp;</a></div>
</div> <!-- .photos-container -->

*/

jQuery.fn.scrollery = function(options) {
    
    var settings = jQuery.extend({        
        animate: true,
        scroll_inc: 1
    }, options);
       
    // cached queries:
    
    var $widget = this;
    var $photos_div = this.find('.photos');
    var $photos = this.find('.photos .photo');
    var $inner  = this.find('.photos-inner');
    var $previous_a = $widget.find('.previous a');
    var $next_a = $widget.find('.next a');
    
    // calculated 'attributes':
    
    var margin_left = 0;
    
    var visible_width = $photos_div.width();
  
    var total_width = 0;
    $inner.find('.photo').each(function (i, el) {
        outerwidth = $(el).outerWidth();       
        total_width += outerwidth;
    });
    
    var max_offset = total_width-visible_width;
    if (max_offset < 0) {
        max_offset = 0;
    }
        
    $inner.css('width', total_width+'px');
    $inner.css('position', 'relative');
    
    // methods:
    
    function check_scroll_edges() {
        // this adds/removes class 'disabled' from the relevant next / previous
        // buttons when we're scrolled to the start or end.
        
        if (margin_left == 0) {
            $previous_a.addClass('disabled');
        } else {
            $previous_a.removeClass('disabled');
        }
        
        if (margin_left == max_offset) {
            $next_a.addClass('disabled');
        } else {
            $next_a.removeClass('disabled');
        }
    }
    
    function center_photo(el) {
        // attempt to center the specifies .photo div
        
        $el = $(el);
        width = $el.outerWidth();
                
        left = $el.position().left;
        half = width/2;
        
        offset = (left+half-visible_width/2);
        if (offset < 0) {
            offset = 0;
        }
                
        if (offset > max_offset) {
            offset = max_offset;
        }
        margin_left = offset;   
        
        $inner.css('margin-left', -offset+'px');
        check_scroll_edges();
    };
    
    function scroll_inc(dir) {
        // scroll one .photo forward or backward 
        // (based on dir which must be 1 for forwards and -1 for backwards) 
        
        offset = margin_left + dir*$photos.outerWidth();
        
        if (offset < 0) {
            offset = 0
        }
        
        if (offset > max_offset) {
            offset = max_offset;
        }
        
        margin_left = offset;
        if (settings.animate) {
            $inner.animate({
                marginLeft: -offset+'px',                
            }, 500 );
        } else {
            $inner.css('margin-left', -offset+'px');
        }
        check_scroll_edges();
    }
    
    function scroll_next() {        
        scroll_inc(settings.scroll_inc);
        return false;
    }
    
    function scroll_previous() {
        scroll_inc(-settings.scroll_inc);
        return false;
    }
        
    // center the selected photo
    $photos_div.find('.selected').each(function(i, el) {
       center_photo(el);
    });
    
    $previous_a.click(scroll_previous);
    $next_a.click(scroll_next);
    
  return this;
}
