/**
 * JavaScript by Jeffrey Ouma.
 * Copyright© 2008 artkenya.net Ltd.
 * All rights reserved.
 *
 * Script name: main.js
 * Purpose: Defines namespaces and common functions that are executed by every page.
 */
Site = {}; // Namespace: Defines functions that are specific to this website, but used widely.
Site.main = {};
Site.main.top = {
	Init: function() {
		try {
			Event.on(window, "scroll", function() {
				try {
					if(Dom.getDocumentScrollTop() === 0) {
						Site.main.top.Hide();
					} else  if (Dom.hasClass(Dom.get("top"), "hide")) {
						Site.main.top.Show();
					}
				} catch (e) {
					errorHandler(e);
				}
			});
			Event.on("top", "click", function() {
				try {
					BLEEZ.util.scroll.Vertical();
				} catch (e) {
					errorHandler(e);
				}
			});
			if(Dom.getDocumentScrollTop() != 0) {
				Site.main.top.Show();
			}
		} catch (e) {
			errorHandler(e);
		}
	},
	Show: function() {
		try {
			var elm = Dom.get("top");
			var anim = new YAHOO.util.Anim(elm, {
				opacity: {
					to: 1
				}
			}, 1, Easing.backOut);
			Dom.setStyle(elm, "opacity", "0");
			Dom.removeClass(elm, "hide");
			anim.animate();
		} catch (e) {
			errorHandler(e);
		}
	},
	Hide: function() {
		try {
			var elm = Dom.get("top");
			var anim = new YAHOO.util.Anim(elm, {
				opacity: {
					to: 0
				}
			}, 1, Easing.backOut);
			Dom.setStyle(elm, "opacity", "1");
			anim.onComplete.subscribe(function() {
				try {
				Dom.addClass(elm, "hide");
				} catch (e) {
					errorHandler(e);
				}
			});
			anim.animate();
		} catch (e) {
			errorHandler(e);
		}
	}
};
Site.main.search = {
	Init: function() {
		try {
			Event.addFocusListener("search", function() {
				try {
					if (Dom.hasClass(this, "default")) {
						this.value = "";
						Dom.removeClass(this, "default");
						Dom.get("searchbutton").disabled = false;
					}
				} catch (e) {
					errorHandler(e);
				}
			});
			Event.on("search", "blur", function() {
				try {
					if (this.value == "") {
						this.value = Dom.get("defaulttext").value;
						Dom.addClass(this, "default");
						Dom.get("searchbutton").disabled = true;
					}
				} catch (e) {
					errorHandler(e);
				}
			});
			Dom.get("search").disabled = false;
		} catch (e) {
			errorHandler(e);
		}
	}
};
Site.main.image = {
	Init: function() {
		try {
			Site.main.image.panel = new YAHOO.widget.Panel("clicktoenlarge", {
				autofillheight: "body",
				fixedcenter: false,
				constraintoviewport: true,
				underlay: "shadow",
				close: true,
				visible: false,
				modal: true,
				iframe: false,
				draggable: true,
				effect: {
					effect: YAHOO.widget.ContainerEffect.FADE,
					duration: 0.25
				}
			});
			Site.main.image.panel.hideEvent.subscribe(Site.main.image.panel.removeMask);
			Site.main.image.panel.render();
			var elmArray = Dom.getElementsByClassName("click-to-enlarge", "a", "bd");
			Event.on(elmArray, "click", function(e) {
				try {
					var elmArray = Dom.getElementsByClassName("enlarge-image", "div", "clicktoenlarge");
					var elm = this.getElementsByTagName("img");
					elm = elm[0];
					Dom.addClass(elmArray, "hide");
					Dom.removeClass("enlargeimage" + elm.getAttribute("imgid"), "hide");
					/*Site.main.image.panel.cfg.setProperty("width", this.getAttribute("imgwidth") + "px");
					 Site.main.image.panel.cfg.setProperty("height", this.getAttribute("imgheight") + "px");*/
					Dom.removeClass("clicktoenlarge", "hide");
					Site.main.image.panel.center();
					Site.main.image.panel.show();
					Event.preventDefault(e);
				} catch (e) {
					errorHandler(e);
				}
			});
			for (var i = 0; i < elmArray.length; i++) {
				var srcImg = elmArray[i].getElementsByTagName("img");
				srcImg = srcImg[0];
				var elm = document.createElement("div");
				elm.className = "enlarge-image";
				elm.id = "enlargeimage" + srcImg.getAttribute("imgid");
				var img = document.createElement("img");
				img.title = srcImg.title;
				img.alt = srcImg.alt;
				img.src = srcImg.src;
				elm.appendChild(img);
				var caption = document.createElement("p");
				caption.innerHTML = "<strong>" + srcImg.title + "</strong>";
				elm.appendChild(caption);
				Site.main.image.panel.appendToBody(elm);
			}
		} catch (e) {
			errorHandler(e);
		}
	}
};
Site.main.gallery = {
	Init: function() {
		try {
			var elmArray = Dom.getElementsByClassName("preload-image", "img", "photogallery");
			for (var i = 0; i < elmArray.length; i++) {
				var preloadImage = new YAHOO.util.ImageLoader.group("content", "mouseover", 2), url = Dom.get(elmArray[i]).getAttribute("imgurl"), w = (Dom.get(elmArray[i]).getAttribute("imgwidth")) ? Dom.get(elmArray[i]).getAttribute("imgwidth") : "", h = (Dom.get(elmArray[i]).getAttribute("imgheight")) ? Dom.get(elmArray[i]).getAttribute("imgheight") : "";
				preloadImage.registerSrcImage(elmArray[i].id, url, w, h);
			}
			Site.main.gallery.imageArray = Dom.getElementsByClassName("gallery-image", "img", "photogallery");
			Site.main.gallery.currentImage = Site.main.gallery.GetCurrentImage();
			Event.on("photogallery-previous", "click", function(e) {
				try {
					Site.main.gallery.previousImage = Site.main.gallery.currentImage;
					Site.main.gallery.currentImage = (Site.main.gallery.currentImage > 1) ? Site.main.gallery.currentImage - 1 : Site.main.gallery.imageArray.length;
					Site.main.gallery.SwitchImage();
					Event.preventDefault(e);
				} catch (e) {
					errorHandler(e);
				}
			});
			Event.on("photogallery-next", "click", function(e) {
				try {
					Site.main.gallery.previousImage = Site.main.gallery.currentImage;
					Site.main.gallery.currentImage = (Site.main.gallery.currentImage < Site.main.gallery.imageArray.length) ? Site.main.gallery.currentImage + 1 : 1;
					Site.main.gallery.SwitchImage();
					Event.preventDefault(e);
				} catch (e) {
					errorHandler(e);
				}
			});
		} catch (e) {
			errorHandler(e);
		}
	},
	GetCurrentImage: function() {
		try {
			for (var i = 0; i < Site.main.gallery.imageArray.length; i++) {
				if (!Dom.hasClass(Site.main.gallery.imageArray[i], "hide")) { return i + 1; }
			};
		} catch (e) {
			errorHandler(e);
		}
	},
	SwitchImage: function() {
		try {
			// Initialize variables
			var previousElm = Site.main.gallery.imageArray[Site.main.gallery.previousImage - 1], currentElm = Site.main.gallery.imageArray[Site.main.gallery.currentImage - 1];
			var elm = Dom.getAncestorByClassName(currentElm, "picture-wrapper"); 
			var fadeOut = new YAHOO.util.Anim(elm, {
				opacity: {
					to: 0
				},
				height: {
					to:currentElm.height
				}
			}, 0.25, YAHOO.util.Easing.easeOut);
			fadeOut.onComplete.subscribe(function() {
				try {
					Dom.setStyle(elm, "height", "auto");
					Dom.addClass(previousElm, "hide");
					Dom.removeClass(currentElm, "hide");
					fadeIn.animate();
					Dom.get("photogallery-currentimage").innerHTML = Site.main.gallery.currentImage;
					Dom.get("photogallery-caption").innerHTML = currentElm.getAttribute("alt");
				} catch (e) {
					errorHandler(e);
				}
			});
			var fadeIn = new YAHOO.util.Anim(elm, {
				opacity: {
					to: 1
				}
			}, 0.45, YAHOO.util.Easing.easeIn);
			fadeOut.animate();
		} catch (e) {
			errorHandler(e);
		}
	}
};
Site.main.paging = {
	Init: function() {
		try {
			if (Dom.get("paging") && Dom.get("paginator-content")) {
				var paginatorRows = parseInt(Dom.get("paginator-rows").value);
				Site.main.paging.content = Dom.get("paginator-content");
				Site.main.paging.records = Dom.getElementsByClassName("paginator-record", "div", Site.main.paging.content);
				Site.main.paging.paginator = new YAHOO.widget.Paginator({
					rowsPerPage: paginatorRows,
					totalRecords: Site.main.paging.records.length,
					containers: ["paging"]
				});
				Site.main.paging.paginator.subscribe("changeRequest", Site.main.paging.Paginate);
				Site.main.paging.paginator.render();
				Site.main.paging.Paginate();
			}
		} catch (e) {
			errorHandler(e);
		}
	},
	Paginate: function(state) {
		try {
			var elmArray = Dom.get("maincontentarea").getElementsByTagName("h1");
			var d = BLEEZ.util.Dimensions(elmArray[0]);
			var fadeOut = new YAHOO.util.Anim(Site.main.paging.content, {
				opacity: {
					to: 0
				}
			}, 0.25, Easing.easeOut);
			fadeOut.onComplete.subscribe(function() {
				try {
					Dom.addClass(Site.main.paging.records, "hide");
					var row = Site.main.paging.paginator.getState();
					if (!state) {
						for (var i = 0; i < row.rowsPerPage; i++) {
							Dom.removeClass(Site.main.paging.records[i], "hide");
						}
					} else {
						for (var i = state.records[0]; i <= state.records[1]; i++) {
							Dom.removeClass(Site.main.paging.records[i], "hide");
						}
						Site.main.paging.paginator.setState(state);
					}
					fadeIn.animate();
					if(Dom.getDocumentScrollTop() > d["top"]) BLEEZ.util.scroll.Vertical(1, d["top"]);
				} catch (e) {
					errorHandler(e);
				}
			});
			var fadeIn = new YAHOO.util.Anim(Site.main.paging.content, {
				opacity: {
					to: 1
				}
			}, 0.45, Easing.easeIn);
			fadeOut.animate();
		} catch (e) {
			errorHandler(e);
		}
	}
};
Site.main.login = { 
	Init: function() {
		try {
			if(Dom.get("sidebarloginemail") && Dom.get("sidebarloginpassword") && Dom.get("sidebarloginsubmit")) {
				Site.main.login.Clear();
				BLEEZ.form.field.Init("sidebarloginemail", "email");
				BLEEZ.form.field.Init("sidebarloginpassword");
				Event.on("sidebarloginlink", "click", function(e) {
					try {
						if (BLEEZ.util.cookieEnabled()) {
							// Hide the sidebar alumni box
							var fadeOut = new YAHOO.util.Anim("sidebaralumni", {
								opacity: {
									to: 0
								}
							}, 0.25, Easing.easeOut);
							fadeOut.onComplete.subscribe(function() {
								try {
									Dom.addClass("sidebaralumni", "hide");
									Dom.setStyle("sidebaralumni", "opacity", 1);
									Dom.setStyle("sidebarlogin", "opacity", 0);
									Dom.removeClass("sidebarlogin", "hide");
									fadeIn.animate();
									Dom.get("sidebarloginemail").focus();
								} catch (e) {
									errorHandler(e);
								}
							});
							var fadeIn = new YAHOO.util.Anim("sidebarlogin", {
								opacity: {
									to: 1
								}
							}, 0.45, Easing.easeIn);
							fadeOut.animate();
						} else {
							var dialog = new BLEEZ.dialog.modal;
							dialog.Show("error-dialog", "Problem Encountered", "<p><strong>Cookies are disabled in your browser.</strong></p>" + 
								"<p>You will not be able to log in until you enable cookies.</p>" + 
								"<p><strong><a href=\"http://www.google.com/support/accounts/bin/answer.py?&answer=61416\" target=\"_blank\" title=\"Find out how you can enable cookies in your browser\">How do I enable cookies in my browser?</a></strong></p>", [{
								text: "OK",
								handler: function() {
									try {
										this.hide();
									} catch (e) {
										errorHandler(e);
									}
								}
							}], "450px");
						}
						Event.preventDefault(e);
					} catch(e) {
						errorHandler(e);
					}
				});
				Event.on("sidebarloginsubmit", "click", function(e) {
					try {
						if (!Site.main.login.Validate()) Event.preventDefault(e);
					} catch (e) {
						errorHandler(e);
					}
				});
				Event.on("sidebarlogincancel", "click", function(e) {
					try {
						// Hide the sidebar login form
						var fadeOut = new YAHOO.util.Anim("sidebarlogin", {
							opacity: {
								to: 0
							}
						}, 0.25, Easing.easeOut);
						fadeOut.onComplete.subscribe(function() {
							try {
								Dom.addClass("sidebarlogin", "hide");
								Dom.setStyle("sidebarlogin", "opacity", 1);
								Dom.setStyle("sidebaralumni", "opacity", 0);
								Dom.removeClass("sidebaralumni", "hide");
								fadeIn.animate();
							} catch (e) {
								errorHandler(e);
							}
						});
						var fadeIn = new YAHOO.util.Anim("sidebaralumni", {
							opacity: {
								to: 1
							}
						}, 0.45, Easing.easeIn);
						fadeOut.animate();
						Site.main.login.Clear();
					} catch(e) {
						errorHandler(e);
					}
				});
			}
		} catch (e) {
			errorHandler(e);
		}
	},
	Clear: function() {
		try {
			BLEEZ.form.field.Clear("sidebarloginemail");
			Dom.get("sidebarloginemail").disabled = false;
			BLEEZ.form.field.Clear("sidebarloginpassword");
			Dom.get("sidebarloginpassword").disabled = false;
			Dom.get("sidebarloginremember").checked = false;
			Dom.get("sidebarloginremember").disabled = false;
			Dom.get("sidebarloginsubmit").disabled = false;
			Dom.removeClass(Dom.getAncestorByClassName("sidebarloginsubmit", "button"), "disabled");
			Dom.get("sidebarlogincancel").disabled = false;
			Dom.removeClass(Dom.getAncestorByClassName("sidebarlogincancel", "button"), "disabled");
		} catch (e) {
			errorHandler(e);
		}
	},
	Validate: function() {
		try {
			if(!BLEEZ.form.field.Validate("sidebarloginemail") || !BLEEZ.form.field.Validate("sidebarloginpassword")) return false;
			return true;
		} catch (e) {
			errorHandler(e);
		}
	}
};
/**
 * Function name: Init
 * Purpose: Executes when the page loads after the DOM is ready. Performs initialization of any
 * elements or values to their default.
 */
