runOnLoad(function(){
	
	$('img.showcaseimg').pngfix();
		
	page_overlay();
	
	if ($(window).width() < 1400) {
		$('body').css('overflow','auto');
	}
	
	var measures = new Array();
	var i=0;
	while (i<$('img.showcaseimg').length) {	
		measures[i]=new Array($('img.showcaseimg:eq('+i+')').width(),$('img.showcaseimg:eq('+i+')').height());
		i=i+1;
	}

	function myCenterPos (o) {
		// returns the centred position of the image
		var id = $(o).attr('id');
		var mymeasures = measures[id];
		var left = ($(window).width()-mymeasures[0])/2;
		var top = ($(window).height()-mymeasures[1])/2;

		if (top < 60) {
			top = 60;
		}
		
		return new Array(left, top);
	}
	
	function myHiddenRightPos (o) {
		// returns the right position of the image before it is animated
		var id = $(o).attr('id');
		var mymeasures = measures[id];
		var left = $(window).width();
		var top = ($(window).height()-mymeasures[1])/2;

		if (top < 60) {
			top = 60;
		}
		
		return new Array(left, top);
	}
	
	function myLeftPos (o) {
		// returns the left position of the image that is being replaced
		var id = $(o).attr('id');
		var mymeasures = measures[id];
		var left = mymeasures[0];
		var top = ($(window).height()-mymeasures[1])/2;

		if (top < 60) {
			top = 60;
		}
		
		return new Array(left, top);
	}
	
	function left_in () {
		$('img.showcaseimg:first').stop();
		var pos = $('img.showcaseimg:first').offset()['left']+15;
	   	$('img.showcaseimg:first').animate({'left':pos}, 'slow','expoout');
		
	}
	
	function out () {
		$('img.showcaseimg:first').stop();
	 	var pos = myCenterPos($('img.showcaseimg:first'));
		$('img.showcaseimg:first').animate({'left':pos[0]}, 'normal','expoout');		
	}
	
	function right_in () {
		$('img.showcaseimg:first').stop();
		var pos = $('img.showcaseimg:first').offset()['left']-15;
	   	$('img.showcaseimg:first').animate({'left':pos}, 'slow','expoout');
		
	}
	
	function previous () {
		li = 0;
		unbindpads();
		$('img.showcaseimg:first').stop();
		$('.nav').block({ backgroundColor: 'transparent', color: 'tansparent'});
		$('img.showcaseimg:first').stop({gotolaststep:true});
		pos = myLeftPos($('img.showcaseimg:first'));
		$('img.showcaseimg:first').pause(50);
		$('img.showcaseimg:first').animate({'left':'-'+pos[0]+'px'},400,null,function (){
				$('img.showcaseimg:first').hide().remove().insertAfter('img.showcaseimg:last');
				});	
		pos = myHiddenRightPos($('img.showcaseimg:eq(1)'));
		$('img.showcaseimg:eq(1)').css({'position':'absolute','top':pos[1]+'px','left':pos[0]+'px'});
		left = ($(window).width()-measures[$('img.showcaseimg:eq(1)').attr('id')][0])/2;
		$('img.showcaseimg:eq(1)').animate({'left':left, 'display':'block'},1200,'expoout',bindpads);
		$('.nav').unblock();
	}

	function next () {
		li = 0;
		unbindpads();
		$('img.showcaseimg:first').stop();
		$('.nav').block({ backgroundColor: 'transparent', color: 'tansparent'});
		$('img.showcaseimg:first').stop({gotolaststep:true});
		pos = $(window).width();
		$('img.showcaseimg:first').pause(50);
		$('img.showcaseimg:first').animate({'left':pos},400,null,function (){
			$('img.showcaseimg:first').hide();
			$.each($('img.showcaseimg:hidden'),function (i,n) { $(n).remove().insertAfter('img.showcaseimg:last'); });
		});	
		pos = myLeftPos($('img.showcaseimg:last'));
		$('img.showcaseimg:last').css({'top':pos[1],'left':'-'+pos[0]+'px'});
		left = ($(window).width()-measures[$('img.showcaseimg:last').attr('id')][0])/2;
		$('img.showcaseimg:last').animate({'left':left, 'display':'block'},1200,'expoout',bindpads);
		$('.nav').unblock();
	}	

	function bindpads(){
		$('div#leftpad').mouseover(left_in);
		$('div#leftpad').mouseout(out);
		$('div#rightpad').mouseover(right_in);
		$('div#rightpad').mouseout(out);
	}

	function unbindpads() {
		$('div#leftpad').unbind('mouseover');
		$('div#leftpad').unbind('mouseout');
		$('div#rightpad').unbind('mouseover');
		$('div#rightpad').unbind('mouseout');
	}
	
	bindpads();

	$.extend($.blockUI.defaults.overlayCSS, { backgroundColor: 'transparent' });
			
	$('img.showcaseimg').hide();
	$('.loading').hide();
	pos = myHiddenRightPos($('img.showcaseimg:first'));
	$('img.showcaseimg:first').css({'top':pos[1],'left':pos[0],'position':'absolute'});
	left = ($(window).width()-measures[$('img.showcaseimg:first').attr('id')][0])/2;
	$('img.showcaseimg:first').animate({'left':left, 'display':'block'},1200,'expoout');

	$('.previous').click(previous);
	$('.next').click(next);
	
	
	// insert timeout here //
	var li = -5000;

	$(document).mousemove(function () {
		li = 0;
	});

    function loop () {
    	if (li == 6000) {
    		if ($('div.jqmOverlay').length == 0) {
    			previous();
    			li = 0;
    		}
    	} else {
    		li = li + 1000;
    	}
    	setTimeout(loop,1000);
    }	

	loop();


	// create and remove link on the image on mousehover
	$('img.showcaseimg').hover(function (){
		link = $(this).attr('href');
		$(this).bind('click',function (){
			location.href=link;
		});
	},function() {
		$(this).unbind('click');
	});	
	
	// display pages in overlays
 	$('#contact').jqm({
		trigger: "a.contact_trigger", 
		overlay: 1,
		onShow: function(h) {
			//$('.nav').hide();
			var bodyWidth = $(window).width();
			var bodyHeight = $(window).height();
			h.w.css('left', (bodyWidth - 470) / 2);
			h.w.css('top', (bodyHeight - 470) / 2);
			h.w.show();
			h.w.children("div.page").children('img.circle').pngfix();
			h.o.css({'position':'absolute'});
		},
		onHide: function(h) {
			h.w.children("div.page").children('img.circle').pngunfix();
			h.w.hide();
			h.o.remove();
			//$('.nav').show();
		}
		});

 	$('#about').jqm({
		trigger: "a.about_trigger", 
		overlay: 1,
		onShow: function (h) {
			//$('.nav').hide();
			var bodyWidth = $(window).width();
			var bodyHeight = $(window).height();
			h.w.css('left', (bodyWidth - 470) / 2);
			h.w.css('top', (bodyHeight - 470) / 2);
			h.w.show();
			h.w.children("div.page").children('img.circle').pngfix();
			h.o.css({'position':'absolute'});
		},
		onHide: function(h) {
			h.w.children("div.page").children('img.circle').pngunfix();
			h.w.hide();
			h.o.remove();
			//$('.nav').show();
		}		
		});
		
	$('#clips').jqm({
		ajax: "/files/clippings.html", 
		trigger: "a.clips_trigger",
		overlay: 85,
		onShow: function (h) {
			h.w.css('top',0);
			h.w.show();
		} 
	});

	$('#publication').jqm({
		ajax: '/files/publication.html', 
		trigger: "a.publication_trigger",
		overlay: 85,
		onLoad: function(h){
			h.w.click(function(){
				h.w.jqmHide();
				$('.jqmOverlay').remove();
			});
		}
	});

	$('#press').jqm({
		ajax: '/files/press.html', 
		trigger: "a.press_trigger",
		overlay: 85,
		onLoad: function(h){
			h.w.click(function(){
				h.w.jqmHide();
				$('.jqmOverlay').remove();
			});
		}
	});
	
	$('#award').jqm({
		ajax: '/files/award.html', 
		trigger: "a.award_trigger",
		overlay: 85,
		onLoad: function(h){
			h.w.click(function(){
				h.w.jqmHide();
				$('.jqmOverlay').remove();
			});
		}
	});
	
	$('#lectures').jqm({
		ajax: '/files/lectures.html', 
		trigger: "a.lectures_trigger",
		overlay: 85,
		onLoad: function(h){
			h.w.click(function(){
				h.w.jqmHide();
				$('.jqmOverlay').remove();
			});
		}
	});
	
	$('#exhibitions').jqm({
		ajax: '/files/exhibitions.html', 
		trigger: "a.exhibit_trigger",
		overlay: 85,
		onLoad: function(h){
			h.w.click(function(){
				h.w.jqmHide();
				$('.jqmOverlay').remove();
			});
		}
	});
	
	// handle iframed overlays sizes
	function page_overlay() {
		$('.clippingsWindow').width($(window).width()-5);
		$('.clippingsWindow').height($(window).height()-20);
		$('.clippingsWindow#header').width($(window).width()-20);
		$('.eventsWindow').width($(window).width()-20);
		$('.eventsWindow').height($(window).height()-20);
	}
	
	$(window).resize(function() {
		page_overlay();
		if ($(window).width() < 1400) {
			$('body').css('overflow','auto');
		}
	});
	
});

