//Ajax加载下一页Json数据
function getDat(PageSize,PageIndex){
	if(nowLoading==true)return;
	nowLoading=true;
	var url="?action=getDat&PageSize="+ PageSize +"&PageIndex="+ PageIndex + "&_"+ Math.random();
	$.get(url,function(json){
		nowLoading=false;
		AddItem(json);
	});
}

//得到Json格式数据，将数据加入框架
function AddItem(json){
	var tpl=document.getElementById('ItemTpl').innerHTML;
	var html="",items=[];
	var gs=JSON.parse(json);
	tpicnum=gs.length;
	for(var i=0;i<gs.length;i++){
		html=tpl.replace('{$pic_url}',gs[i].pic_url)
			.replace('{$title}',gs[i].title)
			.replace(/\{\$TaokeUrl\}/g,gs[i].url);
		items[items.length] = $(html);
		vg.append(items[items.length-1]);
	}
	
	vg.vgrefresh(0,0,0,function(){
		for(var i=0;i<items.length;i++){
			items[i].slideDown('slow');
		}
	});
	
	if(gs.length>0){
		cPageIndex++;	//服务端返回数据：页码加1
	}else{
		isLoadingAll=true;
	}
}


function itempicload(){
	lpicnum++;
	//alert(lpicnum+'|'+tpicnum);
	if(lpicnum>=tpicnum){
		vg.vgrefresh();
		//alert('flush');
		lpicnum=0;
	}
}



$(function(){
	
	$('#header').JDock('top');
	
	vg=$("#container").vgrid({
		easeing: "easeOutQuint",
		time: 400,
		delay: 20
	});
	
//	var dlo=$('#container>div:first')[0];
//	var sw=dlo.offsetWidth+20;
	var sw=270;		//列宽，无奈手工设置
	
	function setNaxtPageDat(e){
		var ah=document.body.offsetHeight;	//网页整体高度
		var st=document.documentElement.scrollTop;		//滚动条位置
		var ch=document.documentElement.clientHeight;	//浏览器窗口高度
		//alert(ah+' | '+st+ ' | ' +ch);
		if(ah-st-200<=ch && isLoadingAll!=true){
			getDat(tpicnum,cPageIndex+1);
		}
	}
	
	function setListBodyWidth(){
		var cols=parseInt(document.documentElement.clientWidth/sw);
	//	alert(cols+"|"+sw+"|"+(cols*sw));
		document.getElementById('ListBody').style.width=(sw*cols)+'px';
//		alert(sw);
	}
	setListBodyWidth();
	setNaxtPageDat();
	
	$(window).scroll(setNaxtPageDat).bind('resize',setListBodyWidth);
	
	//setTimeout("$('.ks-waterfall').dropShadow()",3000);

	
});


