$(document).ready(function(){
	
	var $pagetype = $('body').attr('class');
	if ($pagetype.indexOf('news') >= 0) $pagetype = 'news';
	var $pageheight = $(window).height();
	
	$('#menu .' + $pagetype + ' a').addClass('active');
	
	if ($pagetype == 'current') {
		
		$("#display").wrapInner(document.createElement("tr"));
		$("#display").wrapInner(document.createElement("table"));
		$('#display .exhibition').wrap("<td>").each(function(i) {
			$(this).css('margin-right', $(window).width()-$(this).width()-280);
		});
		
	}
	if ($pagetype == 'about') {
	
		var $pw = $(window).width()-900;
		$("#display").wrapInner(document.createElement("tr"));
		$("#display").wrapInner(document.createElement("table"));
		$("#display .panel").css('margin-right',$pw).wrap("<td>");
		
	}
	if ($pagetype == 'news') {
	
	
	}
	if ($pagetype == 'fairs') {
	
		$("#display").wrapInner(document.createElement("tr"));
		$("#display").wrapInner(document.createElement("table"));
		$("#display .panel").wrap("<td>");
	
	}
	if ($pagetype == 'artists' || $pagetype == 'exhibition' || $pagetype == 'private') {
	
		if ($($pagetype == 'exhibition' && 'title:contains("current")').length > 0) {
			$('#menu .current a').addClass('active');
		}
		
		$("#display").wrapInner(document.createElement("tr"));
		$("#display").wrapInner(document.createElement("table"));
		$("#display .column").wrap("<td>");
		$("#display .artwork").wrap("<td>");
		$("#display .video").wrap("<td>");
		$("#display .scroll").wrap("<td class=\"back\">");
		
		$('.artwork').hover(function(){
			$('.info', this).show();
		}, function(){
			$('.info', this).hide();
		});
		
		$('.artwork').bind('click',function(event){
			$('html, body').stop().animate({
				scrollLeft: $(this).offset().left-280
			}, 1000,'easeInOutExpo');
			$(this).parents('#display').find('.info').hide();
			$('.info', this).show();
			event.preventDefault();
		});
		
		$('.column a').tooltip({ effect: 'toggle', position: 'center right', offset: [0, 6] });
		
		if ($('a.video')) {
			$('a.video').click(function(){
				$(this).colorbox({iframe:true, innerWidth:640, innerHeight:$('img', this).attr('height')});
			});
		}

	}

	$('.scroll a').bind('click',function(event){
		var $anchor = $(this).attr('href');
		$('html, body').stop().animate({
			scrollLeft: $($anchor).offset().left
		}, 1000,'easeInOutExpo');
		event.preventDefault();
	});
	
	$('#display').delay(100).fadeIn(1000, function() {
		if ($pagetype == 'current') {			
			var $anchor = $('#current');
			$('html, body').scrollLeft($($anchor).offset().left);
		}
		$('.artwork, .panel').animate({ opacity: 1 }, 300, function(){
			$(this).css('filter','');
		});
		$('#menu').css('height', $('#display').height()+80);
	});
	
	
	
	var deviceAgent = navigator.userAgent.toLowerCase();
	var agentID = deviceAgent.match(/(iphone|ipod)/);
	
	if (!agentID && $pageheight < 760) {
		scaleImage();
	}
	$(window).resize(function() {
		scaleImage();
	});
	function scaleImage() {
		$resize = $(window).height() - 185;
		$('.artwork img').aeImageResize({ height: $resize });
		$('.exhibition img').aeImageResize({ height: $resize });
	}
	
	if (agentID) {

		$('#container, #artist_name').css('position', 'absolute');
 
	}
	
});

