/*global ZeroClipboard */
var LithiumBin = {
	_config: {
		assetBase: ''
	},

	setup: function(options) {
		this._config = options;
		if (this._config.text) {
			$('#paste').hide();
			$('#clean').show();
		} else {
			$('#clean').hide();
		}
		this.setupColor();
		this.setupCodeSizers();
		this.loadJs();
		this.wrap();
	},

	loadJs: function() {
		$.getScript(this._config.assetBase + "/js/ZeroClipboard/ZeroClipboard.js", function() {
			LithiumBin.setupCopy();
		});
	},

	setupColor: function(options) {
		$('#toggle-color').click(function() {
			$('#clean, #paste').animate({
				opacity : 'toggle'
			},'fast');
		});
	},

	setupCodeSizers: function() {
		$('#code-smaller').click(function() {
			var currentFontSize = parseFloat($('.code:first').css('font-size'), 10);
			$('.code').css('font-size', currentFontSize-1);
		});
		$('#code-bigger').click(function() {
			var currentFontSize = parseFloat($('.code:first').css('font-size'), 10);
			$('.code').css('font-size', currentFontSize+1);
		});
	},

	setupCopy: function() {
		if ($('#copy-to-clipboard')) {
			ZeroClipboard.setMoviePath(this._config.assetBase + "/js/ZeroClipboard/ZeroClipboard.swf");
			var clip = new ZeroClipboard.Client();
			clip.setText($('#clean').text());
			clip.glue($('#copy-to-clipboard')[0]);
			clip.addEventListener('complete', function(client, text) {
                $('#content').prepend('<div id="copied-notification">...copied!</div>');
                $('#copied-notification').animate({
					height: 'show',
					opacity: 'show'
				});
				setTimeout(function() {
					$('#copied-notification').animate({
						height: 'hide',
						opacity: 'hide'
					});
				}, 2000);
            });

		}
	},

	wrap: function() {
		$('#textwrap').toggle(function() {
			$('pre').addClass('wrap');
		}, function() {
			$('pre').removeClass('wrap');
		});
	}
};

