(function(){

var api =
{
	t: null,
	ie: ($.browser.msie && ($.browser.version == 6)),
	init: function()
	{
		$('#footer .buttons .popup').each(function(){
			var t = $(this);
			t.show();
			var h = t.outerHeight();
			t.hide();
			t.data('h', h);
			t.css({ top: '-' + h + 'px' });
		});
		$('#footer .buttons a.ir').hover(function() {
			var p = $('div.popup', this.parentNode);
			var h = p.data('h');
			var o = { top: '-=10px' };
			if (!api.ie)
				o['opacity'] = 'show';
			else
				p.show();
			p.animate(o, "medium");
		}, function() {
			var p = $('div.popup', this.parentNode);
			api.t = setTimeout(function(){
				api.hide1(p);
			}, 250);
		});
		$('#footer .buttons .popup').hover(function(){
			if (api.t)
			{
				clearTimeout(api.t);
				api.t = null;
			}
		}, function(){
			var p = $(this);
			api.t = setTimeout(function(){
				api.hide1(p);
			}, 250);
		});

		if ($('#slidescreen img').length > 1) {	
			var slideShow1 = new SlideShow(
				"slideShow1", 	
				3000, 			
				"slidescreen", 		
				"slides", 		
				"captions", 		
				"prev", 		
				"next", 		
				"active", 		
				"normal", 		
				"none" 			
			);
		}
		
		if ($('#staff-lg').length > 0 && $('#teamlist td').length > 0) {
			window.onload = function() {staffImgs();}
		}

		if (api.ie) {
			try {
				document.execCommand("BackgroundImageCache", false, true);
			} catch(err){};
			if (typeof(DD_belatedPNG) != 'undefined')
			{
				DD_belatedPNG.fix('#menu, #menu li ul, #footer .img img, .box1, #testimonial blockquote span, #contact1 p');
			}
		}

		if ($.browser.msie && ($.browser.version == 7))
		{
			$('#menu > ul > li').hover(function(){
				$('ul', this).css({ left: '-4px' });
			}, function(){
				$('ul', this).css({ left: '-1000em' });
			});
		}
	},
	hide1: function(p){
		var h = p.data('h');
		var o = { top: '+=10px' };
		if (!api.ie)
			o['opacity'] = 'hide';
		p.animate(o, "fast", function(){
			$(this).hide();
		});
	}
};

$(document).ready(api.init);


function staffImgs() {
	if ($('#staff-lg').length > 0 && $('#teamlist td').length > 0) {
		$('#staff-lg').attr("src","/images/team_large/chair.jpg");
		$('#teamlist td').hover(
			function() {
				var img = $(this).find(".large-photo").attr("src");
				$(this).find("p").clone().appendTo('#staff-label');
				$('#staff-lg').attr("src",img);
			}
			,
			function() {
				$('#staff-lg').attr("src","/images/team_large/chair.jpg");
				$('#staff-label').html('');
			}				
		);
	}
}
	
function SlideShow(va,de,sl,th,ca,pr,ne,ac,fa,cu) {	
	var varName = va; //The name of the variable for this particular instance of the SlideShow object
	var delay=de; //milleseconds of delay before going to next slide (3000 = 3 seconds)
	var captionID = ca; //the ID of the element where the captions are held
	var slideID = sl; //the ID of the element wrapped around the main images
	var thumbsID = th; //the ID of the element wrapped around the thumb images
	var prevID = pr; //the ID of the "previous" link
	var nextID = ne; //the ID of the "next" link
	var activeClass = ac; //class name to assign to an active thumbnail
	var fadeSpeed = fa; //speed for a fade - milleseconds or JQuery default values
	var cutOrFade = cu; //Use "cut" to cut in captions, "fade" to fade in captions, anything else for no caption
	
	var timeKeeper;
	var timer = function() {autoFlip();} // this is used so that the setTimeout references the correct SlideShow object.
		
	// loader function
	window.onload = function() {
		// start the auto-rotation	
		timeKeeper = setTimeout(timer,delay);
		// assign function to all thumbnails to display the selected thumbnail.	
		$("#"+thumbsID+" a").click(function(){
			clearTimeout(timeKeeper);
			var slideImgs = $("#"+slideID+" img");
			for(var i=0;i<slideImgs.length;i++){
				$("#"+slideID+" img:first").hide().appendTo("#"+slideID);
				$("#"+captionID+" div:first").hide().appendTo("#"+captionID);
				if($("#"+slideID+" img:last").attr('src')==$(this).attr('href')) {
					$("#"+slideID+" img:last").fadeIn(fadeSpeed);	
					$("#"+captionID+" div").hide();	
					$("#"+captionID+" div:last").show();	
					
					highlightThumb();				
					break;
				}
			}
			return false;
		});	
		// assign the previous and next functions	
		$("#"+prevID).click(function(){eval(varName).flipPrev(); return false;});	
		$("#"+nextID).click(function(){eval(varName).flipNext(); return false;});
		// make the first last to get the slideshow ready
		$("#"+slideID+" img:first").appendTo("#"+slideID);
		$("#"+captionID+" div:first").appendTo("#"+captionID);
		// highlight the first thumbnail
		highlightThumb();
	};
	
	// timer function for auto-rotation of images
	function autoFlip() {
		flipNext();
		timeKeeper = setTimeout(timer,delay);
	}
	
	// function to go to next image in list of images
	function flipNext() {
		clearTimeout(timeKeeper);
		$("#"+slideID+" img:first").hide().appendTo("#"+slideID).fadeIn(fadeSpeed);
		$("#"+captionID+" div:last").hide();
		$("#"+captionID+" div:first").hide().appendTo("#"+captionID).show();
		highlightThumb();
	}
	this.flipNext = flipNext;
	
	// function to go to previous image in list of images
	function flipPrev() {
		clearTimeout(timeKeeper);
		$("#"+slideID+" img:last").prev().show();
		$("#"+captionID+" div:last").prev().show();
		$("#"+slideID+" img:last").fadeOut(fadeSpeed,function(){
				$("#"+slideID+" img:last").prependTo("#"+slideID);
				highlightThumb();
			}
		);
		$("#"+captionID+" div:last").hide(0,function(){
				$("#"+captionID+" div:last").prependTo("#"+captionID);
			}
		);
	}
	this.flipPrev = flipPrev;
	
	// function to assing the active class to thumbnails, then calls the caption loading function
	function highlightThumb() {
		$("#"+thumbsID+" a").removeClass(activeClass).filter(function (index) {return $("#"+slideID+" img:last").attr("src") == $(this).attr("href")}).addClass(activeClass);		
	}
}

})();