/*
 * jScroller2 1.61 - Scroller Script
 *
 * Copyright (c) 2008 Markus Bordihn (markusbordihn.de)
 * Dual licensed under the MIT (MIT-LICENSE.txt)
 * and GPL (GPL-LICENSE.txt) licenses.
 *
 * $Date: 2009-07-16 18:00:00 +0100 (Thu, 16 July 2009) $
 * $Rev: 1.61 $
 */

var ByRei_jScroller2 = {
    info: {
        Name: "ByRei jScroller2",
        Version: 1.61,
        Author: "Markus Bordihn (http://markusbordihn.de)",
        Description: "Next Generation Autoscroller"
    },
    config: {
        refreshtime: 150,
        regExp: {
            px: /([0-9,.\-]+)px/
        }
    },
    obj: [],
    cache: {
        active: false,
        prefix: 'jscroller2' + '_',
        delayer: 0,
        ileave: 0,
        ie: window.detachEvent ? true : false,
        last: {
            element: false
        }
    },
    get: {
        value: function (a, b, c) {
            var i, result = '',
                il = a.length;
            if (a && b) {
                for (i = 0; i < il; i++) {
                    if (c && a[i].indexOf(b) >= 0) {
                        result = a[i].split(b)[1];
                        break
                    } else if (a[i] === b) {
                        result = a[i];
                        break
                    }
                }
            }
            return result
        },
        px: function (a) {
            var b = "";
            if (a) {
                if (a.match(ByRei_jScroller2.config.regExp.px)) {
                    if (typeof a.match(ByRei_jScroller2.config.regExp.px)[1] !== 'undefined') {
                        b = a.match(ByRei_jScroller2.config.regExp.px)[1]
                    }
                }
            }
            return parseFloat(b)
        },
        endless: function (a, b, c, d, e, f, g, h, i) {
            var j;
            switch (a) {
            case 'down':
            case 'right':
                j = ByRei_jScroller2.get.px(d.style[b]) + h;
                if (c > 0 && c <= f) {
                    ByRei_jScroller2.set[b](d, c - g)
                }
                if (j + g >= f && j <= f + h) {
                    ByRei_jScroller2.set[b](d, j);
                    c = j + e * -1
                }
                return c;
            case 'up':
            case 'left':
                j = ByRei_jScroller2.get.px(d.style[b]) - h;
                if (c + e <= f) {
                    ByRei_jScroller2.set[b](d, c + e)
                }
                if (j + g <= f && j + g + h >= 0) {
                    ByRei_jScroller2.set[b](d, j);
                    c = j + g
                }
                return c
            }
        }
    },
    on: {
        blur: function () {
            if (ByRei_jScroller2.cache.last.element && ByRei_jScroller2.cache.last.element !== document.activeElement) {
                ByRei_jScroller2.cache.last.element = document.activeElement
            } else {
                ByRei_jScroller2.stop()
            }
        },
        focus: function () {
            ByRei_jScroller2.start()
        },
        delay: function (a) {
            if (a > 0) {
                for (var i = 0; i < ByRei_jScroller2.obj.length; i++) {
                    if (a === ByRei_jScroller2.obj[i][3].delay) {
                        ByRei_jScroller2.obj[i][3].pause = ByRei_jScroller2.obj[i][3].delay = 0
                    }
                }
            }
        },
        over: function (a) {
            if (a) {
                ByRei_jScroller2.start_stop(a, 1)
            }
        },
        out: function (a) {
            if (a) {
                ByRei_jScroller2.start_stop(a, 0)
            }
        }
    },
    set: {
        left: function (a, b) {
            ByRei_jScroller2._style(a, 'left', b + "px")
        },
        top: function (a, b) {
            ByRei_jScroller2._style(a, 'top', b + "px")
        },
        width: function (a, b) {
            ByRei_jScroller2._style(a, 'width', b + "px")
        },
        height: function (a, b) {
            ByRei_jScroller2._style(a, 'height', b + "px")
        }
    },
    init: {
        main: function () {
            var a = document.getElementsByTagName('div'),
                il = a.length,
                i;
            for (i = 0; i < il; i++) {
                var b = a[i].className.split(' '),
                    direction = null;
                if (ByRei_jScroller2.get.value(b, ByRei_jScroller2.cache.prefix + 'down')) {
                    direction = 'down'
                } else if (ByRei_jScroller2.get.value(b, ByRei_jScroller2.cache.prefix + 'up')) {
                    direction = 'up'
                } else if (ByRei_jScroller2.get.value(b, ByRei_jScroller2.cache.prefix + 'left')) {
                    direction = 'left'
                } else if (ByRei_jScroller2.get.value(b, ByRei_jScroller2.cache.prefix + 'right')) {
                    direction = 'right'
                }
                if (direction) {
                    ByRei_jScroller2.add(a[i], direction)
                }
            }
            if (!ByRei_jScroller2.active) {
                if (ByRei_jScroller2.obj.length > 0) {
                    ByRei_jScroller2.start();
                    if (ByRei_jScroller2.cache.delayer) {
                        for (i = 0; i < ByRei_jScroller2.obj.length; i++) {
                            if (ByRei_jScroller2.obj[i][3].delay > 0) {
                                window.setTimeout("ByRei_jScroller2.on.delay(" + ByRei_jScroller2.obj[i][3].delay + ",0)", ByRei_jScroller2.obj[i][3].delay)
                            }
                        }
                    }
                    if (ByRei_jScroller2.cache.ileave === 0) {
                        if (ByRei_jScroller2.cache.ie) {
                            ByRei_jScroller2.cache.last.element = document.activeElement;
                            ByRei_jScroller2.set_eventListener(document, 'focusout', ByRei_jScroller2.on.blur)
                        } else {
                            ByRei_jScroller2.set_eventListener(window, 'blur', ByRei_jScroller2.on.blur)
                        }
                        ByRei_jScroller2.set_eventListener(window, 'focus', ByRei_jScroller2.on.focus);
                        ByRei_jScroller2.set_eventListener(window, 'resize', ByRei_jScroller2.on.focus);
                        ByRei_jScroller2.set_eventListener(window, 'scroll', ByRei_jScroller2.on.focus)
                    }
                }
            }
        }
    },
    add: function (a, b) {
        var i, il = ByRei_jScroller2.obj.length,
            error = false;
        if (a && b) {
            if (il > 0) {
                for (i = 0; i < il; i++) {
                    if (ByRei_jScroller2.obj[i][1].obj === a) {
                        ByRei_jScroller2.obj[i][3].direction = b;
                        error = true
                    }
                }
            }
        } else {
            error = true
        }
        if (!error) {
            var c = 0,
                speed = 1,
                pause = 0,
                alternate, dynamic, classNames = a.className.split(' '),
                parent = a.parentNode,
                endless = {
                    obj: null,
                    width: null,
                    height: null
                };
            if (parent.className.indexOf('jscroller2') >= 0) {
                parent = parent.parentNode
            }
            if (parent) {
                ByRei_jScroller2._style(parent, 'position', 'relative');
                ByRei_jScroller2._style(parent, 'overflow', 'hidden');
                var d = parent.getElementsByTagName('div');
                for (i = 0; i < d.length; i++) {
                    if (ByRei_jScroller2.get.value(d[i].className.split(' '), ByRei_jScroller2.cache.prefix + b + '_endless')) {
                        endless.obj = d[i]
                    }
                }
                if (a) {
                    ByRei_jScroller2._style(a, 'position', 'absolute');
                    ByRei_jScroller2.set.top(a, 0);
                    ByRei_jScroller2.set.left(a, 0);
                    switch (b) {
                    case "down":
                        ByRei_jScroller2.set.top(a, (a.clientHeight * -1) + parent.clientHeight);
                        break;
                    case "right":
                        ByRei_jScroller2.set.left(a, (a.clientWidth * -1) + parent.clientWidth);
                        break
                    }
                    switch (b) {
                    case "down":
                    case "up":
                        ByRei_jScroller2.set.width(a, parent.clientWidth);
                        break;
                    case "right":
                    case "left":
                        ByRei_jScroller2.set.height(a, parent.clientHeight);
                        break
                    }
                    if (endless.obj) {
                        ByRei_jScroller2._style(endless.obj, 'position', 'absolute');
                        endless.width = endless.obj.clientWidth;
                        endless.height = endless.obj.clientHeight;
                        switch (b) {
                        case "down":
                            ByRei_jScroller2.set.top(endless.obj, endless.height * -1);
                            break;
                        case "up":
                            ByRei_jScroller2.set.top(endless.obj, a.clientHeight);
                            break;
                        case "left":
                            ByRei_jScroller2.set.left(endless.obj, a.clientWidth);
                            break;
                        case "right":
                            ByRei_jScroller2.set.left(endless.obj, a.clientWidth * -1);
                            break
                        }
                        switch (b) {
                        case "down":
                        case "up":
                            ByRei_jScroller2.set.left(endless.obj, 0);
                            ByRei_jScroller2.set.width(endless.obj, parent.clientWidth);
                            break;
                        case "left":
                        case "right":
                            ByRei_jScroller2.set.top(endless.obj, 0);
                            ByRei_jScroller2.set.height(endless.obj, parent.clientHeight);
                            break
                        }
                    }
                }
                if (ByRei_jScroller2.get.value(classNames, ByRei_jScroller2.cache.prefix + 'speed-', 1)) {
                    speed = parseFloat(ByRei_jScroller2.get.value(classNames, ByRei_jScroller2.cache.prefix + 'speed-', 1) || 10) / 10;
                    if (ByRei_jScroller2.cache.ie && speed < 1) {
                        speed = 1
                    }
                }
                ByRei_jScroller2.cache.ileave = (ByRei_jScroller2.get.value(classNames, ByRei_jScroller2.cache.prefix + 'ignoreleave') || ByRei_jScroller2.cache.ileave === 1) ? 1 : 0;
                alternate = ByRei_jScroller2.get.value(classNames, ByRei_jScroller2.cache.prefix + 'alternate') ? 1 : 0;
                dynamic = ByRei_jScroller2.get.value(classNames, ByRei_jScroller2.cache.prefix + 'dynamic') ? 1 : 0;
                if (ByRei_jScroller2.get.value(classNames, ByRei_jScroller2.cache.prefix + 'delay-', 1)) {
                    ByRei_jScroller2.cache.delayer = pause = 1;
                    c = ByRei_jScroller2.get.value(classNames, ByRei_jScroller2.cache.prefix + 'delay-', 1) * 1000
                }
                if (ByRei_jScroller2.get.value(classNames, ByRei_jScroller2.cache.prefix + 'mousemove')) {
                    ByRei_jScroller2.set_eventListener(a, 'mouseover', ByRei_jScroller2.on.over);
                    ByRei_jScroller2.set_eventListener(a, 'mouseout', ByRei_jScroller2.on.out);
                    if (endless.obj) {
                        ByRei_jScroller2.set_eventListener(endless.obj, 'mouseover', ByRei_jScroller2.on.over);
                        ByRei_jScroller2.set_eventListener(endless.obj, 'mouseout', ByRei_jScroller2.on.out)
                    }
                }
                ByRei_jScroller2.obj.push([{
                    height: parent.clientHeight,
                    width: parent.clientWidth
                }, {
                    obj: a,
                    height: a.clientHeight,
                    width: a.clientWidth
                }, {
                    obj: endless.obj,
                    height: endless.height,
                    width: endless.width
                }, {
                    direction: b,
                    speed: speed,
                    pause: pause,
                    delay: c,
                    alternate: alternate,
                    dynamic: dynamic
                }])
            }
        }
    },
    remove: function (a) {
        if (a) {
            for (var i = 0; i < ByRei_jScroller2.obj.length; i++) {
                if (ByRei_jScroller2.obj[i][1].obj === a) {
                    ByRei_jScroller2.obj.splice(i, 1)
                }
            }
            if (ByRei_jScroller2.obj.length <= 0) {
                ByRei_jScroller2.stop()
            }
        }
    },
    scroller: function () {
        var i, il = ByRei_jScroller2.obj.length;
        for (i = 0; i < il; i++) {
            var a = ByRei_jScroller2.obj[i][0],
                child = ByRei_jScroller2.obj[i][1],
                endless = ByRei_jScroller2.obj[i][2],
                option = ByRei_jScroller2.obj[i][3];
            if (!option.pause && !option.delay) {
                if (option.dynamic) {
                    child.height = ByRei_jScroller2.obj[i][1].height = child.obj.clientHeight;
                    child.width = ByRei_jScroller2.obj[i][1].width = child.obj.clientWidth;
                    if (endless.obj) {
                        endless.height = ByRei_jScroller2.obj[i][2].height = endless.obj.clientHeight;
                        endless.width = ByRei_jScroller2.obj[i][2].width = endless.obj.clientWidth
                    }
                }
                switch (option.direction) {
                case 'down':
                case 'up':
                    var b = ByRei_jScroller2.get.px(child.obj.style.top);
                    b = (option.alternate === 2) ? ((option.direction === 'down') ? b - option.speed : b + option.speed) : ((option.direction === 'down') ? b + option.speed : b - option.speed);
                    if (endless.obj && !option.alternate) {
                        b = ByRei_jScroller2.get.endless(option.direction, 'top', b, endless.obj, child.height, a.height, endless.height, option.speed, option.alternate)
                    } else {
                        if (option.alternate) {
                            if (option.alternate === ((option.direction === 'down') ? 1 : 2) && ((child.height > a.height && b + option.speed > 0) || (child.height < a.height && b + child.height + option.speed > a.height))) {
                                ByRei_jScroller2.obj[i][3].alternate = ((option.direction === 'down') ? 2 : 1)
                            }
                            if (option.alternate === ((option.direction === 'down') ? 2 : 1) && ((child.height > a.height && b + child.height < a.height + option.speed) || (child.height < a.height && b < 0))) {
                                ByRei_jScroller2.obj[i][3].alternate = ((option.direction === 'down') ? 1 : 2)
                            }
                        } else {
                            if (option.direction === 'down') {
                                if (b > a.width) {
                                    b = (child.height) * -1
                                }
                            } else {
                                if (b < child.height * -1) {
                                    b = a.height
                                }
                            }
                        }
                    }
                    ByRei_jScroller2.set.top(child.obj, b);
                    break;
                case 'left':
                case 'right':
                    var c = ByRei_jScroller2.get.px(child.obj.style.left);
                    c = (option.alternate === 2) ? ((option.direction === 'left') ? c + option.speed : c - option.speed) : (option.direction === 'left') ? c - option.speed : c + option.speed;
                    if (endless.obj && !option.alternate) {
                        c = ByRei_jScroller2.get.endless(option.direction, 'left', c, endless.obj, child.width, a.width, endless.width, option.speed, option.alternate)
                    } else {
                        if (option.alternate) {
                            if (option.alternate === ((option.direction === 'left') ? 2 : 1) && ((child.width > a.width && c + option.speed > 0) || (child.width < a.width && c + child.width + option.speed > a.width))) {
                                ByRei_jScroller2.obj[i][3].alternate = ((option.direction === 'left') ? 1 : 2)
                            }
                            if (option.alternate === ((option.direction === 'left') ? 1 : 2) && ((child.width > a.width && c + child.width < a.width + option.speed) || (child.width < a.width && c - option.speed < 0))) {
                                ByRei_jScroller2.obj[i][3].alternate = ((option.direction === 'left') ? 2 : 1)
                            }
                        } else {
                            if (option.direction === 'left') {
                                if (c < child.width * -1) {
                                    c = a.width
                                }
                            } else {
                                if (c > a.width) {
                                    c = (child.width) * -1
                                }
                            }
                        }
                    }
                    ByRei_jScroller2.set.left(child.obj, c);
                    break
                }
            }
        }
    },
    start_stop: function (a, b) {
        if (a.target || a.srcElement) {
            var c = a.target ? a.target : a.srcElement;
            for (var i = 0; i < 5; i++) {
                if (c.className.indexOf(ByRei_jScroller2.cache.prefix + 'mousemove') < 0 && c.className.indexOf('_endless') < 0) {
                    c = c.parentNode
                } else {
                    break
                }
            }
            ByRei_jScroller2.pause(c, b)
        }
    },
    start: function () {
        if (!ByRei_jScroller2.timer) {
            ByRei_jScroller2.active = ByRei_jScroller2.timer = window.setInterval(ByRei_jScroller2.scroller, ByRei_jScroller2.config.refreshtime)
        }
    },
    stop: function () {
        if (ByRei_jScroller2.timer) {
            window.clearInterval(ByRei_jScroller2.timer);
            ByRei_jScroller2.active = ByRei_jScroller2.timer = false
        }
    },
    pause: function (a, b) {
        if (a && b >= 0) {
            for (var i = 0; i < ByRei_jScroller2.obj.length; i++) {
                if (a === ByRei_jScroller2.obj[i][1].obj || a === ByRei_jScroller2.obj[i][2].obj) {
                    ByRei_jScroller2.obj[i][3].pause = b
                }
            }
        }
    },
    _style: function (a, b, c) {
        if (a && b) {
            if (a.style) {
                if (typeof a.style[b] !== 'undefined') {
                    if (c) {
                        try {
                            return (a.style[b] = c)
                        } catch (e) {
                            return false
                        }
                    } else {
                        return (a.style[b] === '') ? ((a.currentStyle) ? a.currentStyle[b] : ((window.getComputedStyle) ? window.getComputedStyle(a, '').getPropertyValue(b) : false)) : a.style[b]
                    }
                }
            }
        }
    },
    set_eventListener: function (a, b, c) {
        if (a && b && c) {
            if (ByRei_jScroller2.cache.ie) {
                a.attachEvent("on" + b, c)
            } else {
                a.addEventListener(b, c, false)
            }
        }
    }
};
ByRei_jScroller2.set_eventListener(window, 'load', ByRei_jScroller2.init.main);
