/* Auto clearing text boxes, which clear on first focus */
var AutoClearTextBox = Class.create({
	focus: function (ev) { this.tb.value = ''; }, // on focus, clear box
	blur: function (ev) { this.tb.value = this.initialValue; }, // on blur, restore inital value
	change: function (ev) { // on change, cancel all restoring & clearing
		this.tb.stopObserving('focus',this.focusBound)
				.stopObserving('blur',this.blurBound);
	},
	initialize: function (tb) {
		this.tb = $(tb);
		if (!this.tb) return;
		// create bound events
		this.focusBound = this.focus.bind(this)
		this.blurBound = this.blur.bind(this)
		this.changeBound = this.change.bind(this)
		// store initial value to restore on blur
		this.initialValue = this.tb.value;
		// set up event hooks
		this.tb.observe('focus',this.focusBound)
				.observe('blur',this.blurBound)
				.observe('change',this.changeBound);
	}
});


var TopLogin = Class.create({
	focus: function () { this.form.addClassName('active'); 	},
	blur: function () {  this.form.removeClassName('active'); },
	
	/*passwordFocus: function () {
		this.passwordBox.writeAttribute('type','password');
		this.passwordBox.stopObserving('focus',this.passwordFocusBound);		
	},*/
	initialize: function (form) {
		this.form = form;
		
		form.select('input')
				.invoke('observe','focus',this.focus.bind(this))
				.invoke('observe','blur',this.blur.bind(this));
				
		// password field is set to a text box, so it displays 'password' initially
		// but on focus, the type reverts to 'password'
/*
		this.passwordBox = form.down('input[type=password]');
		if (this.passwordBox.value=='password') {
			this.passwordBox.writeAttribute('type','text');
			this.passwordFocusBound = this.passwordFocus.bind(this);
			this.passwordBox.observe('focus',this.passwordFocusBound);
		}
*/
	}
});


var ExtraSection = Class.create({
	
	state: 'hidden',
	content: [],
	
	setState: function (state) {
		this.state = state;
		this.head.writeAttribute('data_extraContent_state',state);
	},
	
	show: function() {
		this.getContent().invoke('show');
		this.setState('visible');
	},
	
	hide: function() {
		this.getContent().invoke('hide');
		this.setState('hidden');
	},
	
	toggle: function() {
		switch (this.state) {
			case 'hidden':
				this.show();
				break;
			case 'visible':
				this.hide();
				break;
		}
	},
	
	getContent: function () {
		if (!this.foundContent) {
			var el = this.head.next();
			while (el.hasClassName('extra')) {
				this.content.push(el);
				el = el.next();
			}
			this.foundContent = true;
		}
		return this.content;
	},
	
	initialize: function (head) {
		this.head = $(head);
		this.head.observe('click',this.toggle.bind(this));
		this.hide();
	}
});




var ProjectHeroSlideshow = Class.create({

	startTimer: function () {
		this.timer = this.nextImageBound.delay(this.holdDuration);
	},
	
	nextImage: function () {
		this.oldIndex = this.index;
		var outgoing = this.images[this.index];

		this.index++;
		if (!this.images[this.index])
			this.index = 0;
			
		if (this.index == this.oldIndex) return;
		
		var incoming = this.images[this.index];
		
		incoming.addClassName('incoming');

		new Effect.Appear(incoming,{
			duration: this.fadeDuration,
			afterFinish: this.incomingFinishedBound
		});
	},
	
	incomingFinished: function () {
		this.images[this.oldIndex].hide();
		this.images[this.index].removeClassName('incoming');
		
		this.startTimer();
	},

	initialize: function () {
		this.nextImageBound = this.nextImage.bind(this);
		this.incomingFinishedBound = this.incomingFinished.bind(this);
		
		try {
			this.li = $$('.teasers .hero .heroSet').first().up('.hero');
		} catch (e) {
			return;
		}
		
		this.images = [];
		
		this.images.push(this.li.down('img'));
		
		this.li.select('.heroSet').each(function(img){
			img.hide();
			this.images.push(img);
		},this);
		
		this.li.addClassName('ProjectHeroSlideshow_init');
		
		this.holdDuration = parseFloat(this.li.readAttribute('data_heroHold')) || 2.5;
		this.fadeDuration = parseFloat(this.li.readAttribute('data_heroFade')) || 1;
		
		this.index = 0;
		
		this.startTimer();
	}
});



document.observe('dom:loaded',function() {
	$$('input.autoClear').each(function(tb){ new AutoClearTextBox(tb); });
	new AutoClearTextBox('mod_form_2_yourEmailAddress');
	
	var topLogin = $('toplogin');
	if (topLogin && topLogin.tagName=='FORM')
		new TopLogin(topLogin);	

	$$('a[href^="http://"]').invoke('observe','click',function(ev){
		window.open(this.href);
		Event.stop(ev);
	});
	
	// extra hidden content
	$$('.extraHead').each(function(el){ new ExtraSection(el); });

	if ($$('.teasers .hero .heroSet'))
		window.projectHeroSlideshow = new ProjectHeroSlideshow();
	
});
