window.addEvent('domready', function(){
	if (domain == 'uk') var myload = new displayImages({
		domain: 'uk',"base":base,
		images: [{"img":"ram.jpg","lnk":base+"uk/sheep/ewes_for_sale"}]
	});
	else var myload = new displayImages({
		domain: 'nz',"base":base,
		images: [
			{"img":"video1.png","lnk":"javascript:displayPromoVideo()"},
			{"img":"sale.png","lnk":base+"nz/events"}
		]
	});
	$$('#c a').each(function(l){
		var u = l.href; // gets full URL
		if (u.indexOf(location.host) == -1){
			l.set('target', '_blank');
			l.addEvent('click', function(){
				_gaq.push(['_trackEvent', 'Click', 'Outgoing Links', u]);
			});
		} else if (u.match(/uploads\/files\//gi)){
			l.addEvent('click', function(){
				var regex = new RegExp('^http:\/\/'+location.host+'/');
				_gaq.push(['_trackEvent', 'Click', 'Downloads', u.replace(regex,'')]);
			});
		}
	});
});


var displayImages = new Class({
	Implements: [Options, Events],
	options: {domain:'nz', base:"/", images:[], delay:6000},
	initialize: function(options){
		this.setOptions(options);
		this.elm = $('ris_banner');
		this.options.count = 0;
		this.options.counter = 0;
		this.preloader();
	},
	preloader: function(){
		for(this.options.counter=0;this.options.counter<this.options.images.length;this.options.counter++){
			var cnt = this.options.counter;
			this.options.images[cnt].obj = '<a href="'+this.options.images[cnt].lnk+'"><img src="'+this.options.base+'images/ban/'+this.options.domain+'/'+this.options.images[cnt].img+'" alt="" style="border: none" /></a>';
		}
		this.displayImage();
		this.startTimer();
	},
	startTimer: function(){
		this.periodicalTimer = this.displayImage.periodical(this.options.delay, this);
	},
	displayImage: function(options){
		$('ris_banner').empty();
		var inc = this.options.count;
		$('ris_banner').set('html', this.options.images[inc].obj);
		this.options.count++;
		if (this.options.count ==this.options.images.length) this.options.count = 0;
	}
});

function displayPromoVideo(){
	_gaq.push(['_trackEvent', 'Click', 'Promo Video']);
	var mySwiff = new Swiff('http://questvideo.org/player-licensed.swf', {
		width: 640,
		height: 380,
		params: {
			wmode: 'opaque',
			flashvars: 'file=http://questvideo.org/rissington.flv&image=http://questvideo.org/rissington.jpg&stretching=exactfit',
			allowscriptaccess: 'always',
			bgcolour: 'undefined',
			allowfullscreen: 'true'
		}
	});
	comModal = new fbPopup({
		'title':'', 
		data:mySwiff, 
		offsetTop: -100, 
		closeButton:true, 
		overlayEscapes:true,
		width: 670,
		closeButtonText: 'Close'
	});
}

/* MooTools Facebook modal close */
var fbPopup = new Class({
	Implements: [Options, Events],
	options: {
		title: 'no title',
		width: 450,
		height: false,
		overlay: true,
		overlayEscapes: true,
		overlayOpacity: 0.6,
		closeButton: true,
		closeButtonText: 'Close',
		allowEscape: true,
		allowFade: true,
		offsetTop: 0,
		data: new Element('div')
	},
	initialize: function(options){
		this.setOptions(options);
		this.escapeModal = function(e) { if(e.key == 'esc') { this.destruct(); } }.bind(this);
		this.closeModal = function() { this.destruct();}.bind(this);
		this.drawPopup();
	},
	drawPopup: function(){
		if(this.options.overlay) this.createOverlay();
		this.popup();
	},
	createOverlay: function(){
		this.overlay = new Element('div').setProperty('class', 'fboverlay').injectInside(document.body);
		this.overlay.setStyles({opacity: this.options.overlayOpacity});
	},
	getScrollbarWidth: function(){
		if (!Browser.Engine.webkit) return 0;
		document.body.style.overflow = 'hidden';
		var width = document.body.clientWidth;
		document.body.style.overflow = 'scroll';
		width -= document.body.clientWidth;
		if(!width) width = document.body.offsetWidth-document.body.clientWidth;
		document.body.style.overflow = '';
		return width;
	},
	positionEls: function(){
		if(this.overlay) {
			this.overlay.setStyles({top: Window.getScrollTop()+'px', height: Window.getHeight()+'px', width: (Window.getWidth() + Window.getScrollLeft()-this.getScrollbarWidth())+'px'});
		}
		var fbpopupHeight = this.infobox.getHeight() / 2;
		var totop = (window.getSize().y / 2)  + this.options.offsetTop + window.getScrollTop() - fbpopupHeight;
		if (totop < 0) totop = 0;
		var toleft = (window.getSize().x / 2) - (this.options.width /2);
		if (toleft < 0) toleft = 0;
		this.infobox.setStyles({top: totop+'px', left: toleft + 'px'});
	},
	popup: function (){
		this.infobox = new Element('table').setProperty('class', 'fbtable');
		if(this.options.allowFade) {
			this.infobox.setStyles({'opacity': 0, 'visibility': 'hidden'});
		}
		this.infobox.injectInside(document.body);
		this.infobox.setStyle('width', this.options.width+'px');
		var tbody = new Element('tbody').injectInside(this.infobox);
		var trow = new Element('tr').injectInside(tbody);
		var tl = new Element('td').setProperty('class', 'fbtl').injectInside(trow);
		var tm = new Element('td').setProperty('class', 'fbtm').injectInside(trow);
		var tr = new Element('td').setProperty('class', 'fbtr').injectInside(trow);
		var trow = new Element('tr').injectInside(tbody);
		var ml = new Element('td').setProperty('class', 'fbm').injectInside(trow);
		this.m = new Element('td').setProperty('class', 'fbcontent').injectInside(trow);
		var mr = new Element('td').setProperty('class', 'fbm').injectInside(trow);
		var trow = new Element('tr').injectInside(tbody);
		var bl = new Element('td').setProperty('class', 'fbbl').injectInside(trow);
		var bm = new Element('td').setProperty('class', 'fbbm').injectInside(trow);
		var br = new Element('td').setProperty('class', 'fbbr').injectInside(trow);
		if (this.options.title) var title = new Element('h2', {'html': this.options.title, 'class': 'fbtitle'}).inject(this.m);
		this.content = new Element('div').injectInside(this.m);
		if (this.options.height) this.content.setStyles({
			'height': this.options.height+'px',
			'overflow-y': 'auto'
		});
		/* SET THE CONTENT */
		var dataType = typeof (this.options.data);
		if (dataType == 'object'){
			this.options.data.inject(this.content);
		} else { this.content.set('html', this.options.data);}
		/* Add close button */
		if (this.options.closeButton) {
			var closeDiv = new Element('div', {'class':'statusbar'}).inject(this.m);
			this.close = new Element('input', {'type':'button','class':'button', 'value':this.options.closeButtonText, events: {
				'click': this.destruct.bind(this)
			}}).inject(closeDiv);
		}
		// Center items
		this.positionEls();
		if(this.options.allowFade)  {
			var myFx = new Fx.Tween(this.infobox);
			myFx.start('opacity', '0', 1);
		}
		if(this.options.allowEscape)  document.addEvent('keydown',this.escapeModal);
		this.centerItems = function(){	this.positionEls();}.bind(this)
		window.addEvents({ 
			'resize': this.centerItems,
			'scroll': this.centerItems
		});
		if (this.options.overlay && this.options.overlayEscapes) this.overlay.addEvent('click', this.closeModal);
	},
	destruct: function(){
		if(this.options.allowEscape) document.removeEvent('keydown', this.escapeModal);
		window.removeEvent('resize', this.centerItems);
		window.removeEvent('scroll', this.centerItems);
		if (this.overlay) this.overlay.destroy();
		if(this.options.allowFade) {
			new Fx.Tween(this.infobox).start('opacity',0).chain(
				function(){ this.infobox.destroy(); }.bind(this)
			);
		} else this.infobox.destroy();
	}
});