$(function() {
	var height = 0;
	//$("#header_container, #slideshow_container, #divider, #footer_wrapper, #copyright_wrapper").each(function(index, element) { height += $(this).outerHeight(); });
	$("#header_container, #subpage_wrapper, #subpage_container, #subfooter_container, #copyright_container").each(function(index, element) {height += $(this).outerHeight();});
	var height_diff = $(window).height() - height;
	if (height_diff > 0) {
		$("#subpage_container").height($("#subpage_container").height() + height_diff);
	}
	if ($("#news_container").length && $("#news_content").length && $("#news_container").height() < $("#subpage_container").height()) {
		$("#news_container").height($("#subpage_container").height());
		$("#news_content").height($("#news_container").height() - ($("#news_content").offset().top - $("#news_container").offset().top) - 2 * parseInt($("#news_content").css('padding-top'), 10) - 10);
	}
	
	$(".box_editable").each(function(index, element) {
		element = $(element);
		var overlay = $('<div class="box_overlay" />')
			.css({
				width: element.outerWidth() - 2,
				height: element.outerHeight() - 2,
				left: element.offset().left,
				top: element.offset().top
			})
			.appendTo('body');
		new Popup(element, overlay, {hideCollectionOnShow: false});
		// Because the overlay is an overlay, the element loses focus as soon as the overlay is put on top. So the Edit Link has
		// to be triggered by the overlay to fix that.
		new Popup(overlay, element.find('a.box_editable_link'), {hideCollectionOnShow: false});
	});

	$("#slideshow").cycle({
		fx: "scrollHorz",
		pause: true,
		pager: "#slideshow_pager",
		pagerAnchorBuilder: function(index, DOMelement) {
			return '<a href="#"><img src="images/jquery_slides/' + (index + 1) + '.gif" width="37" height="23" /></a>';
		},
		updateActivePagerLink: function(pager, currSlide, clsName) {
			$(pager).find('img').each(function(index) {
				if (index == currSlide) {
					this.src = this.src.replace(/\.gif$/, "_ro.gif");
				} else {
					this.src = this.src.replace(/_ro\.gif$/, ".gif");
				}
			});
		}
	});
	
	$("#accordion").accordion({
		header: ".sidelinks_header",
		collapsible: true,
		autoHeight: false,
		active: (function() {
			var active = 0,
				pageName = getPageName();
			$("#accordion a").each(function(index, element) {
				if (getPageName(element.href.replace(/https?:\/\//, '')) == pageName) {
					active = $(element).closest('div').prev();
					return false;
				}
			})
			return active;
		})()
	});

	// Links to external sites should load in target _blank
	$("#content_container a[href^=http]").filter(function() {
		return this.href.indexOf(location.hostname.replace(/www\./, '')) == -1;
	}).attr('target', '_blank');
});

function getPageName(url) {
	var loc = ((location.pathname.indexOf('/content.php') != -1) || (location.pathname.indexOf('/fr_content.php') != -1)) ? location.pathname + location.search : location.pathname;
	var matches = (url || loc).match(/\/(.*)$/);
	if (matches.length == 2) {
		return matches[1];
	}
	return null;
}

jQuery.expr[':'].focus = function(elem) {
	return elem === document.activeElement && (elem.type || elem.href);
};

(function($) {
	$.fn.backgroundText = function(text) {
		this.each(function() {
			var message = text || this.value;
			$(this)
				.focus(function() {
					$(this).removeClass('idleField').addClass('focusField');
					if (this.value == message)
						this.value = "";
				})
				.blur(function() {
					if (this.value == "" || this.value == message) {
						$(this).removeClass('focusField').addClass('idleField');
						this.value = message;
					}
				})
				.closest("form").submit(function() {
					$(this).find("input[type=text].idleField, input[type=password].idleField, textarea.idleField").val("");
				});
			if ($(this).is(":focus"))
				$(this).focus();
			else
				$(this).blur();
		});
	}
})(jQuery);

(function($) {
	$.fn.rollover = function(rollover) {
		this.each(function() {
			$.data(this, 'unrolledover', this.src);
			$.data(this, 'rolledover', rollover || this.src.replace(/\.(jpe?g|gif|png)$/i, '_ro.$1'));
			$("<img />")[0].src = $.data(this, 'rolledover'); // Preload rollover image
		}).hover(
			function() {
				this.src = $.data(this, 'rolledover');
			}, function() {
				this.src = $.data(this, 'unrolledover');
			}
		);
		
		return this;
	}
})(jQuery);

function stmenu() {
	stm_bm(["menu2e19",960,"","blank.gif",0,"","",0,0,80,0,80,1,0,0,"","",0,0,1,2,"default","hand","",1,25],this);
	stm_bp("p0",[0,4,0,0,0,0,0,0,100,"",-2,"",-2,50,0,0,"#999999","","",3,0,0,"#000000"]);
	stm_ai("p0i0",[2,"","home.jpg","home_ro.jpg",39,22,0,"index.php","_self","","","","",0,0,0,"","",0,0,0,0,1,"",0,"",0,"","",3,3,0,0,"#E6EFF9","#000000","#000000","#000000","8pt Verdana","8pt Verdana",0,0,"","","","",0,0,0]);
	stm_ai("p0i1",[6,1,"#000000","divider.jpg",27,22,0]);
	stm_aix("p0i2","p0i0",[2,"","about.jpg","about_ro.jpg",60,22,0,""]);
	stm_bpx("p1","p0",[1,4,0,5,0,0,0,0,100,"progid:DXImageTransform.Microsoft.Fade(overlap=.5,enabled=0,Duration=0.30)",-2,"progid:DXImageTransform.Microsoft.Fade(overlap=.5,enabled=0,Duration=0.30)",-2,80]);
	stm_aix("p1i0","p0i0",[0,"  Profile","","",-1,-1,0,"profile.php","_self","","","","",0,0,0,"","",0,0,0,0,1,"#c40000",0,"#A33333",0,"","dropdown_ro.jpg",3,1,0,0,"#E6EFF9","#000000","#FFFFFF","#FFFFFF","9pt 'Arial','Verdana'","9pt 'Arial','Verdana'"],180,24);
	stm_aix("p1i1","p1i0",[0,"  Team","","",-1,-1,0,"team.php"],180,24);
	stm_aix("p1i2","p1i0",[0,"  Partnerships","","",-1,-1,0,"partnerships.php"],180,24);
	stm_aix("p1i3","p1i0",[0,"  News","","",-1,-1,0,"news.php"],180,24);
	stm_aix("p1i4","p1i0",[0,"  Contact Us","","",-1,-1,0,"contact.php"],180,24);
	stm_ep();
	stm_aix("p0i3","p0i1",[]);
	stm_aix("p0i4","p0i2",[2,"","practices.jpg","practices_ro.jpg",61]);
	stm_bpx("p2","p1",[1,4,0,5,0,0,0,7]);
	stm_aix("p2i0","p1i0",[0,"  Technologies & Innovation","","",-1,-1,0,"","_self","","","","",0,0,0,"arrow_r[2].gif","arrow_r[2].gif",7,7],180,24);
	stm_bpx("p3","p1",[1,2,0,0]);
	stm_aix("p3i0","p1i0",[0,"  Overview","","",-1,-1,0,"technology_overview.php"],150,24);
	stm_aix("p3i1","p1i0",[0,"  Services","","",-1,-1,0,"technology_services.php"],150,24);
	stm_aix("p3i2","p1i0",[0,"  Clients","","",-1,-1,0,"technology_clients.php"],150,24);
	stm_aix("p3i3","p1i0",[0,"  Blog","","",-1,-1,0,"technology_blog.php"],150,24);
	stm_ep();
	stm_aix("p2i1","p2i0",[0,"  Finance & Banking"],180,24);
	stm_bpx("p4","p3",[]);
	stm_aix("p4i0","p1i0",[0,"  Overview","","",-1,-1,0,"finance_overview.php"],150,24);
	stm_aix("p4i1","p1i0",[0,"  Services","","",-1,-1,0,"finance_services.php"],150,24);
	stm_aix("p4i2","p1i0",[0,"  Clients","","",-1,-1,0,"finance_clients.php"],150,24);
	stm_ep();
	stm_aix("p2i2","p2i0",[0,"  Economic Development"],180,24);
	stm_bpx("p5","p3",[]);
	stm_aix("p5i0","p1i0",[0,"  Overview","","",-1,-1,0,"economic_overview.php"],150,24);
	stm_aix("p5i1","p1i0",[0,"  Services","","",-1,-1,0,"economic_services.php"],150,24);
	stm_aix("p5i2","p1i0",[0,"  Clients","","",-1,-1,0,"economic_clients.php"],150,24);
	stm_aix("p5i3","p1i0",[0,"  Non-Profit","","",-1,-1,0,"economic_non_profit.php"],150,24);
	stm_ep();
	stm_ep();
	stm_aix("p0i5","p0i1",[]);
	stm_aix("p0i6","p0i0",[2,"","contact.jpg","contact_ro.jpg",70,22,0,"contact.php"]);
	stm_ep();
	stm_em();
}

function initFormSubmitLinks() {
	$("a.form_submit_link").click(function() {
		if (!this.title || confirm(this.title)) {
			$(this).closest("form").submit();
		}
	});
}
