Cufon.replace(".cufon");
Cufon.replace("#main-menu a", { hover: true });

$(function() {

	// open all external links in new window
	$("a[href^=http], .external").not(".local").click(function(e) {
		e.preventDefault();
		window.open(this.href);
	});

	// convert email addresses
	$("span.mail").each(function() {
		var address = $(this).text();
		address = address.replace(/ at /, "@");
		address = address.replace(/ dot /g, ".");
		var link = $(this).attr("title") || address;
		$(this).html('<a href="mailto:'+address+'">'+link+'</a>');
	});

	// handle tour date selection
	$(".select-date").live("click", function(e) {
		e.preventDefault();
		var tour_id = $(this).attr("id").replace("reservetour-", "");
		loadSelectDatePopup(tour_id, false);
	});

	// handle quick tour selections from the date selection popup
	$(".choose-tour").live("click", function(e) {
		e.preventDefault();
		var tour_id = $(this).attr("id").replace("choosetour-", "");
		loadSelectDatePopup(tour_id, true);
	});

	// apply coupon codes
	$("#apply-code").click(function() {
		var code = $.trim($("#coupon-code").val());
		if( code ) {
			$.getJSON("/ajax/apply_code/"+code+"/", function(data) {
				if( data.error === "invalid" ) {
					$("#coupon-code-error").html(" - Invalid Code");
				} else {
					$("#coupon-code-error").html("");
					if( data.concierge === "no" ) {
						$("#pay-cash").hide();
						$("#credit-card-section").show();
						$("#pay-with-cash").removeAttr("checked");
					} else {
						$("#pay-cash").show();
					}
					if( data.discount_text ) {
						$("#totals em").html(data.discount_title+": <span>"+data.discount_text+"</span>").show();
					}
					if( data.new_total_formatted ) {
						$("#credit-card-section").show();
						$("#totals strong span").html(data.new_total_formatted);
					}
					if( data.new_total === 0 ) {
						$("#credit-card-section").hide();
					}
				}
			});
		} else {
			$("#coupon-code-error").html(" - Please Enter Code");
			$("#pay-cash, #totals em").hide();
			$("#credit-card-section").show();
			$("#pay-with-cash").removeAttr("checked");
		}
	});
	$("#pay-cash").change(function() {
		if( $("#pay-with-cash").attr("checked") === true ) {
			$("#credit-card-section").hide();
		} else {
			$("#credit-card-section").show();
		}
	});

});

function loadSelectDatePopup(tour_id, reload_only) {
	var d = new Date();
	if( reload_only === false ) {
		$("body").append('<div id="overlay-container"><div id="overlay-content" class=\"loading\">Loading...</div><div id="overlay-curtain" title="[x] Close Date Selector" /></div>');
		//$("head").append('<link type="text/css" rel="stylesheet" id="overlay-css" href="/css/overlay.css?'+d.getTime()+'" />');
		$("#overlay-curtain").live("click", function() {
			removePopup();
		});
	} else {
		$("#overlay-content").addClass("loading").html("Loading...");
		positionPopup();
	}
	$("#overlay-content").load("/ajax/select_date/"+tour_id+"/?"+d.getTime(), function() {
		$(this).removeClass("loading");
		positionPopup();
		Cufon.replace(".cufon");

		var dates = [];
		// get available dates to highlight
		$("#tour-date option:not(.so)").each(function() {
			var date  = $(this).val();
			var year  = date.substring(0, 4);
			var month = date.substring(5, 7) - 1;
			var day   = date.substring(8, 10);
			dates[new Date(year, month, day)] = true;
		});

		$("#datepicker").datepicker({
			dateFormat: "yy-mm-dd",
			dayNamesMin: ["S", "M", "T", "W", "T", "F", "S"],
			prevText: "<",
			nextText: ">",
			buttonImage: "/images/icon_calendar.gif",
			buttonImageOnly: true,
			showOn: "button",

			// update the drop down by choosing the first instance of the selected date
			onSelect: function(date) {
				$("#tour-date option").each(function() {
					// set the selected date to the first match, this covers multiple times in the same day
					if( $(this).val().indexOf(date) >= 0 ) {
						$("#tour-date").val($(this).val());
						return false;
					}
				});
			},

			// before each day is added
			beforeShowDay: function(date) {
				return [dates[date], ""];
			},

			// before the datepicker is opened
			beforeShow: function() {
				$(this).val($("#tour-date").val());
				return {};
			}
		});
	});
}

function removePopup() {
	$("#overlay-css, #overlay-container").remove();
}

function positionPopup() {
	var container = $("#overlay-content"),
		w = container.outerWidth(),
		h = container.outerHeight();

	container.css({ margin: "-"+(h/2)+"px 0 0 -"+(w/2)+"px" });
}


