$(document).ready(function(){documentReady()});

function documentReady() {
	setupRollover();
}

function setupRollover() {
	$(".rollOver").hover(
		function() {
			this.src = this.src.replace("_off","_on");
		},
		function() {
			this.src = this.src.replace("_on","_off");
		}
	);
}

function fadeInTitle() {
	$('#pageTitle').animate({'opacity': 1}, 500);
}

function hideArtisStatement() {
	hidePageTitle();
	$('.text p, #artisStatementBox img').css({'opacity' : 0});
}
function hideContact() {
	$('.expandableBox').css({'opacity' : 0});
}
function hideGallery() {
	hidePageTitle();
	$('.previousNav, .nextNav, .galleryImage').css({'opacity' : 0});
}
function hideGalleries() {
	hidePageTitle();
	$('#galleriesBox').css({'opacity' : 0});
}
function hideMainMenu() {
	$('#mainMenu').css({'top':'-700px'});
	$('#logo .mask').css({'width':'0%'});
}

function hidePageTitle() {
	$('#pageTitle').css({'opacity' : 0});
}

function playArtistStatement() {
	hideArtisStatement();
	fadeInTitle();
	$('#artisStatementBox img').animate({'opacity':1}, 500, function() {
		$('.text p').animate({'opacity': 1}, 500);
	});
}
function playContact() {
	hideContact();
	$('.expandableBox').animate({'opacity':1}, 500);
}
function playGalleries() {
	hideGalleries();
	fadeInTitle();
	$('#galleriesBox').animate({'opacity':1}, 500);
}
function openGallery() {
	hideGallery();
	fadeInTitle();
	$('.galleryImage').animate({'opacity':1}, 500, function() {
		$('.previousNav, .nextNav').animate({'opacity':1}, 500);
	});
}

function changeImage() {
	$('.galleryImage').css({'opacity' : 0});
	$('.galleryImage').animate({'opacity':1}, 500);
}

function playMainMenu() {
	hideMainMenu();
	$('#mainMenu').css({'display' : 'block'});
	$('#logo .mask').animate({'width':'100%'}, 1000, function() {
		$('#mainMenu').animate({'top':'0px'}, 500, function() {
			
		});
	});
}

function initSlideshow() {
	$('.mainImage').attr('imageCount', 0);
	initMainImageClick();
}

function initMainImageClick() {
	$('.mainImage').click(function() {
		var index = $('.mainImage').attr('imageCount');
		if(images[index].document) {
			window.open(images[index].document.link);
		} else {
			$('body').append('<div id="fullImage"><div class="bg"></div>'+images[index].image+'</div>');
			
			var image = '#fullImage img';
			originalWidth = $(image).width();
			originalHeight = $(image).height();
			
			scaleImage(image, '#fullImage', 6, originalWidth, originalHeight);
			
			$(window).resize(function() {
				scaleImage(image, '#fullImage', 6, originalWidth, originalHeight);
			});
			
			$('#fullImage').click(function() { 
				$('#fullImage').remove();
			});
		}
	});
}

function scaleImage(image, window, padding, originalWidth, originalHeight) {
	windowWidth = $(window).width() - padding;
	windowHeight = $(window).height() - padding;
	
	imageRatio = originalWidth / originalHeight;
	widthRatio = originalWidth / windowWidth;
	heightRatio = originalHeight / windowHeight;
	
	if((originalWidth < windowWidth) && (originalHeight < windowHeight)) {
		toSetWidth = originalWidth
		toSetHeight = originalHeight;
		
	} else {
		if(widthRatio > heightRatio) { // size accroding to width
		
			if(widthRatio > 1) { //image width > screen width
				toSetWidth = originalWidth / widthRatio;
			} else { // image width < screen width
				toSetWidth = originalWidth * widthRatio;
			}
			
			if(originalWidth > originalHeight) {
				toSetHeight = toSetWidth / imageRatio;
			} else {
				toSetHeight = toSetWidth * imageRatio;
			}
			
		} else { // size according to height
		
			if(heightRatio > 1) { //image height > screen height
				toSetHeight = originalHeight / heightRatio;
			} else { // image height < screen height 
				toSetHeight = originalHeight * heightRatio;
			}
			
			if(originalWidth > originalHeight) {
				toSetWidth = toSetHeight * imageRatio;
			} else {
				toSetWidth = toSetHeight / imageRatio;
			}
			
		}
	}
	
	leftValue = ((windowWidth - toSetWidth) / 2) + (padding/2);
	topValue = ((windowHeight - toSetHeight) / 2) + (padding/2);
	
	$(image).css({
		'height' : toSetHeight,
		'left' : leftValue,
		'top' : topValue,
		'width' : toSetWidth
	});
}

Object.arrayLength = function(obj) {
    var size = 0, key;
    for (key in obj) {
        if (obj.hasOwnProperty(key)) size++;
    }
    return size;
};
