$(document).ready(initialize);

function initialize()
{
	window.onerror = handleError;

	addStyles();
	
	fixRot13Transformations(); // see rot13.js

	hideFooterWithoutScrollbars();
}

function handleError(msg, url, line)
{
	alert(msg);
	return true; 
}

/* Add Styles */
function addStyles()
{
	$(":text, :password, :file, textarea, select").addClass("input");
}

function handleErrorMessage(form)
{
	if (!form.isValid && form.attr("errormessage") != null)
	{
		alert(form.attr("errormessage"));
	}
}

function detectRssFeed(html)
{
	$("head>link#rssLink").remove();
	
	var rssFeeds = $(html).find("a.syndication");
	
	if (rssFeeds.length > 0)
	{
		$("head").append('<link id="rssLink" href="' + rssFeeds[0].href + '" title="' + rssFeeds[0].title + '" rel="alternate" type="application/rss+xml" />');
	}
}

/* Synchronize checkboxes */
function initializeAllCheckbox(divid)
{
	$("#" + divid + " :checkbox").click(synchronizeCheckboxes);
}

function synchronizeCheckboxes() 
{
	var checkbox = $(this);
	var allcheckbox = $(this).parents("div.options").find(":checkbox[value=All]")[0];
	var othercheckboxes = $(this).parents("div.options").find(":checkbox[value!=All]");
	
	if (this.value == "All")
	{
		othercheckboxes.each(function() 
		{
			this.checked = allcheckbox.checked;
		});
	}
	else
	{
		var allchecked = true;
		
		othercheckboxes.each(function()
		{
			if (!this.checked)
			{
				allchecked = false;
			}
		});
		
		allcheckbox.checked = allchecked;
	}
}

/* Initialize Options */

function initializeOptions()
{
	if ($(".showoptions").length > 0 && $(".hideoptions").length > 0)
	{
		$(".showoptions").show();
		$(".hideoptions").hide();
		$(".options").hide();
		
		$(".options.defaultopen").each(
			function() {
				$(this).show();
				$(this).prevAll(".showoptions:first").hide();
				$(this).prevAll(".hideoptions:first").show();
			}
		);
		
		$(".showoptions").click(function()
		{
			$(this).nextAll(".options:first").show();
			$(this).nextAll(".hideoptions:first").show();
			$(this).hide();
		});
		
		$(".hideoptions").click(function()
		{
			$(this).nextAll(".options:first").hide();
			$(this).prevAll(".showoptions:first").show();
			$(this).hide();
		});
	}
}

/* Hide footer without scrollbars */

function hideFooterWithoutScrollbars()
{
	doHideFooterWithoutScrollbars();
	
	$(window).resize(doHideFooterWithoutScrollbars);
}

function doHideFooterWithoutScrollbars()
{
	if ($(window).height() == $(document).height())
	{
		$("div#footer").hide();
	}
	else
	{
		$("div#footer").show();
	}
}

/* Various Methods */

function isDefined(tester)
{
	return !(typeof(tester) == "undefined");
}

function isChecked(m)
{
	return $(m).attr("checked");
}
