PanelSwaper = function (cfg, type) {
    try {
        if (cfg == undefined || cfg == null) throw Error('config is undefined!');
        if (cfg.box == undefined || cfg.box == null) throw Error('Layout selecter is undefined!');
        this._lock = false;
        this._pnl = $(cfg.box);
        this._pages = $(cfg.pageDot);
        this._currIndex = null;
        this._speed = cfg.speed || 1000;
        this._duration = cfg.duration || 10000;
        this._autoSwap = true;
        this._type = type || '0';

        if (cfg.autoSwap != undefined && cfg.autoSwap != null && !cfg.autoSwap) {
            this._autoSwap = false;
        }

        if (cfg.nextEl != undefined && cfg.nextEl != null) {
            $(cfg.nextEl).click([this], function (event) {
                event.data[0].next();
            });
        }

        if (cfg.prevEl != undefined && cfg.prevEl != null) {
            $(cfg.prevEl).click([this], function (event) {
                event.data[0].previous()
            });
        }

        if (type != undefined && type == '1') {
            if (cfg.item == undefined || cfg.item == null) throw Error('item selecter is undefined!');
            this._items = $(cfg.item);
            this.initFader(cfg.start);
        }
        else {
            if (cfg.width == undefined || !parseInt(cfg.width)) throw Error('mask width is undefined!');
            this._width = parseInt(cfg.width);
            this._position = cfg.position || 'left';
            this.initSilder(cfg.start);
        }
    }
    catch (e) {
        alert(e.message);
    }
}
PanelSwaper.prototype.initSilder = function (startPage) {
    var ref = this;
    var start = (parseInt(startPage) && parseInt(startPage) <= this._pages.length && parseInt(startPage) > 0 ? (parseInt(startPage) - 1) : 0);
    this.activate(start, this._pages[start]);

    this._pages.each(function (index, el) {
        $(el).click(function (e) {
            ref.activate(index, el);
        });
    });

    if (this._autoSwap && this._pages.length > 1) {
        this.startTimer();
        this._pnl.hover(function (e) {
            ref.stopTimer();
        },
        function (e) {
            ref.startTimer();
        });

        $(this._pnl).focusin([this], function (e) { e.data[0].stopTimer(); });
        $(this._pnl).focusout([this], function (e) { e.data[0].startTimer(); });
    }
}
PanelSwaper.prototype.initFader = function (startNum) {
    var ref = this;
    var start = (parseInt(startNum) && parseInt(startNum) <= this._pages.length && parseInt(startNum) > 0 ? (parseInt(startNum) - 1) : 0);
      
    this._pages.each(function (index, el) {
        $(el).click(function (e) {
            ref.activate(index, el);
        });
    });

    $(this._items).hide();
    this.activate(start, this._pages[start]);

    if (this._autoSwap && this._pages.length > 1) {
        this.startTimer();
        this._pnl.hover(function (e) {
            ref.stopTimer();
        },
        function (e) {
            ref.startTimer();
        });

        $(this._pnl).focusin([this], function (e) { e.data[0].stopTimer(); });
        $(this._pnl).focusout([this], function (e) { e.data[0].startTimer(); });
    }
}
PanelSwaper.prototype.activate = function (index, el) {
    if (this._currIndex == index || this._lock) return;
    this._lock = true;
    this._pages.each(function (idx, o) {
        if (o === el) {
            if (!$(el).hasClass('selected')) {
                $(el).addClass('selected');
            }
        }
        else {
            $(o).removeClass('selected');
        }
    });

    if (this._type == 1) {
        this.fadeAction(index);
    }
    else {
        this.sildeAction(index);
    }
    this._currIndex = index;
}
PanelSwaper.prototype.sildeAction = function (index) {
    var obj = {}, ref= this;
    obj[this._position] = -(index * this._width);
    this._pnl.animate(obj, {
        duration: this._speed,
        complete: function() {
            ref._lock = false;
        }
    });
}
PanelSwaper.prototype.fadeAction = function (index) {
    var ref = this;
    $(this._items[this._currIndex]).fadeOut(this._speed);
    $(this._items[index]).fadeIn(this._speed, function () {
        ref._lock = false;
    });
}
PanelSwaper.prototype.next = function() {
    var index = (this._currIndex == (this._pages.length-1)? 0: (this._currIndex+1));
    this.activate(index, this._pages[index]);
}
PanelSwaper.prototype.previous = function() {
    var index = (this._currIndex == 0? (this._pages.length-1): (this._currIndex-1));
    this.activate(index, this._pages[index]);
}
PanelSwaper.prototype.startTimer = function () {
    var ref = this;
    this._run = setTimeout(function () {
        ref.next();
        ref.startTimer();
    }, this._duration);
}
PanelSwaper.prototype.stopTimer = function () {
    clearTimeout(this._run);
    this._run = null;
}
