$(function() {
    var h = null;
    var f = 10;
    var a = 0;
    var j = {
        FIdx: 0,
        EIdx: f,
        isCount: 1
    };
    var d = $("#divLotteryLoading");
    var l = $("#btnLoadMore");
    var m = false;
    var c = function(o) {
        if (o && o.stopPropagation) {
            o.stopPropagation()
        } else {
            window.event.cancelBubble = true
        }
    };
    var b = function() {
        var o = function() {
            return "/" + j.FIdx + "/" + j.EIdx + "/" + j.isCount
        };
		 
        var p = function() {
            d.show();
            GetJPData(Gobal.Webpath, "ajax", "getLotteryList"+o(),
            function(s) {			 
                if (s.code == 0) {
                    if (j.isCount == 1) {
                        a = s.count						
                    }
                    var r = s.listItems;
                    var t = r.length;
                    for (var q = 0; q < t; q++) {
                        var v = '<ul id="' + r[q].id + '"><li class="revConL">' + (r[q].codeType == 1 ? '<span class="z-limit-tips">限时揭晓</span>': "") + '<img src="' + Gobal.LoadPic + '" src2="'+Gobal.imgpath+'/uploads/' + r[q].thumb + '"></li><li class="revConR"><dl><dd><img name="uImg" uweb="' + r[q].q_uid + '" src="'+Gobal.imgpath+'/uploads/' + r[q].userphoto + '"></dd><dd><span>获得者<strong>：</strong><a name="uName" uweb="' + r[q].q_uid + '" class="rUserName blue">' + r[q].q_user + '</a></span>本期购买<strong>：</strong><em class="orange arial">' + r[q].gonumber + '</em>人次</dd></dl><dt>幸运码：<em class="orange arial">' + r[q].q_user_code + '</em><br/>揭晓时间：<em class="c9 arial">' + r[q].q_end_time + '</em></dt><b class="fr z-arrow"></b></li></ul>';
                        var u = $(v);
                        u.click(function() {
                            location.href = Gobal.Webpath+"/mobile/mobile/item/" + $(this).attr("id")
                        }).find('img[name="uImg"]').click(function(w) {
                            location.href = Gobal.Webpath+"/mobile/mobile/userindex/" + $(this).attr("uweb");
                            c(w)
                        });
                        u.find('a[name="uName"]').click(function(w) {
                            location.href = Gobal.Webpath+"/mobile/mobile/userindex/" + $(this).attr("uweb");
                            c(w)
                        });
                        d.before(u)
                    }
                    if (j.EIdx < a) {
                        m = false;
                        l.show()
                    }
                    loadImgFun()
                }
                d.hide()
            })
        };
        this.getInitPage = function() {
            p()
        };
        this.getNextPage = function() {
            j.FIdx += f;
            j.EIdx += f;
            p()
        }
    };
    l.click(function() {
        if (!m) {
            m = true;
            l.hide();
            h.getNextPage()
        }
    });
    h = new b();
    h.getInitPage();
    var e = ",";
    var n = false;
    var g = 0;
    var i = $("#divLottery");

    var k = function() {
        GetJPData(Gobal.Webpath, "ajax", "GetStartRaffleAllList/" + g,
        function(p) {
            if (p.errorCode == 0) {
                o(p)
            }
            setTimeout(k, 5000)
        });
        var o = function(q) {
            g = q.maxSeconds;
            var p = function(t) {
                for (var r = t.length - 1; r > -1; r--) {
                    var s = t[r];
                    if (e.indexOf("," + s.id + ",") < 0) {
                        e += s.id + ",";
                        var u = $('<ul class="rNow rFirst" id="' + s.id + '"><li class="revConL"><img src="' + Gobal.LoadPic + '" src2="' + Gobal.imgpath + '/uploads/' + s.thumb + '"></li><li class="revConR"><h4>(第' + s.qishu + "期)" + s.title + "</h4><h5>价值：￥" + CastMoney(s.money) + '</h5><p name="pTime"><s></s>揭晓倒计时 <strong><em>00</em> : <em>00</em> : <em>0</em><em>0</em></strong></p><b class="fr z-arrow"></b></li><div class="rNowTitle">正在揭晓</div></ul>');
                        u.click(function() {
                            location.href = Gobal.Webpath + "/mobile/mobile/item/" + $(this).attr("id");
                        });
                        i.prepend(u);
                        u.next().removeClass("rFirst");
                        u.StartTimeOut(s.id, parseInt(s.seconds))
                    }
                }
                loadImgFun();
            };
            if (n) {
                p(q.listItems)
            } else {
                Base.getScript(Gobal.Skin + "/js/mobile/LotteryTimeFun.js",
                function() {
                    n = true;
                    p(q.listItems)
                })
            }
        }
    };
    Base.getScript(Gobal.Skin + "/js/mobile/comm.js", k)
});

