$(function(){
	var _ir = new ImageRotator($('#photos'), $('#loader'));
	imagesQueue = imagesQ;
	imagesQueue.onLoaded = function()
	{
		_ir.add(imagesQueue.current);
		_ir.start();
		return;
	}
	imagesQueue.onComplete = function()
	{
	}
	imagesQueue.queue_images(['../images/photos/1.jpg',
	  '../images/photos/3.jpg',
	  '../images/photos/4.jpg',
	  '../images/photos/5.jpg',
	  '../images/photos/6.jpg',
	  '../images/photos/7.jpg',
	  '../images/photos/8.jpg',
	  '../images/photos/9.jpg',
	  '../images/photos/10.jpg',
	  '../images/photos/11.jpg',
	  '../images/photos/12.jpg',
	  '../images/photos/13.jpg',
	  '../images/photos/14.jpg',
      '../images/photos/15.jpg']);
	imagesQueue.process_queue();
	
	
	$('ul.finalister li').click(function(){
		var finalist = $(this).attr('class');
		finalist = '.finalist-'+finalist+':first';
		$('.finalist').fadeIn().find(finalist).show();		
	});
	$('.finalist a.close').click(function(){ 
		$('.finalist').fadeOut(function(){
			$('.finalist > div').hide();
		}); 
		return false;
	})
	
	$('.finalist .left ul li img').click(function(){
		$(this).parent().parent().parent().parent().find('.right img').attr('src', $(this).attr('src'));
	});
	
	$('.finalist a.next').click(function(){
		$('div.fin:visible').hide().next().fadeIn();
		return false;
	});
	$('.finalist a.prev').click(function(){
		$('div.fin:visible').hide().prev().fadeIn();
		return false;
	});
	
	
	$('ul.bubblare li').click(function(){
		$('.bubbla').fadeOut();
		var perma = $(this).attr('class');
		$('#'+perma).fadeIn();
		var imgs = $('#image-'+perma+' img');
		if (imgs.length > 1) {
			var i = new ImageRotator($('#image-'+perma));
			imgs.each(function(n,o){
				i.add(o);
			});	
			i.start(3000);
		}
	});
	$('.bubbla a.close').click(function(){ 
		$('.bubbla').fadeOut(); 
		return false;
	})

	
	if (window.location.hash.match('tldr')) {
		$('.rules a.register').click();
	}
	
});


function ImageRotator(container, loader){
	var self = this;
	self.next = function() {
		self.counter = (self.counter + 1);
		self.update({position: self.counter.mod(self.items.length)});
		return false;
	}
	self.update = function(options) {
		if (typeof options == 'undefined') options = {};
		if (typeof options.duration == 'undefined') options.duration = 1200;
		if (typeof options.position != 'undefined') self.position = options.position.mod(self.items.length);        
		var item = $(self.items[self.position]);
		self.container.append(item.css({zIndex: (self.counter+10), opacity: 0}));
		item.animate({opacity: 1}, options.duration, function(){
			self.next();
		});
	}
	self.add = function(item){
		self.items.push(item);
	}
	self.start = function(duration) {
		if (typeof duration == 'undefined') duration = 700;
		
		if (self.position != -1) return false;
		self.position = 0;
		if (self.loader) self.loader.remove();
		self.container.html(self.items[self.position - 1]);
		self.update({duration: duration});
	}
	self.loader = typeof loader == 'undefined' ? false : loader;
	self.container = container;
	self.items = [];		
	self.counter = 0;
	self.position = -1;
	
	
}
