function AuctionWindow(id, loggedin) {
	this.id = id;
	this.auction_id = 0;
	this.status = "empty";
	this.item_id = 0;
	this.item_name = "";
	this.item_photo = "";
	this.start_time = 0; //seconds - unix timestamp
	this.end_time = 0;
	this.time_added = 0;
	this.time_remaining = 0;
	this.item_price = 0;
	this.item_bidder = "";
	this.loggedin = loggedin;
	this.auction_type = "Penny Auction";
	this.list_price = 0;

	this.flash_price = false;
	this.new_item = true; // possible shortcut to only update bid info
	
	this.is_closed = 0;
	this.bidders = ""; //json list of all bidders? or array?
	
	this.change_status = 1; // allow single-shot rendering of status changes, to avoid flickering
}

AuctionWindow.prototype.update = function(auction_id, status, item_id, item_name, item_photo, 
							start_time, end_time, time_added, time_remaining, item_price, item_bidder, list_price, auction_type, is_closed) {

	// price change will be highlighted on the next render
	this.flash_price = false;
	if((this.item_price > 0) && (item_price > this.item_price)) {
		this.flash_price = true;
	}

	if((this.item_id != item_id) || (this.auction_id != auction_id)) {
		this.new_item = true;
	}
	
	
	// deal with page refreshes on details page
	if(singlewindow != 0) {
	
		// all of these conditions can only happen if the auction id changes
		if(this.auction_id != auction_id) {
	
			// if window goes from active to passive, allow triggering of refreshes
			if(auction_id == 0){
				should_reload = 1;
				
			// if window just changes from one auction to another
			} else if ((this.auction_id != 0) && (auction_id != 0)) {
				// reload immediately
				location.reload(true);
			
			// current auction is empty, and this is not a newly loaded window
			// reload only if trigger is set before
			} else if(should_reload == 1){
				should_reload = 0;
				location.reload(true);
			}
		
		}
	
	}
	
	
	// allow updates to hidden/show of auctions if necessary
	if(status != this.status) {
		this.change_status = 1;
	}

	this.auction_id = auction_id;
	this.status = status;
	this.item_id = item_id;
	this.item_name = item_name;
	this.item_photo = item_photo;
	this.start_time = start_time;
	this.end_time = end_time;
	this.time_added = time_added;
	this.time_remaining = time_remaining;
	this.item_price = item_price;
	this.item_bidder = item_bidder;
	this.auction_type = auction_type;
	this.list_price = list_price;
	this.is_closed = is_closed;

	this.render();
}




AuctionWindow.prototype.render = function() {

	if(this.new_item) {
		this.new_item = false;
		this.render_photo();
		this.render_type();
	}
	
	this.render_button() //this may be too slow to do here....
	this.render_time();
	this.render_misc();
	
	if(this.flash_price) {
		this.flash_price = false;
		this.flashprice();
	}
	
	if(this.is_closed == "1") {
		$('#window_' + this.id + '_auction_type').removeClass();
		$('#window_' + this.id + '_auction_type').addClass("closed");
	}
	
	// hide / show auction window, as needed
	this.hide_show();
	
} // end of render function


AuctionWindow.prototype.hide_show = function() {
	// only hide / show if necessary (to avoid flickering)
	// change_status is set to 1 in update() (approx line 41)
	if(this.change_status == 0)
		return true;
	
	if(this.status == "empty") {
		// hide auction window
		$("#window_" + this.id).removeClass();
		$("#window_" + this.id).addClass("auction_item_hidden");
	
		// display the empty image
		$("#window_" + this.id + "_empty").removeClass();
		$("#window_" + this.id + "_empty").addClass("empty_auction");
	
	} else {
		// hide the empty auction image
		$("#window_" + this.id + "_empty").removeClass();
		$("#window_" + this.id + "_empty").addClass("empty_auction_hidden");
		
		// show the auction window
		$("#window_" + this.id).removeClass();
		$("#window_" + this.id).addClass("auction_item");

	}
	
	this.change_status = 0;
}


