/* --------------------------------------------------
Calendar Functions
---------------------------------------------------- */
function init_calendar() {
	if (document.getElementById("cal_date")) {
		$('#cal_date').datePicker();
	}
}
/* --------------------------------------------------
Donate Functions
---------------------------------------------------- */
function check_float(amount,i) {
	if (isNaN(amount)) {
		amount = '0.00';
		$("#amount"+i+"").addClass("num_error");
		$("#amount"+i+"").val(amount);
	} else {
		$("#amount"+i+"").removeClass("num_error");
	}
	return amount;
}

function reset_init() {
	$("#empty_cart").click( function() {
		var isOK = confirm('Are you sure you want to empty your cart?');
		if (!isOK) return false;
	});
	
	$("a#reset_form").click( function() {
		var isOK = confirm('Are you sure you want to start again? This will erase any data you have entered.');
		if (!isOK) return false;
	});
}

function donate_init() {
	$(".d_amount").blur( function() {
		// Show the spinner
		$("#aloader").show();
		// minus 0 to convert to a number
		var a1 = $("#amount1").val()-0;
		var a2 = $("#amount2").val()-0;
		var a3 = $("#amount3").val()-0;
		var a4 = $("#amount4").val()-0;
		
		a1 = check_float(a1,1);
		a2 = check_float(a2,2);
		a3 = check_float(a3,3);
		a4 = check_float(a4,4);
		
		$("#aloader").hide(); // hide the spinner
		
		// toFixed won't work in IE5, oh well.
		var total = (a1 + a2 + a3 + a4).toFixed(2);
		if (!isNaN(total)) { $("#donate_total").html(total); } // if total is a number
		
		else { $("#donate_total").html('Error'); } // else throw error
		
	});
}

/* --------------------------------------------------
Button Functions
---------------------------------------------------- */
function init_submit_buttons() {
	$(".btn").click( function() { $(this).val("Please Wait"); } );
	$(".btn").hover(
		function() { $(this).addClass("btn_hover"); }
		,function(){ $(this).removeClass("btn_hover"); }
	);
}
/*
function init_small_buttons() {
	$("input.small_btn").click( function() { $(this).val("Please Wait"); } );
	$("input.small_btn").hover(
		function() { $(this).addClass("small_btn_hover"); }
		,function(){ $(this).removeClass("small_btn_hover"); }
	);
}
*/
function init_small_buttons() {
	$("input.small_btn").hover(
		function() { $(this).addClass("small_btn_hover"); }
		,function(){ $(this).removeClass("small_btn_hover"); }
	);
	$("input.small_checkout").hover(
		function() { $(this).addClass("small_btn_hover"); }
		,function(){ $(this).removeClass("small_btn_hover"); }
	);
}

function init_search_button() {
	$("#sgo").hover(
		function() { $(this).addClass("hov"); }
		,function(){ $(this).removeClass("hov");
	});
}

function init_faqs() {
	$("div.faq_answer").hide();
	$("a.open_question").toggle(function(){
	  		var questionID = $(this).get(0).rel;
			$("#"+questionID+"").slideDown('fast');
	  		return false;
	  },function(){
	  		var questionID = $(this).get(0).rel;
	  		$("#"+questionID+"").slideUp('fast');
   });
}

function swap_tour_image(photoID) {
	var newHref = '/img/fullsize/photo'+photoID+'.jpg';
	//var newSrc = $(this).get(0).href;
	var myString = '<img src="' + newHref + '"  height="183" width="260" />';
	$("#masterpic img").fadeOut('fast');
	$("#masterpic").html(myString);
	$("#image_num a").removeClass("current");
	$(this).addClass("current");
	$("#masterpic img").fadeIn('fast');
	return false;
}


function init_shop_cart() {
	
	$("form.add_cart").submit( function() {
		
		var formID = $(this).attr("id");
		var kids = '';
		var quantity = '';
		var productID = '';
		var inCartWrap = '';
		
		kids = $(this).children();
		quantity = $(kids[0]).val();
		productID = $(kids[3]).val();
		inCartWrap = 'inCart'+productID;

		// Hide the form
		$(this).hide();
		
		// Show the In cart block
		$("#"+inCartWrap+"").show();
		
		// Do the insert
		$.ajax({
	    	url: "/incs/ahah/add_to_cart.php?pid="+productID+"&qty="+quantity+"",
	    	type: "GET",
	    	data: "",
	    	success: function(msg){
	    		// show in cart message
	    		$("#"+inCartWrap+"").html('<p class="in_cart"><strong>In Cart</strong> <a href="https://secure.yorkminster.org/shop/checkout/" class="small_checkout">Checkout</a></p>');
	    		// Update the cart contents block
	    		$("#cart_contents").load("/incs/ahah/add_to_cart.php?show=true",null,null);
	    		
	    	}
  		});
  		
		return false;
	});
}


function init_school_booking() {
	if (document.getElementById("booking")) {
		
		$("#school-education").change(
			function() {
				var ev = $("#school-education option:selected").text();
				//alert(ev);
				if (ev=='Other please state') {
					$("#education-other").slideDown("fast");
				}
				else {
					$("#education-other").css("display","none");
				}
			}
		);
		
		
		$("input[@name='school-sen']").click(
			function() {
				var senVal = $("input[@name='school-sen']:checked").val()
				if (senVal=='Yes') {
					$("p.sen-yes").slideDown("fast");
				}
				else {
					$("p.sen-yes").css("display","none");
				}
			}
		);
		
		$("input[@name='school-type']").click(
			function() {
				var senVal = $("input[@name='school-type']:checked").val()
				if (senVal=='Self conducted Visit') {
					$("#self_conducted_visit").slideDown("fast");
				}
				else {
					$("#self_conducted_visit").css("display","none");
				}
			}
		);

		
	}
	
}
	
	
$(document).ready(function(){
	
	init_faqs();
	
	init_search_button();
	
	init_calendar();
	
	donate_init();
	
	reset_init();
	
	init_small_buttons();
	
	init_submit_buttons();
	
	init_shop_cart();
	
	init_school_booking();
	
}); // End jQuery


function DESemail(emailname,emailserver) {
	document.write("<a href='mailto:" + emailname + "@" + emailserver +"'>");
	document.write(emailname + "@" + emailserver); 
	document.write("</a>"); 
}
function DESemailLink(emailname,emailserver) {
	document.write("<a href='mailto:" + emailname + "@" + emailserver +"'>");
	document.write('Email'); 
	document.write("</a>"); 
}
function DESemailApply(emailname,emailserver,subject) {
	document.write("<a href='mailto:" + emailname + "@" + emailserver +"?subject=Job: " + subject + "'>");
	document.write('Apply for this Job'); 
	document.write("</a>"); 
}
function DESquicklink(target) {
	if (target != "") {
	  this.location.href = target;
	}
}
