var FT = FT || {};
FT.env = '';

var Assanka = Assanka || {};

Assanka.$(document).ready(function() {

	// Some FT-supplied theme JS.
	if(document.getElementById("navigation")) FT.preInit.distributeNavItems(document.getElementById("navigation"));
	FT.preInit.removeNojsClassFromBody();
	FT.preInit.addOSClassToBody(navigator);

	// Add the 'on' class to the appropriate element in the blogs drop-down navigation.
	$('ul.subnav li.on ul.subnav a[href="' + document.location.pathname + '"]').parent().addClass('on');

	// Facebook counts
	var urls = [];
	Assanka.$('a.permalink').each(function() { urls.push(this.href); });
	var query = 'SELECT url, share_count, like_count, comment_count, total_count FROM link_stat WHERE url IN("'+urls.join('","')+'")';
	var scriptlink = "https://api.facebook.com/method/fql.query?query="+encodeURIComponent(query)+"&format=json&callback=?";

	if (!urls.length) return;

	Assanka.$.getJSON(scriptlink, function(data) {
		Assanka.$.each(data, function(i,item) {
			if (Assanka.$('a.facebook-counter').length == 1) {
				Assanka.$('a.facebook-counter').html(item.share_count);
			} else {
				Assanka.$('h2.entry-title a').each(function(j, el) {
					if (el.href==item.url) Assanka.$(el).closest('div.post').find('a.facebook-counter').html(item.share_count);
				});
			}
		});
	});
});

// Display a light box
Assanka.showLightBox = function(content, elAnchor, classname, title, width) {

	// Set default classname
	if (typeof(classname) == "undefined") classname = "";

	// Set lightbox HTML
	var newel = Assanka.$('<div class="assankablogs assankalightbox '+classname+'" style="width:'+width+'px"><div class="assankalightboxinner"><div class=\'assankalightboxheader\'><span style=\'float:left;\'>'+title+'</span><a style=\'float:right;\' href="javascript:void(0)" onclick="Assanka.$(this).parents(\'.assankalightbox\').remove();" class=\'assankaLightBoxHideButton\'>Close</a></div>'+content+'</div></div>').hide();

	// Add lightbox to page
	Assanka.$("body").append(newel);

	// Set position of lightbox, so that the bottom of the lightbox lines up with the
	// bottom of the anchor element and is centered horizontally on the page
	// (has to be done after the lightbox is added to the page so that the width() and height() are nonzero)
	newel.css({"position":"absolute", "top":((parseInt(elAnchor.offset().top) - newel.outerHeight()) + elAnchor.outerHeight())+"px", "left":((Assanka.$(window).width() / 2) - (newel.width() / 2))+"px"});

	// Show lightbox
	Assanka.$(newel).show();
};


(function() {
 var host = "mbanewslines.ft.com";
 var useSsl = location.href.match(/^https\:/);
 var script = document.createElement('script');
 script.type = 'text/javascript';
 script.src = 'http'+(useSsl?'s':'')+'://'+host+'/resources/javascript/bootstrap';
 document.getElementsByTagName("head")[0].appendChild(script);
}());

