﻿/* Webfusion Store Front End Logic */

Blog = {};
(function($) {
	$.extend(Blog, {
		// Set this to the initial number of entries on the page
		numEntries: 5,
		// Set this to the number of entries added per page
		entriesPerPage: 5,

		morePosts: function() {
			var self = this;
			var m = 0, y = 0, cid = 0;

			// Try to deduce the current filter from the ulr
			try {
				var s = window.location.pathname;
				if (s.indexOf("/news/month/") == 0) {
					var parts = s.split('/');
					if (parts.length >= 6) {
						m = parseInt(parts[3]);
						y = parseInt(parts[4]);
						if (!isFinite(m) || !isFinite(y))
							m = y = 0;
					}
				} else if (s.indexOf("/news/category/") == 0) {
					var parts = s.split('/');
					if (parts.length >= 4) {
						cid = parseInt(parts[3]);
						if (!isFinite(cid))
							cid = 0;
					}
				}
			} catch (e) {
				m = y = cid = 0;
			}

			$.call({
				url: "/WebServices/Low/FusionWebService.asmx/Invoke",
				data: { entity: "BlogEntry", method: "PresentNextPage", parameters:
					{
						month: m,
						year: y,
						category: cid,
						skip: self.numEntries
					}
				},
				success: function(xhr) {
					if (!xhr.d || !xhr.d.More) {
						$("#morePostsMarker").hide();
					}
					if (xhr.d) {
						$("<div>" + xhr.d.Content + "</div>").hide().insertBefore("#morePostsMarker").slideDown('slow');
					}
					self.numEntries += self.entriesPerPage;
				}
			});
		}
	});
})(jQuery);

Common = {};
(function($) {
	$.extend(Common, {

		newsletterSignup: function() {
			$("#uxNewsletterErrorMessage").text("");
			var name = $("#uxNewsletterName").val();
			var email = $("#uxNewsletterEmail").val();
			if (!name || name == "") {
				$("#uxNewsletterErrorMessage").text("Name required.");
				return;
			}
			if (!email || email == "") {
				$("#uxNewsletterErrorMessage").text("Email address required.");
				return;
			}
			if (!(/([^<>;&=!]+)$/.test(name))) {
				$("#uxNewsletterErrorMessage").text("Invalid characters in name.");
				return;
			}
			if (!(/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i.test(email))) {
				$("#uxNewsletterErrorMessage").text("Invalid email address.");
				return;
			}
			$.redirectTo("/NewsletterSignup.aspx?name=" + encodeURI(name) + "&email=" + encodeURI(email));
		},

		rollover_employer: function() {
			var people = $(".people");
			if (people.length == 0) return;
			var person = people[0];
			person.style.backgroundPosition = "0% 0%";
			person.href = "/employer/default.aspx";
		},

		rollover_teacher: function() {
			var people = $(".people");
			if (people.length == 0) return;
			var person = people[0];
			person.style.backgroundPosition = "62% 0%";
			person.href = "/teacher/default.aspx";
		},

		rollover_learner: function() {
			var people = $(".people");
			if (people.length == 0) return;
			var person = people[0];
			person.style.backgroundPosition = "29% 0%";
			person.href = "/learner/default.aspx";
		},

		rollover_parentcarer: function() {
			var people = $(".people");
			if (people.length == 0) return;
			var person = people[0];
			person.style.backgroundPosition = "93% 0%";
			person.href = "/parentcarer/default.aspx";
		},

		search: function() {
			var text = $("#uxSearchInput").val();
			if (!text || text.length == 0)
				return;
			window.location = '/searchresults.aspx?s=' + escape(text);
		}

	});

	$(document).ready(function() {
		$("#uxSearchInput").keydown(function(event) {
			if (event.keyCode == 13) {
				event.preventDefault();
				Common.search();
			}
		});
	});

})(jQuery);
