var galens = galens||{};

galens.landing = {
	delay : 5000,
	speed : 500,
	active : 0,
	titles : [],
	links : [],
	images : [],
	el : null,
	img : null,
	webkit : /webkit/i.test(navigator.userAgent),
	
	init : function(){
		var self = this;
	
		this.el = $('div.down > a:first');
		this.img = this.el.find('img');
		this.images.push(this.img.attr('src'));
		
		this.el.add($('div.down a[rel]')).each(function(i,e){
			var $this = $(this);
			self.titles.push($this.attr('title'));
			self.links.push($this.attr('href'));
			if (i > 0) {
				self.images.push($this.attr('rel'));
			}
		});
		
		this.start();
	},
	
	start : function(){
		setTimeout(function(){
			galens.landing.fade();
		}, this.delay)
	},
	
	fade : function(){
		if (!this.webkit) {
			this.el.fadeTo(this.speed, 0, function(){
				galens.landing.swap();
			});
		}
		else {
			galens.landing.swap();
		}
	},
	
	swap : function(){
		if (++this.active == this.titles.length) this.active = 0;
		this.el.attr({'href': this.links[this.active], 'title' : this.titles[this.active]})
		this.img.attr({'src' : this.images[this.active], 'alt' : this.titles[this.active]});
		if (!this.webkit) {
			this.el.fadeTo(this.speed, 1, function(){
				galens.landing.start();
			})
		}
		else {
			galens.landing.start();
		}
	}
};


$(function(){
	galens.landing.init();
});