Event.observe(window, 'load', function()
	{
	initComment();
	initAdimg();
    }
);

function initAdimg()
{
	$$('.adimg').each(function(item){
		pos=item.readAttribute('position');
		if(pos)
		{
			new Ajax.Request('/index.php/tk/adinfo/pos/'+pos,
			{
				onSuccess:function(fback)
				{
					var result=fback.responseText.evalJSON(true);
					if(result && result.img )
						{
						output='<a target="_blank" href="' + result.url +'"><img src="'+result.img+'" ';
						if(result.w) output+=( 'width="' + result.w + '" ');
						if(result.h) output+=('height="' + result.h + '" ');
						output += '/></a>';
						item.innerHTML=output;
						}
					
				}
			}
						);
		}
					});

}


function initComment()
{
	$('loadingtd2').hide();
	new Ajax.Request('/index.php/comment/read', {
		method:'post',
		postBody:'tid=2&act=0&eid='+$('comment_eid').value,
		onSuccess: function(transport){
			$('loadingtd').hide();		
			var returnObj=transport.responseText.evalJSON(true);
			if(returnObj!="" && returnObj.cs.length>0)
			{
				var comments=returnObj.cs;
				for(var i=0;i<comments.length;i++)
				{
					$('commenttable').insert('<tr><td align="center">'+comments[i].u+'</td><td >'+comments[i].c+'</td><td align="center">'+comments[i].d+'</td></tr>');					
				}
			}
			else
			{
				$('loadingtd2').show();
			}
		}
	});
}
function sendComment(n)
{
   var fm;
   if(n==1) fm=$('inpagefm'); else  fm=$('popfm');
	if(!$F(fm['comment_content']).strip().empty() && !$F(fm['comment_picvalid']).strip().empty())
	{
		fm.disabled=true;		
		new Ajax.Request('/index.php/comment/write',	{
		method:'post',
		postBody:'id='+$F(fm['comment_eid'])+'&tid=2&comment='+$F(fm['comment_content'])+'&pic='+$F(fm['comment_picvalid']),
		onSuccess:function(fback){
			var result=fback.responseText;
			if(result=="1")
			{
				$('submitBox'+n).update("<span style='color:red'>谢谢您的参与！您的评论将经审核后显示在上面。</span>");
			   fm['comment_content'].value='';
				if(n==2)
					hidebgDiv.delay(1);

			}
			else if(result=="-1")
			{
				alert('您输入的验证码不正确');
			}
			fm.disabled=false;
		}
	}
	);	
	}else
	{
	alert("请输入评论内容和验证码!");
	}
}




function reloadPicvalidsrc(ObjId)
{
$(ObjId).src="/rdimg.php?"+Math.random();
}

function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

var commentused=false;

function showbgDiv()
{
    var pageSize=getPageSize();
	$('coverall').style.width=getPageSize()[0]+"px";
	$('coverall').style.height=getPageSize()[1]+"px";
	$('allcommentbox').style.top= (document.viewport.getScrollOffsets().top+(pageSize[3]-$('allcommentbox').getHeight())/2)+'px';
	new Effect.Appear('coverall', {duration:0.5, from:0, to:0.6});
	if(!commentused)
	{
		requestComments(1);
		commentused=true;
	}	
	new Effect.Appear('allcommentbox', {delay:0.5, duration:1.0});
}

function requestComments(current)
{
    $('commloadingdiv').innerHTML='<img src="/v6img/loading.gif" style="margin-left:266px;margin-top:100px;" />';
	new Ajax.Request('/index.php/comment/read', {
		method:'post',
		postBody:'tid=2&act=1&eid='+$('comment_eid').value+'&p='+current,
	 	onSuccess: function(transport){
		    if(!transport.responseText.blank())
			{
				var returnObj=transport.responseText.evalJSON(true);
				
				if(returnObj!="" && returnObj.cs.length>0)
				{
					var comments=returnObj.cs;
					var htmls="";
					var pager="";
					var pages=returnObj.pages;
					for(var i=0;i<comments.length;i++)
					{
						htmls+='<li class="header">用户： '+comments[i].u+' 发表于 '+comments[i].d+'</li>';
						htmls+='<li>'+comments[i].c+'</li>';
					}
					$('commloadingdiv').innerHTML=htmls;
					if(current==1)
					{
						pager=" 上一页 ";
					}
					else if(current>1 && current<=pages)
					{
						pager=' <span onclick="requestComments('+(current-1)+', '+pages+')">上一页</span> ';
					}
					
					if(current==pages)
					{
						pager+=' 下一页 ';
					}
					else if(current>0 && current<pages)
					{
						pager+=' <span onclick="requestComments('+(current+1)+', '+pages+')">下一页</span> ';
					}
					
					$('allcommentpager').innerHTML=pager+'第'+returnObj.p+'页/共'+pages+'页';
					$('allcommentbox').style.top= (document.viewport.getScrollOffsets().top+(getPageSize()[3]-$('allcommentbox').getHeight())/2)+'px';
				}			
				else
				{
					$('commloadingdiv').innerHTML='<div style="text-align:center;padding-top:100px;color:red">还没有网友评论过呢，就等您了～～</div>';
					$('allcommentpager').innerHTML='第0页/共0页';
				}
			}

		}
	});	
}

function hidebgDiv()
{
	new Effect.Fade('allcommentbox', {duration:0.5});	
	new Effect.Fade('coverall', {delay:0.5, duration:0.5});	
}