Site.main.Init = function() {
	try {
		var elm, elmArray;
		elmArray = Dom.getElementsByClassName("scrub");
		Event.addFocusListener(elmArray, function() {
			try {
				BLEEZ.util.Scrub(this);
			} catch (e) {
				errorHandler(e);
			}
		});
		if(Dom.get("status") && Dom.get("status-message")) {
			var dialog = new BLEEZ.dialog.modal, dialogClass, dialogHeader;
			switch(Dom.get("status").value) {
				case "success":
					dialogClass = "success-dialog";
					dialogHeader = "Success";
					break;
				case "error":
					dialogClass = "error-dialog";
					dialogHeader = "Problem Encountered";
					break;
			}			 
			dialog.Show(dialogClass, dialogHeader, Dom.get("status-message").value, [{
				text: "OK",
				handler: function() {
					try {
						this.hide();
					} catch (e) {
						errorHandler(e);
					}
				}
			}], "450px");
		}
		Site.main.top.Init();
		Site.main.search.Init();
		Site.main.paging.Init();
		Site.main.login.Init();
		if (Dom.get("photogallery")) {
			Site.main.gallery.Init();
		}
		BLEEZ.util.browserDetect.init();
		if (BLEEZ.util.browserDetect.browser != "Internet Explorer" || BLEEZ.util.browserDetect.version > 6) {
			Site.main.image.Init();
		}
	} catch (e) {
		errorHandler(e);
	}
};
try {
	Event.onDOMReady(Site.main.Init);
} catch (e) {
	errorHandler(e);
}

