var width = 420;
var curIndex = 0;
var curChildIndex = 0;

$(document).ready(
	function(){
		fixImages();
		showProg();

		$("ul.tools > li").click(
			function (){
				index = $("ul.tools > li").index(this);
				if (index != curIndex)
				{
					revertToolList();
					showProg(index);
				}
			}
		);
	}
);

function fixImages()
{
	$("div#content img").filter(
			function (index){
				return $(this).width() > width;
			}
		).css("width", width+"px").hover(
				function (){
					var css = {
						width: "",
						position: "relative",
						top: "-"+($(this).height()/4)+"px",
						left: "-"+($(this).width()/2)+"px"
					}
					$(this).css(css);
				},
				function (){
					var css = {
						width: width+"px",
						position: "static",
						top: "auto",
						left: "auto"
					}
					$(this).css(css);
				}
			);
}

function getFeed(url, id)
{
	if ($('#'+id).css('display') == 'none')
	{
		$('#'+id).css('display', 'block');
		if ($('#'+id+' li').length < 2)
		{
			$.post("/modules/rss.php",
					{
						url: url
					},
					function (data, textStatus)
					{
						if (textStatus == "success")
						{
							$('#'+id+' li').remove();
							$('#'+id).html(data);
						}
					},
					"html"
				);
		}
	} else {
		$('#'+id).css('display', 'none');
	}
}

function revertToolList()
{
	e = $("div#prog_container ul");
	e.css("display", "none");
	$("ul.tools > li").eq(curIndex).append(e);
}

function showProg(index)
{
	if (!index)
		index = 0;

	curIndex = index;
	curChildIndex = 0;
	sects = $("ul.tools > li");
	sects.css("background-image", "");
	sects.eq(curIndex).css("background", "url(/templates/TrueFusion/images/toolsListHover.png) no-repeat center right");
	items = $("ul.tools > li > ul.programs");
	$("div#prog_container").empty();
	$("div#prog_container").html(items.get(curIndex));
	$("div#prog_container ul").css("display", "block");
	$("div#prog_container ul > li").css("display", "none");
	$("div#prog_container ul > li").eq(curChildIndex).css("display", "block");
}

function prevProg()
{
	e = $("div#prog_container ul > li");
	curChildIndex = curChildIndex-1;
	if (curChildIndex < 0)
		curChildIndex = e.length-1;
	$("div#prog_container ul > li").css("display", "none");
	$("div#prog_container ul > li").eq(curChildIndex).css("display", "block");
}

function nextProg()
{
	e = $("div#prog_container ul > li");
	curChildIndex = curChildIndex+1;
	if (curChildIndex == e.length)
		curChildIndex = 0;
	$("div#prog_container ul > li").css("display", "none");
	$("div#prog_container ul > li").eq(curChildIndex).css("display", "block");
}
