var ui = {
	
	currentGalleryImage: 0,
	
	init: function() {
	
		setInterval(function(){
			
			var min = null;
			var max = null;
			var images = jQuery("#frontGallery img");
			for (var i = 0; i < images.length; i++) {
				// Get current z-index.
				var value = parseInt(jQuery(images[i]).css("z-index"));
				
				// Get min and max values.
				if (min == null) min = images[i];
				if (max == null) max = images[i];
				
				var minValue = parseInt(jQuery(min).css("z-index"));
				var maxValue = parseInt(jQuery(max).css("z-index"));
				
				// Check if current value is the biggest or the smallest.
				if (value < minValue) min = images[i];
				if (value > maxValue) max = images[i];

			}
			
			// Hide lowest picture.
			jQuery(min).hide();
			
			// Move to top.
			var maxZIndex = parseInt(jQuery(max).css("z-index"));
			jQuery(min).css("z-index", ++maxZIndex);
			
			// Fade in.
			jQuery(min).fadeIn();

		}, 5000);
		
	},
	
	previous: function() {
		
		// To previous.
		ui.currentGalleryImage--;
		
		// Get list of images.
		var images = jQuery("#portfolioImagesAnchor img");
		
		// Limiter.
		if (ui.currentGalleryImage < 0) ui.currentGalleryImage = images.length - 1;
		
		// Set visible.
		jQuery("#portfolioImagesAnchor img").hide();;
		jQuery(images[ui.currentGalleryImage]).show();		

		ui.setGalleryMenu(ui.currentGalleryImage);
		
	},

	next: function() {

		// To previous.
		ui.currentGalleryImage++;
		
		// Get list of images.
		var images = jQuery("#portfolioImagesAnchor img");
		
		// Limiter.
		if (ui.currentGalleryImage == images.length) ui.currentGalleryImage = 0;
		
		// Set visible.
		jQuery("#portfolioImagesAnchor img").hide();;
		jQuery(images[ui.currentGalleryImage]).show();		

		ui.setGalleryMenu(ui.currentGalleryImage);
		
	},
	
	jump: function(id) {
		
		ui.currentGalleryImage = id;
		
		// Show image.
		var images = jQuery("#portfolioImagesAnchor img");
		jQuery("#portfolioImagesAnchor img").hide();;
		jQuery(images[ui.currentGalleryImage]).show();		
	
		ui.setGalleryMenu(ui.currentGalleryImage);
		
	},
	
	setGalleryMenu: function(id) {
		
		jQuery("#portfolioGallerySelector a").removeClass('galleryUnselected');
		jQuery("#portfolioGallerySelector a").removeClass('gallerySelected');

		var links = jQuery("#portfolioGallerySelector a");
		for (var i = 0; i < links.length; i++) {
			if (i == id) {
				jQuery(links[i]).addClass('gallerySelected');
			} else {
				jQuery(links[i]).addClass('galleryUnselected');
			}
		}
		
	}

	
}


jQuery(document).ready(function(){
	ui.init();
	
	// set styles for contacts (this only sets the images)
	jQuery("#contacts > p:eq(1)").css({'backgroundImage' : 'url(http://www.arkd4.fi/wp-content/themes/arkd4/images/naama-olavi.jpg)'});
	jQuery("#contacts > p:eq(2)").css({'backgroundImage' : 'url(http://www.arkd4.fi/wp-content/themes/arkd4/images/naama-edda.jpg)'});
	jQuery("#contacts > p:eq(3)").css({'backgroundImage' : 'url(http://www.arkd4.fi/wp-content/uploads/2011/04/lauri.jpg)'});
	jQuery("#contacts > p:eq(4)").css({'backgroundImage' : 'url(http://www.arkd4.fi/wp-content/uploads/2011/04/anna.jpg)'});

	// do the Godlike hover:
	jQuery("#contacts > p").mouseover(function() {
		jQuery(this).css({'backgroundPosition' : '0px -98px'});
	}).mouseout(function() {
		jQuery(this).css({'backgroundPosition' : '0px 0'});
	});
})

