$(document).bind("ready", function(){
	$("#ShowChangeLog").mousedown(function(){
		$("#ChangeLogHidden").slideDown("fast");
		$("#ChangeLogHidden").fadeIn("fast");
	});
	$("#ShowOtherDownloads").mousedown(function(){
		$("#OtherDownloadsHidden").slideDown("fast");
		$("#OtherDownloadsHidden").fadeIn("fast");
	});
	$("#ShowSuggestionForm").mousedown(function(){
		$("#SuggestionFormHidden").slideDown("fast");
		$("#SuggestionFormHidden").fadeIn("fast");
		$("#ShowSuggestionForm").slideUp("fast");
		$("#ShowSuggestionForm").fadeOut("fast");
	});
	$("#HideSuggestionForm").mousedown(function(){
		$("#SuggestionFormHidden").slideUp("fast");
		$("#SuggestionFormHidden").fadeOut("fast");
		$("#ShowSuggestionForm").slideDown("fast");
		$("#ShowSuggestionForm").fadeIn("fast");
	});
	$(".LabelledField").focus(function(){
		if(this.value==$(this).attr("label")){
			this.value = "";
			this.style.color = null;
		}
	});
	$(".LabelledField").blur(function(){
		if(this.value==""){
			this.value = $(this).attr("label");
			this.style.color = "#AAAAAA";
		}
	});
	$(".LabelledField").each(function(){
		if(this.value==""){
			this.value = $(this).attr("label");
			this.style.color = "#AAAAAA";
		}
	});
	$(".SearchField").focus(function(){
		if(this.value=="Search"){
			this.value = "";
			this.style.color = null;
		}
	});
	$(".SearchField").blur(function(){
		if(this.value==""){
			this.value = "Search";
			this.style.color = "#AAAAAA";
		}
	});
	$("input[type='text']").live("focus", function(){
		cThis = $(this);
		if(cThis.attr("title")!=null){
			if(this.value==cThis.attr("title")){
				this.value = "";
				this.style.color = null;
			}
		}
	});
	$("input[type='text']").live("blur", function(){
		cThis = $(this);
		if(cThis.attr("title")!=null){
			if(this.value==""){
				this.value = cThis.attr("title");
				this.style.color = "#AAAAAA";
			}
		}
	});
	$("input[type='text']").each(function(){
		cThis = $(this);
		if(cThis.attr("title")!=null){
			if(this.value==""){
				this.value = cThis.attr("title");
				this.style.color = "#AAAAAA";
			}
		}
	});
	$(".TabContainer").each(function(){
		ParseTabs($(this));
	});
	$("form.SaveDownloadDo").submit(function(){
		$("#SaveDownloadButton").attr("disabled", "disabled");
		Link = $(this).attr("action").replace(/\.html/, ".json");
		$.post(Link, $(this).serialize(), function(data){
			PopupUtility(data.HTML);
			//$("#SaveDownloadButton").removeAttr("disabled", "disabled");
			//$("#SaveDownloadButton").val("Queued");
			$("#SaveDownloadButton").removeAttr("disabled");
		}, "json");
		return false;
	})
	if($(".TopAdContainer").height()==0){
		$(".TopAdContainer").replaceWith("<div class=\"TopContainer\">Please don't block the ads :(</div>");
	} else {
		//$(".TopAdContainer").replaceWith("<a>:(</a>");
	}
});
function PopupUtility(HTML){
	UtilityBg = $("<div class=\"UtilityBg\"></div>").prependTo(document.body);
	UtilityBg.click(function(){
		//$(this).remove();
	});
	UtilityPopup = $("<div class=\"UtilityPopup\"></div>").prependTo(UtilityBg);
	UtilityPopupData = $("<div class=\"UtilityPopupData\"></div>").prependTo(UtilityPopup);
	UtilityPopupControls = $("<div class=\"UtilityPopupControls\"></div>").appendTo(UtilityPopup);
	UtilityCloseButton = $("<input type=\"button\" value=\"Close\"/>").appendTo(UtilityPopupControls);
	UtilityCloseButton.click(function(){
		$(this).parents(".UtilityBg").remove();
	});
	UtilityPopupData.html(HTML);
	UtilityPopupData.children(".TabContainer").each(function(){
		ParseTabs($(this));
	});
}
function ajaxTooltip(element, location){
	$(".ToolTip").remove();
	Tooltip = $("<div class=\"ToolTip\">Loading</div>");
		Tooltip.prependTo(document.body);
	Tooltip.css("position", "absolute");
	Tooltip.css("left", element.offset().left - (Tooltip.width()/2) + (element.width()/2));
	Tooltip.css("top", element.offset().top + element.height());
	Tooltip.css("opacity", "0");
	Tooltip.animate({"opacity": 1, "top":element.offset().top + element.height()+20}, 200);
	$.get(location, function(data){
		Tooltip.html(data);
	}, "xhtml");
}
function ParseTabs(Container){
	ContainerParent = Container.parent();
	Tabs = ContainerParent.children(".TabbingTab");
	Tabs.each(function(){
		NewElement = $("<a href=\"#\"></a>").prependTo(Container);
		NewElement.attr("id", $(this).attr("id"));
		NewElement.attr("class", $(this).attr("class"));
		NewElement.text($(this).text());
		NewElement.click(function(){
			ContentID = $(this).attr("id").replace(/Tab_([A-Za-z]+)/g, "TabC_$1");
			ContentContainer = $("#"+ContentID);
			ContentContainer.parent().children(".TabbingContent").hide();
			ContentContainer.show();
		});
		NECID = NewElement.attr("id").replace(/Tab_([A-Za-z]+)/g, "TabC_$1");
		$("#"+NECID).hide();
		$(this).remove();
	});
}

