$(document).ready(function(){
	
	// Cufon
	
	Cufon.replace('#breadcrumb a');
	
	// Add tips to input boxes
	
	$("input[data-tip]").focus(function(){
		if( $(this).hasClass("empty") ){
			$(this).removeClass("empty").val("");
		}
	}).blur(function(){
		if( $(this).val() == "" ){
			$(this).addClass("empty").val( $(this).attr("data-tip") );
		}
	}).blur();
	
	// Don't submit search if empty
	
	$("#search").submit(function(){
		if( $("#searchBox").hasClass("empty") ){
			return false;
		}
	});
	
	// Featured Teachers
	
	if( $("#panelFeaturedTeachers ul li").length > 1 ){(function(){
		var scrolling = false;
		var list = $("#panelFeaturedTeachers ul");
		
		var auto = setInterval(function(){
			$("#panelFeaturedTeachers a._next").click();
		},4300);
		
		$("#panelFeaturedTeachers a._prev").click(function(e){
			if( scrolling ){ return false; }
			scrolling = true;
			
			if( e.screenX ){ clearInterval( auto ); }
			
			list.css("left","-300px").children("li:last").prependTo(list);
			list.animate({
				left: "0"
			}, 400, "swing", function(){
				scrolling = false;
			});
			
			return false;
		});
		
		$("#panelFeaturedTeachers a._next").click(function(e){
			if( scrolling ){ return false; }
			scrolling = true;
			
			if( e.screenX ){ clearInterval( auto ); }
			
			list.animate({
				left: "-300px"
			}, 400, "swing", function(){
				list.css("left","0").children("li:first").appendTo(list);
				scrolling = false;
			});
			
			return false;
		});
		
	})();}
	
	// Blog panel toggle
	
	function panelClose( panel ){
		panel.removeClass("open").find("div.brief").stop().animate({
			"height": "14px"
		},400,"swing");
	}
	function panelOpen( panel ){
		panel.addClass("open").find("div.brief").stop().animate({
			"height": panel.find("p").height()+"px"
		},400,"swing");
	}
	
	$("#panelBlogs a._toggle").click(function(){
		var panel = $(this).closest(".panelBlog");
		if( panel.hasClass("open") ){
			panelClose( panel );
		}else{
			panelClose( $(".panelBlog.open") );
			panelOpen( panel );
		}
		return false;
	}).eq(0).click();
	
	// Slideshow
	
	if( $("#slideshow ul.nav li").length > 0 ){(function(){
		var transitioning = false;
		
		if( $("#slideshow ul.nav li").length > 1 ){
			var auto = setInterval(function(){
				var next = $("#slideshow ul.nav a.selected").closest("li").next("li").children("a");
				if( next.length == 1 ){
					next.click();
				}else{
					$("#slideshow ul.nav a:first").click();
				}
			},4500);
		}else{
			$("#slideshow ul.nav").hide();
		}
		
		$("#slideshow ul.nav a").click(function(e){
			
			if( transitioning ){ return false; }
			transitioning = true;
			
			if( e.screenX ){ clearInterval( auto ) }
			
			$("#slideshow ul.nav a.selected").removeClass("selected");
			$(this).addClass("selected");
			
			var old = $("#slideshow .slides li.on");
			
			$("#slide_"+$(this).attr("data-slide-id")).addClass("on").appendTo("#slideshow .slides").stop().fadeTo(400,1,"linear",function(){
				old.removeClass("on").stop().css("opacity","0");
				transitioning = false;
			});
			
			return false;
		}).eq(0).click();
		
	})();}
	
	// "Fetch more comments"
	$("a.fetch_more_comments").live( "click", function(){
		var url = $(this).attr("href");
		$(this).replaceWith('<span class="fetching_more_comments">Fetching more comments...</span>');
		$.ajax({
			url: url,
			success: function( html ){
				$("span.fetching_more_comments").replaceWith( html );
			}
		});
		return(false);
	});
	
	// Insert skype detection after window load (??)
	/*
	$(window).load(function(){
		$("head").append('<script type="text/javascript" src="http://download.skype.com/share/skypebuttons/js/skypeCheck.js"></script>');
	});
	*/
	
	// Questions Answered
	
	$("#questionsAnswered a.section_heading").click(function(){
		$(this).parent("h2").siblings("div.questions").slideToggle();
		return false;
	});
	
	$("#questionsAnswered div.questions a.question").click(function(){
		$(this).parent("h3").next("div.content").slideToggle();
		return false;
	});
	
	
	
});
