﻿(function($) {
    $.fn.wslide = function(h) {
        h = jQuery.extend({
            width: 150,
            internalWidth: -1,
            height: 150,
            pos: 1,
            col: 1,
            visible: 1,
            effect: 'swing',
            fade: true,
            horiz: false,
            autolink: true,
            duration: 1500,
            numberOrder: 'normal',
            timer: 0,
            tdelay: 1000
        }, h);

        function gogogo(g) {

            if (h.timer == 1) {
                h.timer = setTimeout(doTimer, h.tdelay);
            }

            if (h.internalWidth == -1) {
                h.internalWidth = h.width;
            }

            function doTimer() {
                 g.find('a[href*=]').removeClass("act");
                if (h.length - h.visible + 1 == h.pos) {
                    h.pos++;
                    g.find('ul').css('top', h.startYpos + 'px').css('left', h.startXpos + 'px');
                }

                h.pos = (h.pos) % (h.length - h.visible + 1) + 1;
                var f = h.effect;
                if (jQuery.easing.easeInQuad == undefined && (f != 'swing' || f != 'normal')) {
                    f = 'swing'
                }

                var a = g;
                var e = a.attr('id');
                if (e == undefined) {
                    e = 'wslide' + h.pos
                }
                a = $('#' + e + '-' + h.pos);

                var c = a.attr('rel');

                if (!c) {
                    h.timer = setTimeout(doTimer, h.tdelay);
                    return;
                }

                c = c.split(':');
                var d = c[1];
                d = -d;
                c = c[0];
                c = -c;

                if (h.fade) {
                    a.parent().animate({
                        opacity: 0
                    },
                        h.duration / 2,
                        f,
                        function() {
                            $(this)
                            .css('top', d + 'px')
                            .css('left', c + 'px');
                            $(this).animate({
                                opacity: 1
                            }, h.duration / 2, f)
                        })
                }
                else {
                    a.parent().animate({
                        top: d + 'px',
                        left: c + 'px'
                    }, h.duration, f)
                }

                h.timer = setTimeout(doTimer, h.tdelay);
            }

            if (h.visible > 1) {
                // Copy first (h.visible - 1) elements from the begin into the end of the list
                var lst = g.find("li");
                for (var i = h.visible - 1; i >= 0; i--) {
                    $(lst[i]).clone(true).insertAfter($(lst[lst.length - 1]));
                }
            }
            h.length = g.find("li").length;

            g.each(function(i) {
                var a = $(this);
                var e = a.attr('id');
                if (e == undefined) {
                    e = 'wslide' + i
                }

                $(this).wrap('<div class="wslide-wrap" id="' + e + '-wrap"></div>');
                a = $('#' + e + '-wrap');
                var b = a.find('ul li');
                var f = h.effect;
                if (jQuery.easing.easeInQuad == undefined && (f != 'swing' || f != 'normal')) {
                    f = 'swing'
                }

                var g = h.width;
                var j = h.height;

                function resultante(a) {
                    var b = a;
                    b = b.split('px');
                    b = b[0];
                    return Number(b)
                }

                var k = h.internalWidth - (resultante(b.css('padding-left')) + resultante(b.css('padding-right')));
                var l = j - (resultante(b.css('padding-top')) + resultante(b.css('padding-bottom')));
                var m = h.col;

                if (h.horiz) {
                    m = Number(b.length + 1)
                }

                h.startXpos = 0;
                h.startYpos = -1;

                var n = '';
                var le = b.length;
                var o = Math.ceil(Number(b.length) / m);
                a.css('overflow', 'hidden')
                  .css('position', 'relative')
                  .css('text-align', 'left')
                  .css('height', j + 'px')
                  .css('width', g + 'px')
                  .css('margin', '0')
                  .css('padding', '0');
                a.find('ul').css('position', 'absolute')
                   .css('margin', '0').css('padding', '0')
                   .css('width', Number((m + 0) * g) + 'px')
                   .css('height', Number(o * j) + 'px');

                b.css('display', 'block')
                .css('overflow', 'hidden')
                .css('float', 'left')
                .css('height', l + 'px');

                if (h.visible == 1) {
                    b.css('width',  k + 'px');
                }

                b.each(function(i) {
                    var b = a.offset();
                    var c = $(this).offset();
                    t = $(this).attr("title");
                    var txt = t ? t : Number(i + 1);

                    $(this).attr('id', e + '-' + Number(i + 1))
                 .attr('rel', Number(c.left - b.left) + ':' + Number(c.top - b.top))
                 .attr('pos', Number(i + 1));
                    if (h.numberOrder == 'normal') {
                        n += ' <a href="#' + e + '-' + Number(i + 1) + '">' + txt + '</a>';
                    } else {
                        n = ' <a href="#' + e + '-' + Number(i + 1) + '">' + txt + '</a>' + n;
                    }
                });

                if (typeof h.autolink == 'boolean') {
                    if (h.autolink) {
                        a.after('<div class="wslide-menu" id="' + e + '-menu">' + n + '</div>')
                    }
                }
                else if (typeof h.autolink == 'string') {
                    if ($('#' + h.autolink).length) {
                        $('#' + h.autolink).html(n)
                    }
                    else {
                        a.after('<div id="#' + h.autolink + '">' + n + '</div>')
                    }
                }

                if (h.pos <= 0) {
                    h.pos = 1;
                }

                var cPos = h.pos;
                if (h.numberOrder != 'normal') {
                    cPos = le - h.pos + 1;
                }

                var p = '#' + e + '-';
                var q = "";

                $('a[href*="' + p + '"]').click(function() {
                    $('a[href*="' + q + '"]').removeClass("act");
                    $(this).addClass("act");

                    if (h.timer != 0) {
                        clearTimeout(h.timer);
                        h.timer = setTimeout(doTimer, h.tdelay);
                    }

                    var b = $(this).attr('href');
                    b = b.split('#');
                    b = '#' + b[1];
                    q = b;
                    var c = $(b).attr('rel');
                    c = c.split(':');
                    var d = c[1];
                    d = -d;
                    c = c[0];
                    c = -c;

                    h.pos = $(b).attr('pos');

                    if (h.fade) {
                        a.find('ul').animate({
                            opacity: 0
                        },
                        h.duration / 2,
                        f,
                        function() {
                            $(this)
                            .css('top', d + 'px')
                            .css('left', c + 'px');
                            $(this).animate({
                                opacity: 1
                            }, h.duration / 2, f)
                        })
                    }
                    else {
                        a.find('ul').animate({
                            top: d + 'px',
                            left: c + 'px'
                        }, h.duration, f)
                    }

                    return false;
                });

                $('a[href$="' + p + cPos + '"]').addClass("act");
                var r = $('a[href*="' + p + '"]:eq(' + Number(cPos - 1) + ')').attr('href');
                if (r) {
                    r = r.split('#');
                    r = '#' + r[1];
                    q = r;

                    var s = $(r).attr('rel');
                    s = s.split(':');
                    var t = s[1];
                    t = -t;
                    s = s[0];
                    s = -s;

                    h.startXpos = s;
                    h.startYpos = t;
                    a.find('ul').css('top', t + 'px').css('left', s + 'px');
                }
            })

        } gogogo(this);

        return this
    }
})(jQuery);