
$(function(){
    if(typeof scope  == 'undefined'){
                scope  = {};
            }

    /**
     *  轮播组图高度控制类
     * @param id
     */
    var picsHeightManager = {
        isPicDivsBlocked : false,   //swipe pic divs is blocked
        init : function () {
            this.setPicsHeight();

            var self = this;
            window.addEventListener('onorientationchange' in window ?
                             'orientationchange' : 'resize', function () {
                var sf = self;
                setTimeout(function () {
                    sf.setPicsHeight();
                },300);
            }, false);
        },
        setPicsHeight : function () {
            try {
                var oSwipe = $('#j_imgSwipe'),
                    oImg = oSwipe.children().eq(0).find('a').find('img');
                var _swiptContainerWidth = oSwipe.width();
                var _picsWidth = oImg.width();
                var _picsHeight = oImg.height();
                var _swiptContainerHeight = _swiptContainerWidth * _picsHeight / _picsWidth;
                _swiptContainerHeight = Math.floor(_swiptContainerHeight);
                oSwipe.css("height", _swiptContainerHeight + "px");
                oSwipe.find(".swipe-wrap").css("height", _swiptContainerHeight + "px");
                var _swipe_pics = oSwipe.find(".swipe_pic");
                _swipe_pics.css("height", _swiptContainerHeight + "px");
                if (!this.isPicDivsBlocked) {
                    this.isPicDivsBlocked = true;
                    _swipe_pics.css("display", "block");
                }
            } catch (e) {}
        }
    };

    /**
     * 组图轮播
     * @method  imgSwipe
     * @param   {String} CLASS字符串，如'j_imgswipe'
     * @return
     */

    function imgSwipe(id){

        var obj = null,
            oSwipe = $('#' + id),
            oImg = oSwipe.children().eq(0).find('a').find('img'),
            len = oImg.length,
            oSpan = oSwipe.children().eq(1).find('span'),
            len = oImg.length,
            maxCount = len - 2,     // 加载结束量
            count = 1,              // 加载开始量
            ua = navigator.userAgent,
            current = 0;

        obj = new Swipe(document.getElementById(id), {
            startSlide: 0,
            auto: 3600000,
            speed: 300,
            // disableTouch: true,
            callback: function(index, element){

                oSpan.html(index + 1);  // 焦点图当前状态

                var currImg,
                    wrap;

                if(index > current && index >= 1 && index < len - 1)currImg = oImg.eq(index + 1);
                else if(index < current && index >= 2)currImg = oImg.eq(index);
                else if(index == len - 1 && current == 0)currImg = oImg.eq(len - 1);
                else{
                    current = index;
                    return;
                }
                current = index;
                if(currImg.length > 0)wrap = currImg.parent().parent();

                if (currImg.length > 0 && wrap.attr('data-loaded') == '0') {

                    var src = currImg.attr('data-src'),
                        alt = currImg.attr('alt'),
                        _oImg = new Image(),
                        loaded = false,
                        isTimeout = false,
                        timer = null;

                    currImg.attr('alt', '');//加载中去除alt显示
                    wrap.addClass('loading');//添加图片加载中标志
                    _oImg.src = src;
                    _oImg.onload = function(){//图片加载成功
                        wrap.removeClass('loading');
                        currImg.attr('src', src);
                        currImg.attr('alt', alt);
                        wrap.attr('data-loaded', '1');
                    };
					
                }
            }
        });
    }

    // 初始化
    setTimeout(function(){
        if($('#j_imgSwipe').length>0 && $('.j_sax').length==0){
            imgSwipe('j_imgSwipe');
            picsHeightManager.init();      //轮播组图高度控制类
        }                              // 组图
    }, 300);

});



