$(document).ready(function(){
	$("#menu li ul").fadeTo("normal", 0);
	$("#menu li").hover(
		function() { $("ul", this).fadeTo(250, 0.95); }, 
		function() { $("ul", this).fadeTo(1, 0); }
	);
	
	if (document.all) {
		$("#menu li").hoverClass ("hover");
	}
	
	// Tab Set Behaviour
	$("#tabSet ul.tabs li").click(
		function() {			
			// reset elements
			$("#tabSet ul.tabs li").each(function(i){ 
				$(this).removeClass("selected");
				var menuText = $(this).text();
				$(this).empty();
				$(this).append('<a href="'+window.location+'#'+urlCleanFormat(menuText)+'Info">'+menuText+'</a>');
			});
			$(this).addClass("selected");
			
			// Show the related container that contains the content
			$("#tabSet div.infocontainer").addClass("hide");
			$("#"+urlCleanFormat($(this).text()) + "Info").toggleClass("hide");
			
			return false;
		}
	);
	//Expand to selected location if specified
	if(String(window.location).indexOf("#")>0 && document.getElementById(String(window.location).split("#")[1])) {
		$("#tabSet ul.tabs li").each(function(i){ 
			$(this).removeClass("selected");
			var menuText = $(this).text();
			$(this).empty();
			$(this).append('<a href="'+window.location+'#'+urlCleanFormat(menuText)+'Info">'+menuText+'</a>');
		});
		$("#tabSet ul.tabs li a[@href='"+window.location+"#"+String(window.location).split("#")[1]+"']").parents("li").addClass("selected");
		$("#tabSet div.infocontainer").addClass("hide");
		$("#"+String(window.location).split("#")[1]).toggleClass("hide");
	}
	//Expandable FAQs
	$(".faqs .infocontainer li").click( function() { $("div", this).toggle(); } );
	$(".faqs .infocontainer li div").toggle();
	
	// Dynamic Resolution Dependent Layout
	setLayoutType();
	$(window).bind("resize", function(){
		setLayoutType();
	});
	
	// Custom form Behaviour
	$(".add-commitments").click(
		function() {
			$("#commitmentGroup .empty-commitment").clone().appendTo("#commitmentGroup").removeClass("empty-commitment").removeClass("hide").addClass("new-commitment");
			return false;
		}
	);
	
	// Wrap content tables in frame div
	$(".content table").wrap('<div class="frame"></div>');
	
	// Banking Links
	$(".banking-window").attr("title", "Opens in new window");
	if($.browser.safari) {
		$(".banking-window").attr("onclick", "window.open(this.href, 'hbsbankingwindow', 'left=0,top=0,toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes'); return false;");
	} else {
		$(".banking-window").click(
			function(event) {
				var nameNewWindow = "hbsbanking";
				var newWindowHeight = screen.availHeight - 80;
			    var newWindowWidth = screen.availWidth - 18;
				var featuresNewWindow = "left=0,top=0,toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width="+newWindowWidth+",height="+newWindowHeight;
				window.open(this.href, nameNewWindow, featuresNewWindow);
				
				return false;
			} 
		);
	}
	
	// New Window Links
	$(".new-window").attr("title", "Opens in new window");
	if($.browser.safari) {
		$(".new-window").attr("onclick", "window.open(this.href, 'hbsbankingwindow', 'left=0,top=0,toolbar=yes,location=no,status=yes,menubar=yes,scrollbars=yes,resizable=yes'); return false;");
	} else {
		$(".new-window").click(
			function(event) {
				var nameNewWindow = "hbsbanking";
				var newWindowHeight = screen.availHeight - 180;
			    var newWindowWidth = screen.availWidth - 118;
				var featuresNewWindow = "left=0,top=0,toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes,width="+newWindowWidth+",height="+newWindowHeight;
				window.open(this.href, nameNewWindow, featuresNewWindow);
				
				return false;
			} 
		);
	}
	
	// Calculators
	$(".calculator-window").attr("title", "Opens in new window");
	if($.browser.safari) {
		$(".calculator-window").attr("onclick", "window.open(this.href, 'hbscalculators', 'left=0,top=0,toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=705,height=550'); return false;");
	} else {
		$(".calculator-window").click( 
			function() {
				var nameNewWindow = "hbscalculators";
				var featuresNewWindow = "left=0,top=0,toolbar=no,location=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,width=705,height=550";
				window.open($(this).attr("href"), nameNewWindow, featuresNewWindow);
				
				return false;
			} 
		);
	}
	
	//promos windows
	if($.browser.safari) {
		$(".promo-window").attr("onclick", "window.open(this.href, 'promo', 'left=0,top=0,toolbar=no,location=no,status=yes,menubar=no,scrollbars=no,resizable=no,width=470,height=530'); return false;");
	} else {
		$(".promo-window").click( 
			function() {
				var nameNewWindow = "promo";
				var featuresNewWindow = "left=0,top=0,toolbar=no,location=no,status=yes,menubar=no,scrollbars=no,resizable=no,width=470,height=530";
				window.open($(this).attr("href"), nameNewWindow, featuresNewWindow);
				
				return false;
			} 
		);
	}
	
	// Equalise profile items
	$(".people-profiles").each( function(){	$("li", this).vjustify(); }	);
});

$.fn.hoverClass = function(c) {
	return this.each(function(){
		$(this).hover( 
			function() { $(this).addClass(c);  },
			function() { $(this).removeClass(c); }
		);
	});
};

function urlCleanFormat(s) {
	s = s.replace(/\s/g, "").toLowerCase();
	s = s.replace(/&/g, ""); 
	s = s.replace(/\//g, ""); 
	s = s.replace(/'/g, ""); 
	return s;
};

function getViewportSize()
{
	size = [0,0];

	if (typeof window.innerWidth != 'undefined')
	{
		size = [window.innerWidth,window.innerHeight];
	}
	else if (typeof document.documentElement != 'undefined'	&& typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0)
	{
		size = [document.documentElement.clientWidth,document.documentElement.clientHeight];
	}
	else if (document.getElementsByTagName('body')[0] != null)
	{
		size = [document.getElementsByTagName('body')[0].clientWidth,document.getElementsByTagName('body')[0].clientHeight];
	}

	return size;
};

function setLayoutType() {
	activeWidth = getViewportSize()[0];
	(activeWidth<850 ? $("body").addClass("layoutNarrow") : $("body").removeClass("layoutNarrow"));
};

jQuery.fn.vjustify=function() {
    var maxHeight=0;
    this.each(function(){
        if (this.offsetHeight>maxHeight) {maxHeight=this.offsetHeight;}
    });
    this.each(function(){
        $(this).height(maxHeight + "px");
        if (this.offsetHeight>maxHeight) {
            $(this).height((maxHeight-(this.offsetHeight-maxHeight))+"px");
        }
    });
};