var timer;
var initplacebid = false;
var bBided = false;
var executed = false;
var auctionclosed = null;

$(document).ready(function() {
	repeat();
});
function close_repeat() {
	clearTimeout(timer);
}

function call_updater(auction_id, chk, isSilent, bidTemplate, placeBidTemplate) {

	var data_url = "scripts/ajax_auctiondetails.php";

	if(bidTemplate) {
		data_url += "?BidTemplate=" + escape(bidTemplate);

		if(placeBidTemplate) {
			data_url += "&PlaceBidTemplate=" + escape(placeBidTemplate);
		}
	}
	$.ajax({
		url : data_url,
		data : {
			auction_id : auction_id,
			chk : chk
		},
		success : function(xml) {
			addMessages(xml);
		},
		type : "GET",
		error : function() {
			timer = setTimeout("repeat()", repeat_duration);
		}
	});

	function addMessages(xml) {
		if( typeof (xml) != "object") {
			timer = setTimeout("repeat()", repeat_duration);
			return;
		}
		$("auctionitem", xml).each(function(id) {
			auctionitem = $("auctionitem", xml).get(id);

			var bids = $("bids", auctionitem).text();
			var place_bid_table = $("place_bid_table", auctionitem).text();
			var chk = $("chk", auctionitem).text();
			var timeleft = $("timeleft", auctionitem).text();
			var time_left_number = $("time_left_number", auctionitem).text();
			var categoryType = $("categoryType", auctionitem).text();
			var auction_end_time = $("auction_end_time", auctionitem).text();
			var high_price_bidder_id = $("high_price_bidder_id", auctionitem).text();
			var highest_bid = $("highest_bid", auctionitem).text();
			var show_bid_history_link = $("show_bid_history_link", auctionitem).text();
			var start_price = $("start_price", auctionitem).text();

			var ischk = false;
			var firstTime = $("#chk").val() == '';

			auctionclosed = $("closed", auctionitem).text();
			var close_in_progress = $("close_in_progress", auctionitem).text();

			var isplace = false;

			if($("#chk").val() != chk) {
				$("#chk").val(chk);
				ischk = true;
			}

			if(time_left_number < 60) {
				$("#div_time_left").addClass("time_left_red");
				if(time_left_number <= 0)
					isplace = true;
			} else {
				$("#div_time_left").removeClass("time_left_red");
			}

			if(initplacebid)
				isplace = true;

			if(isplace) {
				$("#divPlaceBid").html(place_bid_table);
				initInput();
				initplacebid = false;
			}

			if(ischk) {
				$("#div_time_left").html(timeleft);

				$("#bids").html(bids);

				$("#high_price_bidder_id").val(high_price_bidder_id);

				if(highest_bid == "")
					$("#lblMaxBid").html(eurobidStyle(start_price));
				else
					$("#lblMaxBid").html(eurobidStyle(highest_bid));

				if(show_bid_history_link == "1") {
					$("#hyfullbids").css("display", "");
				} else {
					$("#hyfullbids").css("display", "none");
				}
			}

			if(close_in_progress == "close_in_progress") {
				if($("#divPlaceBid").length > 0) {
					$("#divPlaceBid").html(place_bid_table);
				}
			}

			if(auctionclosed == "closed") {
				if($("#divPlaceBid").length > 0) {
					// Because this code is executed twice for an unknown reason. We check if the script has been executed already
					if(executed == false) {
						// Check if a winning bid is available
						if(highest_bid != "") {
							var auctionID = "";
							// Get the auctionID from the url
							pathArray = window.location.pathname.split(',');
							for(var i = 0; i < pathArray.length; i++) {
								if(pathArray[i] == "auction_id") {
									var auctionID = pathArray[i - 1];
								}
							}
							// Replace the trailing slash
							auctionID = auctionID.replace("/", "");

							//alert(auctionID);
							//alert(highest_bid);
							
							// Get variables
							
							var totaalbedrag = $("#lblMaxBid").text();
							totaalbedrag = totaalbedrag.replace(",-", "");
							var hotelNaam = $("#hotelNaam").text();
							var hotelAdres = $("#hotelAdres").text();
							// Google Ecommerce Analytics code
							//alert("auctionID = " + auctionID + "HotelAdres = " + hotelAdres + " hotelNaam = " + hotelNaam + " categoryType = " + categoryType + " Totaalbedrag = " + totaalbedrag);
							//_gaq.push(['_setAccount', 'UA-3525553-1']);
							//_gaq.push(['_setDomainName', 'hotelkamerveiling.nl']);
							//_gaq.push(['_setAllowHash', 'false']);
							//_gaq.push(['_trackPageview']);
							//_gaq.push(['_trackPageLoadTime']);
							//_gaq.push(['_addTrans', auctionID + "ID", '', totaalbedrag, '', '', '', '', '']);
							//_gaq.push(['_addItem', auctionID + "ID", auctionID + "ID", hotelAdres + " - " + hotelNaam, categoryType, totaalbedrag, '1']);
							//_gaq.push(['_addItem', auctionID + "ID", auctionID, 'Veilingkosten', 'Veilingkosten',7.50, '1']);
							//_gaq.push(['_trackTrans']); 


							// DISABLED. we now show the m4n pixel when a user bids
							// Check if the ogone form is available, if it is then execute the M4N affiliate code.
							//if($('#form_ogone').length > 0){
								//alert(auction_end_time);
								//var prefix = parent.location.protocol;
								//$("#wrapper").append('<img src="' + prefix + '//leads.m4n.nl/_l?lpid=6708&description1=' + auctionID + '&description2=' + hotelNaam + " | " + categoryType + '&description3=Einddatum: ' + auction_end_time + '&price=' + totaalbedrag + '" width="1" height="1"border="0"/>');
							//}

							// Set executed to true, so it can`t be executed twice!
							executed = true;
						}
					}
					$("#divPlaceBid").html(place_bid_table);
				}
				close_repeat();
			} else {
				if($("#bBided").val() == "true") {
					$("#bBided").val("false");
				} else {
					timer = setTimeout("repeat()", repeat_duration);
				}
			}
		});
	}

}

