var tooltip_options = {
    track: true,
    delay: 1000,
    showURL: false,
    showBody: " - "
};

var addthis_config = {
	ui_delay: 500,
	data_use_flash: false
}

function buildFeed(root, feed_data) {
	if(root.attr("class") == "thumbnail-feed") {
		var items = [];
	}
	else {
		var ul = document.createElement('ul');
		$(ul).attr("class", "news-item");
	}
	$(feed_data).each(function(i) {
		if(root.attr("class") == "thumbnail-feed") {
			var a = document.createElement('a');
			var img = document.createElement('img');
			$(img)
				.attr("src", feed_data[i].thumbnail)
				.attr("height", "75")
				.attr("width", "75");
			$(a)
				.attr("class", "thumbnail")
				.attr("target", "_blank")
				.attr("href", feed_data[i].permalink)
				.attr("title", feed_data[i].title + feed_data[i].description);
			$(a).append(img);
			items.push(a);
		}
		else {
			var li = document.createElement('li');
			var first_a = document.createElement('a');
			var img = document.createElement('img');
			var second_a = document.createElement('a');
			$(first_a)
				.attr("href", feed_data[i].permalink)
				.attr("class", "item-title")
				.attr("target", "_blank")
				.attr("title", feed_data[i].description)
				.html(feed_data[i].title)
				.tooltip(tooltip_options);
			$(img)
				.attr("src", "img/plus.jpg")
				.hover(
					function() { $(this).attr("src", "img/plus_over.jpg"); },
					function() { $(this).attr("src", "img/plus.jpg"); }
				);
			$(second_a)
				.attr("href", "http://www.addthis.com/bookmark.php")
				.attr("class", "addthis_button")
				.attr("addthis:url", feed_data[i].permalink)
				.attr("addthis:title", feed_data[i].title);
			second_a.appendChild(img);
			addthis.button(second_a);
			li.appendChild(first_a);
			li.appendChild(second_a);
			ul.appendChild(li);
		}
	});
	if($(root).attr("class") == "thumbnail-feed") {
		$(root).children("a").remove();
		$(items).each(function(i) {
			$(items[i]).tooltip(tooltip_options);
			$(root).append(items[i]);
		});
	}
	else {
		$(root).children("ul").eq(0).remove();
		$(root).append(ul);
	}
	return false;
}

$(document).ready(function() {
    $(".item-title, .thumbnail").tooltip(tooltip_options);
	$(".addthis_button img").hover(
		function() { $(this).attr("src", "img/plus_over.jpg"); },
		function() { $(this).attr("src", "img/plus.jpg"); }
	);
    
    $(".refresher").click(function(e) {
        var section = $(this).children("div").eq(0).attr("title").split('.')[0];
        var id = $(this).children("div").eq(0).attr("title").split('.')[1];
		$.ajax({
			cache: false,
			data: {
				section: section,
				id: id
			},
			dataType: 'json',
			beforeSend: function() {
				$(e.target).attr("class", "refreshing");
			},
			success: function(data, textStatus) {
				if($(e.target).parent().attr("id") == "thumbnail-feed-title")
					buildFeed($(e.target).parent().parent(), data);
				else
					buildFeed($(e.target).parent(), data);
			},
			complete: function() {
				$(e.target).attr("class", "refresher");
			},
			url: '/ajax.php'
		});
        return false;
    });
});