﻿/// <reference path="jquery-vsdoc.js" />
/// <reference path="litium.js" />
(function($) {
	$.fn.addThis = function(options) {
		// settings created by extending the defaults with the options.
		var settings = $.extend(true, {}, $.fn.addThis.defaults, options);

		var getServiceLinks = function() {
			// returns a set of a elements and a span separator element.
			var service;
			var $toolbox = $(document.createElement('span'));

			for (service in settings.services) {
				$toolbox.append('<a class="addthis_button_' + service + '" title="' + settings.services[service] + '"></a>');
			}

			$toolbox.append('<span class="addthis_separator">|</span>');

			return $toolbox.children();
		};

		var getCompactButton = function() {
			// returns an AddThis compact button.
			var $button = $(document.createElement('a'));
			$button.text(settings.compactButton.text);
			$button.attr({
				'class': 'addthis_button_compact',
				'href': settings.compactButton.href
			});

			return $button;
		};

		// public interface.
		this.each(function() {
			if (typeof addthis === 'undefined') {
				return;
			}

			var $linkSourceElement;
			var $this = $(this);
			var $share = settings.containerSelector === undefined ? $this : $this.find(settings.containerSelector);

			$share.append(getServiceLinks());
			$share.append(getCompactButton());

			if (settings.linkSourceElementSelector !== undefined) {
				$linkSourceElement = $this.find(settings.linkSourceElementSelector);
				$share.find('a').attr({
					'addthis:url': $linkSourceElement.attr('href'),
					'addthis:title': $linkSourceElement.text()
				});
			}

			addthis.toolbox($share.get(0));
		});

		return this;
	};

	$.fn.addThis.defaults = {
		compactButton: { /* href attribute and text contents for the compact button */
			'href': '',
			'text': ''
		},
		services: { /* addthis services in toolbox */
			'facebook': 'Dela till Facebook',
			'twitter': 'Twittra om sidan',
			'favorites': 'Lägg till som favorit i din webbläsare',
			'email': 'Tipsa en vän via e-post',
			'print': 'Skriv ut sidan'
		},
		containerSelector: undefined, //'.addthis', /* container element for the toolbox and the compact button */
		username: '', /* addthis username */
		linkSourceElementSelector: undefined /* selector of an a element that points to the url the addthis instance should refer to  */
	};
})(jQuery);

(function() {
	addthis_config = {
		addthis_default_style: true,
		data_use_flash: false,
		//			ui_click: true,
		ui_language: 'sv',
		username: $.fn.addThis.defaults.username
	}; // Global variable :(, somehow it doesn't work if the object is passed as a parameter to `addthis.button()`.	
})();