document.onkeypress = function(event) {
	var target, code, tag;
	if(!event) {
		event = window.event;
		target = event.srcElement;
		code = event.keyCode;
		if(code == 13) {
			tag = target.tagName;
			if(tag == "TEXTAREA") {
				return true;
			} else {
				return false;
			}
		}
	}
}
function initInput() {
	$("input").each(function(index) {
		if($(this).attr("type") == "text" || $(this).attr("type") == "password") {
			if($(this).attr("title") && !$(this).val()) {
				$(this).val($(this).attr("title"));
			}
			$(this).focus(function() {
				$(this).addClass("onFocus");
				if($(this).val() == $(this).attr("title")) {
					$(this).val("");
				}
			}).blur(function() {
				if($(this).val() == "" || $(this).val() == $(this).attr("title")) {
					$(this).removeClass("onFocus");
					$(this).val($(this).attr("title"));
				}
			});

			$(this).attr("tabindex", index + 1);
		}

		$(this).keypress(function(event) {
			if(event.keyCode == 13) {
				if($(this).attr("class").indexOf("tab_loginSubmit") > -1) {
					login('tdlogin');
				} else if($(this).attr("class").indexOf("tab_bidmount") > -1) {
					submitBid();
				} else {
					onEnterTab(parseInt($(this).attr("tabindex")) + 1);
				}
			}
		});
	});
}

function onEnterTab(tabindex) {
	$(":input[tabindex=" + tabindex + "]").focus();
}

function eurobidStyle(bid) {
	if(bid == "" || bid == 0)
		return "-,-";
	return parseInt(bid) + ",-";
}
