	var current_page = "";
	var gallery = {
		ids:{
			pic_port:"port",
			pic_production:"production"
			},
		totals:{
			port:12,
			production:7		},
		current: "",
		page:1,
		page_max:12,
		pages:1,
		remainder:0
	}
	var nav = {
		nav_vars:[]
	}
	var subnav = {
		subnav_gallery_vars:[]
	}
	var pics = {}
	
	var Pic = Class.create({
		initialize: function(o, source, thumb_source, thumb_position, pic_target, set_id, callback){
			if(!set_id){set_id=null;}
			if(!callback){callback=null;}
			this.var_name=o;
			this.target = pic_target;
			this.thumb_position = thumb_position;
			this.source = source;
			this.thumb_src = thumb_source;
			this.thumb = Builder.node("img", {src:thumb_source, onclick:o+".swap("+o+");"});
			if(set_id){this.thumb.id = set_id;}
			Element.insert(thumb_position, this.thumb);
			if(callback == "load_content"){Event.observe(this.target, 'click', load_content);}
			if(callback == "load_gallery"){Event.observe(this.target, 'click', load_gallery);}
			if(callback == "load_about"){Event.observe(this.target, 'click', load_about);}
			this.preload();
		},
		preload: function(){
			this.pic = new Image();
			this.pic.loaded = 0;
			this.pic.onload = this.ready;
			this.pic.onerror = this.fail;
			this.pic.onabort = this.fail;
			this.pic.src = this.source;
		},
		ready: function(){
			this.loaded = 1;
		},
		fail: function(){
			this.loaded = 2;
		},
		swap: function(o){
			Effect.Pulsate(o.thumb_position, {pulses:1, from:.7, duration:.5});
			if(o.pic.loaded == 1){
				var t = $(o.target);
				t.fade({duration:.4, queue:{position: 'end', scope: 'center_image_queue'}});
				setTimeout(function(){
					t.width=1;
					t.height=1;
					t.src=o.pic.src;
					t.width=o.pic.width;
					t.height=o.pic.height;
					t.appear({duration:.5, queue:{position: 'end', scope: 'center_image_queue'}});
				}, 400);
			}
			else if (o.pic.loaded == 2){$(o.target).src="";}
			else setTimeout(o.var_name + ".swap(" + o.var_name + ");", 500);
		}
	});
	
	var navPic = Class.create(Pic,{
		swap: function(){
			for(var i = 0; i<nav.nav_vars.length; i++){
				$(nav.nav_vars[i].target).src=nav.nav_vars[i].thumb_src;
			}
			$(this.target).src=this.source;
		}
	});
	
	var subnavGalleryPic = Class.create(Pic,{
		swap: function(){
			for(var i = 0; i<subnav.subnav_gallery_vars.length; i++){
				$(subnav.subnav_gallery_vars[i].target).src=subnav.subnav_gallery_vars[i].thumb_src;
			}
			$(this.target).src=this.source;
		}
	});
	
	function load_content(event){
		elem = Event.element(event);
		fade_others(".subnav", "subnav_queue");
		switch(elem.id){
			case "pic_bio":
				this_page = "bio";
				break;
			case "pic_resume":
				this_page = "resume";
				break;
			case "pic_portfolio":
				this_page = "portfolio";
				$('subnav_portfolio').appear({duration:.5, queue:{position:'end', scope:'subnav_queue'}});
				break;
			case "pic_contact":
				this_page = "contact";
				break;
			case "pic_news":
				this_page = "news";
				break;
			case "pic_home":
				this_page = "home";
				break;
		}
		if(this_page != current_page){
			pageTracker._trackPageview("/"+this_page);
			fade_others(".content");
			current_page = this_page;
			$(current_page).appear({duration:.75, queue:{position: 'end', scope: 'content_queue'}});
		}
	}
	
	function load_gallery(event, first_load){
		if(!first_load){first_load = false;}
		if(typeof(event) == "object"){
			gallery.page = 1;
			elem = Event.element(event);
			this_gallery = gallery.ids[elem.id];
		}
		else{this_gallery = event;}
		if(!first_load) pageTracker._trackPageview("/"+this_gallery);
		fade_others('.content', 'gallery_queue');
		gallery.current = this_gallery;
		gallery.pages = Math.ceil(gallery.totals[gallery.current]/gallery.page_max);
		gallery.remainder = gallery.totals[gallery.current] % gallery.page_max;
		setTimeout(function(){
		var table_left = $('thumbs_left_table')
		table_left.childElements().each(function(s){s.remove();});
		table_left.insert({bottom:write_thumbs(this_gallery, "left", 1+12*(gallery.page-1), 6+12*(gallery.page-1))});
		var table_right = $('thumbs_right_table')
		table_right.childElements().each(function(s){s.remove();});
		table_right.insert({bottom:write_thumbs(this_gallery, "right", 7+12*(gallery.page-1), 12*(gallery.page))});
		temp_int = setInterval(function(){
			if($H(pics).all(function(n){return $H(n).get("1").pic.loaded;})){
				$("arrow_left_container").style.display="none";
				$("arrow_right_container").style.display="none";
				if (gallery.pages > 1 && gallery.page != gallery.pages){
					var g = gallery.page + 1;
					$("arrow_num_right").src = "images/" + g + "white.png";
					$("arrow_right_container").style.display="block";
				}
				if (gallery.pages > 1 && gallery.page != 1){
					var g = gallery.page - 1; 
					$("arrow_num_left").src = "images/" + g + "white.png";
					$("arrow_left_container").style.display="block";
				}
				var c = $('center_img');
				var b = pics[gallery.totals[gallery.current] - ((gallery.page-1)*gallery.page_max)];
				c.width=1;
				c.height=1;
				c.src = b.pic.src;
				c.width=b.pic.width;
				c.height=b.pic.height;
				if(!first_load) $('portfolio').appear({duration:.5, queue:{position: 'end', scope: 'gallery_queue'}});
				clearInterval(temp_int);
				}
			}, 250);
		}, 450);
	}

	function fade_others(class_name, queue_name){
		if(!queue_name){queue_name = 'content_queue';}
		var a = $$(class_name);
		var prev=false;
		for (var x = 0; x < a.length; x++){
			if (a[x].style.display != "none"){prev = true; a[x].fade({duration:.4, queue:{position: 'end', scope: queue_name}});}
		}
	}
	
	function write_thumbs(this_gallery, side, start, end){
		var i = start - (gallery.page-1)*12;
		if(end > gallery.totals[this_gallery]){end = gallery.totals[this_gallery]};
		$('thumbs_'+side+'_table').insert({bottom:Builder.node("ul", {id: 'thumbs_'+side})});
		for(var s= gallery.totals[this_gallery] - (start-1); s > gallery.totals[this_gallery] - end; s--){
			var this_script = "<script>pics["+s+"] = new Pic('pics["+s+"]', 'photos/"+this_gallery+"/"+s+".jpg', 'photos/"+this_gallery+"/thumb-"+s+".jpg', 'thumb"+s+"', 'center_img');<\/script>";
			$('thumbs_'+side).insert(Builder.node("li", {id:"thumb"+s},
				Builder.node("div", {id:"thumb_number_"+s, className:"thumb_number float_opposite_"+side},
				Builder.node("img", {src:"images/"+i+".png"}, '')
				))
			);
			$('thumbs_'+side).insert({bottom:this_script});
			i++;
		}
	}
	
	function check_page(num, op){
		if(num <= gallery.pages && op == "next"){gallery.page++;}
		if(num > 0 && op == "prev"){gallery.page--;}
		load_gallery(gallery.current);
	}
	
	Event.observe(window, 'load', function(){
		if(BrowserDetect.browser == "Explorer" && BrowserDetect.version < 7){
			var n = "<div><p style='margin-top:50px; font-size:1.5em;'>Please update your copy of Internet Explorer to version 7 or later to view this site.</p><p><a href='http://www.microsoft.com/windows/downloads/ie/getitnow.mspx'>Download from Microsoft.</a></p></div>"
			$('header').replace(n);
			$('header').show();
		}
		else{
			$('header').appear({duration:1});
			current_page = "home";
			gallery.current = "port";
			nav_pic_home = new navPic('nav_pic_home', 'images/maintitle.png', 'images/maintitle.png', 'nav_home', 'pic_home', 'pic_home', "load_content");
			nav_pic_bio = new navPic('nav_pic_bio', 'images/bioon.png', 'images/bio.png', 'nav_bio', 'pic_bio', 'pic_bio', "load_content");
			nav_pic_portfolio = new navPic('nav_pic_portfolio', 'images/galleryon.png', 'images/gallery.png', 'nav_portfolio', 'pic_portfolio', 'pic_portfolio', "load_content");
			nav_pic_resume = new navPic('nav_pic_resume', 'images/resumeon.png', 'images/resume.png', 'nav_resume', 'pic_resume', 'pic_resume', "load_content");
			nav_pic_contact = new navPic('nav_pic_contact', 'images/contacton.png', 'images/contact.png', 'nav_contact', 'pic_contact', 'pic_contact', "load_content");
			nav_pic_news = new navPic('nav_pic_news', 'images/newson.png', 'images/news.png', 'nav_news', 'pic_news', 'pic_news', "load_content");
			nav.nav_vars = [nav_pic_home, nav_pic_bio, nav_pic_resume, nav_pic_portfolio, nav_pic_contact, nav_pic_news];
			temp_int = setInterval(function(){
						if(nav_pic_contact.pic.loaded){
							$('nav').appear({duration:.75});
							clearInterval(temp_int);
						}
					}, 250);
			nav_pic_port = new subnavGalleryPic('nav_pic_port', 'images/portfolioon.png', 'images/portfolio.png', 'nav_port', 'pic_port', 'pic_port', "load_gallery");
			nav_pic_production = new subnavGalleryPic('nav_pic_production', 'images/productionon.png', 'images/production.png', 'nav_production', 'pic_production', 'pic_production', "load_gallery");
			subnav.subnav_gallery_vars = [nav_pic_port, nav_pic_production];
			load_gallery("port", true);
			nav_pic_port.swap();
			init();
		}
	});
	
	function init(){
		if(pics[gallery.totals[gallery.current]]){
			if(pics[gallery.totals[gallery.current]].pic.loaded == 1){
				$(current_page).appear({duration:.5, queue:{position: 'end', scope: 'content_queue'}});
			}
			else{setTimeout("init()", 250);}
		}
		else{setTimeout("init()", 250);}
	}

	function submit_contact(){
		if($('error_msg')){$('error_msg').remove();}
		var form = $('contact_form');
		if(form.getElements().all(function(n){return n.present();})){
			if(check_email($F(form['email']))){
				new Ajax.Request('./contact.php',{
				parameters: form.serialize(),
				onSuccess: function(transport){
					if (transport.responseText){
						contact_error("Message sent successfully.")
					}
					else{contact_error("There was an error trying to send the email.<br />You can try sending again, or send an email directly to:<br /> randyll@wendlfulphotography.com");}
				},
				onFailure: function(){
					alert("AJAX Failed!");
				}
			});
			}else{contact_error("Please make sure you have entered a valid email address.");}
		}else{contact_error("Please fill out all fields.");}
	}
	
	function contact_error(message){
	$('contact_form').insert({after:Builder.node('p', {id:'error_msg'}, message)});
	}
	
	function check_email(address) {
	return address.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\..{2,2}))$)\b/gi);
	}
	