// Hey, this is still a work in progress. It's functional but ugly. -Tommy

function cleanData(data) {
	$.each(data.Items, function(i, item){
		item.Id = i;
		if (item.Attention !== undefined) {
			var sites = [];
			$.each(item.Attention.Items, function(i, url) {
				var a = document.createElement('a');
				a.href = url;
				var site = a.hostname.replace("www.","");
				site = site.substr(0, site.lastIndexOf("."));
				if (sites[site] === undefined) {
					sites[site] = 1;
				} else {
					sites[site]++;
					site += "<sup>" + sites[site] + "</sup>";
				}
				item.Attention.Items[i] = {
					"Url": url,
					"Site": site
				};
			});
		}
	});
	return data;
}

var data, lbTemplate;

$(document).ready(function(){
	var html = $("html"); 
	if ((html.hasClass("ie6")) || (html.hasClass("ie7"))) { return false; }
	// Get lightbox template
	$.get("/templates/lightbox.html", function(response) { lbTemplate = response; });
	// Establish Ids. Temp.
	$.each($(".aItem"), function(i){ $(this).data("Id", i); });
	data = $.getJSON("/data.js", function(response){
		data = cleanData(response);
	});
 	$(".aItem").click(function(event){
 		var obj = $(this);
 		var article = obj.parent();
 		var Id = obj.data("Id");
 		if (obj.data("lb") === undefined) {
	 		var item = data.Items[Id];
	 		var lightbox = $(Mustache.to_html(lbTemplate, item));
	 		var arrow = $("<div class='arrow lb' id='arrow" + Id + "'></div>")
			$("body").append(lightbox);
			obj.append(arrow);
			// Tommy, rewrite this later to use scroll offset instead of article offset.
			if (lightbox.height() + 100 > article.offset().top) {
				lightbox.css("top", article.offset().top + article.height() - 20);
				arrow.addClass("below");
			} else {
				lightbox.css("top", article.offset().top - lightbox.height() - 20);
				arrow.addClass("above");
			}
			var articleCenter = article.offset().left + article.width()/2;
			var windowCenter = $(window).width()/2;
			if (articleCenter <= windowCenter) {
				arrow.addClass("left");
			} else {
				arrow.addClass("right");
			}
			lightbox.fadeIn(500, function() { verifyView(lightbox) } );
			arrow.fadeIn();
	 		obj.data("lb", "#lightbox" + Id + ", #arrow" + Id);
	 	} else {
	 		var lightbox = $(obj.data("lb"));
	 		lightbox.fadeIn(500);
	 		verifyView(lightbox.filter(".lightbox")); // doesn't need to be in a callback since element's already in DOM
	 	}
 		$(".lightbox:visible").not("#lightbox" + Id).hide();
 		$(".arrow:visible").not("#arrow" + Id).hide();
 		return false;
	});
	$(".aClose").live("click",function(event){
		var obj = $(this);
		obj.parent().fadeOut();
		$(".arrow").fadeOut();
		return false;
	});
	function verifyView(lightbox) {
 		if (lightbox.offset().top < $(window).scrollTop()) {
 			$('html, body').stop().animate({scrollTop: lightbox.offset().top - 10}, 1000);
 		} else if ((lightbox.offset().top + lightbox.height()) > ($(window).scrollTop() + $(window).height())) {
 			$('html, body').stop().animate({scrollTop: (lightbox.offset().top + lightbox.height() - $(window).height() + 50)}, 1000);
 		}
	}
});

// analytics
var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-6672519-4']); _gaq.push(['_trackPageview']);
if (!!document.location.search.match("nostats")) { _gaq.push(['_setVar','test_value']); }
(function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
