/* Author: yangzeming
 * Date: 2014-02-10
 * Desc: 滚动组件
 */
define(function (require, exports, module) {

    var Slider = _.Class({
        defaultOp: {
            effect: "slide",
            interval: 2000,
            template: "",
            data: {
                list: [],
                type: "default"
            },
            dataHandler: false,

            duration: 5,
            curIndex: 0,
            index: 0,
            slide: {
                speed: 500,
                direction: "ver" // hor
            }
        },
        initial: function (options) {
            this._eventStack = {};
            this.options = $.extend(true, {}, this.defaultOp, options);
            this.$con = $(this.options.con);
            this.bt = baidu.template;
            var html = this.bt(this.options.template, this.options.data);
            this.$con.html(html);
            this.$list = this.$con.find('[data-type="list"]');
            this.$listItem = this.$list.find('[data-type="listItem"]');
            this.autoHandler = null;
            this.sliderHandler();
            this.index = this.options.index;
            this.curIndex = this.options.curIndex;
            this.lastIndex = this.$listItem.size() - 1;
            this.duration = this.options.duration;
            this.isLoadData = false;
            this.getItemHW();

        },
        checkLoadData: function () {
            var curIndex = this.curIndex,
                count = this.$list.find('[data-type="listItem"]').size();
            if (curIndex + this.duration + 5 >= count) {
                if (!this.isLoadData) {
                    this.trigger("loadData", this.getLastIndex());
                }
            }
        },
        getItemHW: function () {
            if (this.$listItem.size() > 0) {
                var p = parseInt(this.$listItem.css("paddingTop").replace("px", ""), 10) + parseInt(this.$listItem.css("paddingBottom").replace("px", ""), 10),
                    m = parseInt(this.$listItem.css("marginTop").replace("px", ""), 10) + parseInt(this.$listItem.css("marginBottom").replace("px", ""), 10),
                    h = this.$listItem.height() + (p + m) / 2;
                this.itemHeight = h;

                var pw = parseInt(this.$listItem.css("paddingLeft").replace("px", ""), 10) + parseInt(this.$listItem.css("paddingRight").replace("px", ""), 10),
                    mw = parseInt(this.$listItem.css("marginLeft").replace("px", ""), 10) + parseInt(this.$listItem.css("marginRight").replace("px", ""), 10),
                    hw = this.$listItem.width() + pw + mw;
                
                this.itemWidth = hw;
            } else {
                this.itemHeight = 0;
                this.itemWidth = 0;

            }
        },
        getIndex: function () {

            return this.$listItem.eq(this.index);

        },
        getLastIndex: function () {
            return this.$listItem.eq(this.lastIndex);
        },
        getCurIndex: function () {
            return this.$listItem.eq(this.curIndex);
        },
        checkIsStop: function (type) {
            var curIndex = this.curIndex,
                count = this.$list.find('[data-type="listItem"]').size();
            if (type == "next") {
                if (curIndex + this.duration >= count - 1) {
                    return true;
                } else {
                    return false;
                }
            } else {
              
                if (curIndex <= 0) {
                    this.curIndex = 0;
                    return true;
                } else {
                    return false;
                }

            }
        },
        genericList: function (data, templateId) {
            var html = this.bt(templateId, data);
            this.$list.append(html);
            this.$listItem = this.$list.find('[data-type="listItem"]')
            var count = this.$listItem.size();
            this.lastIndex = count - 1;

            if (count > 130) {
                this.stop();
                var $t = this.$list.find('[data-type="listItem"]:gt(' + (this.curIndex - 1) + ')');
                this.reload($t);
                this.play();

            } else {
                this.stop();
                this.play();
            }
        },
        reload: function ($list) {
            this.index = 0;
            this.curIndex = 0;
            var $l = $('<ul data-type="list">');

            //this.$list.css({ "margin-top": "0px" });
            $l.html($list);
            this.$list.remove();
            this.$con.html($l);
            this.$list = this.$con.find('[data-type="list"]');
            this.$listItem = this.$list.find('[data-type="listItem"]');
            this.lastIndex = this.$list.find('[data-type="listItem"]').size() - 1;


        },
        addItem: function () {

        },
        sliderHandler: function () {
            var self = this;
            if (this.options.auto) {
                this.play();

            }
            this.$list.hover(function () { self.stop(); }, function () { self.play(); });
        },
        next: function () {
            var self = this;
            var ani = {};
            if (this.options.slide.direction == "ver") {


                ani = {
                    "marginTop": '-=' + this.itemHeight
                };
            } else {

                ani = {
                    "marginLeft": '-=' + this.itemWidth
                };

            }
            if (self.checkIsStop("next")) {
                self.stop();
                self.trigger("next");

            } else {
                self.curIndex++;
                self.$list.animate(ani, self.options.slide.speed, function () {

                });
            }
            self.checkLoadData();
        },
        prev: function () {
            var self = this;
            var ani = {};
            if (this.options.slide.direction == "ver") {
                ani = {
                    "marginTop": '+=' + this.itemHeight
                };
            } else {

                ani = {
                    "marginLeft": '+=' + this.itemWidth
                };

            }
            if (self.checkIsStop("prev")) {
                self.stop();
                self.trigger("prev");

            } else {
                self.curIndex--;
                self.$list.animate(ani, self.options.slide.speed, function () {

                });
            }
            // self.checkLoadData();

        },
        play: function () {
            var self = this;
            if (this.autoHandler == null) {

                this.autoHandler = setInterval(function () {
                    self.next();
                }, this.options.interval);
            }
        },
        stop: function () {
            clearInterval(this.autoHandler);
            this.autoHandler = null;
        }

    });
    module.exports = Slider;
});