// JavaScript Document

function bidMasterNew(itemid, type) {
	if(type == "single") {
		var fromBid = document.getElementById("Bids").value;
	}
	else if(type == "serial") {
		var fromBid = document.getElementById("fromBids").value;
		var toBid = document.getElementById("toBids").value;
	}
	
	$.post("/ajax/auction/setBid.php", { "fromBid": fromBid, "toBid": toBid, "ItemID": itemid, "type": type }, function(data){
		//$("div#testdiv").fadeIn("slow").before("<p>TEST</p>");
		$("div#bidResponse").html(data).hide().fadeIn("slow");
		getBidsNew(itemid);
		getCreditNew();
		//alert("Data Loaded: " + data);
	});
}

function getBidsNew(itemid, count, active) {
	$.post("/ajax/auction/getBids.php", { "ItemID": itemid, "Count": count, "Active": active }, function(data){
		$("div#allBids"+itemid).html(data).hide().fadeIn("slow");
	});
}

function getCreditNew() {
	$.post("/ajax/auction/credit.php", function(data){
		$("div#cashcontainer").html(data).hide().fadeIn("slow");
	});
}

function startPriceDrop(itemid) {
	$.post("/ajax/auction/pricedrop.php", { "ItemID": itemid }, function(data) {
		$("#priceDropContainer"+itemid).html(data); //.hide().fadeIn("fast");
	});
	var value = $("#priceDropWinner"+itemid).val();
	if(value == 0) {
		setTimeout("startPriceDrop('"+itemid+"')", 5000);
	}
}

function pricedropBuy(itemid) {
	$.post("/ajax/auction/pricedropBuy.php", { "ItemID": itemid }, function(data) {
		$("#pricedropWinnerText").html(data);
		startPriceDrop(itemid);
	});
}

function startPenny(itemid) {
	$.post("/ajax/auction/penny.php", { "ItemID": itemid }, function(data) {
		$("#pennyContainer"+itemid).html(data); //.hide().fadeIn("fast");
	});
	setTimeout("startPenny('"+itemid+"')", 1000);
	/*
	var value = $("#priceDropWinner"+itemid).val();
	if(value == 0) {
		setTimeout("startPriceDrop('"+itemid+"')", 5000);
	}
	*/
}

function pennyPlace(itemid) {
	$.post("/ajax/auction/pennyPlace.php", { "ItemID": itemid }, function(data) {
		$("#pennyText"+itemid).html(data); //.hide().fadeIn("fast");
		getCreditNew();
	});
	//startPenny(itemid);
}

function startClock(itemid, container) {
	//$("#testResponse").append("startClock " + itemid + " " + container +"<br />");
	$.post("/ajax/auction/timeCheckItem.php", { "ItemID": itemid }, function(data) {
		//$("#testResponse").append(data + "<br />");
		//$("#"+container).html("Testing.. ");
		//alert(data);
		var newTime = "";
		delete newTime;
		var newTime = new Date(data);
		//$("#testResponse").append(newTime + "<br />");
		//$("#"+container).countdown({until: newTime, compact: true, serverSync: serverTime, onExpiry: lastSecCheck(itemid, container)});
		$("#"+container).countdown("destroy");
		$("#"+container).countdown({until: newTime, compact: true, serverSync: serverTime, onExpiry: function() {
				//$("#testResponse").append("First " + itemid + " " + container + "<br />");
				$("#"+container).html("Please wait!");
				$.post("/ajax/auction/timeCheck.php", { "ItemID": itemid }, function(data){
					if(data == "1") {	
						//alert("Auction finished!");
						reloadSite();
					}
					else {
						updateClock(itemid, container);
						delete newTime;
						//$("#defaultCountdown").countdown({until: newTime, compact: true, serverSync: serverTime, onExpiry: lastSecCheck});
					}
				});
			}
		});
	});
	//$("#defaultCountdown").countdown({until: newTime, compact: true, serverSync: serverTime, onExpiry: lastSecCheck});
}

function updateClock(itemid, container) {
	//$("#testResponse").append("New " + itemid + " " + container +"<br />");
	$("#"+container).empty();
	startClock(itemid, container);
}

function serverTime() { 
	var time = null; 
	$.ajax({url: '/ajax/serverTime.php', 
			async: false, dataType: 'text', 
			success: function(text) { 
					//alert(text);
					time = new Date(text); 
			}, error: function(http, message, exc) { 
					time = new Date(); 
	}}); 
	return time; 
}

function lastSecCheck(itemid, container) {
	$("#testing").html(itemid + " " + container);
	$("#"+container).html("Please wait!");
	$.post("/ajax/auction/timeCheck.php", { "ItemID": itemid }, function(data){
		if(data == "1") {
			//alert("Auction finished!");
			reloadSite();
		}
		else {
			startClock(itemid, container);
			//$("#defaultCountdown").countdown({until: newTime, compact: true, serverSync: serverTime, onExpiry: lastSecCheck});
		}
	});
}

function reloadSite() {
	window.location.reload(true);
}

function showThread(id) {
	if(document.getElementById("ThreadID"+id).style.display == "none") {
		$("#ThreadID"+id).fadeIn('slow');
		$("#threadShow"+id).hide();
		$("#threadHide"+id).show();
		$.post("/ajax/support/readThread.php", { "ThreadID": id });
	}
	else {
		$("#ThreadID"+id).slideUp('slow');
		$("#threadShow"+id).show();
		$("#threadHide"+id).hide();
	}
}

