function getdata(auctionlist)
{
	if (auctionlist == 'null') return;

	$.getJSON("/auction/maxbids/auctions/"+auctionlist, function(data) {
		$.each(data.auctions, function(i,auctions) {
			if (auctions.bid == '') auctions.bid = 0;
			if (
				($("#auction_"+auctions.id+"_bid").html() != auctions.bid)
				||
				($("#auction_"+auctions.id+"_bid").attr('rel') != $("#currency a.select").html())
				||
				(($("#auction_"+auctions.id+"_bidder").html() == '') && auctions.bidder != '')
				) {
				$("#auction_"+auctions.id+"_bid").attr('rel',$("#currency a.select").html());

				// Did a bid just occur?
				if (
					($("#auction_"+auctions.id+"_bid").html() != "Bid")
					&&
					(($("#auction_"+auctions.id+"_bid").html() != auctions.bid)
						||
					($("#auction_"+auctions.id+"_bidder").html() == ''))
					) {
					$("#auction_"+auctions.id).addClass('justbid');
					$(".bidContent").addClass('justbid');
				}

				// Set next bid amount automatically
				var currentbid = parseFloat(auctions.bid);
				var newbid = currentbid + 1;
				if (currentbid >= 100) {
					newbid = currentbid + 5;
				}
				if (currentbid >= 1000) {
					newbid = currentbid + 10;
				}

				// If no bids, set the next bid amount at the starting price
				if (auctions.bidder == '') newbid = currentbid;

				// Calculate local currency price
				var convertedbid = Math.round($("#currency a.select").attr("rel") * currentbid);
				var saveamount = parseFloat($("#auction_"+auctions.id+"_retail").html())-currentbid;

				$("#auction_"+auctions.id+"_newbid").attr('value', newbid);
				$("#auction_"+auctions.id+"_save").html(saveamount);

				if (saveamount <= 0) {
					$("#auction_"+auctions.id+" .save").html("");
					$("#auction_"+auctions.id+" .retail").html("");
				}

				var currency = $('#currency a.select span').html();
				if (currency == 'USD') {
					$("#auction_"+auctions.id+"_currency").html('');
				} else {
					$("#auction_"+auctions.id+"_currency").html('About ' + convertedbid + ' ' + currency);
				}

				if ($("#loginid").html() == auctions.bidder) {
					$("#auction_"+auctions.id).addClass('winning');
					$(".bidContent").addClass('winning');
					$("#auction_"+auctions.id+"_bidder").html("You!");
				} else {
					$("#auction_"+auctions.id).removeClass('winning');
					$(".bidContent").removeClass('winning');
					$("#auction_"+auctions.id+"_bidder").html(auctions.name);
				}
			} else {
				$("#auction_"+auctions.id).removeClass('justbid');
				$(".bidContent").removeClass('justbid');
			}

			$("#auction_"+auctions.id+"_bid").html(auctions.bid);
			$("#auction_"+auctions.id+"_time").html(auctions.time);

			if (auctions.sec < 600) {
				$("#auction_"+auctions.id).addClass('ending');
				$(".bidContent").addClass('ending');
			}

			if (auctions.sec < 60) {
				$("#auction_"+auctions.id+'_time').css('color','#993333');
				setTimeout('cleartimeflash()', 1000);
			}

			if (
				(auctions.sec < 0) 
				||
				(auctions.state != 'active')
				) {
				$("#auction_"+auctions.id+" .timeLeft").hide();
				$("#auction_"+auctions.id+" .bid").hide();
				$(".bidContent .timeLeft").closest('tr').hide();
				$(".bidContent .bid").closest('tr').hide();
				$("#maxbidbuddy").closest('tr').hide();

				if (auctions.state != 'active') {
					if (auctions.state == 'cancelled') {
						$("#auction_"+auctions.id+" .unsold").show();
					} else if (	(auctions.state == 'pendingReserveOffer') ||
								(auctions.state == 'pendingSecondOffer') ||
								(auctions.state == 'pendingMoreStockOffer'))
								{
						$("#auction_"+auctions.id+" .offer").show();
					} else {
						if ($("#auction_"+auctions.id+" .sold").is(":visible")) {
						} else {
							var flashvars = {};
							var params = {};
							params.wmode = "transparent";
							var attributes = {};
							swfobject.embedSWF("/images/sold_hammer.swf", "auction_sold_"+auctions.id, "225", "300", "9.0.0", false, flashvars, params, attributes);
							swfobject.embedSWF("/images/sold_hammer.swf", "auction_soldlg_"+auctions.id, "340", "340", "9.0.0", false, flashvars, params, attributes);
							setTimeout('$("#auction_sold_'+auctions.id+'").hide()', 8000);
							setTimeout('$("#auction_soldlg_'+auctions.id+'").hide()', 8000);
							$("#auction_"+auctions.id+" .sold").show();
						}
					}

					if ((auctions.state == 'pendingPayment') && ($("#loginid").html() == auctions.bidder)) {
						$("#auction_"+auctions.id+" .checkout").show();
					} else if (!(	(auctions.state == 'pendingReserveOffer') ||
									(auctions.state == 'pendingSecondOffer') ||
									(auctions.state == 'pendingMoreStockOffer'))) {
						$("#auction_"+auctions.id+" .findmore").show();
					}

				}

			}

			if (auctions.reserve == "-1") {
				if (auctions.bid > 1) {
					$("#auction_"+auctions.id+"_1dnoreserve").hide();
					$("#auction_"+auctions.id+"_noreserve").show();
				} else {
					$("#auction_"+auctions.id+"_1dnoreserve").show();
					$("#auction_"+auctions.id+"_noreserve").hide();
				}
				$("#auction_"+auctions.id+"_reservemet").hide();
				$("#auction_"+auctions.id+"_reservenotmet").hide();
			} else if (auctions.reserve == "1") {
				$("#auction_"+auctions.id+"_1dnoreserve").hide();
				$("#auction_"+auctions.id+"_noreserve").hide();
				$("#auction_"+auctions.id+"_reservemet").show();
				$("#auction_"+auctions.id+"_reservenotmet").hide();
			} else {
				$("#auction_"+auctions.id+"_1dnoreserve").hide();
				$("#auction_"+auctions.id+"_noreserve").hide();
				$("#auction_"+auctions.id+"_reservenotmet").show();
				$("#auction_"+auctions.id+"_reservemet").hide();
			}
		});

		if (data.offers > 0) {
			$('#offernotice').show();
			$('#offersnumber').html(data.offers);
		} else {
			$('#offernotice').hide();
		}

		if (data.outbids > 0) {
			$('#outbidnotice').show();
			$('#outbidnumber').html(data.outbids);
		} else {
			$('#outbidnotice').hide();
		}

	});
}

