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

    new ImageSwapper($$('#pHome #slider img'), [
        'images/bg-home3.jpg',
        'images/bg-home4.jpg',
        'images/bg-home5.jpg',
        'images/bg-home2.jpg']);

	new ImageSwapper($$('#pResidential.home #sliderSmall img'), ['images/bg-residential3.jpg', 'images/bg-residential2.jpg']);
	
	new ImageSwapper($$('#pResidential.mann-island #sliderSmall img'), ['images/backgrounds/res-mann/02.jpg', 'images/backgrounds/res-mann/03.jpg']);
	
	new ImageSwapper($$('#pAbout #sliderSmall img'), ['images/backgrounds/com-liverpool/01.jpg', 'images/bg-about2.jpg', 'images/bg-about.jpg']);
	
	new ImageSwapper($$('#pStorefirst #sliderSmall img'), [
	    'images/backgrounds/storefirst/02.jpg',
	    'images/backgrounds/storefirst/03.jpg',
	    'images/backgrounds/storefirst/04.jpg',
	    'images/backgrounds/storefirst/01.jpg'
	    ]);
	
	new ImageSwapper($$('#pHotpod #sliderSmall img'), ['images/backgrounds/hotpod/02.jpg', 'images/backgrounds/hotpod/03.jpg', 'images/backgrounds/hotpod/04.jpg', 'images/backgrounds/hotpod/01.jpg']);
	
	new ImageSwapper($$('#pCommercial.home #sliderSmall img'), ['images/backgrounds/com-liverpool/02.jpg','images/backgrounds/com-liverpool/03.jpg','images/backgrounds/com-liverpool/01.jpg']);
	new ImageSwapper($$('#pCommercial.liverpool #sliderSmall img'), ['images/backgrounds/com-liverpool/02.jpg','images/backgrounds/com-liverpool/03.jpg','images/backgrounds/com-liverpool/01.jpg']);
	new ImageSwapper($$('#pCommercial.blackpool #sliderSmall img'), ['images/backgrounds/com-blackpool/02.jpg','images/backgrounds/com-blackpool/03.jpg','images/backgrounds/com-liverpool/01.jpg']);
	new ImageSwapper($$('#pCommercial.burnley #sliderSmall img'), ['images/backgrounds/com-burnley/02.jpg','images/backgrounds/com-burnley/03.jpg','images/backgrounds/com-burnley/01.jpg']);
	new ImageSwapper($$('#pCommercial.blackburn #sliderSmall img'), ['images/backgrounds/com-blackburn/02.jpg','images/backgrounds/com-blackburn/03.jpg','images/backgrounds/com-blackburn/01.jpg']);
	new ImageSwapper($$('#pCommercial.padiham #sliderSmall img'), ['images/backgrounds/com-padiham/02.jpg','images/backgrounds/com-padiham/01.jpg','images/backgrounds/com-padiham/03.jpg']);
	new ImageSwapper($$('#pCommercial.centurion #sliderSmall img'), ['images/backgrounds/com-centurion/02.jpg','images/backgrounds/com-centurion/01.jpg','images/backgrounds/com-centurion/03.jpg','images/backgrounds/com-centurion/04.jpg','images/backgrounds/com-centurion/05.jpg','images/backgrounds/com-centurion/01.jpg']);

	var enquiryForm = $('EnquiryForm');
	if (enquiryForm) {
		new jpForm(enquiryForm);
	}
});

if (typeof DD_belatedPNG != 'undefined') {
   DD_belatedPNG.fix('.png');
}

var ImageSwapper = new Class({
	image2: null,
	image1: null,
	sources: [],
	delay: 5, // seconds
	initialize: function (image, sources) {
		if (typeOf(image) == 'elements' && image.length > 0) {
			image = image[0];
		}
		if (typeOf(image) != 'element' || typeOf(sources) != 'array' || sources.length == 0) {
			return;
		}

		// check that the current source is in the list, and make it the last one
		var src = image.get('src');
		sources.erase(src);
		sources.push(src);
		if (sources.length < 2) {
			return;
		}

		this.sources = sources;
		this.image1 = image;
		this.next.periodical(this.delay * 1000, this);
		
		// Preload images
		this.sources.each(function(src) {
			new Image().src = src;
		});
	},
	getNextSrc: function () {
		var src = this.image1.get('src');
		var n = 0;
		for (var i = 0; i < this.sources.length; i++) {
			if (this.sources[i] == src) {
				n = i + 1;
				break;
			}
		}
		if (n >= this.sources.length) {
			n = 0;
		}
		return this.sources[n];
	},
	next: function () {
		if (this.image2) {
			this.image1.dispose();
			this.image1 = this.image2;
			this.image2 = null;
		}
		this.image1.setStyle('z-index', 1);
		var src = this.getNextSrc();
		this.image2 = new Element('img', { src: src, styles: { zIndex: 2, opacity: 0, display: 'block' }});
		this.image1.getParent().adopt(this.image2);
		this.image2.fade('in');
	}
});

