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

	// create our Accordion instance
	var myAccordion = new Accordion($('accordion'), 'h3.toggler', 'div.element', {
		opacity: false,
		onActive: function(toggler, element){
			toggler.addClass('active');
		},
		onBackground: function(toggler, element){
			toggler.removeClass('active');
		}
	});

	(function(){
		// postcode search stuff
		var postcode = $('search_postcode');
		if (postcode) {
			postcode.addEvent('focus', function(){
				this.value = '';
			});
		}
	})();

	(function(){
		// show first article
		$("article_1_img").setStyle('display', 'block')

		$$('div.news-articles div').addEvents({
			'mouseenter': function(){
				startGalleryRotation(true);
				show_article(this);
			},
			'mouseleave' : function () {
				startGalleryRotation(false)
			}
		});

		startGalleryRotation(false);

		function startGalleryRotation(stop){ 
			var i = 1; 

			var articles = $$('div.news-articles div');

			var timer = function(){
				if (i == articles.length) {i = 0};
				show_article(articles[i]);
				i++;
			};

			if (stop) {
				$clear(periodical)
			} else {
				periodical = timer.periodical(1700);
			};

			return periodical
		}

		function show_article(article) {
			article.addClass('active');
			$$('div.news-articles > div[id!='+$(article).getProperty('id')+']').removeClass('active');
			// show and hide the appropriate image
			$$('div.news-articles > img[id='+$(article).getProperty('id')+'_img]').setStyle('display', 'inline');
			$$('div.news-articles > img[id!='+$(article).getProperty('id')+'_img]').setStyle('display', 'none');
		}
	})();

});