var template_url = "/wp-content/themes/academy2011";
var social_dropdown = "closed";

$(document).ready(function(){
	
	// social media header tabs
	function open_social_tabs(section) {
		$("#tab_wrapper").animate({
			top:0
			}, 300, 
			function(){
				$("#tab_content_"+section).fadeIn('fast');
			}
		);
	}
	function close_social_tabs() {
		$("a.tab_button").removeClass("active");
		social_dropdown = "closed";
		$(".tab_content_section").fadeOut('fast',function(){
			$("#tab_wrapper").animate({
				top:-400
				}, 300
			);
		});
	}
	$("#tab_close_button").click(function(){
		close_social_tabs();
		return false;
	});
	$("a.tab_button").click(function(){
		var tab_id = $(this).attr("id").replace(/tab_button_/,'');
		if (tab_id == 'facebook') {
			if (social_dropdown == 'closed') {
				// open tab
				social_dropdown = 'facebook';
				$(this).addClass('active');
				open_social_tabs(social_dropdown);
			} else {
				if (social_dropdown == 'facebook') {
					// close tab
					close_social_tabs();
				} else {
					// close others & open facebook
					social_dropdown = 'facebook';
					$("a.tab_button").removeClass("active");
					$(this).addClass('active');
					$(".tab_content_section").fadeOut('fast');
					$("#tab_content_facebook").fadeIn('fast');
				}
			}
		} else if (tab_id == 'rss') {
			if (social_dropdown == 'closed') {
				// open tab
				social_dropdown = 'rss';
				$(this).addClass('active');
				open_social_tabs(social_dropdown);
			} else {
				if (social_dropdown == 'rss') {
					// close tab
					close_social_tabs();
				} else {
					// close others & open rss
					social_dropdown = 'rss';
					$("a.tab_button").removeClass("active");
					$(this).addClass('active');
					$(".tab_content_section").fadeOut('fast');
					$("#tab_content_rss").fadeIn('fast');
				}
			}
		} else if (tab_id == 'twitter') {
			if (social_dropdown == 'closed') {
				// open tab
				social_dropdown = 'twitter';
				$(this).addClass('active');
				open_social_tabs(social_dropdown);
			} else {
				if (social_dropdown == 'twitter') {
					// close tab
					close_social_tabs();
				} else {
					// close others & open twitter
					social_dropdown = 'twitter';
					$("a.tab_button").removeClass("active");
					$(this).addClass('active');
					$(".tab_content_section").fadeOut('fast');
					$("#tab_content_twitter").fadeIn('fast');
				}
			}
		} else if (tab_id == 'email') {
			if (social_dropdown == 'closed') {
				// open tab
				social_dropdown = 'email';
				$(this).addClass('active');
				open_social_tabs(social_dropdown);
			} else {
				if (social_dropdown == 'email') {
					// close tab
					close_social_tabs();
				} else {
					// close others & open email
					social_dropdown = 'email';
					$("a.tab_button").removeClass("active");
					$(this).addClass('active');
					$(".tab_content_section").fadeOut('fast');
					$("#tab_content_email").fadeIn('fast');
				}
			}
		}
		return false;
	});

	// Add target=_blank
	$('a[rel^="blank"]').each(function(){
		$(this).attr("target","_blank");
	});

	// Nav menu mods 
	$(".menu-item-type-custom").each(function(){
		$(this).attr("target","_blank");
	});
	$(".menu-item li").each(function(){
		$(this).has("ul").addClass('menu-has-submenu');
	});

	$("a[rel^='prettyPhoto']").prettyPhoto({
		theme: 'light_square' /*, // light_rounded / dark_rounded / light_square / dark_square / facebook 
		overlay_gallery: false ,
		showTitle: false,
		opacity: 0.5,
		animationSpeed: 'fast',
		default_width: 700*/
	});

	$("span.mailme").each(function(){
		   var spt = $(this);
		   var addr = $(spt).text().replace(" (at) ","@").replace(" dot ",".");
		   $(spt).after('<a href="mailto:'+addr+'">'+ addr +'</a>')
		   $(spt).remove();
	});

	// position big scrolling images on homepage and page dropshadow
	function reposition_scroller() {
		var content_width = 942;
		var window_width = $(window).width();
		var window_height = $(window).height();
		var body_height = $("body").height();
		left_position = (window_width - content_width)/2;
		$("#page_dropshadow").css("left",left_position+"px");
		$("#page_dropshadow").css("min-height",window_height+"px");
		if (window_height > body_height) {
			var clip_height = window_height;
		} else {
			var clip_height = body_height;
		}
		$("#page_dropshadow").css("clip","rect(0px, 952px, "+clip_height+"px, -10px)");
	}
	reposition_scroller();
	$(window).resize(function(){
		reposition_scroller();
	});
	

});

(function() {
	try {
		Typekit.load({
			active: function() {
				var nav_width = 0;
				$("#menu-main-navigation li").each(function(){
					if (!$(this).parent().hasClass('sub-menu')) {
						nav_width += $(this).width();
					}
				});
				//$("#access, #access .menu-header, div.menu").css("width",(nav_width+15)+"px");
			},
			inactive: function() {
				//$("#access, #access .menu-header, div.menu").css("width",(nav_width+15)+"px");
			}
		})
	} catch(e) {}
})();

