﻿function slider(obj, interval, duration, direction,type,rewind, loop, resize, center, defaultcontrols, tumbresize, tumbfind, tumbreplace,current, last, goto) {



     startanim();

    function startanim() {

        current = 0;
        last = 0;
        goto = 0;

        var styleDefaultPlayer = '';


        if (defaultcontrols == true) {
            var t = '';
            var l = '';
            t = ($(obj + ' div.items').innerHeight() - 48) / 2
            l = ($(obj + ' div.items').innerWidth() - 244) / 2

            styleDefaultPlayer = obj + ' .defaultplayer {position:absolute;z-index:12;top:' + t + 'px;left:' + l + 'px;display:none;width:244px;height:48px;background-image: url(http://www.aimfor.nl/js/images/fader/controller.png); background-repeat: no-repeat; background-position: center;} ' + obj + ' .next {float: left; width: 22px; height: 18px;background-image: url(http://www.aimfor.nl/js/images/fader/next.png); background-repeat: no-repeat;background-position: center; cursor: pointer; margin-right: 20px;}  ' + obj + ' .next:hover {background-image: url(http://www.aimfor.nl/js/images/fader/next-over.png);}' + obj + ' .previous {float: left; width: 22px; height: 18px; background-image: url(http://www.aimfor.nl/js/images/fader/previous.png);background-repeat: no-repeat;background-position: center;cursor: pointer;margin-right: 20px;} ' + obj + ' .previous:hover{background-image: url(http://www.aimfor.nl/js/images/fader/previous-over.png);} ' + obj + ' .first {float: left;  width: 13px; height: 18px; background-image: url(http://www.aimfor.nl/js/images/fader/first.png);background-repeat: no-repeat; background-position: center;cursor: pointer; margin-right: 20px;} ' + obj + ' .first:hover {background-image: url(http://www.aimfor.nl/js/images/fader/first-over.png);} ' + obj + ' .last { float: left;width: 13px; height: 18px;background-image: url(http://www.aimfor.nl/js/images/fader/last.png); background-repeat: no-repeat; background-position: center; cursor: pointer;} ' + obj + ' .last:hover {background-image: url(http://www.aimfor.nl/js/images/fader/last-over.png);}' + obj + ' .start {float: left; width: 15px; height: 18px; background-image: url(http://www.aimfor.nl/js/images/fader/start.png); background-repeat: no-repeat; background-position: center; cursor: pointer; margin-right: 20px;} ' + obj + ' .start:hover{ background-image: url(http://www.aimfor.nl/js/images/fader/start-over.png);} ' + obj + ' .stop {float: left;width: 15px; height: 18px; background-image: url(http://www.aimfor.nl/js/images/fader/stop.png); background-repeat: no-repeat; background-position: center; cursor: pointer; margin-right: 20px;} ' + obj + ' .stop:hover {background-image: url(http://www.aimfor.nl/js/images/fader/stop-over.png);} ' + obj + ' .defaultplayer-container{position: relative;top: 15px;left: 39px;}'
        }

        var float;
        if (direction == 'left' || direction == 'right') {

            float = 'float:left;';
        }
        else 
        {
            float = '';
        }

        var style;
        style = '<style type="text/css">' + obj + ' div.item {position:absolute;width:Inherit;height:Inherit;' +  float + ';top:0px} ' + styleDefaultPlayer + '</style>';
        $(obj).prepend(style);


        if (defaultcontrols == true) {

            $(obj + ' div.items').append("<div class=\"defaultplayer\"><div class=\"defaultplayer-container\"><div class=\"first\"></div><div class=\"previous\"></div><div class=\"start\"></div><div class=\"stop\"></div><div class=\"next\"></div><div class=\"last\"></div></div></div>")

        }

        $(obj + ' div.items').css("overflow", "hidden");
        $(obj).css("position", "relative");
        $(obj + ' div.items').css("position", "relative");
        $(obj + ' div.items').css("text-align", "left");

        $(obj + ' div.item').css("width", $(obj + ' div.items').css("width"));
        $(obj + ' div.item').css("height", $(obj + ' div.items').css("height"));



        $(obj + ' div.items div.item').each(function (i, value) {
            var dBox;
            var dir;

            switch (direction) {
                case 'left':
                    dBox = i * parseInt($(obj + ' div.items').css("width").replace('px', ''));
                    dir = "left";
                    
                    break;
                case 'right':
                    dBox = -(i * parseInt($(obj + ' div.items').css("width").replace('px', '')));
                    dir = "left";
                    break;
                case 'top':
                    dBox = (i * parseInt($(obj + ' div.items').css("height").replace('px', '')));
                    dir = "top";
                    break;
                case 'bottom':
                    dBox = -(i * parseInt($(obj + ' div.items').css("height").replace('px', '')));
                    dir = "top";
                    break;
            }

            $(this).css(dir, dBox + 'px');
        });


       last = $(obj + ' div.items div.item:last,' + obj + ' div.items div.item:last').index();
      

        if (parseInt(last) > 0) {
            goto = 1
        }

        if (resize == true) {
            $(obj + ' div.items div.item img').each(function () { $(this).animate({ width: '100%' }, 0) });
        }


        if (defaultcontrols == true) {

            $(obj + ' div.items').mouseenter(function () {

                $(obj + ' div.items div.defaultplayer').fadeIn(750).show();

            });

            $(obj + ' div.items').mouseleave(function () {

                $(obj + ' div.items div.defaultplayer').hide();
            });

        }


        if (last != 0) {
               switch (direction) {
                case 'left':
                    $(obj + ' div.items').touchwipe({
                        wipeLeft: function () { next(); },
                        wipeRight: function () { previous(); },
                        min_move_x: 20,
                        min_move_y: 20,
                        preventDefaultEvents: false
                    });
                    
                    break;
                case 'right':
                    $(obj + ' div.items').touchwipe({
                        wipeLeft: function () { previous(); },
                        wipeRight: function () { next(); },
                        min_move_x: 20,
                        min_move_y: 20,
                        preventDefaultEvents: false
                     });
                    break;
                case 'top':
                    $(obj + ' div.items').touchwipe({
                        wipeUp: function () { previous(); },
                        wipeDown: function () { next(); },
                        min_move_x: 20,
                        min_move_y: 20,
                        preventDefaultEvents: false
                     });
                    break;
                case 'bottom':
                    $(obj + ' div.items').touchwipe({
                        wipeUp: function () { next(); },
                        wipeDown: function () { previous(); },
                        min_move_x: 20,
                        min_move_y: 20,
                        preventDefaultEvents: false
                     });
                    break;

            }
            if ($(obj + ' .next').length > 0) {
                $(obj + ' .next').click(function () {
                    next();
                });
            }

            if ($(obj + ' .previous').length > 0) {
                $(obj + ' .previous').click(function () {
                    previous();
                });
            }

            if ($(obj + ' .start').length > 0) {
                if (loop == true) {
                    $(obj + ' .start').hide()
                }
                $(obj + ' .start').click(function () {
                    start();
                });
            }

            if ($(obj + ' .stop').length > 0) {
                if (loop == false) {
                    $(obj + ' .stop').hide()
                }
                $(obj + ' .stop').click(function () {
                    stop();
                });
            }

            if ($(obj + ' .first').length > 0) {
                $(obj + ' .first').click(function () {
                    first();
                });
            }

            if ($(obj + ' .last').length > 0) {
                $(obj + ' .last').click(function () {
                    golast();
                });
            }

            if (rewind == false) {
                $(obj + ' div.item:first').clone().appendTo(obj + ' div.items');


                switch (direction) {
                    case 'left':
                  
                        $(obj + ' div.item:last').css("left", ((last + 1) * parseInt($(obj + ' div.items').css("width").replace('px', ''))));

                        break;
                    case 'right':
                        $(obj + ' div.item:last').css("left", - ((last + 1) * parseInt($(obj + ' div.items').css("width").replace('px', ''))));
                        break;
                    case 'top':
                        $(obj + ' div.item:last').css("top", ((last + 1) * parseInt($(obj + ' div.items').css("height").replace('px', ''))));
                        break;
                    case 'bottom':
                        $(obj + ' div.item:last').css("top", - ((last + 1) * parseInt($(obj + ' div.items').css("height").replace('px', ''))));
                        break;
                }

                
               
            }


            if ($(obj + ' .paging').length > 0) {
                var pageItem = $(obj + ' .paging').html();
                var html = '';
                var img =  obj + ' div.items div.item img'
                var pre = tumbreplace;
                var rtumb = tumbresize;
                var path = tumbfind;


                $(obj + ' div.items div.item').each(function (i, value) {
                    if (i <= last) {
                        var tumb;
                        tumb = $(img).eq(i).attr("src");

                        if (pre != '') {
                            tumb = tumb.replace(tumbfind, pre)
                        }

                        html = html + pageItem.replace(/\[i\]/g, (i + 1)).replace(/\[title\]/g, $(this).attr("title")).replace(/\[tumb\]/g, tumb);
                    }
                });

                $(obj + ' .paging').html(html);
                $(obj + ' div.paging div.page').each(function (i) { $(this).click(function () { gotopage(i); }); });

                $(obj + ' div.paging div.page').eq(current).removeClass('page').addClass('pageactive');

                if (rtumb == true) {
                    $(obj + ' div.paging div.pageactive img,' + obj + ' div.paging div.page img').each(function () { $(this).animate({ width: '100%' }, 0) });

                    if (center == true) {
                        // $(obj + ' div.paging div.pageactive img,' + obj + ' div.paging div.page img').each(function () { setPos($(this).width(),$(this).height() });
                    }

                }
            }

            if (center == true) {
                $(obj + ' div.items div.item img').load(function () { setPos($(this).width(), $(this).height(), $(obj + ' div.items div.item')); });

            }

            if ($(obj + ' .meter').length > 0 && loop == true) {

                $(obj + ' .meter').animate({ width: 0 }, 0).animate({ width: '100%' }, interval, 'linear', function () { if (loop == true) { nextitem(); } });
            }
            else {
                if (loop == true) {
                    $(obj).animate({ opacity: 1 }, interval, 'linear', function () { nextitem(); });
                }
            }
        }
 
    }

    function nextitem() {

        if (rewind == false && current == last) {
            goto = last + 1;
            
            
        }
        var pixels;
        switch (direction) {
            case 'left':
                pixels = (goto - current) * parseInt($(obj + ' div.items').css("width").replace('px', ''));
                $(obj + " div.items > .item").animate({ left: '-=' + pixels }, { duration: duration, easing: getEasing(type) });
                break;
            case 'right':
                pixels = (goto - current) * parseInt($(obj + ' div.items').css("width").replace('px', ''));
                $(obj + " div.items > .item").animate({ left: '+=' + pixels }, { duration: duration, easing: getEasing(type) });
                break;
            case 'top':
                pixels = (goto - current) * parseInt($(obj + ' div.items').css("height").replace('px', ''));
                $(obj + " div.items > .item").animate({ top: '-=' + pixels }, { duration: duration, easing: getEasing(type) });
                break;
            case 'bottom':
                pixels = (goto - current) * parseInt($(obj + ' div.items').css("height").replace('px', ''));
                $(obj + " div.items > .item").animate({ top: '+=' + pixels }, { duration: duration, easing: getEasing(type) });
                break;
        }


        if ($(obj + ' .paging').length > 0) {
            if (rewind == false && current == last) {
                goto= 0;
            }
            $(obj + ' div.paging div.pageactive').removeClass('pageactive').addClass('page');
            $(obj + ' div.paging div.page').eq(goto).removeClass('page').addClass('pageactive');
            if (rewind == false && current == last) {
                goto = last + 1;
            }
        }

        if (center == true) {

            setPos($next.width(), $next.height(), $next);
        }

        current = goto;

        if ((parseInt(goto) + 1) <= last) {
            goto = parseInt(goto) + 1;
        }
        else {
            current = goto;
            goto = 0;

        }

        if ($(obj + ' .meter').length > 0 && loop == true) {

            $(obj + ' .meter').animate({ width: 0 }, 0).animate({ width: '100%' }, interval, 'linear', function () { if (loop == true) { resetResize(); nextitem(); } })
        }
        else {
            if (loop == true) {
                $(obj).animate({ opacity: 1 }, interval, 'linear', function () { resetResize(); nextitem(); });
            }
        }
    }

    function getEasing(t) {
        if (t != 'Random') {
            return t;
        }
        else {
            var Effects = "swing,linear,easeInQuad,easeOutQuad,easeInOutQuad,easeInCubic,easeOutCubic,easeInOutCubic,easeInQuart,easeOutQuart,easeInOutQuart,easeInQuint,easeOutQuint,easeInOutQuint,easeInSine,easeOutSine,easeInOutSine,easeInExpo,easeOutExpo,easeInOutExpo,easeInCirc,easeOutCirc,easeInOutCirc,easeInElastic,easeOutElastic,easeInOutElastic,easeInBack,easeOutBack,easeInOutBack,easeInBounce,easeOutBounce,easeInOutBounce";
            var aEffects = Effects.split(",");
            var easing = aEffects[parseInt(Math.random() * 27)];
            return easing;


        }


    }



    function resetResize() {
        if (rewind == false && current == (last + 1)) {
            current = 0;
            goto = 1;
            $(obj + ' div.items div.item').each(function (i, value) {
                var dBox;
                var dir;

                switch (direction) {
                    case 'left':
                        dBox = i * parseInt($(obj + ' div.items').css("width").replace('px', ''));
                        dir = "left";

                        break;
                    case 'right':
                        dBox = -(i * parseInt($(obj + ' div.items').css("width").replace('px', '')));
                        dir = "left";
                        break;
                    case 'top':
                        dBox = (i * parseInt($(obj + ' div.items').css("height").replace('px', '')));
                        dir = "top";
                        break;
                    case 'bottom':
                        dBox = -(i * parseInt($(obj + ' div.items').css("height").replace('px', '')));
                        dir = "top";
                        break;
                }

                $(this).css(dir, dBox + 'px');
            });
        }

    }

    function next() {
        if ($(obj + ' .meter').length > 0 && loop == true) {
            $(obj + ' .meter').stop();
        }
        else {
            if (loop == true) {
                $(obj).stop();
            }
        }

        nextitem();
    }

    function previous() {
        if ($(obj + ' .meter').length > 0 && loop == true) {
            $(obj + ' .meter').stop();
        }
        else {
            if (loop == true) {
                $(obj).stop();
            }
        }

        if (current - 1 < 0) {
            goto = last;
        }
        else {
            goto = current - 1;
        }
        nextitem();
    }

    function first() {
        if ($(obj + ' .meter').length > 0 && loop == true) {
            $(obj + ' .meter').stop();
        }
        else {
            if (loop == true) {
                $(obj).stop();
            }
        }
        goto = 0;
        nextitem();
    }

    function gotopage(i) {
        if ($(obj + ' .meter').length > 0 && loop == true) {
            $(obj + ' .meter').stop();
        }
        else {
            if (loop == true) {
                $(obj).stop();
            }
        }
        goto = i;
        nextitem();
    }

    function golast() {
        if ($(obj + ' .meter').length > 0 && loop == true) {
            $(obj + ' .meter').stop();
        }
        else {
            if (loop == true) {
                $(obj).stop();
            }
        }
        goto = last;
        nextitem();
    }

    function start() {
        $(obj + ' .stop').show();
        $(obj + ' .start').hide();
        $(obj + ' .meter').show();
        loop = true;
        nextitem();
    }

    function stop() {
        $(obj + ' .stop').hide();
        $(obj + ' .start').show();

        if ($(obj + ' .meter').length > 0 && loop == true) {
            $(obj + ' .meter').stop();
            $(obj + ' .meter').hide();
        }
        else {
            if (loop == true) {
                $(obj).stop();
            }
        }
        loop = false;
    }
}

