var cW, rssW, h1W, h1PL;

function positionels() {
	var vW = document.viewport.getWidth();
	vW = vW > 1200 ? 1200 : vW;
	
	if (vW > cW) {
		//var right = -((vW-cW-(2*rssW))/2+rssW)
		var left = (vW-cW)/2
	} else {
		var right = 0, left = 0
	}
	/*$('rss').setStyle({
		'right':right+'px'
	})
	*/
	$('h1').setStyle({
		'paddingLeft':h1PL+left+'px',
		'left':'-'+left+'px'
	})
}

document.observe("dom:loaded", function() {
	cW = $$('.container')[0].getWidth();
	//rssW = $('rss').getWidth();
	h1W = $('h1').getWidth();
	h1PL = parseInt($('h1').getStyle('paddingLeft'))
	var searchtext = $('s').readAttribute('value');
	
	positionels();
	Event.observe(document.onresize ? document : window, "resize", positionels)
	
	$$('#totals dd').each(function(el) {
		var num = parseInt(el.innerHTML).toString();
		var off, output = '<dd>';
		for (var i=0; i <= 5; i++) {
			if (num.charAt(5-i) === '') {
				output += '<span>0</span>';
			} else {
				off = i;
				break;
			}
		}
		for (var i=0; i < num.length; i++) {
			output += '<span>'+num.charAt(i)+'</span>';
		}
		el.replace(output+'</dd>');
	})
	
	//faq effects
	$$('body.faqs #content-main dd div').each(function(el) {
		el.hide()
	})
	
	$$('body.faqs #content-main dt').each(function(el) {
		el.setStyle({'cursor':'pointer'})
		Event.observe(el, "click", function(ev) {
			var dd = this.next('dd')
			var others = $$('body.faqs #content-main dd.active');
			if (others[0] != dd) {
				others.each(function(el) {
					Effect.BlindUp(el.down(),{duration:0.2})
					el.removeClassName('active')
				})
				Effect.BlindDown(dd.down(),{duration:0.2})
				dd.addClassName('active')
			} else {
				others.each(function(el) {
					Effect.BlindUp(el.down(),{duration:0.2})
					el.removeClassName('active')
				})
			}
		})
	})
	
	//contact form validation
	if ($('contact')) {
		Event.observe('contact', 'submit', function(ev) {
			var errors = 0;
			$('contact').select('.req').each(function(el) {
				el.removeClassName('revalidate')
				var value = el.getValue()
				if (value === '') {
					el.addClassName('revalidate');
					errors = 1;
				}
				if (el.readAttribute('name') == 'email') {
					if (!value.match(/([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})/)) {
						el.addClassName('revalidate');
						errors = 1;
					}
				}
			})
			if (errors) {
				alert('There appears to be errors in the form.\nPlease check the highlighted fields and try again.')
				Event.stop(ev)
			}
		});
	}
	
	Event.observe('s', 'focus', function(ev) {
		if (this.value == searchtext) {
			this.value = ''
		} else {
			this.select()
		}
	})
	
	Event.observe('s', 'blur', function(ev) {
		if (this.value == '') {
			this.value = searchtext
		}
	})

})
