﻿//----------------------------------------------
//Rolling v1.0 Source By Bermann
//dobermann75@gmail.com
//----------------------------------------------

document.write("<script type='text/javascript' src='/js/min.rico.js'><\/script>");

var RollingEffect = Class.create();
RollingEffect.prototype = {
    initialize: function(element) {
        this.animator = new Rico.Effect.Animator();
        this.element = $(element);
        this.startLeft = this.element.offsetLeft;
        this.startSize = this.element.offsetWidth;
    },
    fadeEffectStarted: false,
    play: function(effect) {
        this.animator.play(effect, {steps:20, duration:400});
    },
    toggleFade: function(){
        this.play(new Rico.Effect.FadeTo(this.element, this.nextFadeState()));
    },
    fadeIn: function(){
        this.play(new Rico.Effect.FadeTo(this.element, 1));
    },
    fadeOut: function(){
        this.play(new Rico.Effect.FadeTo(this.element, 0));
    },
    nextFadeState: function(){
        this.fadeEffectStarted = !this.fadeEffectStarted;
        return !this.fadeEffectStarted ? 1 : 0;
    }
};
/*
function Rolling(canvas,width,height,direction,pixelgap,timegap,idlegap) {
    this.canvas = canvas;
    this.width = width;
    this.height = height;
    this.direction = direction; // 0: no effect, 1: left, 2: right, 3: up, 4: down, 5: fadeinout
    this.pixelgap = pixelgap;
    this.timegap = timegap;
    this.idlegap = idlegap;
    this.navi = null;
    this.isNavi = false;
    this.box = document.createElement("DIV");
    this.box.style.zIndex = 0;
    this.box.style.width = this.width + "px";
    this.box.style.height = this.height + "px";
    this.box.style.overflow = "hidden";
    this.setTimeOut = null;
    this.current = 0;
    this.next = null;
    this.count = 0;
    this.link = {};
    this.linkElement = null;
    this.isLink = false;
    this.degree = 0;
}
*/

