/*
* scroller v1.0. - jQuery image gallery
* Copyright (c) 2011 Chris Yeung
*
* Dual licensed under the MIT and GPL licenses:
* 	http://www.opensource.org/licenses/mit-license.php
* 	http://www.gnu.org/licenses/gpl.html
*/

(function ($) {

    $.fn.imagegallery = $.fn.ImageGallery = function (options) {
        var totalPage = 0;
        var curPage = 1;
        var galleryWidth = 0;
        var galleryContainer = null;
        var gallery = null;
        var animating = false;
        var i = 0;

        var defaults = {  // default values
            pageSize: 3,
            animateSpeed: 'normal',
            prevButton: 'ig_prev',
            nextButton: 'ig_next',
            pageButton: ''
        };

        var options = $.extend(defaults, options);

        var methods = {
            init: function () {
                curPage = 1;
                galleryWidth = gallery.find('li:first').width() * options.pageSize;
                galleryContainer.css({
                    'overflow': 'hidden',
                    'width': galleryWidth,
                    'position': 'relative',
                    'top': '0px',
                    'left': '0px'
                });
                gallery.css({
                    'width': galleryWidth * totalPage + "px",
                    'position': 'relative',
                    'top': '0px',
                    'left': '0px'
                });
                // init prev/next button
                $("#" + options.prevButton).click(function () {
                    if (!animating) {
                        methods.ig_prevClick();
                    }
                });
                $("#" + options.nextButton).click(function () {
                    if (!animating) {
                        methods.ig_nextClick();
                    }
                });
                $("#" + options.pageButton).click(function () {

                });
            },
            goToPage: function (direction) {
                if (direction == "left") {
                    if (curPage > 1) {
                        curPage--;
                        animating = true;
                        gallery.animate({
                            left: "+=" + galleryWidth + "px"
                        }, options.animateSpeed, function () {
                            animating = false;
                        });
                    }
                } else if (direction == "right") {
                    if (curPage < totalPage) {
                        curPage++;
                        animating = true;
                        gallery.animate({
                            left: "-=" + galleryWidth + "px"
                        }, options.animateSpeed, function () {
                            animating = false;
                        });
                    }
                }
            },
            ig_prevClick: function () {
                methods.goToPage('left');
            },
            ig_nextClick: function () {
                methods.goToPage('right');
            }
        }

        if (this.find('ul li').size() > options.pageSize) {
            galleryContainer = this;
            gallery = $(this.find('ul')[0]);
            totalPage = Math.ceil(this.find('li').size() / options.pageSize);
            methods.init();
        }
    };

})(jQuery);
