function addToPulldown(elements) {
	elements.remove().appendTo("#pulldown").hide();
	var height = 0;
	
	elements.each(
		function() {
			if (location.hash != "#" + this.id) return;
			openPulldown($(this), "instant");
		}
	);
}

function openPulldown(child, speed, callback) {
	if (navigator.userAgent.indexOf("MSIE 7") != -1) speed ="instant";
	$('#main').css('background-attachment', 'fixed');
	
	if ($("#pulldown > :visible").length > 0) {
		closePulldown(speed,
			function() {
				child.show();
				$("#pulldown").css(
					{
						"height": "" + child.height() + "px",
						"marginTop": "-" + child.height() + "px"
					}
				);
				
				scrollTo(0, 0);
				if (speed == "instant") {
					$("#pulldown").css("marginTop", "0");
					if (callback) callback();
				} else {
					$("#pulldown").animate({
						"marginTop": "0"
					}, speed, callback);
				}
			}
		);
	} else {
		child.show();
		$("#pulldown").css(
			{
				"height": "" + child.height() + "px",
				"marginTop": "-" + child.height() + "px"
			}
		);
		
		child.find('a.close-pulldown').live('click',
			function(e) {
				e.preventDefault();
				$("a[href=#" + child.attr('id') + '-hide]').attr("href", "#" + child.attr('id'));
				closePulldown(speed);
			}
		);
		
		scrollTo(0, 0);
		if (speed == "instant") {
			$("#pulldown").css("marginTop", "0");
			if (callback) callback();
		} else {
			$("#pulldown").animate(
				{
					"marginTop": "0"
				},
				speed, callback
			);
		}
	}
}

function closePulldown(speed, callback) {
	if (navigator.userAgent.indexOf("MSIE 7") != -1) speed ="instant";
	
	if (speed == "instant") {
		$("#pulldown").css("marginTop", "-" + $("#pulldown > :visible").height() + "px");
		$("#pulldown").children().hide();
		if (callback) callback();
	} else {
		$("#pulldown").animate(
			{
				"marginTop": "-" + $("#pulldown > :visible").height() + "px"
			},
			speed, function() {
				$("#pulldown").children().hide();
				$('#main').css('background-attachment', 'scroll');
				if (callback) callback();
			}
		);
	}
}

function switchPulldownFocus(speed, child) {
	closePulldown(speed,
		function() {
			openPulldown(child);
		}
	);
}

$(document).ready(
	function() {
		if ($("#pulldown").children().length > 0) {
			openPulldown($("#pulldown > *:first-child"), "instant");
		}
	}
);