// JavaScript Document for common routines

// jquery onload actions
$(document).ready(function(){
	homepagelink()
	scroller()
	maptabs()
	regularity()
	clearinputext()
	externallinks()
	senddetails()
	downloadclick()
	submitclick()
	$("#forgotten-fields").hide();
	validatesignup()
	login()
	
});

function homepagelink() {
	//alert('running');
	//$("#promolinktest").click(function(){
	//	alert('running');
	//})
	$("#promolinktest").animate({opacity: 0}, 10);
}

// add target="blank" for external links
function externallinks() {
	$("a[rel='external']").attr("target", "_blank");
}

function clearinputext() {
	var defaulttext = ""
	$("input[type=text]").focusin(function(){
		defaulttext = $(this).val();
		if (defaulttext.indexOf("...") > 0) {
			$(this).val("");
		}
	});
	$("input[type=text]").focusout(function(){
		if ($(this).val() == "") {
			$(this).val(defaulttext);
		}
	});
}

function scroller() {
	$("#scroller div.scroller").css("display", "none"); 			//hide all divs
	$("#scroller #scroller-tabs li a").removeClass("selected");	//remove the active classes
	
	$("#scroller div.scroller:eq(0)").css("display", "block");	//show the second div
	$("#scroller #scroller-tabs li:eq(0) a").addClass("selected"); 
	
	$("#scroller #scroller-tabs li a").click(function () {		//if someone click a tab
		$("#scroller div.scroller").css("display", "none");		//hide all divs
		$("#scroller #scroller-tabs li a").removeClass("selected"); //remove the active classes
		
		var divid = $(this).attr("href");				//grab the value of the link
		$(this).addClass("selected");					//show the selected class on the tab clicked
		$(divid).css("display", "block");				//show the div for the clicked tab
		return false;
	});
}

function maptabs() {
	$("div.tabbed").css("display", "none"); 			//hide all tabbed divs
	$("#tabs li a").removeClass("selected");			//remove the active classes
	
	$("div.tabbed:eq(0)").css("display", "block");		//show the second div
	$("#tabs li:eq(0) a").addClass("selected"); 
	
	$("#tabs li a").click(function () {					//if someone click a tab
		$("div.tabbed").css("display", "none");			//hide all divs
		$("#tabs li a").removeClass("selected"); 		//remove the active classes
		
		var divid = $(this).attr("href");				//grab the value of the link
		$(this).addClass("selected");					//show the selected class on the tab clicked
		$(divid).css("display", "block");				//show the div for the clicked tab
		return false;
	});
}

function regularity() {
	$("#once").css("display", "none"); 			//hide the option fieldsets
	$("#once div, #monthly div").css("display", "none"); 	//hide the description divs
	$("#once label, #monthly label").removeClass("selected");
	$("label span.description").css("display", "none");
	
	$("input[name=regularity]:radio").click(function () {
		var choice = $("input:radio[name=regularity]:checked").val();
		$("#once, #monthly").css("display", "none");

		$("#" + choice).css("display", "block");		
	});
	
	$("#once label, #monthly label, #other div").click(function () {					//if someone click a tab
		$("#once label, #monthly label, #other div").removeClass("selected"); 		//remove the active classes
		
		$(this).addClass("selected");					//show the selected class on the tab clicked
	});
	
	$("#once label, #monthly label").hover(
		function () {
			$(this).animate({width: "260"}, 100, "swing", function() {
				$(this).children(".description").fadeIn(100);
			});
			
		},
		function () {
			$(this).stop();
			$(this).children(".description").fadeOut(100, function(){
				$(this).parent().animate({width: "110"}, 100, "swing");
			});
		}
	);
}

function senddetails() {
	$("#updates").fancybox({
		'autoDimensions': false,
        'width': 600,
        'height': 'auto',
        'hideOnContentClick': false
	});
}

