var Premios = {

	prefijo: 'premios09',

	init: function() {
		if($('verify-key') != null) {
			$('verify-key').addEvent('click', function(){
				return Premios.verifyKey();
			});
		}

		if($('generate-button') != null) {
			$('generate-button').addEvent('click', function(){
				return Premios.generateButton();
			});
		}
	},

	verifyUrl: function() {
		url = Premios.sanitizeUrl($('url').get('value'));
		if(url != '') {
			window.location = Site.base + '/' + Premios.prefijo + '/votar/' + url;
			return true;
        } else {
			alert('No has indicado ninguna URL');
			return false;
		}
	},
	
	generateButton: function() {
		url = Premios.sanitizeUrl($('url').get('value'));
		if(url != '') {
			width = $$('input[name=tam]').filter(function(item){return item.checked})[0].get('value');
			code = $('template-html-' + width).get('value').replace('{url}', url);

			$('source_html').set('value', code);

			if($('real-button') != null) $('real-button').destroy();

			var Real = new Element('div', {
				'id'		: 'real-button',
				'class'		: 'example',
				'html'		: (code)
			}).inject($('clear-buttons'), 'before');

			return true;
        } else {
			alert('No has indicado ninguna URL');
			return false;
		}
	},

	sanitizeUrl: function(url) {
		url = url.replace(/^http:\/\/?/, '');
		url = url.replace(/\/$/, '');
		return url.trim();
    },

	copyUrl: function(id) {
		url = $('url').get('value');
		$('copy-url-' + id).getPrevious().set('value', url);
		return false;
    }
};

window.addEvent('domready', function() {
	Premios.init();
});