//LOSE IE6 FLICKER BUG
//--------------------

try {
  document.execCommand("BackgroundImageCache", false, true);
} catch(err) {}

//CALL FUNCTIONS ONDOMREADY
//-------------------------

$(function() {
	//TOURS CALENDAR
	
	$.ajax({
		url: "/eventwax/get_dates.php",
		dataType: "json",
		success: function(calendarEvents) {
	
			$('#tours_calendar').datepicker({
				nextText: '&raquo;',
				prevText: '&laquo;',
				beforeShowDay: function(date) {
					for (i = 0; i < calendarEvents.length; i++)
					{
						if (date.getDate() == calendarEvents[i]['date_day'] && date.getMonth() == calendarEvents[i]['date_month']-1 && date.getFullYear() == calendarEvents[i]['date_year'])
						{
							return [true, 'ui-state-active', calendarEvents[i]['name'], calendarEvents[i]['url']];
						}
					}						
					return [false, 'ui-state-active'];
				},
				onSelect: function(dateText, inst)
				{
					var d = new Date(dateText);
					var data = inst.settings.beforeShowDay(d);					
					window.location = data[3];
				}
			});
			
		}
	});
	
});


$(document).ready(function() {
	//call functions on page load.
	
	$("#newsletter .text").attr("rel", $("#newsletter .text").val()).focus(function() {
		if ($(this).val() == $(this).attr("rel")) {
			$(this).val("");
		};
	}).blur(function() {
		if ($(this).val() == "") {
			$(this).val($(this).attr("rel"));
		};
	});
	/*$("#newsletter form").submit(function() {
		
		$.get($(this).attr("action"), {frmNewsletterEmail: $(".text", this).val()}, function(data) {
			alert(data);
		});		
		return false;
		
	});*/
	$("#newsletter img").click(function() {
		$("#newsletter .text").focus();
	});

});

function billingCountry(element) {

	if (element.value != "United Kingdom") {
		$("#lblCounty").html("County/State *");
		$("#lblPostcode").html("Postcode/Zipcode *");
		$("#frmAddressCopy").attr("disabled", true);
		$("select[name='bill_state']").css("display", "none").attr("name", "bill_state_select");
		$("input[name='bill_state_text']").css("display", "inline").attr("name", "bill_state");
	} else {
		$("#lblCounty").html("County *");
		$("#lblPostcode").html("Postcode *");
		$("#frmAddressCopy").attr("disabled", false);
		$("input[name='bill_state']").css("display", "none").attr("name", "bill_state_text");
		$("select[name='bill_state_select']").css("display", "inline").attr("name", "bill_state");
	};

};

function doToursForm(element) {

	var form = document.getElementById("toursForm");
		
	if (form.frmTypeOfTour.value == "group") {
		//group options
		
		if (element.name == "frmTypeOfTour" || element.name == "frmTime") {
			
			if (element.name == "frmTypeOfTour") {
				//element being changed was type of tour, amend the time drop down and number in party
				form.frmTime.options.length = 0;
				form.frmTime.options[0] = new Option("12 o'clock daytime tour", "12 o'clock daytime tour", false, false);
				form.frmTime.options[1] = new Option("7pm", "7pm", false, false);
				form.frmTime.options[2] = new Option("7.30pm", "7.30pm", false, false);
			};
			
			if (form.frmTime.value == "7pm" || form.frmTime.value == "7.30pm") {
				var minimum = 20;
			} else {
				var minimum = 12;
			};
			
			if (element.name == "frmTime" || element.name == "frmTypeOfTour") {
			
				form.frmNumberInParty.options.length = 0;
				for (var i=minimum; i<=34; i++) {
					form.frmNumberInParty.options[i-minimum] = new Option(i, i, false, false);
				};
				
			};
			
		};
		
        /*
		$("#mealOptions-individual").css("display", "none");		
		if (form.frmMealRequirements.value == "With meal") {
			$("#mealOptions-group").css("display", "block");
		} else {
			$("#mealOptions-group").css("display", "none");
		};
        */
		
		
	} else {
		//individual options
		
		if (element && element.name == "frmTypeOfTour") {
			//element being changed was type of tour, amend the time drop down and number in party
			form.frmTime.options.length = 0;
			form.frmTime.options[0] = new Option("12 noon", "12 noon", false, false);
			
			form.frmNumberInParty.options.length = 0;
			for (var i=0; i<=11; i++) {
				form.frmNumberInParty.options[i] = new Option(i+1, i+1, false, false);
			};
			
		};	
		
        /*
		$("#mealOptions-group").css("display", "none");
		if (form.frmMealRequirements.value == "With meal") {
			$("#mealOptions-individual").css("display", "block");
		} else {
			$("#mealOptions-individual").css("display", "none");
		};
        */
		
	};
	
};

//CHECKOUT ADDRESS CLONING
//------------------------

function checkoutBillingToDelivery(check) {

	var form = document.getElementById("checkoutForm");
	
	if (check.checked == true) {
		
		form.ship_name.value = form.bill_name.value;
		form.ship_company.value = form.bill_company.value;
		form.ship_addr_1.value = form.bill_addr_1.value;
		form.ship_addr_2.value = form.bill_addr_2.value;
		form.ship_city.value = form.bill_city.value;
		form.ship_state.selectedIndex = form.bill_state.selectedIndex;
		//form.ship_country.value = form.bill_country.value;
		form.ship_post_code.value = form.bill_post_code.value;
		
		setTimeout("document.getElementById('frmAddressCopy').checked = false;", 500);
		
	};
	
};


function gallery(li, imageURL) {

	var imgID = document.getElementById("galleryImg");
	
	$("#gallery li").removeClass();
	$(li).addClass("active");
	
	imgID.src = imageURL;	
	
	return false;	
};

$(document).ready(function(){
    $("#whatsnew_twitter").tweet({
        username: ["chiltern_brewer", "Kings_Head"],
        avatar_size: 32,
        count: 3,
		/*
        auto_join_text_default: "we said,", 
        auto_join_text_ed: "we",
        auto_join_text_ing: "we were",
        auto_join_text_reply: "we replied to",
        auto_join_text_url: "we were checking out",
		*/
        loading_text: "loading tweets..."
    });
	$("#whatsnew .tabs a").click(function() {
		var index = $("#whatsnew .tabs a").index(this);
		
		$("#whatsnew .tabs a").not(this).removeClass('active');
		$(this).addClass('active');
		
		
		$("#whatsnew div:not(:eq("+index+"))").hide();
		$("#whatsnew div:eq("+index+")").show();
		
		return false;
	});
});
