(function() {
    try {
      Typekit.load({
        active: function() {
        	var typekit_elems = $("#nav, #strip li a, #util-nav li a, .footer-links li, .news-article h3 a, .news-article h3 span, .headline-article h3, .contact-info");
          // As soon as the fonts are active, fade in the example
          // Don't fade in browsers that don't do proper opacity, like IE
          if (jQuery.support.opacity) {
            typekit_elems.addClass('typekit-loaded').css('visibility', 'visible').hide().fadeIn();
            $("a.text-arrow-link").hide();
          } else {
            typekit_elems.addClass('typekit-loaded').css('visibility', 'visible');
            $("a.text-arrow-link").hide();
          }
        },
        inactive: function() {
        	var typekit_elems = $("#nav, #strip li a, #util-nav li a, .footer-links li, .news-article h3 a, .news-article h3 span, .headline-article h3, .contact-info");
          // If the fonts are inactive, just show the example
          // You can apply fallback styles using the wf-inactive class in your CSS
          typekit_elems.css('visibility', 'visible').addClass('typekit-loaded');
        }
      })
    } catch(e) {}
})();

function fix_google_iframe(width, height) {
	var iframe = $('.gadget iframe');
	var src = $('.gadget iframe').attr("src");
	var div = $(".gadget div");
	div.html('<iframe src="about:blank" frameborder=0 width="'+width+'" height="'+height+'" horizontalscrolling="no" verticalscrolling="yes"></iframe>')
	var ifr = div.children(":first-child"); 
	ifr.attr("src",src);
}

$(document).ready(function() {

	$("#findacourse").submit(function() {
		if ($("#qc").val() == "") {
			$("#qc").val(" ");
		}
	});
	fix_google_iframe(640, 900);		   
	
	$(".info").tipTip({
		defaultPosition: 'right'
	});
	$(".date").datepicker();
	$("#q").inputHint();
	if($("#inner-content").length) {
		var inner_content_height = $("#inner-content").height();
		var sidebar_height = $("#sidebar").height();
		if(inner_content_height > sidebar_height) {
			$("#sidebar-pattern").css("height", inner_content_height + 0);
		}
		else {
			$("#sidebar-pattern").css("height", sidebar_height + 10);
		}
	}
	
	$(".credit-info").click(function() {
		$("#credit-info").dialog({
			width: 600,
			modal: true
		});
	});
	
	$("#babsimlive-course").click(function() {
		$("#babsimlive-info").dialog({
			width: 450,
			modal: true
		});
	});
	
	$("#terms-link").click(function() {
		$("#terms").dialog({
			width: 600,
			modal: true
		});
		/*newWin= window.open("");
		newWin.document.write($("#terms").html());
		newWin.print();
		newWin.close();
		*/
	});
	
	if($("#sidebar").length) {
		$("#sidebar ul li").hover(
			function() {
				$(this).addClass("active");
				$("ul", this).stop(true, true).fadeIn();
			},
			function() {
				$(this).removeClass("active");
				$("ul", this).stop(true, true).fadeOut();
			}
		);
	}
	
	$("#slant-nav a").click(function() {
		var duration = 350;
		var start_num = $(".start-num").html();
		var end_num = $(".end-num").html();
		if($(this).hasClass("prev")) {
			start_num--;
			if(start_num == 0) start_num = end_num;
		}
		else {
			start_num++;
			if(start_num > end_num) start_num = 1;
		}
		$(".start-num").html(start_num);
		$(".slide").fadeOut(duration, function() {
			$(".slide:eq(" + parseInt(start_num-1) + ")").fadeIn(duration);
		});
		
	});
	
	$(".slide:first").show();
	
	$(".containers > div").not(":first").not(".page-curl").hide();
	
	$(".tabs li a").hover(
		function() {
			if($(this).hasClass("active") == false) $(this).addClass("hover");
		},
		function() {
			if($(this).hasClass("active") == false) $(this).removeClass("hover");
		}
	);
	
	$(".tabs li a").click(function() {
		var tab_index = $(".tabs li a").index($(this));
		$(".tabs li a").removeClass("active hover");
		$(this).addClass("active");
		$(".tabs li").removeClass("active");
		$(this).parent().addClass("active");
		$(".containers > div").not(".page-curl").hide();
		$(".containers > div:eq(" + tab_index + ")").show();
	});
	
	$(".link-list li:has(.category)").each(function() {
		var num_courses = $(this).find("a.course").length;
		if(num_courses == 0) $(this).hide();
		$(this).children(".category").children("b").html(num_courses);
	});
	
	$(".search-list li:has(.category)").each(function() {
		var num_courses = $(this).find("a.course").length;
		if(num_courses == 0) $(this).remove();
		$(this).children(".category").children("b").html(num_courses);
	});
	
	var count_results = $(".search-list .course").length;
	if(count_results == 1) {
		$(".category").each(function() {
			$(this).children("span").html('-');
			$(this).next("ul").show();
		});
	}
	
	$(".link-list a.category, .search-list a.category").click(function() {
		var nested_ul = $(this).siblings("ul");
		var plus_minus = $(this).children("span");
		if(nested_ul.is(":hidden")) {
			nested_ul.show();
			plus_minus.html('-');
		}
		else {
			nested_ul.hide();
			plus_minus.html('+');
		}
	});
	
	$(".methods li a").click(function() {
		$(this).parent().siblings().children("a").removeClass("clicked");
		if($(this).hasClass("clicked")) {
			$(this).removeClass("clicked");
			var method_type = "course";
		}
		else {
			$(this).addClass("clicked");
			var method_type = $(this).attr("rel");
		}
		
		$(".link-list li:has(.category), .search-list li:has(.category)").each(function() {
			$(this).find("a.course").hide();
			var matched_courses = $(this).find("a." + method_type);
			matched_courses.show();
			var num_courses = matched_courses.length;
			$(this).children(".category").children("b").html(num_courses);
		});
	});
	
	$("#strip li a").hover(
		function() {
			$(this).siblings("a.text-arrow-link").addClass("active");
		},
		function() {
			$(this).siblings("a.text-arrow-link").removeClass("active");
		}
	);
	
	$("#strip li").hover(
		function() {
			$(this).children("a.text-arrow-link").stop(true, true).fadeIn();
		},
		function() {
			$(this).children("a.text-arrow-link").stop(true, true).fadeOut();
		}
	);
	
	$.get($('.livechatlink').attr("rel"),function(data){
		var target = $('.livechatlink');
		target.append(data);
		if (data=="Online") target.addClass("online");
	});
	
	$('div:has(iframe)').css('border','none');
	
});