/*倒计时*/
	$(function() {
    var a = function() {
        var c = $("#divTimerItems");
        var f = c.find("div[name=timerItem]");
        if (f.length > 0) {		 
            var b = function() {
                f.each(function() {
                    var m = $(this);
                    var n = parseInt(m.attr("time"));
                    if (n > 0) {
                        var l = function() {
                            window.location.reload()
                        };
                        m.countdowntime(n, l)
                    }
                })
            };			 
            Base.getScript(Gobal.Skin + "/js/mobile/CountdownFun.js", b)
        }
        var i = c.find("div[name=waiterItem]");
        if (i.length > 0) {
            i.each(function() {
                var m = $(this);
                var n = parseInt(m.attr("time"));
                if (n > 0) {
                    var l = function() {
                        window.location.reload()
                    };
                    setTimeout(l, n * 1000)
                }
            })
        }
        var g = ",";
        var k = false;
        var h = 0;
        var j = function() {
            GetJPData(Gobal.Webpath, "ajax", "show_msjxshop/" + h,
            function(m) {
			   
                if (m.errorCode == 0) {
                    l(m)
                }
                setTimeout(j, 5000)
            });
            var l = function(n) {			 
                h = n.maxSeconds;
                var m = function(r) {				 
                    var q = $("#divLottery");
                    for (var o = r.length - 1; o > -1; o--) {
                        var p = r[o];
						//alert(g.indexOf(",14,"))
                        if (g.indexOf("," + p.codeID + ",") < 0) {
                            g += p.codeID + ",";
                            var s = $('<div class="m-lott-conduct" id="' + p.codeID + '"><p class="z-lott-tt">(第' + p.period + "期)" + p.goodsSName + '<b class="z-arrow"></b><span class="z-lott-time">揭晓倒计时<span class="minute">00</span>:<span class="second">00</span>:<span class="millisecond">0</span><span class="last">0</span></span></p></div>');
                            s.click(function() {                                
                                location.href = Gobal.Webpath+"/mobile/mobile/item/"+ $(this).attr("id")
                            });
                            q.prepend(s);
                            s.StartTimeOut(p.codeID, parseInt(p.seconds))
                        }
                    }
                };
                if (k) {
                    m(n.listItems)
                } else {
                    Base.getScript(Gobal.Skin + "/js/mobile/indexLotteryFun.js",
                    function() {
                        k = true;
                        m(n.listItems)
                    })
                }
            }
        };
        j();
        var e = function() {
            GetJPData(Gobal.Webpath, "ajax", "slides",
            function(s) {
                if (s.state == 0) {
                    var r = s.listItems;
                    var n = $("<ul/>");
                    n.addClass("slides");
                    var p = "";
                    for (var o = 0; o < r.length; o++) {
                        var m = '<li style="background-color:' + r[o].alt + ';"><a href="' + r[o].url + '"><img src="' + r[o].src + '" alt="" width="' + r[o].width + '" height="' + r[o].height + '" /></a></li>';
                        n.append(m)
                    }
                    var q = $("#sliderBox");
                    q.append(n).flexslider()
                }
            });
            var l = parseInt($("#hidStartAt").val());
            $("#autoLotteryBox").flexslider({
                slideshow: false,
                animationLoop: false,
                controlType: 1,
                controlPos: 1,
                startAt: l
            })
        };
        Base.getScript(Gobal.Skin + "/js/mobile/Flexslider.js", e);
        var d = function(l) {
            if (l && l.stopPropagation) {
                l.stopPropagation()
            } else {
                window.event.cancelBubble = true
            }
        };
        $("li", c).each(function() {
            var m = $(this);
            var l = m.attr("codeid");
            m.click(function() {
                location.href = Gobal.Webpath+"/mobile/mobile/item/" + l 
            });
            var n = m.attr("uweb");
            if (n != undefined) {
                m.find("uImg").click(function(o) {
                    d(o);
                    location.href = Gobal.Webpath+"/mobile/home/" + n
                });
                m.find("uName").click(function(o) {
                    d(o);
                    location.href = Gobal.Webpath+"/mobile/home/" + n
                })
            }
        });
        $("#ulRecommend > li").each(function() {
            var l = $(this);
            l.click(function() {
                location.href = Gobal.Webpath+"/mobile/mobile/item/" + l.attr("id") 
            })
        })
    };
    a();

    (function(){

        var div = $('#divLottery');

        var update = function(info){

            var html = '';
			
            html += '<div id="lott-'+info.id+'"><ul class="rNow">';
            html += '   <li class="revConL">';
            html += '       <img src="' + Gobal.imgpath + '/uploads/'+info.thumb+'"></li><li href="'+Gobal.Webpath+'/mobile/mobile/item/'+info.id+'" class="revConR">';
            html += '       <h4>(第'+info.qishu+'期)'+info.title+'</h4></br></br>';
            html += '       <p name="pTime"><s></s>揭晓倒计时 <strong>';
            html += '          <span class="minute">99</span>:<span class="second">99</span>:<span class="millisecond">99</span></strong></p>';
            html += '       <b class="fr z-arrow"></b></li><div class="rNowTitle">正在揭晓</div></ul></div>';


            div.prepend(html);

            var mydiv = div.find('div#lott-'+info.id);
            var minute = mydiv.find('span.minute');
            var second = mydiv.find('span.second');
            var millisecond = mydiv.find('span.millisecond');
            var times = (new Date().getTime()) + info.times * 1000;
            var timer = setInterval(function(){
                var time = times - (new Date().getTime());
                if ( !info.times || time < 1 ) {
                    clearInterval(timer);
                    minute.parent().html('正在计算……');
                    var checker = function(){
                       
                             if ( 1==2 ) {
                              setTimeout(checker,1000);
                             } else {
                              var a = mydiv.find('.z-lott-tt a');
                              a.html('恭喜 <span class="blue">'+info.q_user+'</span> 获得 (第'+info.qishu+'期)'+info.title+'<b class="z-arrow"></b>');
                              a.css('overflow','hidden').css('width','99%');
                              a.parent().css('white-space','nowrap').css('padding','0 13px 0 5px');
                             }
                            
                        
                    };

                    setTimeout(checker,1000);
                    return;
                }

                i =  parseInt((time/1000)/60);
                s =  parseInt((time/1000)%60);
                ms =  String(Math.floor(time%1000));
                ms = parseInt(ms.substr(0,2));
                if(i<10)i='0'+i;
                if(s<10)s='0'+s;
                if(ms<10)ms='0'+ms;
                minute.html(i);
                second.html(s);
                millisecond.html(ms);
            }, 41);
        };

        var gid = '';
        var thread = function(){
            $.getJSON(Gobal.Webpath+"/api/getshop/lottery_shop_json/"+new Date().getTime(),{'test':true,'gid':gid},function(info){
                if(info.error == '0' && info.id != 'null'){
                    if ( ('_'+gid+'_').indexOf('_'+info.id+'_') === -1 ) {
                        gid =  gid + '_' + info.id;
                        update(info);
                    }
                }
            });
        };

        setInterval(thread, 4000);
        thread();
    })();
});