/*!
 * (c) 2011 Robert Wetzlmayr (http://wetzlmayr.ch/)
 */
var behaviour = {
	initialize : function () {
		// flicker suppressor
		$('article.image img').css('visibility', 'hidden');

		$(window).resize(function() {
			// ABOUT
			// align top of "text" images in #stage with header nav
			var imgText = $('article.text > img');
			if (imgText.length) {
				var header = $('header');
				var top =  ~~($('html').height() - $('header > div').height()) / 2;
				if (top < 0) top = 0;
				imgText.css('position', 'absolute');
				imgText.css('top', top+'px').css('left', header.outerWidth(true)+40+'px');
				// nav @ position: fixed
				header.css('position', 'fixed').css('top', top);
				return;
			}
			
			// ADVERTISING, DESIGN, MAKING OF
			// scale exhibit down
			var exhibit = $('article.image img');
			if (exhibit.length) {
				// flicker suppressor
				exhibit.css('visibility', 'hidden');
						
				// scale exhibit down
				var stage = $('#stage');
				var maxwidth = Math.min(exhibit.data('width'), $(window).width() 
						- $('header').outerWidth())
						- ~~$('#stage').css('padding-left').replace(/px/, '')
						- ~~$('#stage').css('padding-right').replace(/px/, '');
				var maxheight = Math.min(exhibit.data('height'), $(window).height() 
						- $('.details').outerHeight()
						- ~~$('#stage').css('padding-top').replace(/px/, '') 
						- ~~$('.details').css('margin-top').replace(/px/, '') 
						- 20 /* cheatin' */);
				
				var newwidth = Math.max(exhibit.data('width'), exhibit.width());
				var newheight = Math.max(exhibit.data('height'), exhibit.height());			
				var aspectratio = newwidth / newheight;
				
				if (newwidth > maxwidth) {
					newwidth = maxwidth;
					newheight = ~~(newwidth / aspectratio);
				}
				
				if (newheight > maxheight) {
					newheight = maxheight;
					newwidth = ~~(newheight * aspectratio);
				}
				
				// minimum height
				if (newheight < 80) {
					newheight = 80;
					newwidth = ~~(newheight * aspectratio);
				}
				exhibit.height(newheight);
				exhibit.width(newwidth);
				exhibit.css('visibility', 'visible');
			}

			// ADVERTISING, DESIGN, MAKING OF
			// position exhibits center/center
			var exhibit = $('article.image img, article.video .mejs-video, article.video video, article.vimeo iframe');
			if (exhibit.length) {
				// h-center
				var article = $('#stage > article');
				article.width(exhibit.outerWidth(true));
			}
		});
		
		$(window).resize();
		
	}
};


$(document).ready( function() {
	$('body').addClass('js');
    try {
    	$('video,audio').mediaelementplayer();
    } catch(e) {}
	behaviour.initialize();
});