function cleartimeflash(id) {
	$(".timeLeft span").css('color','#333333');
}

var clicking = false;

function theRotator() {
	$('div.rotator').each(function (i) {
		var thisid = $(this).attr('id');

		var auctionid = thisid.substr(8);

		var count = $(this).find('ul li').size();

		if (count > 1) {
			var interval = 20000 / count;
			setTimeout('rotate('+auctionid+','+interval+')',interval);
		}
		var pic = 0;
		var rotatorid=$(this).attr('id');
		$(this).find('ul li').each(function (i) {
			pic++;
			$(this).attr('id',rotatorid+'_'+pic);
		});
	});
}

function rotate(auctionid, interval) {
	if (!clicking) {
		var rotator = $("#rotator_"+auctionid);

		//Get the first image
		var current = (rotator.find('ul li.show') ? rotator.find('ul li.show') : rotator.find('ul li:first'));

		//Get next image, when it reaches the end, rotate it back to the first image
		var next = ((current.next().length) ? ((current.next().hasClass('show')) ? rotator.find('ul li:first') :current.next()) : rotator.find('ul li:first'));	

		//Set the fade in effect for the next image, the show class has higher z-index
		next.addClass('show');

		//Hide the current image
		current.removeClass('show');
	}

	setTimeout('rotate('+auctionid+','+interval+')',interval);
}

