Page = Class.create();
Page.prototype = {
	
	current: 'start',
		
	positions: null,
	
	initialize: function() {
		this.positions = new Object();
		this.positions['start']		= 70;
		this.positions['portfolio']	= 85;
		this.positions['about']		= 30;
		this.positions['clients']	= 30;
		this.positions['contact']	= 30;
		
	},
	
	section: function(name) {
		if (name=='about') {
			return about;
		}
		else if (name=='portfolio') {
			return portfolio;
		}
		else if (name=='clients') {
			return clients;
		}
		else if (name=='contact') {
			return contact;
		}
	},
	
	open: function(section) {

		if (section == this.current) return;
		
		var last = this.current;
		
		var s = this.positions[section];
		
		var wait = 0;
		Effect.Fade(last, {duration: 0.3});
		if (last!='portfolio' && last!='start') {
			new Effect.Morph(last, {
				style: {
					left:'60%'
				},
				duration: 0.4
			});
			
			
		}
		if (last=='portfolio') {
			wait = 400;
		}
		
		var obj = this;
		setTimeout(function() {
			if (s != obj.positions[obj.current]) {
				var dur = 0.5;
		
				new Effect.Morph('top',{
					style: {
						height: s+'%'
					},
					duration: dur
				});
				new Effect.Morph('page', {
					style: {
						top: s+'%',
						height: (100-s)+'%'
					},
					afterFinish: function(r) {
						Page.default_page()._open(section,last);
					},
					duration: dur
				});		
			}
			else {
				Page.default_page()._open(section,last);
			}
		},wait);
	},
	
	_open: function(section,last) {

		this.current = section;	

		var dur = 0.5;
		
		if (section=='portfolio') {
			new Ajax.Updater(section, 'parts/'+section+'.php', {
				evalScripts:true,
				onSuccess: function(r) {
					Effect.Appear(section, {duration: dur});
					setTimeout(function() {
						Page.default_page().section(section).awake();
						if (last!='start') Page.default_page().section(last).close();
					},300);
				}
			});
			return;
		}
		
		
		$(section).style.left = '-30%';		
		new Ajax.Updater(section, 'parts/'+section+'.php', {
			onSuccess: function(r) {
				Effect.Appear(section, {duration: dur});
				new Effect.Morph(section, {
					style: {
						left:'30%'
					},
					duration: dur
				});
				setTimeout(function() {
					Page.default_page().section(section).awake();
					if (last!='start') Page.default_page().section(last).close();
				},300);
			}
		});	
			
	},
	
	
	resize: function() {
		ScrollArea.update_all();
	}
	
}

Page._default_page = null;
Page.default_page = function() {
	if (!Page._default_page) {
		Page._default_page = new Page();
	}
	return Page._default_page;
}

Event.observe(window,'resize',Page.default_page().resize.bindAsEventListener(Page.default_page()));
