var flagg =1;//1表示焦点不在这里
$(document).ready(function () {
	$('#nextPrice').focus(function(){
		flagg =2;
	});

	$('#nextPrice').blur(function(){
		flagg =1;
	});

	//增加一个定时器函数
	setInterval("getBidData()",5000);
});

//每5秒发送一次请求
function getBidData()
{
	curHref = window.location.href;
	
	//截取BookingAuction/auction/后面的字符串
	curPos = curHref.indexOf("domain/");
	curParams = curHref.substring(curPos,curHref.length);

	newURL = "/BookingopenAuction/ajaxbiddata/"+curParams+"/currand/"+Math.round(Math.random()*1000000);
	//alert(newURL);

	$.getJSON(newURL, {},
		function(data){
			document.getElementById("action_offer").innerHTML = data['action_offer'];
			document.getElementById("auction_times").innerHTML = data['auction_times'];
			document.getElementById("max_price").innerHTML = data['max_price'];

			if(flagg ==1)document.getElementById("nextPrice").value = data['nextPrice'];

			document.getElementById("endAuctionTime").innerHTML = data['endAuctionTime'];
			document.getElementById("leader").innerHTML = data['leader'];
		});
}

function getBidInfo(){
	flagg = 2;//nextprice得到焦点
	$('#sbm').attr('disabled','disabled');
	price = $("#nextPrice").val();
	uid = $("#user_id").val();
	bid = $("#booking_id").val();
	dm = $("#getDomain").text();
	dm1 = $("#domain1").val();
	if( uid !=0)
	{
		$.getJSON("/BookingopenAuction/getAuction", {userId: uid,price: price,bidId:bid},
			function(json){
				var popdiv = '<div id="append" class="hide"></div><div id="pop" class="hide"><div id="close"><a href="javascript:popdown();">关闭</a></div><div class="msgbox"><ul class="form"><li >'+json.msg+'</li>';
				if(json.state == true) popdiv = popdiv+ '<li class="submit"><span><input class="btn_submit" type="button" value="确认出价" onclick="javascript:bidPrice();" /></span><span><input class="btn_submit" type="button" value="取消" onclick="javascript:popdown();" /></span></li></ul></div></div>'
				else  popdiv = popdiv+ '<li class="submit"> <a href="/Finance/addFund/jumper/BookingopenAuction_auction_domain_'+dm+'">现在充值</a> &nbsp;&nbsp;&nbsp; <span><input class="btn_submit" type="button" value="取消" onclick="javascript:popdown();" /></span></li></ul></div></div>'
				$('#container').after(popdiv);
				popup();
			}
		);
	}
	else
	{
		window.location.href='/BookingopenAuction/getauction?domain='+dm1;
	}
}

function popup(w,h) 
{
	var scrollWidth = document.body.scrollWidth;
	var scrollHeight = document.body.scrollHeight;
	var clientHeight = document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body.clientHeight;
	var scrollTop = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;
	if(clientHeight > scrollHeight){scrollHeight = clientHeight;}
	if(w == undefined){w = 560;}
	if(h == undefined){h = 248;}

	$('#append').removeClass('hide').addClass('show').css({
        position: "absolute",
        left: "0px",
        top: "0px",
        zIndex: '998',
        width: '100%',
        height: scrollHeight + 'px',
        backgroundColor: '#000',
        filter: "progid:DXImageTransform.Microsoft.Alpha(opacity=40,finishOpacity=100,style=0)",
        opacity: 0.4
	});

	$('#pop').addClass('show').removeClass('hide').css({
			position: "absolute",
			padding: '0',
			display: '',
			left: (scrollWidth-w)/2 + 'px',
			top: ((clientHeight - h) / 2 + scrollTop) + 'px',
			width: w + 'px',
			/*height: h + 'px',*/
			zIndex: '999',
			backgroundColor: '#FFF',
			overflow: 'hidden'
		});
}

function popdown(refresh) {
	$('#append').addClass('hide');
	$('#pop').addClass('hide');
	$('#sbm').attr('disabled','');
	if(refresh != undefined) document.location.reload();
}

//submit
function bidPrice()
{
	popdown();
	price = $("#nextPrice").val();
	uid = $("#user_id").val();
	bid = $("#booking_id").val();
	$.getJSON("/BookingopenAuction/bid",{userId: uid,price: price,bidId:bid},
		function(json){
			if(json.state == true) {
				var popdiv = '<div id="pop" class="hide"><div id="close"><a href="javascript:popdown();">关闭</a></div><div class="success msgbox"><h6>成功</h6><ul><li>'+json.msg+'</li><li class="submit"><span><input class="btn_submit" type="button" value="确定" onclick="javascript:popdown(true);" /></span></li></ul></div>';
			}
			else
			{
				var popdiv = '<div id="pop" class="hide"><div id="close"><a href="javascript:popdown(true);">关闭</a></div><div class="error msgbox"><h6>出错了</h6><ul><li>'+json.msg+'</li><li class="submit"><span><input class="btn_submit" type="button" value="确定" onclick="javascript:popdown(true);" /></span></li></ul></div>';
			}
			$('#container').after(popdiv);
			popup(560,248);
		}
	);
}

function showLeftTime(times){
    if(times <= 0){
        $("#lefttime").html("<span>0</span>天<span>0</span>小时<span>0</span>分<span>0</span>秒");
        return;
    }
    var msPerDay=24*60*60;//*1000  
    var e_daysold=times/msPerDay  
    var daysold=Math.floor(e_daysold);  
    var e_hrsold=(e_daysold-daysold)*24;  
    var hrsold=Math.floor(e_hrsold);  
    var e_minsold=(e_hrsold-hrsold)*60;  
    var minsold=Math.floor(e_minsold);  
    var seconds=Math.floor(times-daysold*msPerDay-hrsold*60*60-minsold*60);
    if(minsold<10){
        minsold= "0" + minsold;
    }
    if(seconds<10){
        seconds= "0" + seconds;
    }
    if(daysold>0){
        var str = "<span>" + daysold + "</span>天<span>" + hrsold + "</span>小时<span>" + minsold + "</span>分<span>" +seconds + "</span>秒"; 
    }else{
        var str = "<span>" +  hrsold + "</span>小时<span>" + minsold + "</span>分<span>" +seconds + "</span>秒"; 
    }
    //alert(str);
    $("#lefttime").html(str);
    times -= 1;
    if(times<=0){
        clearTimeout(timeoutHandler);
        window.location.href = window.location.href;
        return;
    }
    timeoutHandler = setTimeout("showLeftTime(" + times + ")", 1000);
}


