arbitraryWindowWidth = 1280;
arbitraryWindowHeight = 831;

if (typeof window.innerWidth != 'undefined') {
	thisWindowWidth = window.innerWidth;
	thisWindowHeight = window.innerHeight;
} else {
	thisWindowWidth = document.documentElement.clientWidth;
	thisWindowHeight = document.documentElement.clientHeight;
}

if(!($.browser.msie)) {
	var unloaded = false;
	window.addEventListener("unload", pageUnloaded, false);
	if(unloaded) window.addEventListener("load", pageLoaded, false);
}

$(document).ready(function() {
	$.loadImages(['back0.jpg', 'back1.jpg', 'back2.jpg']);
	
	setSizes();

	var menu = setTitles();
	
	$('.cell').hover(function() {
		var cellIndex = $('.cell').index(this);
		$('.cell').hide();
		
		$(this).removeClass(function() {
			return 'onload' + cellIndex;
		}).addClass('dotted').show();
		
		$('#cellTitle').css('left', menu[cellIndex].position).text(menu[cellIndex].title).show();
		
		$('#thisImg').attr('src', menu[cellIndex].backImg);
		$('#image').show();
		
		var imgW = $('#thisImg').width();
		var imgH = $('#thisImg').height();
		var resizedH = Math.floor(Math.ceil(imgH / imgW * thisWindowWidth));
		$('#thisImg').attr({width: thisWindowWidth, height: resizedH});
		
		$('#image').css({'top': '50%', 'margin-top': '-' + resizedH / 2 + 'px'});
		
	}, function() {
		$('.cell').removeClass('dotted').addClass(function(i) {
			return 'onload' + i;
		}).show();
		
		$('#cellTitle').hide();
		$('#image').hide();
	});
});

function pageUnloaded() {
	unloaded = true;
}

function pageLoaded() {
	window.location.reload(false);
	unloaded = false;
}

function setSizes() {
	var windowHeightCoeff = thisWindowHeight / arbitraryWindowHeight;
	var windowWidthCoeff = thisWindowWidth / arbitraryWindowWidth;
	var fontSizeCoeff = (windowHeightCoeff + windowWidthCoeff) / 2;	
	var originalFontSize = $('.text').css('font-size');
	var originalFontSpacing = $('.text').css('font-spacing');
	var fontSize = parseFloat(originalFontSize, 10);
	var fontSpacing = parseFloat(originalFontSpacing, 10);
	var newFontSize = fontSize * fontSizeCoeff;
	var newFontSpacing = fontSpacing * fontSizeCoeff;
	$('.text').css({'font-size': newFontSize, 'font-spacing': newFontSpacing});
}

function setTitles() {
	var menuTitles = new Array();
	
	menuTitles[0] = new Object();
		menuTitles[0].title = 'WORKS';
		menuTitles[0].position = '42%';
		menuTitles[0].backImg = 'back0.jpg';
		
	menuTitles[1] = new Object();
		menuTitles[1].title = 'CONTACT';
		menuTitles[1].position = '8.5%';
		menuTitles[1].backImg = 'back1.jpg';
		
	menuTitles[2] = new Object();
		menuTitles[2].title = 'BLINDSIDE';
		menuTitles[2].position = '23%';
		menuTitles[2].backImg = 'back2.jpg';
		
	return menuTitles;
}
