1/*
2 * jquery.socialshareprivacy.js
3 *
4 * Copyright (c) 2012 Mathias Panzenböck
5 *
6 * is released under the MIT License http://www.opensource.org/licenses/mit-license.php
7 *
8 */
9(function ($, undefined) {
10	"use strict";
11
12	$.fn.socialSharePrivacy.settings.services.hackernews = {
13		'status'            : true,
14		'dummy_line_img'    : 'images/dummy_hackernews.png',
15		'dummy_box_img'     : 'images/dummy_box_hackernews.png',
16		'dummy_alt'         : '"Hacker News"-Dummy',
17		'txt_info'          : 'Two clicks for more privacy: The Hacker News button will be enabled once you click here. Activating the button already sends data to Hacker News &ndash; see <em>i</em>.',
18		'txt_off'           : 'not connected to Hacker News',
19		'txt_on'            : 'connected to Hacker News',
20		'perma_option'      : true,
21		'display_name'      : 'Hacker News',
22		'txt_n_points'      : '{points} points',
23		'txt_one_point'     : '1 point',
24		'referrer_track'    : '',
25		'title'             : $.fn.socialSharePrivacy.getTitle,
26		'button'            : function (options, uri, settings) {
27			var url = uri + options.referrer_track;
28			var title = typeof(options.title) === 'function' ?
29				options.title.call(this, options, uri, settings) :
30				String(options.title);
31			var prot = ('https:' === document.location.protocol ? 'https:' : 'http:');
32
33			var $code;
34			if (settings.layout === 'line') {
35				$code = $('<div class="hackernews-widget">'+
36					'<a class="name" target="_blank">Y</a>'+
37					'<span class="points"><i></i><u></u><a target="_blank">submit</a></span></div>');
38			}
39			else {
40				$code = $('<div class="hackernews-widget">'+
41					'<div class="points"><i></i><u></u><a target="_blank">submit</a></div>'+
42					'<a class="name" target="_blank">Y</a></div>');
43			}
44
45			$code.find("a").attr("href", prot+"//news.ycombinator.com/submitlink?"+$.param({
46				"u": url,
47				"t": title
48			}));
49
50			$.ajax(prot+"//api.thriftdb.com/api.hnsearch.com/items/_search?filter[fields][url][]="+encodeURIComponent(url), {
51				dataType: "jsonp",
52				success: function (data) {
53					var item = data.results[0];
54					if (item) {
55						item = item.item;
56						var points = $.fn.socialSharePrivacy.formatNumber(item.points);
57						$code.find("a").attr("href", prot+"//news.ycombinator.com/item?id="+item.id);
58						$code.find(".points a").text(points).attr('title',
59							item.points === 1 ?
60							options.txt_one_point :
61							options.txt_n_points.replace(/{points}/g, points));
62					}
63				}
64			});
65
66			return $code;
67		}
68	};
69})(jQuery);
70