var bjarteRettedal = {
	browser_height : 0,
	browser_width : 0,
	fp_image_width: 1,
	fp_image_height: 1,
	fp_ct_height : 0.8,
	g_title : '',
	g_photos : [],
	g_titles : [],
	g_widths : [100],
	g_heights : [100],
	g_index : 0,
	frontpage_a: 1,
	nav_container_ml : 1,
	nav_container_mt : 1,
	nav_container_h : 1,
	nav_container_w : 1,
	logo_w : 1,
	ct_height: 0.8,
	ct_r: 1.5,
	background_image: "",
	first_nav : 1,
	bar_font_size : 1,
	preload_photos: [],
	
	checksize: function(){
		if ($(window).height() < 480){
			$('link[href^=/media/css/project.css]').attr('href', '/media/css/project_small.css');
		}
	},
		 
	
	frontpage: function(photo, width, height, photos){
		 var img = new Image();
		this.preload_photos = photos;
		img.onload = function (){
			$('.front_page_photo img').attr({'src' : photo});
			$('.front_page_photo img').attr({'width': bjarteRettedal.n_w, 'height': bjarteRettedal.nav_container_h });
			$('.front_page').fadeIn(500);
			bjarteRettedal.preload();
		};
		 img.src = photo;
		$('.front_page_button').click(function(){
			$('.front_page_button').hide();
			bjarteRettedal.shade(1);
			$('.menu').slideDown(1000);
			$('.copyright').fadeIn(1000);
			$('.separator_3').hide();
			$('.separator_2').fadeIn(1000);
			$('.menu_content_1').fadeIn(1000);
			$('.menu_content_2').fadeIn(1000);
		});
		
	},
	
	shade : function(onoff){
		if(onoff){
		$('.page_overlay').fadeIn(800);
		}
		else{
		$('.page_overlay').fadeOut(800);
		}
	},
	
	init : function(p_width, p_height){
		this.browser_width = $(window).width();
		this.browser_height = $(window).height();
		this.ct_r = p_width / p_height;
		this.nav_container_h = Math.round(this.browser_height * this.ct_height);
		this.nav_container_w = Math.round(this.nav_container_h * this.ct_r);
		this.nav_container_ml = Math.round((this.browser_width - this.nav_container_w)/2);
		this.nav_container_mt = Math.round((this.browser_height - this.nav_container_h)/2);
		this.logo_w = Math.round(((this.nav_container_h * 0.12)/84)*200);
		this.logo_w_2 = Math.round(((this.nav_container_h * 0.09)/84)*200);
		$('.logo').css({'width' : this.logo_w_2 });
		$('.menu_content_1').css({'width' : this.logo_w });
		$('.menu_content_2').css({'width' : this.logo_w  });
		$('.menu_content_1').hide();
		$('.menu_content_2').hide();
		$('.separator_2').css({'width' : this.logo_w });
		$(".front_page").hide();
		$(".menu").hide();
		$(".gallery").hide();
		$(".about_page").hide();
		$(".copyright_page").hide();
		$('.gallery_wait_graphic').hide();
		$('.gallery_closed_bar').hide();
		$('.gallery_nav_container').hide();
		$('.page_overlay').hide();
		$('.page_overlay_2').hide();
		$('.gallerier').hide();
		$('.copyright').hide();
		$('.design').hide();
		$('#gallerier').hide();
		$('.separator_2').hide();
		$('.front_page').css({'height': this.nav_container_h, 'width': this.nav_container_w, 'left': this.nav_container_ml, 'top': this.nav_container_mt });
		$('.nav_container').css({'height': this.nav_container_h, 'width': this.nav_container_w, 'left': this.nav_container_ml, 'top': this.nav_container_mt });
		$(window).resize(function() {
			bjarteRettedal.resize();
		});
		$('.gallery_nav_l').hover(function(){
			$('.gallery_nav_l img').fadeIn(500).delay(500).fadeOut(500);
		},  function(){});
		$('.gallery_nav_r').hover(function(){
			$('.gallery_nav_r img').fadeIn(500).delay(500).fadeOut(500);
		}, function(){});

		$('.gallery_nav_r').click(function(){
			if(bjarteRettedal.first_nav == 1){
				bjarteRettedal.first_nav = 0;
				$('.gallery_nav_menu').slideUp(800);
				$('.gallery_closed_bar').show(800);	
			}
			if (bjarteRettedal.g_index == (bjarteRettedal.g_photos.length - 1)){
				bjarteRettedal.g_index = 0;
			} 
			else {
				bjarteRettedal.g_index = bjarteRettedal.g_index + 1;
			}
			bjarteRettedal.showphoto();
		});
		$('.gallery_nav_l').click(function(){
			if(bjarteRettedal.first_nav == 1){
				bjarteRettedal.first_nav = 0;
				$('.gallery_nav_menu').slideUp(800);
				$('.gallery_closed_bar').show(800);	
			}
			if (bjarteRettedal.g_index == 0){
				bjarteRettedal.g_index = (bjarteRettedal.g_photos.length - 1);
			} 
			else {
				bjarteRettedal.g_index = bjarteRettedal.g_index - 1;
			}
			bjarteRettedal.showphoto();
		});
		$('.gallery_home').click(function(){
		bjarteRettedal.shade(1);
		$('.nav_container').show();
		$('.gallery_nav_container').hide();
			
		});
		$('#About_button').click(function(){
			$('.copyright_page').hide();
			$('.about_page').fadeIn(500);
			$('.design').fadeIn(500);
		});
		$('#Copyright_button').click(function(){
			$('.about_page').hide();
			$('.copyright_page').fadeIn(500);
		});
		$('.gallery_menu_img').click(function(){
			$('.gallery_nav_menu').slideUp(800);
			$('.gallery_closed_bar').show(800);
		});
		$('.gallery_closed_bar').click(function(){
			$('.gallery_closed_bar').hide();
			$('.gallery_nav_menu').slideDown(800);
			
		});
		
		
	},
	
	preload: function() {
		for(k=0; k < bjarteRettedal.preload_photos.length; k++){
			 var img=new Image();
   			 img.src=bjarteRettedal.preload_photos[k];
		}
			
	},
	
	gallery: function(options) {
		this.g_title = options.title;
		this.g_photos = options.photos;
		this.g_titles = options.titles;
		this.g_widths = options.widths;
		this.g_heights = options.heights;
		this.g_index = 0;
		$('.front_page').hide();
		$('.nav_container').hide();
		$('.about_page').hide();
		$('.copyright_page').hide();
		$('.page_overlay_2').show();
		$('.design').hide();
		this.showphoto();
		bjarteRettedal.shade(0);
		$('.gallery').show();
		$('.gallery_nav_container').show();
		$('.page_overlay_2').fadeOut(5000);
		this.display_ui();
		this.first_nav = 1;
		
	},
	
	showphoto : function() {
		$('.gallery_photo img').hide();
		$('.gallery_wait_graphic').show();
		var photo = this.g_photos[this.g_index];
		var title = this.g_titles[this.g_index];
		var g_title = '#' + this.g_title + '_'+ String(this.g_index);
		this.pho = new Image();
		this.pho.onload = function (){
			$('.gallery_photo img').attr({'src' : photo, 'alt' : title});
			bjarteRettedal.photoscale();
			$('.gallery_title').html(title);
			$('.gallery_text').html($(g_title).html());
			$('.gallery_wait_graphic').hide();
			$('.gallery_photo img').fadeIn(300);
		};
		this.pho.src = photo;
	},
	
	photoscale : function(){
		var height = this.g_heights[this.g_index];
		var width = this.g_widths[this.g_index];
		var c_h = Math.round(this.browser_height * 0.8);
		var c_w = Math.round(this.browser_width * 0.8);
		var r_h = c_h / height;
		var r_w = c_w / width;
		var r = Math.min(r_w, r_h);
		var n_h = (Math.round(r * height));
		var n_w = (Math.round(r * width));
		var m_h = (Math.round((this.browser_height - n_h)/2));
		var m_w = (Math.round((this.browser_width - n_w)/2));
		var nav_h = Math.round(n_h * 0.2);
		var nav_m_h = Math.round((n_h - nav_h)/2);
		var spinner_h = Math.round(this.browser_height /2 );
		var spinner_w = Math.round(this.browser_width /2 );
		$('.gallery_wait_graphic').css({'margin-top' : spinner_h, 'margin-left' : spinner_w});
		$('.gallery_photo img').css({'width' : n_w, 'height' : n_h});
		$('.gallery_nav_container').css({'width' : n_w, 'height' : n_h, 'left' : m_w, 'top': m_h });
		$('.gallery_photo').css({ 'left' : m_w, 'top': m_h  });
		$('.gallery_nav_l img').css({'width': 'auto', 'height': nav_h });
		$('.gallery_nav_l img').css({'margin-top' : nav_m_h, 'margin-left': '12px'});
		$('.gallery_nav_r img').css({'width': 'auto', 'height': nav_h });
		$('.gallery_nav_r img').css({'margin-top' : nav_m_h, 'margin-right': '12px'});
	
	},
	
	resize : function(){
		this.browser_height = $(window).height();
		this.browser_width = $(window).width();
		this.nav_container_h = Math.round(this.browser_height * this.ct_height);
		this.nav_container_w = Math.round(this.nav_container_h * this.ct_r);
		this.nav_container_ml = Math.round((this.browser_width - this.nav_container_w)/2);
		this.nav_container_mt = Math.round((this.browser_height - this.nav_container_h)/2);
		this.logo_w = Math.round(((this.nav_container_h * 0.12)/84)*200);
		this.logo_w_2 = Math.round(((this.nav_container_h * 0.09)/84)*200);
		$('.gallery_bg').css({'width': this.browser_width});
		$('.gallery_bg').css({'height': this.browser_height});
		$('.logo').css({'width' : this.logo_w_2 });
		$('.menu_content_1').css({'width' : this.logo_w });
		$('.menu_content_2').css({'width' : this.logo_w  });
		$('.separator_2').css({'width' : this.logo_w });
		$('.front_page').css({'height': this.nav_container_h, 'width': this.nav_container_w, 'left': this.nav_container_ml, 'top': this.nav_container_mt });
		$('.nav_container').css({'height': this.nav_container_h, 'width': this.nav_container_w, 'left': this.nav_container_ml, 'top': this.nav_container_mt });
		$('.front_page_photo img').attr({'height': this.nav_container_h, 'width': this.nav_container_w });
		
		
		this.photoscale();
	},
	
	display_ui : function(){
		$('.gallery_nav_l img').fadeIn(500).delay(1500).fadeOut(500);
		$('.gallery_nav_r img').fadeIn(500).delay(1500).fadeOut(500);
		$('.gallery_menu').show(500, function(){
			$('.gallery_title').show();
			$('.gallery_text').show();
			$('.gallery_home').show();
		});
		
	}
	
	
};
