if (home == undefined) {
    var home = {}
}

home.special = {}

home.special.config = {
    SPECIAL_WIDTH:      120,
    ROTATION_SPEED:     3000,
    DIRECTION_LEFT:     'left',
    DIRECTION_RIGHT:    'right',
    CURRENT_SPECIAL_NO:  2
}

home.special.board = function(container) {
    this.container      = jQuery(container);
    this.currentSpecial = null;
    this.container.children().css('opacity', 0);
    this.monitor();
}

home.special.board.prototype = {

    update: function(specialTag, speed) {
        this.unit = new home.special.unit(specialTag);
        this.switchTo(this.unit, speed);
    },

    monitor: function() {
        jQuery(this.container).children().hover(function() {
            rotate.stop();
        }, function() {
            rotate.start();
        });
    },

    switchTo: function(unit, speed) {
        var nextSpecial = this.container.find('li#board_' + unit.getName())[0];
        if (this.currentSpecial == null && this.currentSpecial == nextSpecial) {
            return;
        }

        jQuery(nextSpecial).animate({'opacity': 1}, speed).css({'z-index': 10});
        jQuery(this.currentSpecial).animate({'opacity': 0}, speed, 'easeOutExpo').css({'z-index': 1});
        this.currentSpecial = nextSpecial;
    }
}

home.special.unit = function(element) {
    this.element = element;
    this.linkTag = jQuery(this.element).find('a').first();
}

home.special.unit.prototype = {

    getName: function() {
        return this.linkTag.attr('id');
    }
}

home.special.arrow = function(element) {
    this.element = jQuery(element);
    this.unit    = null;
    this.monitor();
}

home.special.arrow.prototype = {

    monitor: function() {
        jQuery(this.element).hover(function() {
            rotate.stop();
        }, function() {
            rotate.start();
        });
    },

    moveTo: function(unit, specialNumLeftSide) {
        if (this.unit != null && this.unit.getName() == unit.getName()) {
            return;
        }
        arrow.unit = unit;
        jQuery(this.element).animate(
            {left: specialNumLeftSide * 120},
            10,
            function() {
                board.switchTo(unit);
            }
        );
    }
}

home.special.buttons = function(leftBtn, rightBtn) {
    this.leftBtn  = leftBtn;
    this.rightBtn = rightBtn;
    this.monitor();
}

home.special.buttons.prototype = {

    monitor: function() {
        this.monitorClick();
        this.monitorMouseout();
    },

    monitorClick: function() {
        jQuery(this.leftBtn).click(function() {
            if (rotate.isAnimating()) {
                return;
            }
            rotate.stop();
            rotate.turnToDirection(home.special.config.DIRECTION_RIGHT);
            rotate.speedUp();
            rotate.oneStep();
        });

        jQuery(this.rightBtn).click(function() {
            if (rotate.isAnimating()) {
                return;
            }
            rotate.stop();
            rotate.turnToDirection(home.special.config.DIRECTION_LEFT);
            rotate.speedUp();
            rotate.oneStep();
        });
    },

    monitorMouseout: function() {
        jQuery(this.rightBtn).mouseout(function() {
            rotate.start();
        });
        jQuery(this.leftBtn).mouseout(function() {
            rotate.start();
        });
    }
}

home.special.rotate = function(element) {
    this.element          = element;
    this.direction        = home.special.config.DIRECTION_LEFT;
    this.speed            = home.special.config.ROTATION_SPEED;
    this.currentSpecialNo = home.special.config.CURRENT_SPECIAL_NO;
    this.animating        = false;
    this.rotating         = false;
    this.monitor();
}

home.special.rotate.prototype = {

    monitor: function() {
        var _rotate = this;
        arrow.moveTo(new home.special.unit(jQuery(this.element).children().eq(_rotate.currentSpecialNo)), _rotate.currentSpecialNo);
        jQuery(this.element).children().hover(function() {
            if (_rotate.animating) {
                return;
            }
            _rotate.stop();
            _rotate.updateCurrentSpecialTo(this);
            arrow.moveTo(new home.special.unit(this), _rotate.currentSpecialNo);
        }, function() {
            _rotate.start();
        });
    },

    start: function() {
        if (this.rotating) {
            return;
        }
        this.rotating = true;
        this.speedDown();
        var _rotate = this;
        this.timer = jQuery.timer(this.speed, function() {
            _rotate.oneStep();
        });
    },

    oneStep: function() {
        if (this.animating) {
            return;
        }
        this.animating = true;
        if (this.direction == home.special.config.DIRECTION_LEFT) {
            this.runToLeft();
        } else {
            this.runToRight();
        }
    },

    isAnimating: function() {
        return this.animating;
    },

    updateCurrentSpecialTo: function(special) {
        var _rotate = this;
        jQuery(this.element).children().each(function(i, elem) {
            if (elem == special) {
                _rotate.currentSpecialNo = i;
            }
        });
    },

    runToLeft: function() {
        var firstElement = this.element.children().eq(0);
        jQuery(this.element).append(jQuery(firstElement).clone(true));
        this.slideMarginLeft(firstElement, -1 * home.special.config.SPECIAL_WIDTH, this.getElementAt(this.currentSpecialNo + 1), firstElement);
    },

    runToRight: function() {
        var childElements = jQuery(this.element).children();
        var lastElement = childElements.eq(childElements.length - 1);
        var newElement = jQuery(lastElement).clone(true);
        jQuery(newElement).css('margin-left', (-1 * home.special.config.SPECIAL_WIDTH) + 'px');
        jQuery(this.element).prepend(newElement);
        this.slideMarginLeft(newElement, 0, this.getElementAt(this.currentSpecialNo - 1), lastElement);
    },

    slideMarginLeft: function(element, marginLeft, nextSpecial, trashElement) {
        var _rotate = this;
        board.update(nextSpecial, _rotate.speed / 3);
        jQuery(element).animate(
            {marginLeft: marginLeft + 'px'},
            _rotate.speed / 3,
            'easeOutQuint',
            function() {
                trashElement.remove();
                _rotate.animating = false;
            }
        );
    },

    speedUp: function() {
        this.speed = 500;
    },

    speedDown: function() {
        this.speed = home.special.config.ROTATION_SPEED;
    },

    turnToDirection: function(direction) {
        this.direction = direction;
    },

    getCurrentElement: function() {
        return this.getElementAt(this.currentSpecialNo);
    },

    getElementAt: function(index) {
        return jQuery(this.element).children().eq(index);
    },

    stop: function() {
        this.timer.stop();
        this.rotating = false;
    }
}

var rotate;
var board;
var arrow;
var buttons;
jQuery(function() {
    jQuery(document).ready(function() {
        buttons = new home.special.buttons(jQuery('#prevBtnF'), jQuery('#nextBtnF'));
        board = new home.special.board(jQuery('#featureSliderVisual ul').first());
        arrow = new home.special.arrow(jQuery('#featureSliderArrow'));
        rotate = new home.special.rotate(jQuery('#specialContainer'));
        rotate.start();
    });
});

