var AdObj = {
	"leaderboard" : {
		0 : "8932054679",
		1 : "8932054679",
		2 : "8932054679",
		3 : "1670996447"
	},
	"square" : {
		0 : "0511923618",
		1 : "0511923618",
		2 : "0511923618",
		3 : "7096844645"
	}
}

$(function() {
	$.global = {
		slideshowAnimating : false,
		footerLinksShown : false,
		sponsorTotalWidth : 0,
		allowFormSubmit : true,
		defaultFormAction : null
	};
	
	$("[rel*=external]").each(function() {
		$(this).attr("target", "_blank");
	});
	
	$(".tabbed_content .tabs li").click(function() {
		$(".tabbed_content .tabs .active").removeClass("active");
		$(this).addClass("active");
		var num = $(this).index();
		$(".tabbed_content .content.active").fadeOut("fast", function() {
			$(this).removeClass("active");
			$(".tabbed_content .content").eq(num).fadeIn("fast", function() {
				$(this).addClass("active");
			});
		});
		return false;
	});
	
	$("#call_login_dropdown a:contains('Login')").click(function() {
		$(this).attr("href", "#");
		$(".loginbar .login_dropdown").slideToggle();
		return false;
	});
	$(".loginbar .login_dropdown").hide();
	
	
	$(".titles a").click(function() {
		if(!$.global.slideshowAnimating) {
			$.global.slideshowAnimating = true;
			var beginIndex = 0;
			var endIndex = 0;
			var direction = 1;
			$(".titles a").each(function() {
				if ($(this).hasClass("active")) {
					beginIndex = $(this).removeClass("active").index();
				}
			});
			endIndex = $(this).addClass("active").index();
			if (endIndex < beginIndex) direction = -1;
			var toShow = $(".slideshow .picture_container .picture").eq(endIndex);
			if (!toShow.hasClass("active")) {
				$(".slideshow .picture_container .picture.active").stop().animate({
					"top" : (-305 * direction)
				}, 400, function() {
					$(this).removeClass("active");
				});
				toShow.css("top", (305 * direction)).stop().animate({
					"top" : 0			
				}, 400, function() {
					$(this).addClass("active");
					$.global.slideshowAnimating = false;
				});
			}
			else {
				$.global.slideshowAnimating = false;
			}
		}
		return false;
	});
	
	$(".frontpage_slider .links a").live("click", function() {
		var parent = $(this).parents(".frontpage_slider");
		var slide_i = $(this).index();
		parent.find(".slide.active").stop(true, true).fadeOut(function() {
			$(this).removeClass("active");
			parent.find(".slide").eq(slide_i).fadeIn(function() {
				$(this).addClass("active");
			});
		});
		return false;
	});
	
	$("#moreResourcesLink").live("click", function() {
		$("#moreResources").slideToggle();
		return false;
	});
	
	$(".rss_feed.obscured_links li a.link").attr("href", $(this).find(".obscured_link").text());
	/*
	$(".tertiary_nav").data("isOpen", false);
	$(".tertiary_nav ul").each(function() {
		var link = $("#" + $(this).attr("id").replace("sublinks", "link"));
		$(this).css("left", link.position().left);
	});
	$(".secondary_nav a").hover(function() {
		var calledID = $(this).attr("id");
		var calledOffsetLeft = $("#" + calledID).position().left;
		var tNav = $(".tertiary_nav");
		if (!tNav.data("isOpen")) {
			tNav.stop().slideDown("fast", function() {
				tNav.data("isOpen", true);
			});
		}
		var toShow = $("#" + calledID.replace("link", "sublinks"));
		//tNav.find("ul").css("opacity", .3);
		tNav.find("ul").hide();
		//toShow.css("opacity", 1);
		toShow.css({
			"position" : "absolute",
			"top" : 0,
			"left" : calledOffsetLeft
		}).show();
	});
	$(".breadcrumb").mouseleave(function() {
		$(".tertiary_nav").data("isOpen", false).stop().slideUp("fast");
	});
	*/
});

$(window).load(function() {
	fixNavPadding();
});


function startSponsorSlider() {
	var leftFilled = 0;
	var sponsorLogos = $(".sponsor_slider a");

	for(var i = 0; i < sponsorLogos.length; i++) {
		$.global.sponsorTotalWidth = leftFilled;
		//initialize spacing
		var el = $(sponsorLogos[i]);
		var w = el.width();
		el.css("left", leftFilled);
		leftFilled += 20; //right padding
		leftFilled += w; //width

		console.log("initializing slider " + i);
		animateSlider(el, w);
	}
}

function animateSlider(el, w) {
	console.log("animating slider " + el);
	var cur = parseInt(el.css("left"));
	el.animate({
		"left" : cur - w
	}, 4000, function() {
		if (cur - w < -120) {
			el.css("left", ($.global.sponsorTotalWidth - 120));
		}
		animateSlider(el, w);
	});
}

function fixNavPadding() {
	var w = $(".navigation ul").width();
	var padLeft = parseInt($(".navigation li.root").css("paddingLeft"));
	var padRight = parseInt($(".navigation li.root").css("paddingRight"));
	
	var currentWidth = 0;
	var currentPadding = 0;
	var numElements = 0;
	$(".navigation li.root").each(function() {
		currentWidth += ($(this).outerWidth());
		currentPadding += (padLeft + padRight);
		numElements++;
	});
	
	var padToAdd = Math.floor((w - currentWidth) / (2 * numElements));
	
	$(".navigation li.root").each(function() {
		$(this).css("paddingLeft", padLeft + padToAdd)
			.css("paddingRight", padRight + padToAdd);
	});
}

function getAdSlot(type) {
	return AdObj[type][portalID];
}
