//info 出价信息，节拍时间
//price 当前出价 我的出价
//bid 买家出价 ip 时间
//context 上半信息
//boold血统书
/**
1、初始化 -ready
2、显示，隐藏内容-showContext(id)
3、隐藏内容-hideContext(id)
4、显示价格--showPrice(id,obj,lang)
5、输出价格--getRemainderTime()
6、隐藏价格--hidePrice(id)
*/
var mousePositionTop;//坐标
var jsid;//序号id
var count = 0;//开始和结束的时间差
var timeThreadOne;//第一层线程
var timeThreadTwo; //第二层线程,用于不选检测剩余时间
var timeThreadThree;//第三层线程，用于小于5分钟的异步
var timeThreadFour;//第4层线程，用于小于5分钟的的倒计时

var jslang;//用于倒计时的显示，因为参数无法传递
var preNoteId=0;//上一个点开节点id

/* 开始全部收缩*/
$(document).ready(function (){
  var i=1;
  while($("#title"+i).length > 0){
 	$("#context"+i).hide();
	$("#info"+i).hide();
	$("#price"+i).hide();
	$("#bid"+i).hide();
	$("#priceButtom"+i).hide();
   	i++;
  }
	
});

 
 	
 	/**
 	显示内容，其他关闭，得到坐标位置，方便收回定位
 	1、显示或者是隐藏内容部分
 	2、隐藏上一个点开的节点
 	3、得到newsid
 	4、增加点击率 get方法
 	5、获取节点title的纵坐标，赋值给全局变量mousePositionTop
 	6、依据全局变量定位页面
 	*/
	function showContext(id){
	if($("#context"+id).is(":visible")){
		$("#context"+id).hide();
	}else{
		$("#context"+id).show(1000);
	}
	 
	 if(preNoteId != id){
	 	$("#context"+preNoteId).hide();//关闭内容，需要判断是否重复点开
	}
	$("#price"+preNoteId).hide(); 
	$("#info"+preNoteId).hide();
	$("#bid"+preNoteId).hide();
	$("#priceButtom"+preNoteId).hide();
	preNoteId = id;
	var newsid = $("#newsid"+id).attr("value");
	if($("#context"+id).is(":visible")){
		$.post("PriceAndIpManager.shtml",{action:"addClick",newsid:newsid});//点击率+1
		//提示框
		Dialog.alertNoneButton("正在加载内容，请稍候...(Page is loading, please wait. . .)");
		$.post("PriceAndIpManager.shtml",{action:"ShowContent",newsid:newsid},function(data){
		// 返回content
		Dialog.close();
		$("#contenttd"+id ).empty();
		$("#contenttd"+id ).append(data);
		mousePositionTop =  $("#title"+id).offset().top;
		window.scrollTo(0,mousePositionTop);
		});
	}//end content is viseble
	
    }  
	
	
	/**
	1、隐藏内容节点
	2、依据全局变量定位页面
	**/
	function hideContext(id){
	 $("#context"+id).hide(1000);
	 window.scrollTo(0,mousePositionTop);
    } 
	
	
	//显示价格  关闭其他     显示时间 这里的 obj参数已经不存在意义，为了兼容之前没有除掉
	//这里的时间是从hidden input得到
	/**
	参数说明：id节点。
			 obj不存在意义，但为了页面代码不变保留。返回的data付给obj，obj再付给count
			 lang语言 6-9
			 
	1、lang赋值给全局变量jslang
	2、得到newsid
	3、如果info节点是展示的。点击率+1，get方法，append(data)
	4、get方法，得到时间、赋值给全局变量count，如果info节点是显示的，setInterval调用
	   getRemainderTime实现倒计时
	5、显示或者隐藏出价等节点
	6、隐藏上一次点开的节点
	7、把title的纵坐标赋值给mousePositionTop
	8、id赋值给jsid，用于显示倒计时myRemainderTime
	9、定位页面的纵坐标
	*/
	function showPrice(id,obj,lang){
	jslang = lang;
	var newsid = $("#newsid"+id).attr("value");
	if($("#info"+id).is(":hidden")){
	//点击率+1，并追加显示
	$.post("PriceAndIpManager.shtml",{action:"addClick",newsid:newsid},function(data){
		$("#click"+id).empty();
		$("#click"+id).append(data);
		});
	
	}// end 追加点击率
	
	//对时间的处理
	
	  $.post("PriceAndIpManager.shtml?",{action:"getTime",newsid:newsid},function(data){
	    //alert("对时间的处理");
		 count = data;//转为全局
		 clearInterval(timeThreadOne);
		 clearInterval(timeThreadTwo);//停止循环剩余时间
		 clearInterval(timeThreadThree);//3
		 clearInterval(timeThreadFour);
		 if($("#info"+id).is(":visible")){
		 	//第一层 循环倒计时
			timeThreadOne =window.setInterval("getRemainderTime()",1000);
			 //循环剩余时间
			 timeThreadTwo = window.setInterval(function(){
			  clearInterval(timeThreadThree);
			 	 if(count < 300 && count > 0){
			 	   //如果时间小于5分钟停止上一层的倒计时,开始下面的倒计时
				 	    timeThreadThree = window.setInterval(function(){
						if( $("#info"+id).is(":visible")){
						$.post("PriceAndIpManager.shtml?",{action:"getTime",newsid:newsid},function(data){
						//有人出价
						//alert("有人出价");
						var postMark = 0;
						if(Number(data) - Number(count) > 300){
							count = data;//转为全局
							postMark =1 ;
						}
						 if($("#info"+id).is(":visible") && (postMark)){
							 clearInterval(timeThreadOne);
							 clearInterval(timeThreadFour);
						 	 //这里再次调用
							 timeThreadFour =window.setInterval("getRemainderTime()",1000);
							 
						 }
						
					   });
						}
					
				   },15000);// end 异步
			 }//end 5分钟内.问题出现在这块
			 
			 },15000);//end 循环剩余时间 
			 //time = timetemp;
		    
		 }
		
	});
	
	 $("#info"+id).toggle();
	 $("#price"+id).toggle();
	 $("#bid"+id).toggle();
	 $("#priceButtom"+id).toggle();
	 
	 if(preNoteId != id){
	 
     $("#info"+preNoteId).hide();
	 $("#price"+preNoteId).hide();
	 $("#bid"+preNoteId).hide();
	 $("#priceButtom"+preNoteId).hide();
	 
	 }
	 $("#context"+preNoteId).hide();
	 
	 preNoteId = id;
	 mousePositionTop =  $("#title"+id).offset().top;
	 jsid =id;
	 window.scrollTo(0,mousePositionTop);
    } 
    
    //计算时间，并输出
	function getRemainderTime(){
	count = count - 1;
	
	var d = count / (86400);
	var h = (count % (86400)) / (3600);
	var m = ((count % (86400)) % (3600)) / 60 ;
	var s = ((count % (86400)) % (3600)) % 60 ;

	var strTime = "";
	if(d <= 0 && h <= 0 && m <= 0 && s <= 0 ){
	 if(jslang == 6){
	 	strTime = "<font color=red>拍卖已结束</font>";
	 	$("#showpriceimg-"+jsid).attr("src","images/button/end_auction_cn.gif");
	 }
		
	 else if(jslang ==7){
	 	strTime = "<font color=red>Auction is ended</font>";
	 	$("#showpriceimg-"+jsid).attr("src","images/button/end_auction_en.gif");
	 }	
	    
	 else if(jslang  == 8) {
	 	strTime = "<font color=red>Veiling is afgelopen</font>";
	 	$("#showpriceimg-"+jsid).attr("src","images/button/end_auction_nl.gif");
	 }  
	 	
	 else if(jslang == 9){
	 	strTime = "<font color=red>拍賣已結束</font>";
	 	$("#showpriceimg-"+jsid).attr("src","images/button/end_auction_tw.gif");
	 }
	 //结束倒计时的运行 
	 clearInterval(timeThreadThree);  
	 clearInterval(timeThreadOne);
	 clearInterval(timeThreadTwo);
	 clearInterval(timeThreadThree);
	 clearInterval(timeThreadFour);   
	  //清理公共 变量
	  count = null;
	  timeThreadOne = null;
	  timeThreadTwo = null;
	  timeThreadThree = null;
	  timeThreadFour = null;
	  jslang = null;
	}
	else{
	
	if( jslang == 6 || jslang == 9)
		strTime = "<font color=red>"+Math.floor(d) + "</font><font color=blue>天</font>" + " - <font color=red>"+Math.floor(h) + "</font><font color=blue>小时</font>" + " - <font color=red>"+ Math.floor(m) + "</font><font color=blue>分钟</font>  " + " - <font color=red>"+ s + "</font><font color=blue>秒</font>";
	else if(jslang == 7)
	    strTime = "<font color=red>"+Math.floor(d) + "</font><font color=blue>Day</font>" + " - <font color=red>"+Math.floor(h) + "</font><font color=blue>Hours</font>" + " - <font color=red>"+ Math.floor(m) + "</font><font color=blue>Mins</font>  " + " - <font color=red>"+ s + "</font><font color=blue>Seconds</font>";
	else if(jslang == 8)
	     strTime = "<font color=red>"+Math.floor(d) + "</font><font color=blue>Dagen</font>" + " - <font color=red>"+Math.floor(h) + "</font><font color=blue>Uren</font>" + " - <font color=red>"+ Math.floor(m) + "</font><font color=blue>Minuten</font>  " + " - <font color=red>"+ s + "</font><font color=blue>Seconden</font>";
	}
	
	  
	$("#myRemainderTime"+jsid).html(strTime);
}
/*
		1、隐藏出价信息
		2、定位页面坐标
		3、关闭倒计时的运行
	*/
	function hidePrice(id){
	$("#info"+id).hide(1000);
	 $("#price"+id).hide(1000);
	 $("#bid"+id).hide(1000);
	 $("#priceButtom"+id).hide(1000);
	 window.scrollTo(0,mousePositionTop);
	 //结束倒计时的运行 
	 clearInterval(timeThreadThree);  
	 clearInterval(timeThreadOne);
	 clearInterval(timeThreadTwo);
	 clearInterval(timeThreadThree);
	 clearInterval(timeThreadFour);  
	}
