/**
 * @function 获取时间间隔 copyright (C) 2006-2007 LoveN9.com Inc.
 * @params baseTime：基准时间；calTime：需要计算的时间；格式为Unxi时间戳整数
 * @return 返回对象：obj.inc: 1正差的时间，-1;负数时间差，0：相同；obj.time:n年n月n天n时n分n秒；obj.date:n年n月n天
 */
function getTimeInterval(baseTime,calTime){
	var _baseTime = new Date(baseTime);
	var _calTime = new Date(calTime);
	var _zeroTime = new Date(0);
	var retObj = {inc:0,date:"",time:""};
	if(_baseTime > _calTime){//倒计时
		var Times = new Date(_baseTime - _calTime);
		var Years = 	Times.getYear() - _zeroTime.getYear();
		var Months = 	Times.getMonth() - _zeroTime.getMonth();
		var Dates = 	Times.getDate() - _zeroTime.getDate();
		var Hours = 	Times.getHours() - _zeroTime.getHours();
		var Minutes = 	Times.getMinutes() - _zeroTime.getMinutes();
		var Seconds = 	Times.getSeconds() - _zeroTime.getSeconds();
		
		var html = (Years>0)?(Years+"年"):"";
		html = html+((Months>0)?(Months+"月"):"");
		html = html+((Dates>0)?(Dates+"天"):"");
		retObj.date = html;
		
		html = html+((Hours>0)?(Hours+"时"):"");
		html = html+((Minutes>0)?(Minutes+"分"):"");
		html = html+((Seconds>0)?(Seconds+"秒"):"");
		
		retObj.time = html;
		retObj.inc = -1;
	}else if(_baseTime == _calTime){
		retObj.inc = 0;
	
	}else{
		var Times = new Date(_calTime - _baseTime);
		var Years = 	Times.getYear() - _zeroTime.getYear();
		var Months = 	Times.getMonth() - _zeroTime.getMonth();
		var Dates = 	Times.getDate() - _zeroTime.getDate();
		var Hours = 	Times.getHours() - _zeroTime.getHours();
		var Minutes = 	Times.getMinutes() - _zeroTime.getMinutes();
		var Seconds = 	Times.getSeconds() - _zeroTime.getSeconds();
		
		var html = (Years>0)?(Years+"年"):"";
		html = html+((Months>0)?(Months+"月"):"");
		html = html+((Dates>0)?(Dates+"天"):"");
		retObj.date = html;
		
		html = html+((Hours>0)?(Hours+"时"):"");
		html = html+((Minutes>0)?(Minutes+"分"):"");
		html = html+((Seconds>0)?(Seconds+"秒"):"");
		
		retObj.time = html;
		retObj.inc = 1;
	}
	return retObj;
}
/**
 * 婚礼时钟
 */
function wedTimeInterval(){
	var objWedTime = getTimeInterval(wedbirthday,servertime);
	$n9qry("#oWedTimer").empty();
	if(objWedTime.inc >0){
		if(objWedTime.date==''){
			$n9qry("#oWedTimer").html("<span><b>婚礼进行时"+objWedTime.time+"</b></span>");
		}else{
			$n9qry("#oWedTimer").html("<span><b>一起走过第"+objWedTime.date+"</b></span>");
		}
	}else{ //婚礼倒计时
		$n9qry("#oWedTimer").html("<span><b>倒计时"+objWedTime.time+"</b></span>");
	}
	servertime = servertime+1000;
}
function getStoryTime(storyDTStr){
	var storyTime = Date.parse(storyDTStr);
	objCulTime = getTimeInterval(0,storyTime);
	if(objCulTime.inc > 0){
		return "婚后"+objCulTime.date;
	}else{
		return "婚前"+objCulTime.date;
	}
}
function showComments(comments)
{
	if(comments!=''){
		try{
			var comments_array = comments.parseJSON(function (key, value){
    			return key.indexOf('date') >= 0 ? new Date(value) : value;
			});
			$n9qry("#oCommentCounts").html("<b>"+comments_array.length+"</b>");
			$n9qry("#oComments").empty() ;
			for(var i=0;i<comments_array.length;i++){
				var newComment = $n9qry("<span>"+i+"#&nbsp;<b>"+comments_array[i].name+"</b>&nbsp;说：</b><br />"+comments_array[i].comment+"<br />发表于："+comments_array[i].time+"<br /></span><br />");
				newComment.appendTo("#oComments");
			}
		}catch(e){
			alert('Wrong'+e);
		}
	}
}
function showStoryComments(comments){
	if(comments!=''){
		try{
			var comments_array = comments.parseJSON(function (key, value){
				return key.indexOf('date') >= 0 ? new Date(value) : value;
			});
			$n9qry("#oCommentCounts").html("<b>"+comments_array.length+"</b>");
			$n9qry("#oComments").empty() ;
			for(var i=0;i<comments_array.length;i++){
				var newComment = $n9qry("<ul><li class='cComment_line'><b>"+(i)+"#&nbsp;"+comments_array[i].name+"</b> 于 "+comments_array[i].time+"</li>"+
										"<li>"+comments_array[i].comment+"<br /><br /></li></ul>");
				newComment.appendTo("#oComments");
			}
		}catch(e){
				alert('Wrong'+e);
		}
	}
}
function getPosition(e){                //获取元素相对文档的绝对位置
	var left = 0;
	var top  = 0;
	while (e.offsetParent){
		left += e.offsetLeft;
		top  += e.offsetTop;
		e     = e.offsetParent;
	}
	left += e.offsetLeft;
	top  += e.offsetTop;
	return {x:left, y:top};
}
function getMouseOffset(target, ev){                //        获取鼠标相对元素的相对位置
	ev = ev || window.event;
	var elementPos    = getPosition(target);
	var mousePos  = mouseCoords(ev);
	return {x:mousePos.x - elementPos.x, y:mousePos.y - elementPos.y};
}

function mouseCoords(ev){                //获取鼠标相对文档的绝对位置
	if(ev.pageX || ev.pageY){
		return {x:ev.pageX, y:ev.pageY};
	}
	return {
		x:ev.clientX + document.body.scrollLeft - document.body.clientLeft,
		y:ev.clientY + document.body.scrollTop  - document.body.clientTop
	};
}

$n9qry(document).ready(function(){
	setInterval("wedTimeInterval()",1000);
	$n9qry.get("action.php?action=getvisitcount",function(data){$n9qry("#oWebVisits").html(data);});
});
