/*
 *  실 목록 상단의 배너?를 위한 자바스크립트
 *  무혼마
 *  v1.0.0, 2009-07-14
 */

var _BannerBox = function() {
    this.container = null;
    this.order = null;
    this.timer = null;
};
_BannerBox.prototype.init = function(container) {
    this.container = container;

    var self = this;

    var ancherList = container.find('div.status a');
    for (var i = 0; i < ancherList.length; ++i) {
        var content = document.getElementById('stBannerBoxContent' + (i + 1)), ancher = $(ancherList[i]);
        if (!content) {
            ancher.css('display', 'none');
        } else {
            ancher.mouseover(function(e) { self.mouseOver(e); });
            ancher.click(function() { return false; });
            if ($(content).css('display') == 'block') {
                this.order = i;
                ancher.find('img').attr('src', 'images/sealbanner/sealbox_num_' + (i + 1) + '_on.gif');
            }
        }
    }

    this.setTimer();
};
_BannerBox.prototype.mouseOver = function(e) {
    var target = $(e.currentTarget), order;
    if (target.hasClass('first')) order = 0;
    else if (target.hasClass('second')) order = 1;
    else if (target.hasClass('third')) order = 2;
    else if (target.hasClass('fourth')) order = 3;
    else if (target.hasClass('fifth')) order = 4;
    else if (target.hasClass('sixth')) order = 5;
    else if (target.hasClass('seventh')) order = 6;
    else if (target.hasClass('eighth')) order = 7;
    else if (target.hasClass('ninth')) order = 8;

    if (order == this.order) return;

    var ancherList = this.container.find('div.status a');

    // 새 선택은 활성화, 이전 선택은 제거
    $(ancherList[order]).find('img').attr('src', 'images/sealbanner/sealbox_num_' + (order + 1) + '_on.gif');
    $(document.getElementById('stBannerBoxContent' + (order + 1))).css('display', 'block');
    $(ancherList[this.order]).find('img').attr('src', 'images/sealbanner/sealbox_num_' + (this.order + 1) + '.gif');
    $(document.getElementById('stBannerBoxContent' + (this.order + 1))).css('display', 'none');
    this.order = order;

    this.setTimer();
};
_BannerBox.prototype.setTimer = function() {
    var self = this;

    if (this.timer) {
        clearInterval(this.timer);
        this.timer = null;
    }
    // 3초마다 한 번씩 바뀌면 너무 빨리 바뀐다.
    // 현재 페이지 변환 이외의 다른 작업을 캡쳐하는 방법이 없어서 일단은 냅둔다.
    this.timer = setInterval(function(){ self.nextBanner(); }, 5000);
};
_BannerBox.prototype.nextBanner = function() {
    order = this.order + 1;
    var content = document.getElementById('stBannerBoxContent' + (order + 1));
    if (!content) order = 0;

    if (order == this.order) return;

    // 새 선택은 활성화, 이전 선택은 제거
    var ancherList = this.container.find('div.status a');
    $(ancherList[order]).find('img').attr('src', 'images/sealbanner/sealbox_num_' + (order + 1) + '_on.gif');
    $(document.getElementById('stBannerBoxContent' + (order + 1))).css('display', 'block');
    $(ancherList[this.order]).find('img').attr('src', 'images/sealbanner/sealbox_num_' + (this.order + 1) + '.gif');
    $(document.getElementById('stBannerBoxContent' + (this.order + 1))).css('display', 'none');
    this.order = order;
}

var BannerBox = new _BannerBox();







var SlideBox = function(container) {
    this.container = null;
    this.ul = null;
    this.now_page = null;
    this.max_page = null;
    this.speed = 400; /* 애니메이션 속도를 조정하려면 이 것 */

    this.init(container);
};
SlideBox.prototype.init = function(container) {
    var self = this;

    this.container = container;

    this.now_page = 1;
    this.max_page = Math.ceil(container.find('ul li').length / 4);

    this.ul = this.container.find('ul');

    this.container.find('div.left a').click(function() { self.movePrev(); return false; });
    this.container.find('div.right a').click(function() { self.moveNext(); return false; });

    this.changeButtonState();
};

SlideBox.prototype.moveNext = function() {
    if (this.now_page >= this.max_page) return;

    var left = this.now_page * -460;
    this.ul.animate({ left: left }, this.speed);

    this.now_page++;
    this.changeButtonState();
};
SlideBox.prototype.movePrev = function() {
    if (this.now_page <= 1) return;

    var left = (this.now_page - 2) * -460;
    this.ul.animate({ left: left }, this.speed);

    this.now_page--;
    this.changeButtonState();
};
SlideBox.prototype.changeButtonState = function() {
    if (this.now_page == 1)
        this.container.find('div.left').css('display', 'none');
    else
        this.container.find('div.left').css('display', 'block');

    if (this.now_page == this.max_page)
        this.container.find('div.right').css('display', 'none');
    else
        this.container.find('div.right').css('display', 'block');
};