﻿var currentIndex = 1;
var totalPage = 18;
var currPage = 0;
var loading = false;
var bigpage = 1;

$(document).ready(function() {
    autoLoad();
    $(window).scroll(autoLoad);


    //下一页
    $('#aNext').click(function() {
        if ($('#aNext').children(0).attr('src') == 'http://static.qigle.com/imges/shop/flast_gray.jpg') {
            return;
        }
        if (bigpage > 0) {
            bigpage = bigpage + 1;
            currPage = 0;
            loading = false;
            currentIndex = 1;
            $('.spaceL_Cpage').empty();
            $('#aPiv').children("img").attr('src', 'http://static.qigle.com/imges/shop/fnew.jpg');
            autoLoad();
            $(window).scroll(autoLoad);
        }
    });

    //上一页
    $('#aPiv').click(function() {
        if (bigpage > 1) {
            bigpage = bigpage - 1;
            currPage = 0;
            loading = false;
            currentIndex = 1;
            $('.spaceL_Cpage').empty();
            autoLoad();
            $(window).scroll(autoLoad);
        }
        if (bigpage == 1) {
            $('#aNext').children(0).attr('src', 'http://static.qigle.com/imges/shop/flast.jpg');
            $('#aPiv').children("img").attr('src', 'http://static.qigle.com/imges/shop/flnew.jpg');
        }
    })

    //前往第几页
    $('#aToGo').click(function() {
        bigpage = $('#txtNum').val();
        currPage = 0;
        loading = false;
        currentIndex = 1;
        $('.spaceL_Cpage').empty();
        autoLoad();
        if (bigpage == 1) {
            $('#aPiv').children("img").attr('src', 'http://static.qigle.com/imges/shop/flnew.jpg');
        } else {
            $('#aPiv').children("img").attr('src', 'http://static.qigle.com/imges/shop/fnew.jpg');
        }
        $(window).scroll(autoLoad);
        $('#txtNum').attr('value', '');
    });


});
function autoLoad() {

    if (loading == true) {

        return;
    }

    var autoDiv = $('#autoLoad');
    var $contentList = $('#contentList');
    var top = autoDiv.offset().top;
    if (!loading && (($(window).scrollTop() + $(window).height()) > top)) {
        loading = true;
        currPage++;

        if (currPage > totalPage) {
            loading = true;
            $(window).unbind('scroll');
            $('#divPage').attr('style', 'margin: 50px auto 20px;width: 500px;display:block;');
            return;

        }


        autoDiv.text('加载中……');
        var html = "";
        
        var thisIndex = 1;
        $.getJSON('http://www.qigle.com/Ajax/AjaxHotsPhotoList', { 'page': (totalPage * (bigpage - 1)) + currPage }, function(data) {

            if (data.result == 'false') {
                autoDiv.text('没有最新动态了');
                loading = true;
                $(window).unbind('scroll');
                return;
            }
            $.each(data, function(index, entry) {
            html = '<div class="brand_new"><div class="spaceL_Cpagepic"><a href="/item/' + entry["itemId"]
                    + '" target="_blank"><img style="width:180px;" src="' + entry["itemImg"] + '" alt="' + entry["itemTitle"]
					+ '"/></a></div><div class="spaceL_Cpagetext"><h1 class="comm C666"> <label class="mR8"><a href="javascript:void(0)" class="notlikeit" onclick="AddUserlikeItem(\'' + entry["itemId"] + '\',\'' + $('#hidUserId').val() + '\',\'' + entry["uid"] + '\');"><span class="nums" id="span_' + entry["itemId"] + '">' + entry["itemNum"] + '</span>我喜欢</a></label> <label class="mR8"><a href="/photo/' + entry["itemId"] + '#divTalk" target="_blank" class="discuss"><b class="nums">' + entry["itemTc"]
					+ '</b>评论</a></label> </h1><div class="item_list_down_middle"><div class="item_list_headpic"><a href="/u/' + entry["uid"] + '/photo" target="_blank"><img alt="" style="width:30px; height:30px;" src="' + entry["headpic"]
                    + '"/></a></div><div class="item_list_down_text"><p class="CFF679A"><a href="/u/' + entry["uid"] + '/photo" target="_blank">' + entry["name"] + '</a></p><p class="C999"><a href="javascript:void(0)">' + entry["itemTime"]
                //+ '<p class="C999 mT5">' + entry["time"]</p>
					+ '</a></p></div><div class="clear"></div></div><div class="spaceL_Cpagetexttuijian">推荐说：';
                if (entry["pdesc"]) {
                    html += entry["itemDesc"];
                } else {
                html += entry["itemTitle"];
                }
                html += '</div></div></div>';
                thisIndex = readIndex();
                $contentList.children(':eq(' + (thisIndex - 1) + ')').append(html);
            });
            autoDiv.text('');
            loading = false;
        });
    }
}
function readIndex() {
    var temp;
    if (currentIndex <= 0) {
        temp = 1;
        currentIndex = temp + 1;
    }
    else if (currentIndex >= 3) {
        temp = 3;
        currentIndex = 1;
    }
    else {
        temp = currentIndex++;
    }
    return temp;
}


