/* NAMESPACE */

	if (typeof JS == 'undefined') {
		var JS = {};
	}
	if (typeof JS.Fix == 'undefined') {
		JS.Fix = {};
	}
	

/* SCRIPT */

	JS.External = new Class({

		initialize: function()
		{
			$$('a').each(function(link) {
				link.addEvent('click', function(event) {
					if (link.getAttribute('href').match(/^https?:\/\//)) {
						window.open(link.href, 'potton.co.uk');
						event.stop();
					}
				}.bindWithEvent(this));
			});
		},
	});

	JS.Fix.Height = new Class({

		initialize: function(source, elements)
		{
			this.source = source;
			this.elements = elements;
			$(window).addEvent('resize', this.refresh.bindWithEvent(this));
			this.refresh();
		},

		refresh: function(event)
		{
			this.elements.each(function(element) {
				element.setStyle('height', 'auto');
			}, this);
			var height = this.source.getSize().y;
			this.elements.each(function(element) {
				element.setStyle('height', height + 'px');
			}, this);
		}

	});
	JS.Fix.Height.factory = function(source, elements)
	{
		$$(source).each(function(source) {
			new JS.Fix.Height(source, source.getElements(elements));		
		});
	}

	JS.Fader = new Class({
		initialize: function(elements, interval, duration) {
			this.elements = elements;
			if (this.elements.length < 2) {
				return;
			}
			this.interval = interval * 400;
			this.duration = duration * 400;
			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.duration, 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;
		}
	});
	
	JS.Rollover = new Class({
		initialize: function()
		{
			this.image = $$('#image')[0];
			if (!this.image) {
				return;
			}
			this.images = {};
			$$('.y_thumb').each(function(link, i) {
				this.images[i] = new Element('img', {src: link.href, id: 'image'});
				link.addEvent('mouseover', this._mouseover.bindWithEvent(this, i))
					.addEvent('click', this._click.bindWithEvent(this))
					.setStyles({cursor: 'default', outline: 0});
			}, this);
		},

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

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