slide.js


define(['jquery'], function (jquery) {
    function buildSmooth(config, motivateCallBack) {
        var timer = -1;
        var cfg = config;
        var motiCallBack = motivateCallBack;

        if (cfg == null || cfg.smoothSlide == null) {
            throw new Error('config > smoothSlide is not configed.');
        }

        if (cfg.timer != null && cfg.timer != -1) {
            timer = cfg.timer;
        }

        var interval;
        var smoothSlide = cfg.smoothSlide;
        if (smoothSlide.windowObj == null) {
            throw new Error('config > smoothSlide >windowObj is not configed.');
        }

        var winObj = jquery(smoothSlide.windowObj);
        winObj.css('position', 'relative');
        if (smoothSlide == null
            || smoothSlide.slideLayerParent == null) {
            throw new Error('config > smoothSlide > slideLayerParent is not configed.');
        }

        if (smoothSlide.type == null) {
            throw new Error('config > smoothSlide > type is not configed.');
        }

        var muChildren;
        var lnmObj;
        switch (smoothSlide.type) {
            case 'Multi':
                if (smoothSlide.mutilMotivateObj == null) {
                    throw new Error('config > smoothSlide > mutilMotivateObj is not configed.');
                }

                var muObj = jquery(smoothSlide.mutilMotivateObj);
                muChildren = muObj.children();
                break;
            case 'LastNext':
                lnmObj = smoothSlide.lNMotivateObj;
                if (lnmObj == null) {
                    throw new Error('config > smoothSlide > lNMotivateObj is not configed.');
                }
                break;
            case 'Both':
                if (smoothSlide.mutilMotivateObj == null) {
                    throw new Error('config > smoothSlide > mutilMotivateObj is not configed.');
                }

                var muObj = jquery(smoothSlide.mutilMotivateObj);
                muChildren = muObj.children();
                lnmObj = smoothSlide.lNMotivateObj;
                if (lnmObj == null) {
                    throw new Error('config > smoothSlide > lNMotivateObj is not configed.');
                }
                break;
            default:
                throw new Error('config > smoothSlide > type is not configed.');
                break;
        }



        var parentLayer = jquery(smoothSlide.slideLayerParent);
        var firstLayer = parentLayer.children().first();
        var secondLayer=parentLayer.children().eq(1);
        var lastSecLayer = parentLayer.children().eq(parentLayer.children().size()-2);
        var lastLayer = parentLayer.children().last();
        firstLayer.clone(true).appendTo(parentLayer);
        lastLayer.clone(true).prependTo(parentLayer);
        var layerCount = parentLayer.children().size();
        if (layerCount <= 1) {
            return;
        }

        var ulWidth = (-winObj.width() * layerCount);
        parentLayer.css({ 'position': 'absolute', 'top': '0px', 'left': ulWidth + 'px', 'width': Math.abs(ulWidth) + 'px' });
        var index = 0;
        var prevNextFn = function () {
            if (lnmObj != null) {
                if (lnmObj.last == null) {
                    throw new Error('config > smoothSlide > slideLayerParent > last is not configed.');
                }

                jquery(lnmObj.last).click(function () {
                    if (!parentLayer.is(':animated')) {
                        SmoothnessLayer(--index);
                    }
                    try {
                        motiCallBack($(this), index, layerCount);
                    }
                    catch (err) {
                        throw new error('Motivate call back function is not work:' + err.description);
                    }
                });

                if (lnmObj.next == null) {
                    throw new Error('config > smoothSlide > slideLayerParent > next is not configed.');
                }

                jquery(lnmObj.next).click(function () {
                    if (!parentLayer.is(':animated')) {
                        SmoothnessLayer(++index);
                    }
                    try {
                        motiCallBack($(this), index, layerCount);
                    }
                    catch (err) {
                        throw new error('Motivate call back function is not work:' + err.description);
                    }
                });
            }
        };

        var muiltiFn = function () {
            if (muChildren != null) {
                muChildren.each(function (i) {
                    jquery(this).click(function () {
                        if (!parentLayer.is(':animated')) {
                            SmoothnessLayer(i + 1);
                            try {
                                motiCallBack($(this), index, layerCount, true);
                            }
                            catch (err) {
                                throw new error('Motivate call back function is not work:' + err.description);
                            }

                            index = i + 1;
                        }
                    });

                });
            }
        };

        switch (smoothSlide.type) {
            case "LastNext":
                prevNextFn();
                index = 0;
                if (lnmObj.next != null) {
                    jquery(lnmObj.next).trigger('click');
                    if (timer != null && timer != -1) {
                        interval = setInterval(function () {
                            jquery(lnmObj.next).trigger('click');
                        }, timer);
                    }
                }
                break;
            case "Multi":
                muiltiFn();
                if (muChildren != null) {
                    muChildren.eq(0).trigger('click')
                }
                break;
            case "Both":
                prevNextFn();
                muiltiFn();
                jquery(lnmObj.next).trigger('click');
                if (timer != null && timer != -1) {
                    interval = setInterval(function () {
                        jquery(lnmObj.next).trigger('click');
                    }, timer);
                }
                break;
        }

        function SmoothnessLayer(currentNum) {
            var winWidth = winObj.width();
            parentLayer.animate({ left: -currentNum * winWidth }, 800, function () {
                if (currentNum == 0) {
                    parentLayer.css('left', -winWidth * (layerCount - 2));
                    index = layerCount - 2;
                }
                else if (currentNum == layerCount - 1) {
                    parentLayer.css('left', -winWidth);
                    index = 1;
                }
            });
        }
    };

    function buildCover(config, motivateCallBack) {
        var timer = -1;
        var cfg = config;
        var motiCallBack = motivateCallBack;

        if (cfg == null || cfg.smoothSlide == null) {
            throw new Error('config > smoothSlide is not configed.');
        }

        if (cfg.timer != null && cfg.timer != -1) {
            timer = cfg.timer;
        }

        var interval;
        var smoothSlide = cfg.smoothSlide;
        if (smoothSlide.windowObj == null) {
            throw new Error('config > smoothSlide >windowObj is not configed.');
        }

        var winObj = jquery(smoothSlide.windowObj);
        winObj.css('position', 'relative');
        if (smoothSlide == null
            || smoothSlide.slideLayerParent == null) {
            throw new Error('config > smoothSlide > slideLayerParent is not configed.');
        }

        if (smoothSlide.type == null) {
            throw new Error('config > smoothSlide > type is not configed.');
        }

        var muChildren;
        var lnmObj;
        switch (smoothSlide.type) {
            case 'Multi':
                if (smoothSlide.mutilMotivateObj == null) {
                    throw new Error('config > smoothSlide > mutilMotivateObj is not configed.');
                }

                var muObj = jquery(smoothSlide.mutilMotivateObj);
                muChildren = muObj.children();
                break;
            case 'LastNext':
                lnmObj = smoothSlide.lNMotivateObj;
                if (lnmObj == null) {
                    throw new Error('config > smoothSlide > lNMotivateObj is not configed.');
                }
                break;
            case 'Both':
                if (smoothSlide.mutilMotivateObj == null) {
                    throw new Error('config > smoothSlide > mutilMotivateObj is not configed.');
                }

                var muObj = jquery(smoothSlide.mutilMotivateObj);
                muChildren = muObj.children();
                lnmObj = smoothSlide.lNMotivateObj;
                if (lnmObj == null) {
                    throw new Error('config > smoothSlide > lNMotivateObj is not configed.');
                }
                break;
            default:
                throw new Error('config > smoothSlide > type is not configed.');
                break;
        }



        var parentLayer = jquery(smoothSlide.slideLayerParent);
        var layerCount = parentLayer.children().size();
        if (layerCount <= 1) {
            return;
        }

        var index = 0;
        var prevNextFn = function () {
            if (lnmObj != null) {
                if (lnmObj.last == null) {
                    throw new Error('config > smoothSlide > slideLayerParent > last is not configed.');
                }

                jquery(lnmObj.last).click(function () {
                    if (!parentLayer.is(':animated')) {
                        CoverLayer(--index, layerCount);
                    }
                    try {
                        motiCallBack($(this), index, layerCount);
                    }
                    catch (err) {
                        throw new error('Motivate call back function is not work:' + err.description);
                    }
                });

                if (lnmObj.next == null) {
                    throw new Error('config > smoothSlide > slideLayerParent > next is not configed.');
                }

                jquery(lnmObj.next).click(function () {
                    if (!parentLayer.is(':animated')) {
                        CoverLayer(++index, layerCount);
                    }
                    try {
                        motiCallBack($(this), index, layerCount);
                    }
                    catch (err) {
                        throw new error('Motivate call back function is not work:' + err.description);
                    }
                });
            }
        };

        var muiltiFn = function () {
            if (muChildren != null) {
                muChildren.each(function (i) {
                    jquery(this).click(function () {
                        if (!parentLayer.is(':animated')) {
                            index = i;
                            CoverLayer(i, layerCount);
                            try {
                                motiCallBack(jquery(this), index, layerCount, true);
                            }
                            catch (err) {
                                throw new error('Motivate call back function is not work:' + err.description);
                            }
                        }
                    });

                });
            }
        };

        switch (smoothSlide.type) {
            case "LastNext":
                prevNextFn();
                index = 0;
                if (lnmObj.next != null) {
                    if (timer != null && timer != -1) {
                        interval = setInterval(function () {
                            jquery(lnmObj.next).trigger('click');
                        }, timer);
                    }
                }
                break;
            case "Multi":
                muiltiFn();
                if (muChildren != null) {
                    muChildren.eq(0).trigger('click')
                }
                break;
            case "Both":
                prevNextFn();
                muiltiFn();
                if (timer != null && timer != -1) {
                    interval = setInterval(function () {
                        jquery(lnmObj.next).trigger('click');
                    }, timer);
                }
                break;
        }

        function CoverLayer(currentNum, layerCount) {
            var vLayer = parentLayer.children(':visible');
            if (currentNum < 0) {
                index = layerCount - 1;
            }
            if (currentNum >= layerCount) {
                index = 0;
            }
            var zIndex = index + 1;
            vLayer.css({ 'z-index': 1 });
            parentLayer.children().eq(index).css({ 'z-index': 2 }).fadeIn(2000, function () {
                if (!jquery(this).is(':animated')) {
                    vLayer.hide();
                }
            });

        }
    };

    return {
        buildSmoothSlide: buildSmooth,
        buildCoverSlide: buildCover
    };
});