﻿$.fn.gallery = function () {
    var speed = 1000;
    var easeType = "easeInOutExpo";
    var $gallery = this;
    var $itemContainer = $(this).find(".items");
    var _index = 0;
    var _timer = 0;

    function _scrollTo(index) {
        if (_timer) clearTimeout(_timer);
        var $items = $itemContainer.find("li");
        var galleryWidth = $gallery.innerWidth();
        var width = $items.outerWidth(true);
        if (index >= $items.length) index = 0;
        var x = (index >= 0) ? $items.eq(index).position().left : 0;
        $itemContainer
            .stop()
            .animate({ left: -x }, speed, easeType);

        _index = index;
        _timer = setTimeout(_scrollNext, 7000);
    };

    function _scrollNext() { _scrollTo(_index + 1); };

    $gallery.find(".thumbnails li").each(function (index) {
        var $this = $(this);
        $this.unbind("click").click(function () {
            _scrollTo(index);
        });
    });

    _timer = setTimeout(_scrollNext, 7000);
    return this;
};


//$(function () {
//    function _next() {
//        Slidebox('next');
//    }

//    var interval = 7000;
//    var timer = setInterval(_next, timer);
//});


////slide page to id
//function Slidebox(slideTo, autoPlay) {
//    var animSpeed = 1000; //animation speed
//    var easeType = 'easeInOutExpo'; //easing type
//    var sliderWidth = $('#slidebox').width();
//    var leftPosition = $('#slidebox .container').css("left").replace("px", "");
//    $("#slidebox .content").each(function (i) {
//        totalContent = i * sliderWidth;
//        $('#slidebox .container').css("width", totalContent + sliderWidth);
//    });
//    if (!$("#slidebox .container").is(":animated")) {
//        if (slideTo == 'next') { //next
//            if (autoPlay == 'stop') {
//                clearInterval(autoPlayTimer);
//            }
//            if (leftPosition == -totalContent) {
//                $('#slidebox .container').animate({ left: 0 }, animSpeed, easeType); //reset
//            } else {
//                $('#slidebox .container').animate({ left: '-=' + sliderWidth }, animSpeed, easeType); //next
//            }
//        } else if (slideTo == 'previous') { //previous
//            if (autoPlay == 'stop') {
//                clearInterval(autoPlayTimer);
//            }
//            if (leftPosition == '0') {
//                $('#slidebox .container').animate({ left: '-' + totalContent }, animSpeed, easeType); //reset
//            } else {
//                $('#slidebox .container').animate({ left: '+=' + sliderWidth }, animSpeed, easeType); //previous
//            }
//        } else {
//            var slide2 = (slideTo - 1) * sliderWidth;
//            if (leftPosition != -slide2) {
//                clearInterval(autoPlayTimer);
//                $('#slidebox .container').animate({ left: -slide2 }, animSpeed, easeType); //go to number
//            }
//        }
//    }
//}