AuctionWindow.prototype.render_misc = function() {

	this.hide_show();

	if(this.status == "empty") {
		//nothing in auction box...
		$("#window_" + this.id + "_auction").html("");
		$("#window_" + this.id + "_product").html("");
		$("#window_" + this.id + "_title").html("");		
		$("#window_" + this.id + "_price").html("");
		$("#window_" + this.id + "_retail").html("");
		$("#window_" + this.id + "_bidder").html("");
	} else {
	
//TODO: put in checks for blank values...
	
		$("#window_" + this.id + "_title").html("<h1><a href='" + rootpath + "/auction/details/id/" + this.id + "'>" + this.item_name + "</a></h1>");
		$("#window_" + this.id + "_auction").html("<h2>Auction #" + this.auction_id + "</h2>");
		$("#window_" + this.id + "_product").html("Item #" + this.item_id);
		$("#window_" + this.id + "_price").html(formatmoney(this.item_price));
		$("#window_" + this.id + "_retail").html("<h3>List Price: " + formatmoney(this.list_price) + "</h3>");
		$("#window_" + this.id + "_bidder").html(this.item_bidder);
	}

}



// call only when needed to change a button type!
// references globals: bidsRemaining, loggedin
AuctionWindow.prototype.render_button = function() {

	// remove bindings / remove classes
	$("#window_" + this.id + "_button").unbind();
	$("#window_" + this.id + "_button").removeClass();
	
	// set new class and mousedown binding if needed
	
	if(!loggedin) {
	
		$("#window_" + this.id + "_button").attr('value','bid');
		$("#window_" + this.id + "_button").addClass("orangeBtn2");
		$("#window_" + this.id + "_button").bind('mousedown', function(event) {
			window.location = rootpath + "/login/login";
			return true;
		});
		
//TODO: add binding to go to login screen
		
	} else if (this.status == "empty") {
	
		$("#window_" + this.id + "_button").addClass("hiddenBtn");
		
	} else if (this.status == "stopped") {
	
		$("#window_" + this.id + "_button").addClass("hiddenBtn");
		
	} else if (this.status == "won") {
	
		$("#window_" + this.id + "_button").addClass("hiddenBtn");
	
	} else if(bidsRemaining <= 0) {
	
		$("#window_" + this.id + "_button").attr('value','buy bids');
		$("#window_" + this.id + "_button").addClass("blueBtn");
		$("#window_" + this.id + "_button").bind('mousedown', function(event) {
			window.location = rootpath + "/bids";
			return true;
		});
	
	} else if ((this.time_remaining <= 0) || (this.status == "check")) {
	
		$("#window_" + this.id + "_button").attr('value','Bid');
		$("#window_" + this.id + "_button").addClass("greyBtn");
	
	} else { // good to go!
	
		$("#window_" + this.id + "_button").attr('value','Bid');
		$("#window_" + this.id + "_button").addClass("greenBtn");
	
		$("#window_" + this.id + "_button").bind('mousedown', {auction: this}, function(event) {
		
				// change username to make things look faster
				// currently commented out, because it breaks the background flashing
				//$("#window_" + this.id + "_bidder").html(myusername);
		
				//try to block duplicate bids
				if(currentlybidding)
					return true;
					
				currentlybidding = true;
				myPath3 = rootpath + "/public/gateway/registerBid.php?id=" + event.data.auction.id;
		
				//TODO: put in retry method?
				$.ajaxSetup ({ cache: false});
				$.getJSON(myPath3, function(result) {
					currentlybidding = false; //re-permit bidding
		
					// indicate if bid was successful
					event.data.auction.bid_indicator(result["Success"] == "Bid completed");

					if(result["Display"] == "1") {
						alert(result["Error"]);
					}
				});
				return false;
		});
	
	}

}


