﻿///<reference path="./K2046.js" />
X.Transfer = {
    Attach: function(options) {
        if (!options) return;
        options.container = X.$(options.container);
        if (!options.container || !options.elements) return;
        X.Transfer.__Init(options);
        if (options.manual) return;
        X.Transfer.__DoEvent(options);
    }

    , __Init: function(options) {
        options.delay = options.delay || 10;
        options.timeout = options.timeout || 1000;
        options.step = options.step || 5;
        options.direction = options.direction || X.Transfer.Direction.Left;
        options.previous = options.previous || (options.elements.length - 1);
        options.index = options.index || 0;
        options.index = options.index >= options.elements.length ? 0 : options.index;

        options.width = options.elements[0].offsetWidth;
        options.height = options.elements[0].offsetHeight;
        options.container.style.position = "relative";
        options.container.style.overflow = "hidden";
        for (var x = 0; x < options.elements.length; x++) {
            options.elements[x].style.position = "absolute";
            if (x != options.index) {
                options.elements[x].style.left = options.width + "px";
                options.elements[x].style.top = options.height + "px";
            }
        }

        X.Events.Attach(options.container, "onmouseover", X.Transfer.Stop, options);
        X.Events.Attach(options.container, "onmouseout", X.Transfer.Start, options);

        if (options.numbers) {
            for (var x = 0; x < options.numbers.length; x++) {
                X.Events.Attach(options.numbers[x], "onmouseover", X.Transfer.Show, { options: options, index: x });
                X.Events.Attach(options.numbers[x], "onmouseout", X.Transfer.Start, options);
            }
        }
    }

    , Start: function(options) {
        options.stop = false;
        X.Transfer.__DoEvent(options);
    }

    , Stop: function(options) {
        clearTimeout(options.nexttimer);
        options.stop = true;
    }

    , Show: function(params) {
        var o = params.options;
        o.stop = true;
        if ((o.waiting || o.manual) && o.index == params.index) {
            clearTimeout(o.nexttimer);
            return;
        }
        if (o.index != params.index) {
            o.elements[o.previous].style.top = o.height + "px";
            o.elements[o.previous].style.left = o.width + "px";
            o.previous = o.index;
            o.index = params.index;
        }
        clearTimeout(o.delaytimer);
        o.elements[o.previous].style.top = "0px";
        o.elements[o.previous].style.left = "0px";
        o.elements[o.index].style.top = o.height + "px";
        o.elements[o.index].style.left = o.width + "px";
        X.Transfer.SetNumber(o, o.index);
        o.direction(o);
    }

    , __DoEvent: function(options) {
        if (options.stop || options.manual) return;
        options.waiting = true;
        options.nexttimer = setTimeout(function() {
            options.waiting = false;
            options.previous = options.index;
            options.index = options.index + 1;
            options.index = options.index >= options.elements.length ? 0 : options.index;
            options.elements[options.index].style.top = options.width + "px";
            options.elements[options.index].style.left = options.width + "px";
            X.Transfer.SetNumber(options, options.index);
            options.direction(options);
        }, options.timeout);
    }

    , Direction: {
        Left: function(options) {
            var c = options.elements[options.previous];
            var n = options.elements[options.index];
            var h = c.offsetWidth;
            var p = parseInt(c.style.left) || 0;
            var l = (h + p);
            var step = X.Transfer.__GetStep(l, options.step);
            n.style.top = "0px";
            clearTimeout(options.nexttimer);
            clearTimeout(options.delaytimer);
            if (step > 0) {
                c.style.left = (p - step) + "px";
                n.style.left = h + (p - step) + "px";
                options.delaytimer = setTimeout(function() { X.Transfer.Direction.Left(options); }, options.delay);
            }
            else {
                X.Transfer.__DoEvent(options);
            }
        }

        , Up: function(options) {
            var c = options.elements[options.previous];
            var n = options.elements[options.index];
            var h = c.offsetHeight;
            var p = parseInt(c.style.top) || 0;
            var l = (h + p);
            var step = X.Transfer.__GetStep(l, options.step);
            n.style.left = "0px";
            clearTimeout(options.nexttimer);
            clearTimeout(options.delaytimer);
            if (step > 0) {
                c.style.top = (p - step) + "px";
                n.style.top = h + (p - step) + "px";
                options.delaytimer = setTimeout(function() { X.Transfer.Direction.Up(options); }, options.delay);
            }
            else {
                X.Transfer.__DoEvent(options);
            }
        }

        , Right: function(options) {
            var c = options.elements[options.previous];
            var n = options.elements[options.index];
            var h = c.offsetWidth;
            var p = parseInt(c.style.left) || 0;
            var l = (h - p);
            var step = X.Transfer.__GetStep(l, options.step);
            n.style.top = "0px";
            clearTimeout(options.nexttimer);
            clearTimeout(options.delaytimer);
            if (step > 0) {
                c.style.left = (p + step) + "px";
                n.style.left = (p + step) - h + "px";
                options.delaytimer = setTimeout(function() { X.Transfer.Direction.Right(options); }, options.delay);
            }
            else {
                X.Transfer.__DoEvent(options);
            }
        }

        , Down: function(options) {
            var c = options.elements[options.previous];
            var n = options.elements[options.index];
            var h = c.offsetHeight;
            var p = parseInt(c.style.top) || 0;
            var l = (h - p);
            var step = X.Transfer.__GetStep(l, options.step);
            n.style.left = "0px";
            clearTimeout(options.nexttimer);
            clearTimeout(options.delaytimer);
            if (step > 0) {
                c.style.top = (p + step) + "px";
                n.style.top = (p + step) - h + "px";
                options.delaytimer = setTimeout(function() { X.Transfer.Direction.Down(options); }, options.delay);
            }
            else {
                X.Transfer.__DoEvent(options);
            }
        }

        , Random: function(options) {
            var direction = [
                X.Transfer.Direction.Left,
                X.Transfer.Direction.Up,
                X.Transfer.Direction.Right,
                X.Transfer.Direction.Down
            ];
            direction[X.Round(Math.random() * 10, 0) % direction.length](options);
        }
    }

    , Events: {
        OnBeforeTransfer: function(options) {
            if (options.before && typeof (options.before) == "function") {
                options.before(options);
            }
        }

        , OnComplete: function(options) {
            if (options.complete && typeof (options.complete) == "function") {
                options.complete(options);
            }
        }
    }

    , SetNumber: function(options) {
        if (!options.numbers) return;
        for (var x = 0; x < options.numbers.length; x++) {
            options.numbers[x].className = "normal";
        }
        options.numbers[options.index].className = "active";
    }

    , __GetStep: function(length, step) {
        var result = Math.abs(length) / step;
        result = result < 1 ? 1 : result;
        result = length < 0 ? -result : result;
        return result;
    }
};