if (typeof Proworx === 'undefined') {
	var Proworx = {};
}

Proworx.Rollover = new Class({

	initialize: function()
	{
		this.images = $$('.primary');
		if (!this.images.length) {
			return;
		}

		this.image = null;
		this.images.each(function(image) {
			this._fix.delay(500, this, image);
		}.bind(this));
		$$('a.secondary').each(function(link, i) {
			link.addEvent('mouseover', function(event) {
					this._mouseover(event, i);
				}.bind(this))
				.addEvent('click', function(event) {
					this._click(event);
				}.bind(this))
				.setStyles({cursor: 'default', outline: 0});
		}, this);

		this.image = this.images[0];
	},

	_fix: function(image)
	{
		var size = image.getSize();
		if (size.y > 375) {
			var ratio = 295 / size.y;
			image.width = size.x * ratio;
			image.setStyle('left', (473 - image.width) / 2 + 'px');
			image.setStyle('top', 0);
			image.setStyle('margin', '0 auto');
		}
	},

	_mouseover: function(event, i)
	{
		this.image.id = '';
		this.image = this.images[i];
		this.image.id = 'primary';
	},

	_click: function(event)
	{
		event.stop();
	}
});

Proworx.Fader = new Class({
    initialize: function(elements, interval, duration) {
		this.elements = elements;
		if (!this.elements.length) {
			return;
		}
		this.interval = interval * 1000;
		this.duration = duration * 1000;
        this.effects = [];
		this.index = 0;
		this.elements.each(function(element, i) {
			element.get('tween').setOptions({duration: this.duration});
			if (i != 0) {
				element.setStyle('opacity', 0);
			}
		}, this);
		this.timer = this.rotate.periodical(this.interval, this);
    },
	rotate: function() {
		var index = (this.index == this.elements.length - 1) ? 0 : this.index + 1;

		this.elements[index].setStyle('z-index', 1);
		this.elements[this.index].setStyle('z-index', 0);

		this.elements[index].fade(1);
		var delay = function(element) {
			element.fade(0);
		}
		delay.delay(this.duration, this, this.elements[this.index]);

		this.index = index;
	}
});

Oakley = new Class({

	initialize: function()
	{
		if (!$('i0_minimumBedrooms')) {
			return;
		}

		this.sales = $('i0_isLet_0')
			.addEvent('click', this.toggle.bindWithEvent(this, 'sales'));
		this.lettings = $('i0_isLet_1')
			.addEvent('click', this.toggle.bindWithEvent(this, 'lettings'))
		if (this.sales.checked) {
			this.toggle(null, 'sales');
		} else {
			this.toggle(null, 'lettings');
		}
	},

	toggle: function(ev, type)
	{
		var old = type == 'sales' ? 'lettings' : 'sales';
		$$('.' + old).each(function(el) {
			el.setStyle('display', 'none');
			el.getElements('select').each(function(select) {
				select.selectedIndex = 0;
			}.bind(this));
		}.bind(this));
		$$('.' + type).each(function(el) {
			el.setStyle('display', 'block');
		}.bind(this));
	}

});
