
$(document).ready(function(){
buildFeeds();
//runtimer();
});

function runtimer() {
	window.setInterval('buildFeeds()', 10000);
}
function limitFeeds(masterSelector, sourceSelector) {
$(masterSelector).each(function() {
	if (sourceSelector) {
		$(this).find(".all").hide();
		$(this).find(sourceSelector).show();
	} else {
		$(this).find(".all").show();
	}
	var totaldisplay = $(this).attr("totaldisplay");
	if (!totaldisplay) {
		totaldisplay = "10";
	}
	doDisplay(this,totaldisplay);
	$(this).attr("limited","true");
	$(this).attr("sourceselector",sourceSelector);
	$(this).attr("masterselector",masterSelector);
});

return false;
}
function buildFeeds() {
$(".feed").each(function() {
 var item = this;
 var feedid = $(this).attr("feedid");
 var template = $(this).attr("template");
 var totalitems = $(this).attr("totalitems");
 var totalitemsperfeed = $(this).attr("totalperfeed");
 var totaldisplay = $(this).attr("totaldisplay");
 var rotator = $(this).attr("rotator");
 var rotatornav = $(this).attr("rotatornav");
 
 if (!totalitems) {
	totalitems = "10";
 }
 if (!totalitemsperfeed) {
	totalitemsperfeed = "10";
 }
 if (!totaldisplay) {
	totaldisplay = totalitems;
 }
 if (!rotatornav) {
	rotatornav = rotator + " .navigation";
 }
 var url = "/api/proxy.php/utility/feed.json/" + feedid + "?utilityviewmodel.totalitemsperfeed=" + totalitemsperfeed + "&utilityviewmodel.totalitems=" + totalitems;
 $.getJSON(url
	,function(result) {
		$(item).empty();
		if (result.FeedItems) {
			$("#" + template).render(result.FeedItems).appendTo(item);
			wrapThickbox(item);
			doDisplay(item,totaldisplay);
			if ($(item).attr("limited") == "true") {
				limitFeeds($(item).attr("masterselector"), $(item).attr("sourceselector"));
			}
			if (rotator) {
			doRotator(rotator, rotatornav);
			}
			$(item).find(".clickable").click(function() {
				var dest = $(this).attr("clickdestination");
				if (dest) {
					window.location = dest;
					return false;
				};
			});
		}
	}
	, function(result, error) {	alert("error"); }
	);
	});
}

function doDisplay(item, totaldisplay) {
var td = totaldisplay - 1;
$(item).find(".limitable:visible:gt(" + td + ")").each(function() {
	$(this).hide();
});
}
function wrapThickbox(element) {
        $(element).find("a.thickbox").click(function() {
            var url = $(this).attr("href");
            tb_show("", url, "");
			return false;
        });
}

