(function($) {
	window.$fn = {
		nav: function() {
			$("#nav li").each(function() {
				var a = this.getElementsByTagName("a")[0];
				if (a.href == window.location) {
					$(this).addClass("active temple");
					$(this).parents("ul:not(#nav),li").addClass("active temple");
				}
				else if (window.location.pathname.indexOf('.') != -1) {
					if (a.href.substring(a.href.lastIndexOf('/') + 1) == window.location.pathname.substring(window.location.pathname.lastIndexOf('/') + 1)) {
						$(this).addClass("active temple");
						$(this).parents("ul:not(#nav),li").addClass("active temple");
					}
					else if (typeof npid != "undefined") {
						var t = "pid"+npid;
						if (a.id == t) {
							$(this).addClass("active temple");
							$(this).parents("ul:not(#nav),li").addClass("active temple");
						}
					}
				}
			});
			$("#nav li.active>a").addClass("active");
			$("#nav li.active>ul").addClass("active temple");
		},
		setnav: function(l1, l2) {
			var count = 0;
			var that;
			$("#nav>li").each(function() {
				if (count == l1) {
					$(this).addClass("active temple");
					that = $(this).find("li");
				}
				count++;
			});
			count = 0;
			if (that.length > 0) {
				that.each(function() {
					if (count == l2) {
						$(this).addClass("active temple");
					}
					count++;
				});
			}
			$("#nav li.active>a").addClass("active");
			$("#nav li.active>ul").addClass("active temple");
		},
		equal: function(col1, col2) {
			col1 = col1 || "#main";
			col2 = col2 || "#page .sidebar:eq(0)";
			p = "#page";
			h = Math.max($(col1).height(), $(col2).height(), $(p).height());
			minh = 565;
			if (h < minh) {
				$(col1).height(minh + "px");
				$(col2).height(minh + "px");
			}
			//$(col1).height($(p).height() + "px");
			//$(col2).height($(p).height() + "px");
			//if($(col1).height() <= $(col2).height()) {
				//$(col1).height($(col2).height() + "px");
			//} else {
				//$(col2).height($(col1).height() + "px");
			//}
		}
	}
	$(document).ready(function() {
		$("#nav a").bind("mouseover", function() {
			var that = $(this).parents("li")[0];

			var ul = that.getElementsByTagName("ul")[0];

			if(typeof ul != "undefined" || $(that).parents("li").length > 0) {
				$("#nav *").removeClass("active");
			} else {
				$fn.nav();
			}

			$(that).parents("ul,li").addClass("active");

			$(ul).addClass("active")

			if($(that).hasClass("temple") || $(that).parents("li").eq(0).hasClass("temple")) {
				$(ul).find(".temple").addClass("active");
			}

			$("#nav li.active>a").addClass("active");
		});

		$("#nav>li").bind("mouseout", function() {
			$("#nav *").removeClass("active");
			$fn.nav();
		});
		if($("#affiliations").length > 0) {
			$("#affiliations").cycle({
				//before: onBefore,
				fx: "fade",
				cleartype: false,
				speed: 1200
			});
		}
		//$fn.equal();
	});
})(jQuery)

function getObj(id) { return document.getElementById(id); }
$(function(){
    $('body').onImagesLoad({
        selectorCallback: selectorImagesLoaded
    });
    function selectorImagesLoaded($selector){
		col1 = "#main";
		col2 = "#page .sidebar:eq(0)";
		p = "#page";
		snh = 0;
		$("#page #nav ul").each(function() {
			var tmph = $(this).height();
			if (tmph > snh) {
				snh = tmph;
			}
		});
		snh = snh + 174; //offset
		h = Math.max($(col1).height(), $(col2).height(), $(p).height());
		minh = 565;
		if (snh > minh) {
			minh = snh;	
		}
		if (h < minh) {
			$(col1).height(minh + "px");
			$(col2).height(minh + "px");
		}
    }
});
function onBefore(curr,next,opts) {
    var $slide = $(next);
    var w = $slide.outerWidth();
    var h = $slide.outerHeight();
    $slide.css({
        marginTop: (90 - h) / 2,
        marginLeft: (196 - w) / 2
    });
}
