/* hopped-up */

$(document).ready(function() {
	Page.init();
});

var Page = 
{
	RATIO : 1.497,
	MINIMUM_WIDTH : 955,
	DEFAULT_SECTION : $("#photo"),

	galleryImages : ["img/gallery/bg_1.jpg",
					"img/gallery/bg_2.jpg",
					"img/gallery/bg_3.jpg",
					"img/gallery/bg_4.jpg",
					"img/gallery/bg_5.jpg",
					"img/gallery/bg_6.jpg",
					"img/gallery/bg_7.jpg",
					"img/gallery/bg_8.jpg",
					"img/gallery/bg_9.jpg",
					"img/gallery/bg_10.jpg"],

	imageIndex : 0,
	
	navigation_present : false,
	actualSection : "",
	actualSubSection : "",
	
	NAVIGATION_MARGIN_TOP : function(){return Page.getViewportHeight() * (3/5);},
	NAVIGATION_MARGIN_LEFT : function(){return (Page.getViewportWidth() > Page.MINIMUM_WIDTH ? Page.getViewportWidth() : Page.MINIMUM_WIDTH) - 58 - 25;},
	
	init : function()
	{
		Page.actualSection = Page.DEFAULT_SECTION;
		
		// activate menu
		$("a.internal").click(function() {
			Page.switchSection(this);
	   });
	
		$("a.subLink").click(function(){
			Page.switchSubSection(this.rel);
		});

		Page.activateGalleryNavigation();

		// optimize gallery image display and positioning of gallery navigation
		Page.updateGallery();

		// optimze on resize 
		$(window).resize(function(){
			Page.updateGallery();
		});
		
		$('a.email').nospam({
		  replaceText: true
		});
		
		Page.switchSection($("#start")[0]);
	},
	
	switchSection : function(section)
	{
		if(section != Page.actualSection){
			$("#" + Page.actualSection.rel).hide();
			$(Page.actualSection).removeClass("activeLink");

			$("#" + section.rel).fadeIn(200);
			$(section).addClass("activeLink");

			Page.actualSection = section;
			Page.actualSubSection = "";
			$('.selectable').hide();
		}
	},
	
	switchSubSection : function(subSection)
	{
		if(subSection != Page.actualSubSection){
			if(Page.actualSubSection){
				$("." + Page.actualSubSection).hide();	
			}
			
			$("." + subSection).fadeIn(200);
			
			Page.actualSubSection = subSection;
		}
	},
	
	updateGallery : function()
	{
		// optimize gallery image display
		Page.updateGalleryImage();
		// positioning
		Page.positionGalleryNavigation(Page.NAVIGATION_MARGIN_TOP, Page.NAVIGATION_MARGIN_LEFT);
	},
	
	activateGalleryNavigation : function()
	{
		$("div.gallery_navigation").click(function(){
			if(this.id == "back"){
				Page.imageIndex = Page.imageIndex - 1;
			}else{
				Page.imageIndex = Page.imageIndex +1;
			}

			if(Page.imageIndex == Page.galleryImages.length){
				Page.imageIndex = 0;
			}

			if(Page.imageIndex < 0){
				Page.imageIndex = Page.galleryImages.length -1;
			}

			Page.createGalleryImage(Page.galleryImages[Page.imageIndex], true);
		});
	},
	
	updateGalleryImage : function()
	{
		Page.createGalleryImage($('#gallery_image').attr('src'), false);
	},
	
	createGalleryImage : function(imageFile, transition)
	{				
		maximalWidth = Page.getViewportWidth() > Page.MINIMUM_WIDTH ? Page.getViewportWidth() : Page.MINIMUM_WIDTH;

		//alert(Page.getViewportWidth());

		optimalWidth = maximalWidth;

		img_div = '<div id="gallery"><img id="gallery_image" width="'+optimalWidth+'px" src="' + imageFile + '" alt=""></div>';

		$("#gallery").replaceWith(img_div);
	},
	
	positionGalleryNavigation : function(top, left)
	{
		$("div.gallery_navigation").css("margin-top", top);
		$("div#forth").css("margin-left", left);
	},
	
	getViewportWidth : function()
	{
		var viewportWidth;
		if (typeof window.innerWidth != 'undefined'){
			viewportWidth = window.innerWidth;
		} else if (typeof document.documentElement != 'undefined'
		     && typeof document.documentElement.clientWidth !=
		     'undefined' && document.documentElement.clientWidth != 0){
			 viewportWidth = document.documentElement.clientWidth;
		} else {
			viewportWidth = document.getElementsByTagName('body')[0].clientWidth;
		}
		return viewportWidth;
	},
	
	getViewportHeight : function()
	{
		var viewportHeight;
		
		if (typeof window.innerHeight != 'undefined'){
			viewportHeight = window.innerHeight;
		} else if (typeof document.documentElement != 'undefined'
		     && typeof document.documentElement.clientHeight !=
		     'undefined' && document.documentElement.clientHeight != 0){
			 viewportHeight = document.documentElement.clientHeight;
		} else {
			viewportHeight = document.getElementsByTagName('body')[0].clientHeight;
		}
		
		return viewportHeight;
	},
	
	maximalViewport : function() 
	{
		var width, height;
		if (document.layers){
			width = screen.width-10;
			height = screen.height -20;
		}
		else{
			width=screen.width-2;
			height=screen.height;
		}
		window.resizeTo(width,height);
		window.moveTo(1,0);
	}
};
