var blank = new Image();
blank.src = '';
function fixPng(png) {
	// get src
	var src = png.src;
	// set width and height
	if (!png.style.width) { png.style.width = $(png).width(); }
	if (!png.style.height) { png.style.height = $(png).height(); }
	// replace by blank image
	png.onload = function() { };
	png.src = blank.src;
	// set filter (display original image)
	png.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
}

var slidetimer;
var slides = new Array();

if(!Array.indexOf){
	Array.prototype.indexOf = function(obj){
		for(var i=0; i<this.length; i++){
			if(this[i]==obj){
				return i;
			}
		}
	}
}

function autoforward (current) {
	var thisindex = slides.indexOf(current);
	var nextindex = (thisindex + 1) % slides.length;

	var nextsliderel = slides[nextindex];
	var nextslidejq = $('.splash-slide[rel="'+ nextsliderel +'"]');

	$('#splash-menu li').removeClass('selected');
	$('#splash-menu li[rel="'+ nextsliderel +'"]').addClass('selected');
	nextslidejq.hide();
	nextslidejq.detach();
	nextslidejq.insertBefore($('#splash-menu'));
	nextslidejq.fadeIn();

	slidetimer = setTimeout('autoforward("'+ nextsliderel +'")',4000);
}

$(function() {
	// top nav
	$('#menu-top-navigation>li').hover(
		function(){
			$(this).find('ul').show();
		},
		function(){
			$(this).find('ul').hide();
		}
	);

	/*
	// left nav
	$('#menu-left-navigation>li>ul').hide();
	$('#menu-left-navigation .current-page-ancestor>ul').show();

	$('#menu-left-navigation>li>a').click(function(){
		if (!$(this).parent().find('ul').is(':visible')) {
			$('#menu-left-navigation>li>ul').slideUp();
			$(this).parent().find('ul').slideDown();
			return false;
		}
	});
	*/

	// home splash
	if ($('#splash-box').length>0) {
		// visual setup
		$('#splash-menu li a').wrapInner('<span>');
		$('#splash-menu li a span').each(function(){
			$(this).css('top', $(this).parent().height()/2 - $(this).height()/2);
		});

		$('#splash-menu>li').each(function(){
			slides.push($(this).attr('rel'));
		});

		slidetimer = setTimeout('autoforward("slide1")',4000);

		$('#splash-menu li').hover(function(){
			clearTimeout(slidetimer);
			if (!$(this).hasClass('selected')) {
				$('#splash-menu li').removeClass('selected');
				$(this).addClass('selected');

				var hovered = $(this);
				$('.splash-slide').each(function(){
					if ($(this).attr('rel') == hovered.attr('rel')) {
						$(this).hide();
						$(this).detach();
						$(this).insertBefore($('#splash-menu'));
						$(this).fadeIn();
					}
				});
			}
		}, function(){
			slidetimer = setTimeout('autoforward("'+ $(this).attr('rel') +'")',4000);
		});
	}

	// small image/rollovers
	if ($('#members').length>0) {

		// visual setup
		$("#members").addClass("JS_active");

		$("#members").before("<p>Click each photo for details.</p>");
		$("#members").after("<div class=\"member_info_active\"></div>");

		$("#members li").each(function() {
			$(this).children(".member_photo").before("<div class=\"small_photo\"></div>");
			$(this).children("div.small_photo").html($(this).children(".member_photo").html());
		});

		$("#members li").children(".member_photo").hide();

		$("#members li").children("div.small_photo").each(function(){
			$(this).find('img').hide();
			$(this).find('img').load(function(){
				var logowidth = $(this).width();
				var logoheight = $(this).height();

				var aspect = logowidth / logoheight;

				if (aspect < 1.0) {
					$(this).css("width", logowidth*0.22);
					$(this).css("height", logoheight*0.22);
				} else {
					$(this).css("width", 44*aspect);
					$(this).css("height", 44);

				}

				$(this).css('top', 22 - $(this).height()/2);
				$(this).css('left', 22 - $(this).width()/2);

				// Use reaspmling algorithm in IE via filter (commonly used for alpha PNGs)
				if (/MSIE (7)/.test(navigator.userAgent)) {
					$(this).each(function() {
						if (!this.complete) {
							this.onload = function() { fixPng(this) };
						} else {
							fixPng(this);
						}
					});
				}
				$(this).show();
			});
		});

		$(".member_info_active div.small_photo").hide();
		$(".member_info_active .member_photo").show();

		$("#members li").click(
			function(){
				$("#members li").removeClass('select');
				$(this).addClass('select');
				$(this).parent().next().html($(this).html());

				$(".member_info_active div.small_photo").hide();
				$(".member_info_active .member_photo").show();

				var divheight = $(this).parent().next().children(".member_photo").height();
				var logoheight = $(this).parent().next().children(".member_photo").children("a").height();
				$(this).parent().next().children(".member_photo").children("a").css("margin-top", (divheight-logoheight)/2);

				divheight = $(this).parent().next().height();
				descheight = $(this).parent().next().children("div.description").height();
				$(this).parent().next().children("div.description").css("margin-top", (divheight-descheight)/2);
			}
		);

		$("#members li").hover(
			function(){
				$(this).addClass('hover');
			}, function(){
				$(this).removeClass('hover');
			});

		// show first item
		var firstitem = $('#members li:first');
		$(firstitem).parent().next().html($(firstitem).html());
		$(firstitem).addClass('select');

		$(".member_info_active div.small_photo").hide();
		$(".member_info_active .member_photo").show();

		var divheight = $(firstitem).parent().next().children(".member_photo").height();
		var logoheight = $(firstitem).parent().next().children(".member_photo").children("a").height();
		$(firstitem).parent().next().children(".member_photo").children("a").css("margin-top", (divheight-logoheight)/2);

		divheight = $(firstitem).parent().next().height();
		descheight = $(firstitem).parent().next().children("div.description").height();
		$(firstitem).parent().next().children("div.description").css("margin-top", (divheight-descheight)/2);

	}

	// hospital tour
	if ($('#tour').length>0) {

		$('#tour').parent().css('position', 'relative');
		$('#tour').addClass('js-on');

		var containerwidth = 0;
		$('#tour li').each(function() {
			containerwidth += $(this).outerWidth(true);
		});

		$('#tour').width(containerwidth);

		$('#tour').wrap('<div id="tour-container">');

		$('#tour-container').after('<div id="tour-control"><div id="tc-prev"></div><div id="tc-next">&#8250;</div></div>')

		$('#tc-prev').click(function(){
			$('#tour').stop(true, true);
			if ($('#tour').css('left').slice(0, -2) < 0) {
				if ($('#tour').css('left').slice(0, -2) >= -520) {
					$('#tc-prev').html('');
				}
				$('#tour').animate({
					left: '+=520'
				}, {
					duration: 'fast'
				});
				$('#tc-next').html('&#8250;')
			}
			return false;
		});

		$('#tc-next').click(function(){
			$('#tour').stop(true, true);
			if ($('#tour').css('left').slice(0, -2) > -(containerwidth-520)) {
				if ($('#tour').css('left').slice(0, -2) <= -(containerwidth-1040)) {
					$('#tc-next').html('');
				}
				$('#tour').animate({
					left: '-=520'
				}, {
					duration: 'fast'
				});
				$('#tc-prev').html('&#8249;')
			}
			return false;
		});

	}
});
