/*
 * 	Easy Slider - jQuery plugin
 *	written by Alen Grakalic	
 *	http://cssglobe.com/post/3783/jquery-plugin-easy-image-or-content-slider
 *
 *	Copyright (c) 2009 Alen Grakalic (http://cssglobe.com)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 */
/*
*	markup example for $("#images").easySlider();
*	
* 	<div id="images">
*		<ul>
*			<li><img src="images/01.jpg" alt="" /></li>
*			<li><img src="images/02.jpg" alt="" /></li>
*			<li><img src="images/03.jpg" alt="" /></li>
*			<li><img src="images/04.jpg" alt="" /></li>
*			<li><img src="images/05.jpg" alt="" /></li>
*		</ul>
*	</div>
*
*/

// aangepast door Ronan
var activeItem = 0;
var totalItems = 0;
(function($) {
    $.fn.easySlider = function(options) {

        // default configuration properties
        var defaults = {
            prevId: 'prevBtn',
            prevText: 'Previous',
            nextId: 'nextBtn',
            nextText: 'Next',
            orientation: '', //  'vertical' is optional;
            speed: 800,
            overrideWidth: 0,
            offset: 0,
            autoReturn: false, // auto return to start when last item is reached and user presses "next" anyway
            switchFunction: function() { }
        };

        var options = $.extend(defaults, options);

        return this.each(function() {
            var obj = $(this);
            var animated = false;
            var s = Math.ceil($("li", obj).length);
            var w = $("#slider").width();
            if (options.overrideWidth > 0)
                w = options.overrideWidth;
            var h = obj.height();
            var ts = s - 1;
            var t = 0;
            totalItems = ts;
            var vertical = (options.orientation == 'vertical');
            $("ul", obj).css('width', w * s);
            $("ul", obj).css('list-style-type', 'none');
            $("li", obj).css('width', w);
            if (!vertical) $("li", obj).css('float', 'left');

            $("#" + options.nextId).click(function() {
                if (t < ts || options.autoReturn)
                    animate("next");
            });
            $("#" + options.prevId).click(function() {
                if (t > 0 || options.autoReturn)
                    animate("prev");
            });

            if (!options.autoReturn)
                $("#" + options.prevId).fadeOut();

            // offset direct toepassen (scrollen naar een andere afbeelding dan de eerste)

            if (options.offset != 0 && !animated) {
                t = parseInt(options.offset) - 1;
                animate("next");
                animated = true;
            }

            function animate(dir) {
                if (dir == "next") {
                    if (t == ts)
                        t = 0;
                    else
                        t = (t >= ts) ? ts : t + 1;
                } else {
                    if (t == 0)
                        t = ts;
                    else
                        t = (t <= 0) ? 0 : t - 1;
                };
                activeItem = t;
                if (!vertical) {
                    p = (t * w * -1);
                    $("ul", obj).animate(
						{ marginLeft: p },
						options.speed, "", options.switchFunction()
					);
                } else {
                    p = (t * h * -1);
                    $("ul", obj).animate(
						{ marginTop: p },
						options.speed, "", options.switchFunction()
					);
                }

                if (!options.autoReturn) {
                    if (t == 0)
                        $("#" + options.prevId).fadeOut();
                    else
                        $("#" + options.prevId).fadeIn();

                    if (t == ts)
                        $("#" + options.nextId).fadeOut();
                    else
                        $("#" + options.nextId).fadeIn();
                }
            };
            //$("a", "#" + options.prevId).fadeIn();
            //$("a", "#" + options.nextId).fadeIn();
        });

    };

})(jQuery);