function getnewsecurity() {
	var newcode=Math.floor(Math.random()*255);
	$('#securekey').attr('value', newcode);
	$('#secureimage').attr('src', '/secureimage?id='+newcode+'&width=190&height=45&size=18&background=F8F8FF&border=000000&grid=E5E5FF&colour=000000');
}

jQuery(document).ready(function($) {
	getdata($("#auctionbiddinglist").html());
	setInterval( "getdata('"+$("#auctionbiddinglist").html()+"')", 2000);

	function placebid(auction) {
		var amount = $("#auction_"+auction+"_newbid").attr("value");
		var prevamount = $("#auction_"+auction+"_bid").html();
		if ((amount - prevamount) > 10) {
			var answer = confirm("Confirm your bid of US$"+amount);
			if (!answer) {
				return false;
			}
		}
		$.getJSON("/auction/placebid/id/"+auction+"/amount/"+amount, function(data) {
			getdata($("#auctionbiddinglist").html());
		});
	}

	$(".auction_placebid").click(function() {
		var auction = ($(this).attr("rel"));
		placebid(auction);
	});

	$(".auction_placebid_amount").keypress(function(event) {
		if (event.keyCode == '13') {
			var auction = ($(this).attr("rel"));
			placebid(auction);
		}
	});

	function placebidbuddy(auction) {
		var amount = $("#auction_"+auction+"_newbidbuddy").attr("value");
		var answer = confirm("Confirm your maximum bid of US$"+amount);
		if (!answer) {
			return false;
		}
		$.getJSON("/auction/placebidbuddy/id/"+auction+"/amount/"+amount, function(data) {
			getdata($("#auctionbiddinglist").html());
			$("#maxbidbuddy").html($("#auction_"+auction+"_newbidbuddy").attr("value"));
		});
	}

	$(".auction_placebidbuddy").click(function() {
		var auction = ($(this).attr("rel"));
		placebidbuddy(auction);
	});

	$(".auction_placebidbuddy_amount").keypress(function(event) {
		if (event.keyCode == '13') {
			var auction = ($(this).attr("rel"));
			placebidbuddy(auction);
		}
	});

	function makeoffer(auction) {
		var amount = $("#auction_"+auction+"_makeoffer").attr("value");
		var answer = confirm("Confirm your offer of US$"+amount);
		if (!answer) {
			return false;
		}
		var url = "/auction/makeoffer/id/"+auction+"/amount/"+amount;
		$("#addtowatchlistresult").load(url);
	}

	$(".auction_makeoffer").click(function() {
		var auction = ($(this).attr("rel"));
		makeoffer(auction);
	});

	$(".auction_makeoffer_amount").keypress(function(event) {
		if (event.keyCode == '13') {
			var auction = ($(this).attr("rel"));
			makeoffer(auction);
		}
	});

	var selectopen = false;
	$(".cat-list").hide();
	$("a.select").click(function() {
		if (selectopen) {
			$(".cat-list").hide();
			selectopen = false;
		} else {
			selectopen = true;
			$(".cat-list").hide();
			$(this).next('div').show();
		}
		return false;
	});

	$("body").click(function () {
		if (selectopen) {
			$(".cat-list").hide();
			selectopen = false;
			return false;
		}
	});

	$(".cat-list a").click(function() {
		url = $(this).attr('href');
		if (url == '#') {
			$(this).closest('div').prev('a').children('span').html($(this).html());
			$(this).closest('div').prev('a').attr('rel',($(this).attr('rel')));
			$(".cat-list").hide();
			$("form.search").attr('action','/browse/'+$(this).attr('rel'));
			return false;
		}
		window.location = url;
	});

	$("#currency .cat-list a").click(function() {
		$.get('/setcurrency/currency/'+$(this).html());
	});

	$("#addtowatchlist").click(function() {
		var url = "/account/watchlist/add/"+$(this).attr("rel");
		$("#addtowatchlistresult").load(url);
		return false;
	});

	$(".removefromwatchlist").click(function() {
		$("#bidresults").load($(this).attr('href'));
		return false;
	});

	$("#acceptoffer").click(function() {
		var id = $(this).attr("rel");
		var amount=$('#auction_'+id+'_bid').html();
		var answer = confirm("Confirm your acceptance of our offer of US$"+amount);
		if (!answer) {
			return false;
		}
		var url = "/auction/acceptoffer/id/"+id;
		$("#addtowatchlistresult").load(url);
		return false;
	});

	theRotator();

	$('div.rotator').mousedown(function() {
		clicking = true;
		return false;
	});

	$('div.rotator').mouseup(function() {
		clicking = false;
	});

	$('div.rotator').mousemove(function(e) {
		$(this).attr('rel', clicking);
		if (clicking == false) return false;

		var position = e.pageX-$(this).position().left;
		var width = $(this).width();
		var count = $(this).find('ul li').size();
		if (count > 1) {
			var imagenum = Math.round((count-1)*position/width)+1;
			var thisimage = $(this).attr("id")+'_'+imagenum;
			$('#'+$(this).attr("id")+" li").removeClass('show');
			$('#'+thisimage).addClass('show');
		}
		return false;
	});

	// Process form requests
	$(".form").live("submit", function() {
		var action = $(this).attr('action');
		var thisnext = $(this).next('.formresult');
		thisnext.html('<img src="/images/ajax-loader.gif" />');
		$.post(action,$(this).serialize(), function(rdata){
			thisnext.html(rdata);
		});
		return false;
	});

	$('.facebox').click(function() {
		href=($(this).attr("href"));
		jQuery.facebox({ ajax: '' });
		if (href && (href.length > 0)) {
			$('#facebox .content').load(href+'?facebox=1');
		}
		return false;
	});

	// Checkout calculation
	$('.checkoutcheckbox').change(function() {
		var total = 0;
		var handling = 0;

		// Clear certificate if removing auction
		if ($(this).attr('name').substr(0,7) == 'auction') {
			var auction = $(this).attr('name').substr(7);

			if (!$(this).attr('checked')) {
				$('input[name="certificate'+auction+'"]').attr('checked', $(this).attr('checked'));
			}
		}

		// Calculate total cost
		$('body').find('.checkoutcheckbox').each(function() {
			if ($(this).attr('checked')) {
				total += parseInt($(this).attr('rel'));
			}
		});
		$('body').find('.checkoutproduct').each(function() {
			if ($(this).attr('checked')) {
				handling += 3;
			}
		});

		var shipping = Math.ceil(handling / 30) * 10;

		shipping += handling;

		if (total >= 250) {
			shipping = 0;
		}

		$('#checkoutsubtotal span.amount').html(total);
		$('#checkoutshippingamount span.amount').html(shipping);

		total += shipping;

		$('#checkouttotal span.amount').html(total);
		$('input[name="totalamount"]').attr('value',total);
	});

	$('.bidlist').hide();
	$('.showbidlist').click(function () {
		$('.bidlist').toggle();
		return false;
	});

	if (typeof isRunningIE6OrBelow == 'undefined') {
		$('.cntBanner, .side220').nivoSlider({
			pauseTime:10000,
			directionNav:false,
			controlNav:false,
			pauseOnHover:true
		});
	} else {
		$('.cntBanner img:first').show();
		$('.side220 img:first').show();
	}

	$('.title').textOverflow('...', true);

	$('.tooltip').click(function() {
		return false;
	});

	$('.newsecurity').click(function() {
		getnewsecurity();
		return false;
	});
});


