function showCover() {
	openWindow(coverImage, coverWidth, coverHeight);
}

function showPages() {
	openWindow(pagesImage, pagesWidth, pagesHeight);
}

function openWindow(image, width, height) {
	if(thumbnailWindow && !thumbnailWindow.closed && thumbnailWindow.open) {
		thumbnailWindow.close();
		thumbnailWindow = null;
	}
	var options = 'resizable=no, ';
	options += 'toolbar=no, ';
	options += 'status=no, ';
	options += 'scrollbars=no, ';
	options += 'menubar=no, ';
	options += 'width=' + width + ', ';
	options += 'height=' + height;
	var address = 'preview.php?img=' + image;
	thumbnailWindow = window.open(address, 'podglad', options);
	thumbnailWindow.focus();
}

////////////////////////////////////////////////////////////////////////////////
// global variables
//////////////////////////////////////////////////////////////////////////////

var currentDiv;
var currentGallery;
var currentGalleryPosition;

var aboutSlide;
var profileSlide;
var gallerySlide;
var referencesSlide;
var awardsSlide;
var contactSlide;

var aboutHighlight;
var profileHighlight;
var galleryHighlight;
var referencesHighlight;
var contactHighlight;

var aboutScroll;
var profileScroll;
var galleryScroll;
var referencesScroll;
var awardsScroll;
var contactScroll;

var booksBackground;
var albumsBackground;
var magazinesBackground;
var printsBackground;

var sliderToShow; 

var coverImage;
var coverWidth;
var coverHeight;
var pagesImage; 
var pagesWidth;
var pagesHeight;

var thumbnailWindow;
                  
////////////////////////////////////////////////////////////////////////////////
// initialization
//////////////////////////////////////////////////////////////////////////////

function init() {
	// menu slide	
	var slideDuration = 500;

	aboutSlide = new Fx.Style('about', 'height', {duration: slideDuration, onComplete: showNewSlider});
	profileSlide = new Fx.Style('profile', 'height', {duration: slideDuration, onComplete: showNewSlider});
	gallerySlide = new Fx.Style('gallery', 'height', {duration: slideDuration, onComplete: showNewSlider});
	referencesSlide = new Fx.Style('references', 'height', {duration: slideDuration, onComplete: showNewSlider});
	awardsSlide = new Fx.Style('awards', 'height', {duration: slideDuration});
	contactSlide = new Fx.Style('contact', 'height', {duration: slideDuration, onComplete: showNewSlider});

	aboutSlide.hide();
	profileSlide.hide();
	gallerySlide.hide();
	referencesSlide.hide();
	awardsSlide.hide();
	contactSlide.hide();

	var highlightDuration = 250;
	aboutHighlight = new Fx.Color('aboutMenu', 'color', {duration: highlightDuration});
	profileHighlight = new Fx.Color('profileMenu', 'color', {duration: highlightDuration});
	galleryHighlight = new Fx.Color('galleryMenu', 'color', {duration: highlightDuration});
	referencesHighlight = new Fx.Color('referencesMenu', 'color', {duration: highlightDuration});
	contactHighlight = new Fx.Color('contactMenu', 'color', {duration: highlightDuration});

	booksBackground = new Fx.Color('galleryBooksMenu', 'background-color', {duration: highlightDuration});
	albumsBackground = new Fx.Color('galleryAlbumsMenu', 'background-color', {duration: highlightDuration});
	magazinesBackground = new Fx.Color('galleryMagazinesMenu', 'background-color', {duration: highlightDuration});
	printsBackground = new Fx.Color('galleryPrintsMenu', 'background-color', {duration: highlightDuration});
 
 	aboutScroll = new Fx.Scroll('aboutText');
 	profileScroll = new Fx.Scroll('profileText');
 	galleryScroll = new Fx.Scroll('galleryText');
 	referencesScroll = new Fx.Scroll('referencesText');
 	awardsScroll = new Fx.Scroll('awardsText');
  	contactScroll = new Fx.Scroll('contactText');
  
	initMenuButton('about');
	initMenuButton('profile');
	initMenuButton('gallery');
	initMenuButton('references');
	initMenuButton('contact');

	initGalleryMenuButton('galleryBooks');
	initGalleryMenuButton('galleryAlbums');
	initGalleryMenuButton('galleryMagazines');
	initGalleryMenuButton('galleryPrints');

	// initial menu selection
	currentDiv = 'about';
	getHighlightEffect(currentDiv).toColor('ACACAC');
	getSlideEffect(currentDiv).goTo(270);
	
	sliderToShow = '';
	
	// gallery menu
	var opacityDuration = 250;
	galleryItemOpacity = new Fx.Style('galleryItem', 'opacity', {duration: opacityDuration, transition: Fx.Transitions.linear});
	booksOpacity = new Fx.Style('galleryBooks', 'opacity', {duration: opacityDuration, transition: Fx.Transitions.linear});
	albumsOpacity = new Fx.Style('galleryAlbums', 'opacity', {duration: opacityDuration, transition: Fx.Transitions.linear});
	magazinesOpacity = new Fx.Style('galleryMagazines', 'opacity', {duration: opacityDuration, transition: Fx.Transitions.linear});
	printsOpacity = new Fx.Style('galleryPrints', 'opacity', {duration: opacityDuration, transition: Fx.Transitions.linear});

	hideGalleryItemsPanel();

	galleryItemOpacity.set(0);
	booksOpacity.set(0);
	albumsOpacity.set(0);
	magazinesOpacity.set(0);
	printsOpacity.set(0);
	$('galleryItem').setStyle('visibility', 'visible');
	$('galleryBooks').setStyle('visibility', 'visible');
	$('galleryAlbums').setStyle('visibility', 'visible');
	$('galleryMagazines').setStyle('visibility', 'visible');
	$('galleryPrints').setStyle('visibility', 'visible');
}

