/** 
 * @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 = {
 
	miniSearch: function() {
		var searchForm, searchField;

		searchForm = $("#search-bar", "legacy-header", true);

		searchField = $("#search-bar input.field", "legacy-header", true);

		YAHOO.util.Event.addListener(searchField, "focus", function(e) {
			var origin;

			origin = YAHOO.util.Event.getTarget(e);

			clearDefault(origin)
		});
			
		YAHOO.util.Event.addListener(searchForm, "submit", function(e) {
			var origin;

			origin = YAHOO.util.Event.getTarget(e);

			if (origin["searchString"].value === "Search...") {
				alert("Please enter a search term.");

				YAHOO.util.Event.preventDefault(e);

				return false;
			}
		});
	},

	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;
	},

		/**
	 * Create the home page guide tooltips.
	 */
	tooltips: function() {
		var canvas, links, tooltip;

		//canvas = document.getElementById("map");
		//if (!canvas) { return; }

		//canvas = canvas.getElementsByTagName("div");
		//if (!canvas) { return; }
		
		//canvas = canvas[2];
		//links = canvas.getElementsByTagName("a");

		links = $("a", "map");
		
		tooltip = new YAHOO.widget.Tooltip("date-tooltip", { 
			context: links,
			effect: {
				effect: YAHOO.widget.ContainerEffect.FADE,duration:0.20
			},
			width: 218,
			zIndex: 150
		});
		tooltip.setHeader("");
		tooltip.setFooter("");
		tooltip.contextMouseOverEvent.subscribe( 
			function(type, args) {
				var context = args[0];
				if (context && context.id === "B3") {
					return false;
				} else {
					return true;
				}	
			}
		);

		// Set the text for the tooltip just before we display it.

		

		tooltip.contextTriggerEvent.subscribe(
			function(type, args) {
				var context = args[0];


				var contSub = context.href.substring(context.href.indexOf("#")+1);

				var cont = document.getElementById(contSub);

				this.cfg.setProperty("text", "<p>" + cont.innerHTML + "</p>");
			}
		);

		// Overide the moveTo function to provide an offset for the speech bubble
	//	tooltip.moveTo = function(x, y) {
		//	this.cfg.setProperty("xy", [x - 43 ,y - this.element.offsetHeight + 20]); 
		//};
		tooltip.moveTo = function(x, y){
			var region = YAHOO.util.Dom.getRegion(this.element);

			this.cfg.setProperty("xy", [x - 43, y - (region.bottom - region.top)]); 
		};
	}
};

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.widget.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;

					modal.setBody(modalFinish(o));
					
					_modal = modal;

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

					var validation = new UKISA.widget.FormValidation("application-form", {
						scrollTo: false,
						callback: function(e) {
							var el, action, ajax, callback2;				

							callback2 = {
								success: function(o) {
									//debugger;
									_modal.setBody(modalFinish(o));
								},
								failure: function(o) {
									//debugger;
									_modal.setBody(modalFinish(o));
								}
							};

							el = this.context.form;

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

							YAHOO.util.Connect.setForm(el); 

							// Send the Ajax request.
							ajax = YAHOO.util.Connect.asyncRequest("POST", action + "?ajax=1", callback2);

							YAHOO.util.Event.preventDefault(e);
							YAHOO.util.Event.stopEvent(e);

							return 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;
		}
	}
};


UKISA.site.DigitalColourPalette = {
	download: function(bt, title, software) {
		var modal, canvas, content, callback, busy, ajax, qs, action, message, itemId, i, ix, modalFinish;

		var modalError = function() {
			var a = document.createElement("div");
			a.id = "software-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 = "software-content";

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

			a.appendChild(b);

			return a;
		};

		// Create the modal.
		modal = new UKISA.widget.Modal("software-modal");
		modal.show();
		modal.setHeader(title);

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

		message = document.getElementById("software-status");

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

		message = document.createElement("p");
		message.className = "error";
		message.id = "software-status";

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

				modal.setBody(modalFinish(o));
				
				_modal = modal;

				var validation = new UKISA.widget.FormValidation("palette-form", {
					scrollTo: false,
					callback: function(e) {
						var el, action, ajax, callback2, submit;				

						callback2 = {
							success: function(o) {
								_modal.setBody(modalFinish(o));
							},
							failure: function(o) {
								_modal.setBody(modalFinish(o));
							}
						};

						el = this.context.form;

						//Get the button
						submit = $("input.submit", el, true);
						if (submit) {
							submit.src = "/web/images/global/loading.gif";
							submit.disabled = true;
						}

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

						YAHOO.util.Connect.setForm(el); 
						var qs = [
							"ajax=1",
							"softwareName=" + title,
							"software=" + software
						];

						//debugger;

						var replaceParameter = function(p, v, source) {
							var url = source;

							if (typeof source === "object") {
								// Cheeky grab of the private property.
								url = source._sFormData;
							}

							url = url.replace(new RegExp(p + "=.*?(&|$)", "i"), p + "=" + v + "$1");

							// Now set the property with the changed value.
							if (typeof source === "object") {
								// Cheeky grab of the private property.
								source._sFormData = url;
							}

							return url;
						};

						replaceParameter("successURL", encodeURIComponent("/colours/tools/digital/palette_form_software.jsp?" + qs.join("&")), YAHOO.util.Connect);

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

						YAHOO.util.Event.preventDefault(e);
						YAHOO.util.Event.stopEvent(e);

						return false;
					}
				});

				validation.rule("first-name", {
					required: true,
					messages: {
						required: "Please enter your first name."
					}
				});
				
				validation.rule("surname", {
					required: true,
					messages: {
						required: "Please enter your last name."
					}
				});
				
				validation.rule("email", {
					required: true,
					email: true
				});

				validation.rule("users", {
					required: true,
					messages: {
						required: "Please enter a number of user licences."
					}
				});
				
			},
			failure: function(o) {
				modal.setBody(modalError());
			},
			timeout: 8000
		};	

		// IE6 workaround where the input names cannot match the <form> attribute names.
		action = "/colours/tools/digital/palette_form.jsp";

		// Set up the query string.
		qs = [
			"ajax=1",
			"softwareName=" + title,
			"software=" + software
		];

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

		return false;
	}
};