function downloadclick() {
	$("#lent-wrapper #content-extra a").click(function(){
		if($.cookie("registered")) {
			email = $.cookie("registered");
			file = $(this).attr("href");
			//alert( $.cookie("registered") + " " + file );
			if(file.indexOf("logdownload") == -1) {
				$(this).attr("href", "/logdownload" + file + "/email/" + email + "/");
			};
			return;
		} else {
			$("#submitlink").attr("href", $(this).attr("href"));
			$("#updates").click();
			return false;
		};
	});
}
function submitclick() {
	$("#submitlink").click(function() {
		if($("#firstname").val() == "") {
			$("#firstname").css("background","#faabab").focus(function(){$(this).css("background","#fff")});
			var errorflag = 1;
		};
		if($("#lastname").val() == "") {
			$("#lastname").css("background","#faabab").focus(function(){$(this).css("background","#fff")});
			var errorflag = 1;
		};
		if($("#telephone").val() == "") {
			$("#telephone").css("background","#faabab").focus(function(){$(this).css("background","#fff")});
			var errorflag = 1;
		};
		email = $("#email").val();
		if(email == "") {
			$("#email").css("background","#faabab").focus(function(){$(this).css("background","#fff")});
			var errorflag = 1;
		} else if (email.indexOf("@") == -1) {
			$("#email").css("background","#faabab").focus(function(){$(this).css("background","#fff")});
			var errorflag = 1;
		};
		
		if(errorflag != 1) {
			//email = $("#email").val();
			file = $(this).attr("href");
			
			$("#submitlink").attr("href", "/logdownload" + file + "/email/" + email + "/");		
			$.post("/logdownload/", $("#responseform").serialize(),function(data) {
				$.cookie("registered", email);
				$("#fancybox-close").click();
			});
		} else {
			return false;
		};
		
	});
}

function login() {
	$("a[href=#loginbox]").fancybox({
		'autoDimensions': false,
        'width': 500,
        'height': 'auto',
        'hideOnContentClick': false
	});
}

function orgform() {
	$("a[href=#orgbox]").fancybox({
		'autoDimensions': false,
        'width': 500,
        'height': 'auto',
        'hideOnContentClick': false
	});
}

function validatesignup() {
	$("#join").validate({
		rules: {
			firstname: "required",
			lastname: "required",
			email: {
				required: true,
				email: true
			},
			emailconfirm: {
				required: true,
				email: true,
				equalTo: "#email"
			},
			town: "required",
			country: "required",
			password: "required"
		},
		messages: {
			firstname: "Please enter your firstname",
			lastname: "Please enter your lastname",
			email: "Please enter a valid email address",
			emailconfirm: {
				required: "Please retype your email address",
				equalTo: "Please enter the same email as above"
			},
			town: "Please enter your town",
			country: "",
			password: "Please provide a password"
		}
	});
}

function orgautocomplete(){
	if ($("#organisation").length){ // No point burdening every page with this request.
		$.ajax({
			type: "POST",
			url: '/writeXML/',
			data: 'type=organisations',
			success: function(xmlResponse) {
				// alert(xmlResponse);
				// Get each node from the returned xml
				var data = $("child", xmlResponse).map(function() {
					return {
						// Get the name and id for each one
						value: $("orgname", this).text(),
						id: $("orgid", this).text()
					};
				}).get();
				$("#organisation").autocomplete({
					source: data,
					minLength: 2,
					select: function(event, ui) {
						$("#orgid").val(ui.item.id);
											
					}
				});
			}
		})
	}
}

function typeautocomplete(){
	
}

function validateorg() {
	$("#orgform").validate({
		rules: {
			orgname: "required",
			orgpostcode: "required",
			orgtown: "required",
			orgrole: "required",
			orgadmin: "required"
		},
		messages: {
			orgname: "required",
			orgpostcode: "required",
			orgtown: "required",
			orgrole: "required",
			orgadmin: "required"
		}
	});
}

function submitorg(){
	$("#no-submit").click(function() {
		if ($("#orgform").valid()){
			var organisationname = $("#orgname").val()
			
			$.post("/saveorganisation/", $("#orgform").serialize(),
				function(data) {
					if(data > 0){
						$("#orgid").val(data);
						$("#organisation").val(organisationname);
					} else {
						alert("We could not create your organisation, please give us a call or email your request to admin@spurgeons.org");
					}
					
				});
			
			
			$.fancybox.close();
			orgautocomplete();
		}
    return false;
	});
}
