/** 
 * @fileOverview Main site code that should follow the namespace UKISA.site.*
 * @version 0.0.1
 * @changeLog Created.
 */

UKISA.namespace("site");

/**
 * Navigation related methods such as creating modals/dialogs or enhancing normal links.
 */
 UKISA.site.Navigation = {
	stripecardTooltips: function() {
		var stripecards, stripecardToolTip;

		stripecards = $("div.stripecard a", "content");

		if (stripecards.length) {
			stripecardToolTip = new YAHOO.widget.Tooltip("stripecard-tooltip", {  
				context: stripecards,
				showDelay: 200,
				xyoffset: [0, -5]
			}); 
		}
	},
	/**
	 * Create a modal box for the terms.
	 *
	 * @requires UKISA.site.Modal
	 */
	terms: function(o) {
		var modal, success, failure, callback, link;

		// Used for closures;
		link = o;

		success = function(text) {
			var a = document.createElement("div");
			a.id = "terms-modal";
			a.innerHTML = text;
		
			return a;
		};

		var failure = function() {
			var a = document.createElement("div");
			a.id = "add-to-basket-modal";

			var b = document.createElement("p");
			b.className = "error";
			b.innerHTML = "Sorry, there was a problem and we could not fetch the page for you.";

			a.appendChild(b);

			return a;
		};
		
		modal = new UKISA.widget.Modal("");
		modal.show();
		
		
		callback = {
			success: function(o) {
				var q;

				var footer = [
					"<p class=\"print-modal\"",
						"<a class=\"print\" href=\"#\" onclick=\"return UKISA.util.print('modal-terms-print-reference');\">Print</a>",
					"</p>"
				];

				modal.setBody(success(o.responseText));
				modal.setFooter(footer.join(""));

				// Create an IFRAME to allow printing of the contents.
				var iframe = document.createElement("iframe");
				iframe.id = "modal-terms-print-reference";
				iframe.name = "modal-terms-print-reference";
				iframe.src = link.href;
				iframe.width = "1";
				iframe.height = "1";
				iframe.style.position = "absolute";
				iframe.style.left = "-999em";
				iframe.style.top = "-999em";
				document.body.appendChild(iframe);
			},
			failure: function(o) {
				modal.setBody(failure());
			}
		};	

		// Send the Ajax request.
		ajax = YAHOO.util.Connect.asyncRequest("GET", o.href, callback);

		return false;
	}
};

UKISA.site.Training = {
	Basket: {
		add: function(bt) {
			var modal, canvas, content, callback, busy, ajax, qs, action, message, itemId, i, ix, modalFinish;

			var el = YAHOO.util.Dom.getAncestorByTagName(bt, "form");

			var modalError = function() {
				var a = document.createElement("div");
				a.id = "book-a-place-content";

				var b = document.createElement("p");
				b.className = "error";
				b.innerHTML = "Sorry, there was a problem.";

				a.appendChild(b);

				return a;
			};

			var modalFinish = function(o) {
				var a = document.createElement("div");
				a.id = "book-a-place-content";

				var b = document.createElement("div");
				b.className = "content";
				b.innerHTML = o.responseText;

				a.appendChild(b);

				return a;
			};

			// Create the modal.
			modal = new UKISA.site.Modal("book-a-place-modal");
			modal.show();

			// Get the canvas DIV
			content = document.getElementById("content");

			message = document.getElementById("book-a-place-status");

			if (message) {
				message.parentNode.removeChild(message);
			}

			message = document.createElement("p");
			message.className = "error";
			message.id = "book-a-place-status";

			callback = {
				success: function(o) {
					var enhance, validation, q;

					modal.setBody(modalFinish(o));

					enhance = new UKISA.widget.FormEnhancer(document.body);

					var validation = new UKISA.widget.FormValidation("application-form", {
						scrollTo: false	
					});

					validation.rule("title", {
						required: true,
						messages: {
							required: "Please select your title."
						}
					});

					validation.rule("EMAIL.6.FirstName", {
						required: true,
						messages: {
							required: "Please enter your first name."
						}
					});

					validation.rule("EMAIL.7.LastName", {
						required: true,
						messages: {
							required: "Please enter your last name."
						}
					});

					validation.rule("EMAIL.8.Email", {
						required: true,
						email: true
					});
					
					if (Cufon) {
						q = YAHOO.util.Selector.query("h1, h2, h3, h4", "book-a-place-modal");
						if (q) {
							Cufon.replace(q);
						}
					}
				},
				failure: function(o) {
					modal.setBody(modalError());
				},
				timeout: 8000
			};	

			// Set up the query string.
			qs = [
				"date=",
				el["date"].value,
				"&",
				"location=",
				el["location"].value,
				"&",
				"course=",
				el["course"].value
			];

			// IE6 workaround where the input names cannot match the <form> attribute names.
			action = (el.getAttributeNode) ? el.getAttributeNode("action").value : el.getAttribute("action");

			// Send the Ajax request.
			ajax = YAHOO.util.Connect.asyncRequest("POST", action, callback, qs.join(""));

			return false;
		}
	}
};