var BC = {};
BC.Location = new Class({
	
	initialize: function() {
		this.plot = $('location-plot');
		this.plots = this.plot.getElements('li');
		this.label = $('location-label');
		this.current = '';
		
		this.plots.each(this.setup, this);
	},
	
	setup: function(li) {
		li.addEvents({
			'mouseenter': this.mouseEnter.bind(this, li),
			'mouseleave': this.mouseLeave.bind(this, li)
		});
	},
	
	mouseEnter: function(li) {
		li.addClass('active');
		this.label.set('text', li.getElement('a span').get('text'));
	},
	
	mouseLeave: function(li) {
		li.removeClass('active');
		this.label.set('text', this.current);
	}
	
});

BC.Key = {
	
	send: function() {
		new Request.JSON({
			'url': '/get-key',
			'onSuccess': this.catchResult.bind(this),
			'onFailure': this.catchFailure.bind(this)
		}).post({
			'email': $('bc2-email').get('value'),
			'captcha': $('captcha').get('value')
		});
		this.setMessage('Loading...');
	},
	
	catchResult: function(o, t) {
		if(console) { console.log(t); }
		if(!$chk(o)) {
			this.catchFailure();
		} else if(o.result == 'error') {
			this.refreshCaptcha();
			this.setMessage(o.message);
		} else {
			$('bc2-giveaway').empty().adopt(new Element('p', {'text': o.message}));
		}
	},
	
	catchFailure: function() {
		this.setMessage("We're getting hammered, keep trying.");
		this.refreshCaptcha();
	},
	
	refreshCaptcha: function() {
		if($('captcha-image')) {
			$('captcha-image').set('src', '/plugins/captcha/'+Math.random());
		}
	},
	
	setMessage: function(str) {
		$('bc2-key-message').set('text', str);
	}
	
};

window.addEvent('domready', function() {
	new BC.Location();
});