window.addEvent('domready', function() {

	// set vars
	
	home = 1;
	
	// show/hide (function)

	tog = function(el,state,time) {
		if (!time) { time = 2000; }
		$(el).set('morph',{
			'duration': time
		}).morph({
			'-moz-opacity': state,
			opacity: state
		});
	}
	
	// hide the content areas for now
	
	$$('.content').each(function(el) {
		el.dissolve({duration: 0});
	});
	
	// hide the login
	
	$('logon').set('dissolve',{'duration': 0}).dissolve();
	
	// when home/logo is clicked from non-home screen
	
	home_init = function() {
		if (home == 0) {
			$$('.content').each(function(el) {
				el.dissolve();
			});
			$$('#nav li a.current')[0].removeClass('current');
			$$('#nav li a')[0].set('class','current');
			home = 1;
			slides[0].removeEvents();
			iterate();
			iter = iterate.periodical(2000);
			$$('#nav','#head').each(function(el) {
				el.set('morph',{
					   'duration': 2000,
					   'unit': '%'
				}).morph({
					top: [100*(160/window.getSize().y), 50]
				});
			});
		}
	}
	
	// arrange non-home screen (function)
	
	arrange = function() {
		if (home == 1) {
			$$('#nav','#head').each(function(el) {
				el.set('morph',{
					duration: 2000,
					transition: 'expo:out'
				}).morph({
					top: [window.getSize().y/2, 160]
				});
			});
			home = 0;
		}
			$clear(iter);
			
			// make title clickable
			
			slides.each(function(el) {
				tog(el,0);
			});
			tog(slides[0],1);
			slides[0].addEvents({
				'mouseover': function() {
					this.style.backgroundImage = 'url(images/logo_hover.jpg)';
				},
				'mouseout': function() {
					this.style.backgroundImage = 'url(images/logo1.jpg)';
				},
				'click': function() {
					home_init();
				}
			});
	}
	
	// open the login box
	
	login_pop = function() {
		$('logon').set({
			'reveal': {'duration': 500}
		}).reveal();
		tog($('logon'), .8, 500);
		$('close').addEvent('click', function() {
			$('logon').dissolve();
		});
	}
	
	// destroy initial head
	
	$$('#head h1 a.first')[0].destroy();
	
	// slideshow
	
	var x = 0;
	var first = 1;
	var periodical;
	var slides = $$('#head h1 a');
	
	var iterate = function() {
		if (x > 0) {
 			tog( slides[x-1],0 );
			tog( slides[x],1 );
		}
		if (x < 1 && first < 1) {
			tog( slides[slides.length-1],0 );
			tog( slides[x],1 );
		}
		
		x++;

		if ( x == slides.length ) {
			first = 0;
			x = 0;
		}
	}
	
	iterate();
	iter = iterate.periodical(2000);
	
	// nav
	
	$$('#nav li a').each(function(el) {
		el.addEvent('click',function(ev) {
			var name = el.get('text').toLowerCase().trim();
			ev.stop();
			if (name != 'home' && name != "client login") {
				arrange();
			}
			if (name == 'home') {
				home_init();
			}
			if (name != 'client login') {
				$$('#nav li a.current')[0].removeClass('current');
				el.set('class','current');
				$$('.content').each(function(el) {
					el.dissolve();
				});
			}
			if (name != 'home' && $(name)) {
				$(name).reveal({duration: 1500});
			}
			if (name == 'client login') {
				login_pop();
				name = 'home';
			}
		});
	});
		
});