function setPos(w, h, o) {
    var top;
    var left;

    if (w < $(obj + ' div.items').innerWidth()) {
        left = ($(obj + ' div.items').innerWidth() - w) / 2;
        o.css("left", left);
    }
    if (h < $(obj + ' div.items').innerHeight()) {
        top = ($(obj + ' div.items').innerHeight() - h) / 2;
        o.css("top", top);
    }
}

(function ($) { $.fn.touchwipe = function (settings) { var config = { min_move_x: 20, min_move_y: 20, wipeLeft: function () { }, wipeRight: function () { }, wipeUp: function () { }, wipeDown: function () { }, preventDefaultEvents: true }; if (settings) $.extend(config, settings); this.each(function () { var startX; var startY; var isMoving = false; function cancelTouch() { this.removeEventListener('touchmove', onTouchMove); startX = null; isMoving = false } function onTouchMove(e) { if (config.preventDefaultEvents) { e.preventDefault() } if (isMoving) { var x = e.touches[0].pageX; var y = e.touches[0].pageY; var dx = startX - x; var dy = startY - y; if (Math.abs(dx) >= config.min_move_x) { cancelTouch(); if (dx > 0) { config.wipeLeft() } else { config.wipeRight() } } else if (Math.abs(dy) >= config.min_move_y) { cancelTouch(); if (dy > 0) { config.wipeDown() } else { config.wipeUp() } } } } function onTouchStart(e) { if (e.touches.length == 1) { startX = e.touches[0].pageX; startY = e.touches[0].pageY; isMoving = true; this.addEventListener('touchmove', onTouchMove, false) } } if ('ontouchstart' in document.documentElement) { this.addEventListener('touchstart', onTouchStart, false) } }); return this } })(jQuery);