var Rolling = Class.create();
Rolling.prototype = {
    box: null,
    initialize: function(canvas,width,height,direction,pixelgap,timegap,idlegap) {
        this.canvas = canvas;
        this.width = width;
        this.height = height;
        this.direction = direction; // 0: no effect, 1: left, 2: right, 3: up, 4: down, 5: fadeinout
        this.pixelgap = pixelgap;
        this.timegap = timegap;
        this.idlegap = idlegap;
        this.navi = null;
        this.isNavi = false;
        this.box = document.createElement("DIV");
        this.box.style.zIndex = 0;
        this.box.style.width = this.width + "px";
        this.box.style.height = this.height + "px";
        this.box.style.overflow = "hidden";
        this.setTimeOut = null;
        this.current = 0;
        this.next = null;
        this.count = 0;
        this.link = {};
        this.linkElement = null;
        this.isLink = false;
        this.degree = 0;
    },

    Init: function () {
        var This = this;
        var temp = null;

        this.box.onmouseover = function () { window.clearTimeout(This.setTimeOut); }
        this.box.onmouseout = function () { This.setTimeOut = window.setTimeout(function () { This.Run(); },This.timegap); }
        this.canvas.appendChild(this.box);
        this.box.style.position = "absolute";
        for (var i = 0; i < this.box.childNodes.length; i++) {
            this.box.childNodes[i].style.position = "absolute";
            //this.box.childNodes[i].style.overflow = "hidden";
            //this.box.childNodes[i].style.width = this.width;
            //this.box.childNodes[i].style.height = this.height;
            if (i > 0) {
                if (this.direction == 0) {
                    this.box.childNodes[i].style.display = "none";
                } else if (this.direction == 1) {
                    this.box.childNodes[i].style.left = (this.box.childNodes[i - 1].offsetLeft + this.box.childNodes[i - 1].offsetWidth) + "px";
                    if ((this.box.childNodes[i - 1].offsetLeft + this.box.childNodes[i - 1].offsetWidth) < this.width) this.box.childNodes[i].style.left = this.width;
                } else if (this.direction == 2) {
                    this.box.childNodes[i].style.left = (this.box.childNodes[i - 1].offsetLeft - this.box.childNodes[i - 1].offsetWidth) + "px";
                    if (this.box.childNodes[i - 1].offsetWidth < this.width) this.box.childNodes[i].style.left = 0 - this.width;
                    if (this.box.childNodes[i].offsetWidth > this.width) this.box.childNodes[i].style.left = 0 - this.box.childNodes[i].offsetWidth;
                } else if (this.direction == 3) {
                    this.box.childNodes[i].style.top = (this.box.childNodes[i - 1].offsetTop + this.box.childNodes[i - 1].offsetHeight) + "px";
                } else if (this.direction == 4) {
                    this.box.childNodes[i].style.top = (this.box.childNodes[i - 1].offsetTop - this.box.childNodes[i - 1].offsetHeight) + "px";
                } else if (this.direction == 5) {
                    //this.box.childNodes[i].style.filter = "alpha(opacity=0)";
                    this.box.childNodes[i].style.display = "none";
                    temp = new RollingEffect(this.box.childNodes[i].id);
                    temp.fadeOut();
                }
            }
        }
    },

    Run: function () {
        var This = this;
        if (this.box.hasChildNodes() == false) { return; }

        if (this.direction == 0) { this.noEffect(); }
        else if (this.direction == 1) { this.moveLeft(); }
        else if (this.direction == 2) { this.moveRight(); }
        else if (this.direction == 3) { this.moveUp(); }
        else if (this.direction == 4) { this.moveDown(); }
        else if (this.direction == 5) { this.fadeInOut(); }
    },

    SetNavigation: function (space, onTags, offTags) {
        this.isNavi = true;
        var tempElement1, tempElement2, tempsrc1, tempsrc2;

        this.navi = document.getElementById(space);

        for (var i = 0; i < this.box.childNodes.length; i++) {
            tempElement1 = onTags;
            tempElement2 = offTags;

            tempElement1 = tempElement1.replaceAll("!@!", i + 1);
            this.navi.innerHTML += tempElement1;

            tempElement2 = tempElement2.replaceAll("!@!", i + 1);
            this.navi.innerHTML += tempElement2;
        }

        this.ActiveNavigation();
    },

    ActiveNavigation: function () {
        if (this.isNavi == true) {
            var index;

            for (var i = 0; i < this.box.childNodes.length; i++) {
                index = i * 2;
                if (this.navi.childNodes[index] != undefined) {
                    if (this.current == i) {
                        this.navi.childNodes[index].style.display = "";
                        this.navi.childNodes[index + 1].style.display = "none";
                    } else {
                        this.navi.childNodes[index].style.display = "none";
                        this.navi.childNodes[index + 1].style.display = "";
                    }
                }
            }
        }
    },

    SelectNode: function (s) {
        var This = this;
        window.clearTimeout(this.setTimeOut);

        this.current = s - 2;

        if (this.direction == 0) {
            this.count = this.idlegap;
        } else if (this.direction == 1) {
            this.box.childNodes[this.current].style.left = (this.box.childNodes[this.current - 1].offsetLeft + this.box.childNodes[this.current - 1].offsetWidth) + "px";
        } else if (this.direction == 2) {
            this.box.childNodes[this.current].style.left = (this.box.childNodes[this.current - 1].offsetLeft - this.box.childNodes[this.current - 1].offsetWidth) + "px";
        } else if (this.direction == 3) {
            this.box.childNodes[this.current].style.top = (this.box.childNodes[this.current - 1].offsetTop + this.box.childNodes[this.current - 1].offsetHeight) + "px";
        } else if (this.direction == 4) {
            this.box.childNodes[this.current].style.top = (this.box.childNodes[this.current - 1].offsetTop - this.box.childNodes[this.current - 1].offsetHeight) + "px";
        } else if (this.direction == 5) {
            this.count = this.idlegap;
        }

        this.setTimeOut = window.setTimeout(function () { This.Run(); },this.timegap);
    },

    SetLink: function (eLink) {
        this.isLink = true;
        this.linkElement = document.getElementById(eLink);

        this.ActiveLink();
    },

    ActiveLink: function () {
        var This = this;

        if (this.isLink == true) {
            this.linkElement.style.cursor = "";
            this.linkElement.onclick = null;

            if (this.link.url[this.current] != undefined && this.link.url[this.current] != "")
            {
                this.linkElement.onclick = function() {
                    if (This.link.target[This.current] == "_blank") {
                        open(This.link.url[This.current]);
                    } else if (This.link.target[This.current] == "_top") {
                        top.location.href = This.link.url[This.current];
                    } else {
                        location.href = This.link.url[This.current];
                    }
                }
                This.linkElement.style.cursor = "pointer";
            }
        }
    },

    noEffect: function () {
        var This = this;
        if (this.current == null) { this.current = 0; }
        window.clearTimeout(this.setTimeOut);

        this.count += this.timegap;

        if (this.count >= this.idlegap) {
            if (this.current >= 0) {
                this.box.childNodes[this.current].style.display = "none";
            }

            this.next = (this.current + 1 >= this.box.childNodes.length) ? 0 : this.current + 1;
            this.current = (this.current + 1 >= this.box.childNodes.length) ? 0 : this.current + 1;

            this.box.childNodes[this.next].style.display = "";

            this.count = 0;

            for (var i = 0; i < this.box.childNodes.length; i++) {
                if (this.current != i) {
                    this.box.childNodes[i].style.display = "none";
                } else {
                    this.box.childNodes[i].style.display = "";
                }
            }

            this.ActiveNavigation();
            this.ActiveLink();
        }

        this.setTimeOut = window.setTimeout(function () { This.Run(); },this.timegap);
    },

    moveLeft: function () {
        var This = this;
        if (this.current == null) { this.current = 0; }
        window.clearTimeout(this.setTimeOut);
        for (var i = 0; i < this.box.childNodes.length; i++) {
            this.box.childNodes[i].style.left = (this.box.childNodes[i].offsetLeft - this.pixelgap) + "px";
        }

        this.next = (this.current + 1 >= this.box.childNodes.length) ? 0 : this.current + 1;
        if (this.box.childNodes[this.next].offsetLeft <= 0 && this.box.childNodes.length > 1) {
            var nextPosition = 0;
            for (var i = 1; i < this.box.childNodes.length; i++) { nextPosition += this.box.childNodes[i].offsetWidth; }

            if (nextPosition < this.box.childNodes[this.next].offsetWidth) nextPosition = this.box.childNodes[this.next].offsetWidth;
            if (nextPosition < this.width) nextPosition = this.width;

            this.box.childNodes[this.current].style.left = nextPosition + "px";
            this.current = (this.current + 1 >= this.box.childNodes.length) ? 0 : this.current + 1;
            this.setTimeOut = window.setTimeout(function () { This.Run(); },this.idlegap);
        } else if (this.box.childNodes.length == 1) {
            if (this.box.childNodes[0].offsetLeft < (0 - this.box.childNodes[0].offsetWidth)) {
                this.box.childNodes[0].style.left = this.width + "px";
                this.setTimeOut = window.setTimeout(function () { This.Run(); },this.timegap);
            } else if (this.box.childNodes[0].offsetLeft == 0) {
                this.setTimeOut = window.setTimeout(function () { This.Run(); },this.idlegap);
            } else {
                this.setTimeOut = window.setTimeout(function () { This.Run(); },this.timegap);
            }
        } else {
            this.setTimeOut = window.setTimeout(function () { This.Run(); },this.timegap);
        }
    },

    moveRight: function () {
        var This = this;
        if (this.current == null) { this.current = 0; }
        window.clearTimeout(this.setTimeOut);
        for (var i = 0; i < this.box.childNodes.length; i++) {
            this.box.childNodes[i].style.left = (this.box.childNodes[i].offsetLeft + this.pixelgap) + "px";
        }
        this.next = (this.current + 1 >= this.box.childNodes.length) ? 0 : this.current + 1;
        if (this.box.childNodes[this.next].offsetLeft >= 0 && this.box.childNodes.length > 1) {
            this.box.childNodes[this.current].style.left = -(this.box.childNodes[this.current].offsetWidth * (this.box.childNodes.length - 1)) + "px";
            if (this.box.childNodes[this.current].offsetWidth < this.width) this.box.childNodes[this.current].style.left = 0 - this.width;

            this.current = (this.current + 1 >= this.box.childNodes.length) ? 0 : this.current + 1;
            this.setTimeOut = window.setTimeout(function () { This.Run(); },this.idlegap);
        } else if (this.box.childNodes.length == 1) {
            if (this.box.childNodes[0].offsetLeft > this.width) {
                this.box.childNodes[0].style.left = (0 - this.box.childNodes[0].offsetWidth) + "px";
                this.setTimeOut = window.setTimeout(function () { This.Run(); },this.timegap);
            } else if (this.box.childNodes[0].offsetLeft == 0) {
                this.setTimeOut = window.setTimeout(function () { This.Run(); },this.idlegap);
            } else {
                this.setTimeOut = window.setTimeout(function () { This.Run(); },this.timegap);
            }
        }  else {
            this.setTimeOut = window.setTimeout(function () { This.Run(); },this.timegap);
        }
    },

    moveUp: function () {
        var This = this;
        if (this.current == null) { this.current = 0; }
        window.clearTimeout(this.setTimeOut);
        for (var i = 0; i < this.box.childNodes.length; i++) {
            this.box.childNodes[i].style.top = (this.box.childNodes[i].offsetTop - this.pixelgap) + "px";
        }
        this.next = (this.current + 1 >= this.box.childNodes.length) ? 0 : this.current + 1;
        if (this.box.childNodes[this.next].offsetTop <= 0) {
            var nextPosition = 0;
            for (var i = 1; i < this.box.childNodes.length; i++) { nextPosition += this.box.childNodes[i].offsetHeight; }
            this.box.childNodes[this.current].style.top = nextPosition + "px";
            this.current = (this.current + 1 >= this.box.childNodes.length) ? 0 : this.current + 1;
            this.setTimeOut = window.setTimeout(function () { This.Run(); },this.idlegap);
        } else {
            this.setTimeOut = window.setTimeout(function () { This.Run(); },this.timegap);
        }
    },

    moveDown: function () {
        var This = this;
        if (this.current == null) { this.current = 0; }
        window.clearTimeout(this.setTimeOut);
        for (var i = 0; i < this.box.childNodes.length; i++) {
            this.box.childNodes[i].style.top = (this.box.childNodes[i].offsetTop + this.pixelgap) + "px";
        }
        this.next = (this.current + 1 >= this.box.childNodes.length) ? 0 : this.current + 1;
        if (this.box.childNodes[this.next].offsetTop >= 0) {
            this.box.childNodes[this.current].style.top = -(this.box.childNodes[this.current].offsetHeight * (this.box.childNodes.length - 1)) + "px";
            this.current = (this.current + 1 >= this.box.childNodes.length) ? 0 : this.current + 1;
            this.setTimeOut = window.setTimeout(function () { This.Run(); },this.idlegap);
        } else {
            this.setTimeOut = window.setTimeout(function () { This.Run(); },this.timegap);
        }
    },

    fadeInOut: function () {
        var This = this;
        if (this.current == null) { this.current = 0; }
        window.clearTimeout(this.setTimeOut);

        var div = null;

        this.count += this.timegap;

        if (this.count >= this.idlegap) {
            if (this.current >= 0) {
                div = new RollingEffect(this.box.childNodes[this.current].id);
                div.fadeOut();
            }

            this.next = (this.current + 1 >= this.box.childNodes.length) ? 0 : this.current + 1;
            this.current = (this.current + 1 >= this.box.childNodes.length) ? 0 : this.current + 1;

            this.box.childNodes[this.next].style.display = "";
            div = new RollingEffect(this.box.childNodes[this.next].id);
            div.fadeIn();

            this.count = 0;

            for (var i = 0; i < this.box.childNodes.length; i++) {
                if (this.current != i) {
                    this.box.childNodes[i].style.display = "none";
                    div = new RollingEffect(this.box.childNodes[i].id);
                    div.fadeOut();
                }
            }

            this.ActiveNavigation();
            this.ActiveLink();
        }

        this.setTimeOut = window.setTimeout(function () { This.Run(); },this.timegap);
    }
}

String.prototype.replaceAll = function(searchStr, replaceStr) {
    var temp = this;
    while (temp.indexOf(searchStr) != -1) {
        temp = temp.replace(searchStr, replaceStr);
    }

    return temp;
}
