//
if (typeof Array.prototype.push == 'undefined')
{
	Array.prototype.push = function ()
	{
		for (var i = 0; i < arguments.length; i++)
		{
			this[this.length] = arguments[i];
		}

		return this.length;
	}
}

var ROOT;
var LANGUAGE;
var WRAPPER;
var PAGE;
var CSS;

//
function init(root, language, width, height)
{
	ROOT = root;
	LANGUAGE = language;
	WRAPPER = document.getElementById('wrapper');
	PAGE = WRAPPER.className.replace(/(\-.*)?\s.*/, '');
	CSS = cssEnabled(WRAPPER, width, height);

	if (CSS)
	{
		ensureVisible(true);

		switch (PAGE)
		{
			case 'home':
				preloadImage(ROOT + 'images/icons/editorial_over_' + LANGUAGE + '.jpg');
				preloadImage(ROOT + 'images/icons/kids_over_' + LANGUAGE + '.jpg');
				preloadImage(ROOT + 'images/icons/portrait_over_' + LANGUAGE + '.jpg');
				preloadImage(ROOT + 'images/icons/miscellaneous_over_' + LANGUAGE + '.jpg');

				break;

			case 'biography':
			case 'contacts':
				preloadImage(ROOT + 'images/icons/home_' + PAGE + '_over.jpg');

				break;

			case 'editorial':
			case 'kids':
			case 'portrait':
			case 'miscellaneous':
				preloadImage(ROOT + 'images/icons/home_gallery_over.jpg');
				preloadImage(ROOT + 'images/leaves/editorial_over_' + LANGUAGE + '.jpg');
				preloadImage(ROOT + 'images/leaves/kids_over_' + LANGUAGE + '.jpg');
				preloadImage(ROOT + 'images/leaves/portrait_over_' + LANGUAGE + '.jpg');
				preloadImage(ROOT + 'images/leaves/miscellaneous_over_' + LANGUAGE + '.jpg');

				break;
		}
	}
}

//
function cssEnabled(element, width, height)
{
	return (element.offsetWidth == width && element.offsetHeight == height);
}

//
function ensureVisible(load)
{
	if (load)
	{
		window.onresize = ensureVisible;
	}

	setWrapperPosition();
}

//
function setWrapperPosition()
{
	var windowSize = getWindowSize();
	var windowWidth = windowSize[0];
	var windowHeight = windowSize[1];
	var className = WRAPPER.className.replace(/\s.*/, ' ');

	var classY = (windowHeight > WRAPPER.offsetHeight) ? 'middle' : 'top';
	var classX = (windowWidth > WRAPPER.offsetWidth) ? 'center' : 'left';

	className += classY + '-' + classX;

	if (WRAPPER.className != className)
	{
		WRAPPER.className = className;
	}
}

//
function getWindowSize()
{
	var windowWidth, windowHeight;

	if (window.innerWidth)
	{
		windowWidth = window.innerWidth;
		windowHeight = window.innerHeight;
	}
	else if (document.documentElement && document.documentElement.clientWidth)
	{
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	}
	else if (document.body && document.body.clientWidth)
	{
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	return new Array(windowWidth, windowHeight);
}

//
function preloadImage(src)
{
	var image = new Image();

	image.onabort = image.onerror = function ()
	{
		alert('Image: ' + this.src);
	}

	image.src = src;
}

//
function popupIllustration(id, width, height)
{
	var features = new Array();

	if (CSS)
	{
		var left = Math.round((screen.width - width) / 2);
		var top = Math.round((screen.height - height) / 2);

		features.push('left=' + left);
		features.push('top=' + top);
		features.push('screenX=' + left);
		features.push('screenY=' + top);
		features.push('width=' + width);
		features.push('height=' + height);
		features.push('innerWidth=' + width);
		features.push('innerHeight=' + height);
		features.push('menubar=0');
		features.push('location=0');
		features.push('status=0');
		features.push('scrollbars=0');
	}

	window.open(ROOT + LANGUAGE + '/illustration.php?id=' + id, '', features.join(', '));
}