$(document).ready(function() {
	$("a.greybox").click(function(){
		var t = this.title || $(this).text() || this.href;
		var GB_ANIMATION = true;
		GB_show(t,this.href,480,360);
		return false;
	});

	$("dd").hide();
	$("dd.expand").show();

	$("dt a").click(function(){
		$("dd:visible").slideUp(400);
		if ($(this).parent().next().is(":visible")) {$(this).parent().next().slideUp(400);} else {$(this).parent().next().slideDown(400);}
		return false;
	});
	
	$(".ourwork, .behindthescenes, .ourservices, .ourproducts, .whoweare").tooltip({
		track: true, 
		delay: 0, 
		showURL: false, 
		opacity: 0.95, 
		fixPNG: true, 
		showBody: " - ", 
		extraClass: "pretty fancy", 
		top: -15, 
		left: 5 
	});


	$('div.group-media:not(.processMediaTabs-processed)').addClass('processMediaTabs-processed').each(function () {

		var mediaContent = $(this).children("div.content");
		
		$("div.mediaTabs a, div.mediaThumbs a").click(function(){
			var mediaID = $(this).attr("id").split('_');
			if(mediaID.length > 2){
				// hide any field-item that is currently visible
				$(mediaContent).find("div.field-item").each(function(){
					if($(this).is(":visible"))
						$(this).hide();
				});
				
				if(mediaID.length > 3){
					for(var i=3; i < mediaID.length; i++){
						mediaID[2]+= '_' + mediaID[i];
					}
				}
				
				// display the newly clicked element
				var activeElement = $(mediaContent).find("#"+mediaID[2].replace(/\./g, "\\."));
				$(activeElement).css("display", "");

				// adjust the image (if it hasn't already been adjusted) so that it is vertically centered
				$(activeElement).find("div.emimageCrop").children("img").each(function(){
					positionVertical(this);
				});
			}
			return false;
		});

	});

});

function positionVertical(element){
	if($(element).parent() != null && $(element).height() != $(element).parent().height() && $(element).css("top") == '0px'){
		var offset = ($(element).height() - $(element).parent().height()) / -2.0;
		$(element).css("top", offset+'px');
	}
}

function slideSwitch() {
	var $active = $('#slideshow img.active');

	if ( $active.length == 0 ) $active = $('#slideshow img:last');

	var $next =  $active.next().length ? $active.next()
		: $('#slideshow img:first');

	$active.addClass('last-active');

	$next.css({opacity: 0.0})
		.addClass('active')
		.animate({opacity: 1.0}, 1000, function() {
			$active.removeClass('active last-active');
	});
}

$(function() {
	setInterval( "slideSwitch()", 5000 );
});