function initMenuButton(elementId) {
	var menuId = getMenuId(elementId);
	var slideEffect = getSlideEffect(elementId);
	$(menuId).addEvent('click', 
		function(){
			hideGalleryItemsPanel();
			if (elementId == currentDiv) {
				return;
			}
			
			sliderToShow = elementId;
			var slideEffect = getSlideEffect(currentDiv);
			slideEffect.clearTimer();
			slideEffect.goTo(0);

			if (currentDiv == 'references') {
				awardsSlide.clearTimer();
				awardsSlide.goTo(0);
			}

			var oldMenu = currentDiv;
			currentDiv = elementId;
			revertMenu(oldMenu);
		}
	);
	
	$(menuId).addEvent('mouseover', 
		function(){
			highlightMenu(elementId);
		}
	);
	
	$(menuId).addEvent('mouseout', 
		function(){
			revertMenu(elementId);
		}
	);
}

function initGalleryMenuButton(elementId) {
	var menuId = getMenuId(elementId);

	$(menuId).addEvent('click', 
		function(){
			currentGallery = elementId;
			showGalleryItemsPanel(elementId);
		}
	);
	
	$(menuId).addEvent('mouseover', 
		function(){
			highlightGalleryMenu(elementId);
		}
	);
	
	$(menuId).addEvent('mouseout', 
		function(){
			revertGalleryMenu(elementId);
		}
	);
}

////////////////////////////////////////////////////////////////////////////////
// getters
//////////////////////////////////////////////////////////////////////////////

function getSlideEffect(elementId) {
	switch(elementId) {
		case 'about':      return aboutSlide;
		case 'profile':    return profileSlide; 
		case 'gallery':    return gallerySlide; 
		case 'references': return referencesSlide; 
		case 'awards':     return awardsSlide;
		case 'contact':    return contactSlide;
	}
}

function getBackgroundEffect(elementId) {
	switch(elementId) {
		case 'galleryBooks':     return booksBackground;
		case 'galleryAlbums':    return albumsBackground;
		case 'galleryMagazines': return magazinesBackground;
		case 'galleryPrints':    return printsBackground;
	}
}

function getHighlightEffect(elementId) {
	switch(elementId) {
		case 'about':      return aboutHighlight;
		case 'profile':    return profileHighlight;
		case 'gallery':    return galleryHighlight;
		case 'references': return referencesHighlight;
		case 'contact':    return contactHighlight;
	}
}

function getSlideEffect(elementId) {
	switch(elementId) {
		case 'about':      return aboutSlide;
		case 'profile':    return profileSlide;
		case 'gallery':    return gallerySlide;
		case 'references': return referencesSlide;
		case 'contact':    return contactSlide;
	}
}

function getScrollEffect(elementId) {
	switch(elementId) {
		case 'about':      return aboutScroll;
		case 'profile':    return profileScroll;
		case 'gallery':    return galleryScroll;
		case 'references': return referencesScroll;
		case 'awards':     return awardsScroll;
		case 'contact':    return contactScroll;
	}
}

function getMenuId(elementId) {
	return elementId + 'Menu';
}

function showNewSlider() {
	if (sliderToShow != '') {
		slideEffect = getSlideEffect(sliderToShow);
		slideEffect.clearTimer();
		slideEffect.goTo(270);
		
		if (sliderToShow == 'references') {
			awardsSlide.clearTimer();
			awardsSlide.goTo(270);
		}
	}
	sliderToShow = '';
}

////////////////////////////////////////////////////////////////////////////////
// menu highlighting
//////////////////////////////////////////////////////////////////////////////

