﻿function fader(obj, interval, duration, 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 style;
        style = '<style type="text/css">' + obj + ' div.item {display:none; position:absolute;width:Inherit;height:Inherit;text-align:center;} ' + obj + ' div.active {position:absolute; display:block;width:Inherit;height:Inherit;text-align:center;} ' + 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.active').css("width", $(obj + ' div.items').css("width"));


        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.active img,' + obj + ' div.items div.item img').each(function () { $(this).animate({ width: '100%' }, 0) });
        }


        if (defaultcontrols == true) {
  
            $(obj + ' div.items').mouseenter(function () {

                //if (isOver == false) {
                // isOver = true;
       
                $(obj + ' div.items div.defaultplayer').fadeIn(750).show();
                //}

            });

            $(obj + ' div.items').mouseleave(function () {
                //if (isOver == true) {
                //isOver = false;

                $(obj + ' div.items div.defaultplayer').hide();

                //}
            });

        }


        if (last != 0) {
            

            $(obj + ' div.items').touchwipe({
                wipeLeft: function () { previous() },
                wipeRight: function () { next() },
                min_move_x: 20,
                min_move_y: 20,
               
                preventDefaultEvents: false
            });

            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 ($(obj + ' .paging').length > 0) {
                var pageItem = $(obj + ' .paging').html();
                var html = '';
                var img = obj + ' div.items div.active img,' + obj + ' div.items div.item img'
                var pre = tumbreplace;
                var rtumb = tumbresize;
                var path = tumbfind;


                $(obj + ' div.items div.active,' + obj + ' div.items div.item').each(function (i, value) {
                    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')); });

                $(obj + ' div.items div.active img').load(function () { setPos($(this).width(), $(this).height(), $(obj + ' div.items div.active')); });
            }

            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() {
        
        var $active = $(obj + ' div.items div.active,' + obj + ' div.items div.item').eq(current);
        var $next = $(obj + ' div.items div.active,' + obj + ' div.items div.item').eq(goto);



        if ($(obj + ' .paging').length > 0) {
            $(obj + ' div.paging div.pageactive').removeClass('pageactive').addClass('page');
            $(obj + ' div.paging div.page').eq(goto).removeClass('page').addClass('pageactive');
        }

        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) { nextitem(); } })
        }
        else {
            if (loop == true) {
                $(obj).animate({ opacity: 1 }, interval, 'linear', function () { nextitem(); });
            }
        }

        $active.fadeOut(duration, 'linear').delay(duration).removeClass('active').addClass('item');
        $next.fadeIn(duration, 'linear').delay(duration).removeClass('item').addClass('active');

    }


    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);
