function init() {
 	clouds_init()
	if ($$('body#home').length == 0) tiles_init()
	if ($$('body#contact').length > 0) form_init()
	if ($$('body#home').length > 0) panels_init()
}

function form_init() {
	$$('form span.tip').each(function(span) {
		field = $(Element.readAttribute(span.parentNode.getElementsByTagName("LABEL")[0], 'for'))
	 	if ((field.nodeName == 'INPUT') || (field.nodeName == 'TEXTAREA')) {
	 		if (field.value == '') {
	 			field.value = span.innerHTML
	 			field.style.color = '#CCC'
	 		}
			Event.observe(field, 'focus', form_focus_field)
			Event.observe(field, 'blur', form_blur_field)
			Event.observe('contact_form', 'submit', form_submit)
	 	}
	})
}

function form_submit(e) {
	$$('form span.tip').each(function(span) {
		field = $(Element.readAttribute(span.parentNode.getElementsByTagName("LABEL")[0], 'for'))
		if ((field.nodeName == 'INPUT') || (field.nodeName == 'TEXTAREA')) {
			if (field.value == span.innerHTML) field.value = ''
		}
	})
	return true
}

function form_focus_field() {
	this.style.color = '#525252'
	span = $(this.parentNode.getElementsByTagName('span')[0])
	if (this.value == span.innerHTML) {
		this.value = ''
	}
}

function form_blur_field() {
	span = $(this.parentNode.getElementsByTagName('span')[0])
	if (this.value == '') {
		this.style.color = '#CCC'
		this.value = span.innerHTML
	}
}

function tiles_init() {
	order = [3,7,9,1,4,8,6,2,5]
	pictures = $$('#feature li img')
	pictures[2].parentNode.style.backgroundColor = '#477610'
	new Effect.Opacity(pictures[2], {duration:1.0, from:0.99, to:0.0, delay:2, queue:'fadeout' });
	new PeriodicalExecuter(function() { animate_tiles(order) }, 5);
}

function animate_tiles(order) {
	last = ($('feature').getAttribute('rel') == null) ? 0 : parseInt(feature.getAttribute('rel'))
	next = (last + 1) % order.length
	pictures = $$('#feature li img')
	colors = ['#477610', '#113375', '#DF7A07', '#296DAD']
	pictures[order[next]-1].parentNode.style.backgroundColor = colors[Math.floor(Math.random() * colors.length)]

	new Effect.Opacity(pictures[order[next]-1], {duration:1.0, from:0.99, to:0.0, queue:'fadeout' });
	new Effect.Opacity(pictures[order[last]-1], {duration:1.0, from:0.0, to:0.99, delay: 1.5, queue:'fadeout' });
	pictures[order[last]-1].src = 	pictures[order[next]-1].src
	$('feature').setAttribute('rel', next)
}

function panels_init() {
	$$('#feature li').each(function(panel) {
		Event.observe(panel, 'click', animate_panels)
		panel.style.cursor = 'pointer'
	})
}

function animate_panels(e) {
	header = $$('#' + this.id + " h3").first()
	paragraph = $$('#' + this.id + " p").first()
	if (header.hasClassName('up') == false) {
		$$("#feature li h3.up").each(function(h) {
			new Effect.MoveBy(h, 150, 0, {duration:0.5, queue: 'movedown'})
			new Effect.Opacity(h.parentNode.getElementsByTagName('p')[0], {duration:0.5, from:0.99, to:0, queue:'movedown' });
			h.removeClassName('up')
		})
		header.addClassName('up')

		new Effect.Opacity(paragraph, {duration:0.5, from:0, to:0.99, queue:'moveup' });
		new Effect.MoveBy(header, -150, 0, {duration:0.5, queue: 'moveup'})
		spam_matt(header.innerHTML.stripTags().gsub(' ', '_'))
	} 
}

function spam_matt(header) {
	// new Ajax.Request('/spammatt?' + header, {method:'get'})
	urchinTracker(('/home?' + header))
}

function go_to_thank_you_page() {
	window.location = '/thankyou'
}

function clouds_init() {
	body = $$('body').first()
	body.style.backgroundPosition = "0px 0px"
	new PeriodicalExecuter(function() { animate_clouds(body, 1) }, 0.15);
}

function animate_clouds(object, amount) {
	left = object.style.backgroundPosition.split(" ")[0].gsub("px", "")
	object.style.backgroundPosition = (left - amount) + "px" + " " + 0 + "px"
}

document.observe('dom:loaded', init)