function highlightMenu(elementId) {
	if (currentDiv != elementId) {
		var colorChange = getHighlightEffect(elementId);
		colorChange.clearTimer();
		colorChange.toColor('ACACAC');
	}
}

function revertMenu(elementId) {
	if (currentDiv != elementId) {
		var colorChange = getHighlightEffect(elementId);
		colorChange.clearTimer();
		colorChange.toColor('565656');
	}
}

////////////////////////////////////////////////////////////////////////////////
// gallery highlighting
//////////////////////////////////////////////////////////////////////////////

function highlightGalleryMenu(elementId) {
	var backgroundChange = getBackgroundEffect(elementId);
	backgroundChange.clearTimer();
	backgroundChange.toColor('373737');
}

function revertGalleryMenu(elementId) {
	var backgroundChange = getBackgroundEffect(elementId);
	backgroundChange.clearTimer();
	backgroundChange.toColor('5E5E5E');
}

function showGalleryItemsPanel(type) {
	revertCurrentGalleryPosition();
	$('coverThumb').setStyle('visibility', 'hidden');
	$('pagesThumb').setStyle('visibility', 'hidden');
	
	galleryItemOpacity.clearTimer();
	booksOpacity.clearTimer();
	albumsOpacity.clearTimer();
	magazinesOpacity.clearTimer();
	printsOpacity.clearTimer();
	galleryItemOpacity.goTo(1);

	switch (type) {
		case 'galleryBooks':
			booksOpacity.goTo(1);
			albumsOpacity.goTo(0);
			magazinesOpacity.goTo(0);
			printsOpacity.goTo(0);
		break;
		case 'galleryAlbums':
			booksOpacity.goTo(0);
			albumsOpacity.goTo(1);
			magazinesOpacity.goTo(0);
			printsOpacity.goTo(0);
		break;
		case 'galleryMagazines':
			booksOpacity.goTo(0);
			albumsOpacity.goTo(0);
			magazinesOpacity.goTo(1);
			printsOpacity.goTo(0);
		break;
		case 'galleryPrints':
			booksOpacity.goTo(0);
			albumsOpacity.goTo(0);
			magazinesOpacity.goTo(0);
			printsOpacity.goTo(1);
		break;
	}
}

function hideGalleryItemsPanel() {
	galleryItemOpacity.clearTimer();
	booksOpacity.clearTimer();
	albumsOpacity.clearTimer();
	magazinesOpacity.clearTimer();
	printsOpacity.clearTimer();
	
	galleryItemOpacity.goTo(0);
	booksOpacity.goTo(0);
	albumsOpacity.goTo(0);
	magazinesOpacity.goTo(0);
	printsOpacity.goTo(0);
}

function highlightGalleryItem(buttonId) {
	highlight = new Fx.Color(buttonId, 'background-color', {duration: 1});
	highlight.toColor('373737');
}

function conditionalRevertGalleryItem(buttonId) {
	if (buttonId != currentGalleryPosition) {
		highlight = new Fx.Color(buttonId, 'background-color', {duration: 1});
		highlight.toColor('5E5E5E');
	}
}

function revertCurrentGalleryPosition() {
	if (currentGalleryPosition) {
		highlight = new Fx.Color(currentGalleryPosition, 'background-color', {duration: 1});
		highlight.toColor('5E5E5E');
	}
}

function loadGallery(buttonId, firstFile, width1, height1, secondFile, width2, height2) {
	revertCurrentGalleryPosition();
	highlightGalleryItem(buttonId);
	currentGalleryPosition = buttonId;

	var directory = buttonId;
	coverImage = directory + '/' + firstFile;
	coverWidth = width1;
	coverHeight = height1;
	pagesImage = directory + '/' + secondFile;
	pagesWidth = width2;
	pagesHeight = height2;
	
	document.images['coverThumb'].src = directory + '/tb_' + firstFile;
	document.images['pagesThumb'].src = directory + '/tb_' + secondFile;

	$('coverThumb').setStyle('visibility', 'visible');
	$('pagesThumb').setStyle('visibility', 'visible');
}

////////////////////////////////////////////////////////////////////////////////
// Scroll
//////////////////////////////////////////////////////////////////////////////

function up(elementId) {
	var scroll = getScrollEffect(elementId);
	var element = scroll.element;
	var scrollTop = element.scrollTop; 
	scroll.custom(scrollTop, Math.max(scrollTop - 100, 0)); 
}

function down(elementId) {
	var scroll = getScrollEffect(elementId);
	var element = scroll.element;
	var scrollTop = element.scrollTop; 
	scroll.custom(scrollTop,
		Math.min(scrollTop + 100, element.scrollHeight - element.offsetHeight)); 
}
