// Loads up the various functions we are going to use
$(document).ready(function(){
	swapLetter(); 
	imgCaption();
	latestShows(); 
	comment();
	reply();
	newwindow(".email a, .emailSub a", "resizable=1,width=550,height=450");
	newwindow("#odeo a", "resizable=1,width=300,height=425");
	newwindow(".liveStream a", "resizable=1,width=320,height=260");
	newwindow("#liveStream a", "resizable=1,width=320,height=260");
	onFocus("input");
	onFocus("#comForm textarea");
	goValidate("#comForm");
	goValidate("#id2349664");
	filter();
	$("#juitterContainer").ready(function(){
	$.Juitter.start("searchWord","DesignTip");
	});
	
});

// Makes the latest shows expand and contract
function latestShows() {
	$("#sNav dd:first").css("display", "block");
	$("#sNav dt").mouseover(
	 function () {
		if ($(this).next().css("display") == "none") {
		$("#sNav dd").slideUp("fast");
        $(this).next().slideDown("fast");
		};
      });
}

// Adds in the drop cap
function swapLetter() {
  	var first_paragraph = $('#body p')[0];
  	if (!first_paragraph) { return false };
  	var text = first_paragraph.firstChild.nodeValue;
	if (!text) { return false };
	var first_letter = text.substr(0,1);
	if ( text ) {
    first_paragraph.firstChild.nodeValue = text.slice(1);
  };
  $("<span class='firstLetter'>" + first_letter +"</span>").prependTo( first_paragraph );
};


// Starts validating a submitted form
function goValidate(v) {
	$(v).validate({
	  errorPlacement: function(error, element) {
		 error.appendTo( element.parent("p").prev("p") );
	   }
	 });	
};

// Opens email subscription in a new window

function newwindow(x, y) {
	$(x).click(function(){
			window.open (this.href, "mywindow", y); 	
			return false;
	});
};

// Allows you to add focus events to any element
function onFocus(f) {
	$(f).focus(function() {
		$(this).addClass("focus");
	});
	$(f).blur(function() {
		$(this).removeClass("focus");
	});
};

// Wraps images and adds caption

function imgCaption() {
	$(window).load(function() {
	$("#body img").each( function() {
	if ($(this).width() < 370) {
		$(this).wrap("<div class='img floatMe' style='width:" + ($(this).width() + 2) + "px'></div>");
			if ($(this).attr("title")!=undefined) { $(this).after("<p class='caption'>" + $(this).attr("title") + "</p>") };
			if ($(this).parents("a").attr("title")!=undefined) { $(this).after("<p class='caption'>" + $(this).parents("a").attr("title") + "</p>") };
		$(this).parents("a").addClass("imgSur");
	} else {
		$(this).wrap("<div class='img' style='width:" + ($(this).width() + 2) + "px'></div>");
			if ($(this).attr("title")!=undefined) { $(this).after("<p class='caption'>" + $(this).attr("title") + "</p>") };
			if ($(this).parents("a").attr("title")!=undefined) { $(this).after("<p class='caption'>" + $(this).parents("a").attr("title") + "</p>") };
		$(this).parents("a").addClass("imgSur");
	};
	if ($(this).width() > 600) {
		$(this).parents(".img").width("580px");
	}
	});
							});
};

// Allows for posts to be filters on the archive page

	function filter() {
		if ($("body").hasClass("archive")) { 
			$("#catListing ul").hide();
			$("#catListing  a").toggle(function() {
					$("#catListing ul").load($(this).attr("href"), function() {
					$("#catListing li a").click(function() { 
													   hideCat($(this).text()) 
													   $(this).toggleClass("filter");
													   return false;
													   });
						$("#catListing ul").slideDown();
				});	
				return false;
			},
			function() {
				$("#catListing ul").slideUp();
				$("#content li:hidden").animate({ "opacity": "show" }, "slow");
			});
			
		};
	};
	
	
	function hideCat(v) {
		$("#content li a").each(function() {
			if ($(this).attr("rel") == v)	{
				$(this).parent("li").animate({ "opacity": "toggle" }, "slow");
			};
		});
	};


// Makes the reply links in comments show the comment box.
function reply() {
	var currentURL = window.location;
	if (currentURL.hash == "#respond") {		
		 $("#comForm").slideDown("fast");
	}
	
	
};