var timers = new Array();
var intervals = new Array();
var t_bypass = 0;

function startTimer(item_id) {
	if ($('#timer_' + item_id).length > 0) {
		var elem = $('#timer_' + item_id);
		var hour = $(elem).children('.hour');
		var minute = $(elem).children('.minute');
		var second = $(elem).children('.second');
		clearInterval(timers[item_id]);
		
		timers[item_id] = setInterval(function () {
			var h = parseFloat($(hour).html());
			var m = parseFloat($(minute).html());
			var s = parseFloat($(second).html());
			
			if (h == 0 && m == 0 && s == 0) {
				clearInterval(timers[item_id]);
				finishAuction(item_id);
				return false;
			}
			
			if (h == 0 && m == 0 && s <= 31) {
				$("#timer_"+item_id).css("color","#DA3853");
			}
			else {
				$("#timer_"+item_id).css("color","#000000");
			}
				
			if (s == 0) {
				if (m == 0) {
					if (h == 0) {
						clearInterval(timers[item_id]);
						finishAuction(item_id);
						return false;
					}
					else {
						$(hour).html(padZero(h - 1));
						$(minute).html(59);
					}
				} 
				else {
					$(minute).html(padZero(m - 1));
				}
				$(second).html(59);
			}
			else {
				$(second).html(padZero(s - 1));
			}
			
		},1000);

	}
}

function finishAuction(item_id) {
	closeBid(item_id);
	clearInterval(timers[item_id]);
	clearInterval(intervals[item_id]);
	//$("#timer_" + item_id).css("color","#000000");
	//$('#timer_' + item_id).html('Auction Over');
	$('#timer_' + item_id).html('<img src="images/loading.gif"/>');
	$('#bidboton_' + item_id).css('display','none');
	
	$.get("ajax.confirm.php",{item_id:item_id},function (data) {
		if (data == 'Y') {
			ajaxGetPage('ajax.finished.php?item_id='+item_id,'bid_item_'+item_id);
		}
		else {
			$('#bidboton_' + item_id).css('display','block');
			ajaxGetPage('ajax.bid.php?&item_id='+item_id+'','high_bid_box_'+p_id,false,'stretch');
		}
	} );
}

function padZero(num) {
	if (num < 10) 
		return '0'+num;
	else 
		return num;
}

function openBid(elem,id,min_increase,initial_bid) {
	maskOn();

	var high_bid = $('#gethigh_'+id).html();
	high_bid = high_bid.replace('<compare>','');
	high_bid = high_bid.replace('</compare>','');
	high_bid = high_bid.replace('<COMPARE>','');
	high_bid = high_bid.replace('</COMPARE>','');
	high_bid = parseFloat(high_bid);
	high_bid = (isNaN(high_bid)) ? 0 : high_bid; 	
	high_bid = high_bid.toFixed(2);
	
	var high_proxy = $('#getproxy_'+id).attr('value');
	high_proxy = parseFloat(high_proxy);
	high_proxy = (isNaN(high_proxy)) ? 0 : high_proxy; 	
	high_proxy = high_proxy.toFixed(2);
	
	if (high_proxy > 0 && high_proxy > high_bid)
		$('#heighest').show();
	else if (high_proxy <= high_bid)
		$('#heighest').hide();
	
	$('#item_id').attr('value',id);
	$('#bid_current').html(high_bid);
	$('#bid_max').html(high_proxy);
	min_increase = (high_bid > 0) ? min_increase:initial_bid;
	$('#bid_min').html('$'+parseFloat(min_increase).toFixed(2));
	$('#bid_amount').attr('value',(parseFloat(high_bid) + parseFloat(min_increase)).toFixed(2));
	$('#bidbox').fadeIn(600);
	$('#bid_amount').focus();
	$('#bid_amount').select();
}


function closeBid(item_id) {
	if (item_id) {
		if (item_id != $('#item_id').attr('value'))
			return false;
	}
	$('#bidbox').fadeOut(600);
	maskOff();
}

function placeBid(elem) {
	closeBid();
	
	var p_id = $(elem).siblings('#item_id').attr('value');
	var bid_amount = $('#bid_amount').attr('value');
	var item_id = $('#item_id').attr('value');

	ajaxGetPage('ajax.bid.php?bid_amount='+bid_amount+'&item_id='+item_id+'&bid_placed=1','high_bid_box_'+p_id,false,'stretch');
	t_bypass = item_id;
}

function refreshBid(item_id) {
	if (item_id.length > 0) {
		clearInterval(intervals[item_id]);
		intervals[item_id] = setInterval(function() {		
			if (t_bypass != item_id) {
				ajaxGetPage('ajax.bid.php?item_id='+item_id,'high_bid_box_'+item_id,false,'stretch');
			}
			else {
				t_bypass = 0;
			}
		},3000);
	}	
}

function maskOn() {
	if (!($('#mask').length > 0)) {
		$('body').prepend('<div id="mask"></div>');
		$('#mask').height($('body').height());
		$('#mask').width($('body').width());
	}
	$('#mask').fadeIn(600);
	document.getElementById('mask').style.filter  = "alpha(opacity=50)";

}

function maskOff() {
	if ($('#mask').length > 0) {
		$('#mask').fadeOut(600);
	}
}

function openHist(item_id) {
	maskOn();
	$('#hist_box').fadeIn(600);
	ajaxGetPage('ajax.hist.php?auction_id='+item_id,'hist_box');
}

function closeHist() {
	$('#hist_box').fadeOut(600);
	maskOff();
}