AuctionWindow.prototype.render_time = function() {

	if((this.auction_id > 0) && (this.status == "running")) {
	
		if(this.time_remaining > 15) {
			$("#window_" + this.id + "_time").html(intToTime(this.time_remaining));
		} else if(this.time_remaining >= 3) {
			$("#window_" + this.id + "_time").html("<div class='resetTimer'>" + intToTime(this.time_remaining) + "</div>");
		} else if(this.time_remaining >= 2) {
			$("#window_" + this.id + "_time").html("<div class='resetTimer'>going</div>");	
		} else if(this.time_remaining >= 1) {
			$("#window_" + this.id + "_time").html("<div class='resetTimer'>GOING</div>");
		} else {
			$("#window_" + this.id + "_time").html("<div class='resetTimer'>GOING</div>");
		}
	
	} else if(this.status == "check") {
	
		$("#window_" + this.id + "_time").html("<div class='resetTimer'>GOING</div>");
	
	} else if(this.status == "pending") {
	
		$("#window_" + this.id + "_time").html("<div class='resetTimer'>SOLD</div>");
	
	} else if(this.status == "won") {
	
		$("#window_" + this.id + "_time").html("<div class='resetTimer'>SOLD</div>");
	
	} else if(this.status == "stopped") {
	
		$("#window_" + this.id + "_time").html("<div class='resetTimer'>Paused...</div>");
	
	} else { //empty
	
		$("#window_" + this.id + "_time").html("");
		
	}

	/*
	
	
	
	*/

}


AuctionWindow.prototype.render_type = function() {
	
	//remove any existing classes
	$('#window_' + this.id + '_auction_type').removeClass();
	
	//set current type
	if(this.is_closed == "1") {
		$('#window_' + this.id + '_auction_type').addClass("closed");
	} else if(this.auction_type == "Charity Auction") {
		$('#window_' + this.id + '_auction_type').addClass("charity");
	} else if(this.auction_type == "Pro") {
		$('#window_' + this.id + '_auction_type').addClass("pro");
	} else if(this.auction_type == "Trainee") {
		$('#window_' + this.id + '_auction_type').addClass("trainee");
	} else if(this.auction_type == "Double Up") {
		$('#window_' + this.id + '_auction_type').addClass("double");
	}  else if(this.auction_type == "Free") {
		$('#window_' + this.id + '_auction_type').addClass("free");
	}  else if(this.auction_type == "Expert") {
		$('#window_' + this.id + '_auction_type').addClass("expert");
	}
	
}


AuctionWindow.prototype.render_photo = function() {

	if(this.status == "empty") {
		$("#window_" + this.id + "_photo").html("");
	} else {
		if(this.item_photo != "") {
			$("#window_" + this.id + "_photo").html("<a href='" + rootpath + "/auction/details/id/" + this.id + "'><img src='" + rootpath + "/public/images/products/" + this.item_photo + "' alt='" + this.item_name + "' /></a>");
		} else {
			$("#window_" + this.id + "_photo").html("<img src='/public/images/img_no_image.jpg' width='100' height='100' alt='No image' />");
		}
	}
}

AuctionWindow.prototype.bid_indicator = function(is_ok) {
	if(is_ok) {
		$("#window_" + this.id + "_ok").addClass("accept");
	} else {
		$("#window_" + this.id + "_ok").addClass("deny");
	}

	setTimeout ( "clear_bid_indicator(" + this.id + ");", 1000 );
}

AuctionWindow.prototype.flashprice = function() {
	$("#window_" + this.id + "_price").removeClass();
	$("#window_" + this.id + "_price").addClass("resetBids");
	setTimeout ( "clear_flashprice(" + this.id + ");", 200 );
}

/***************************/
/* Misc Non-class methods */
/***************************/
function clear_bid_indicator(window_id) {
	$("#window_" + window_id + "_ok").removeClass();
}

function clear_flashprice(window_id) {
	$("#window_" + window_id + "_price").removeClass();
	$("#window_" + window_id + "_price").addClass("bids");
}
