$(function(){
	
	var inputs = new Array();
	
	$("input.placeholder").each(function(i){
		
		if($(this).parents("form").attr("id") == "comment_form"){
			switch ($(this).attr("id")){
				case "name":
					if($(this).val() == ""){
						$(this).val("Your Name");
						inputs[i] = [$(this), $(this).val()];
					}else{
						$(this).removeClass("placeholder");
						inputs[i] = [$(this), "Your Name"];
					}
					break;
				case "email":
					if($(this).val() == ""){
						$(this).val("Your Email");
						inputs[i] = [$(this), $(this).val()];
					}else{
						$(this).removeClass("placeholder");
						inputs[i] = [$(this), "Your Email"];
					}
					break;
				case "url":
					if($(this).val() == "http://"){
						$(this).val("Your URL");
						inputs[i] = [$(this), $(this).val()];
					}else{
						$(this).removeClass("placeholder");
						inputs[i] = [$(this), "Your URL"];
					}
					break;
			}
		}else{
			//set up the array of values
			inputs[i] = [$(this), $(this).val()];
		}
		
		
		
		$(this).focus(function(){
			if($(this).val() == inputs[i][1]){
				$(this).attr("value", "").removeClass("placeholder");
			}
		});
		
		$(this).blur(function(){
			if($(this).val() == ""){
				$(this).attr("value", inputs[i][1]).addClass("placeholder");
			}
		});
	});
	
	$("li.comment-field textarea")
		.focus(function(){
			$(this).siblings("span").addClass("active");
			if($(this).val() == "What’s on your mind?"){
				$(this).val("");
			}
		})
		.blur(function(){
			$(this).siblings("span").removeClass("active");
			if($(this).val() == ""){
				$(this).val("What’s on your mind?");
			}
		});
		
		$("#comment_form #name").keyup(function(){
			if($(this).val() != ""){
				$("#your-preview h4 a").text($(this).val());
			}else{
				$("#your-preview h4 a").text("You");
			}
			
		});
		
		if($("#comment_form #name").val() != "Your Name"){
			$("#your-preview h4 a").text($("#comment_form #name").val());
		}
		
		$("#comment_form a.button").click(function(){
			if($("#url").val() == "Your URL"){
				$("#url").val("http://");
			}
			if($("#email").val() == "Your Email"){
				$("#email").val("");
			}
			if($("#name").val() == "Your Name"){
				$("#name").val("");
			}
			if($("#comment").val() != "What’s on your mind?"){
				$(this).parents("form").submit();
			}else{
				alert("You need to enter a comment!");
			}
			return false;
		});
		
		$("input:checkbox").each(function(){
			//set up variables
			var elem = $(this).get();

			//wrap with the proper elements
			$(elem).wrap("<div class='checker'></div>");
			$(elem).wrap("<span>");

			//more variables
			var thespan = $(elem).parents("span");
			var thediv = $(elem).parents("div.checker");

			//hide normal input and add focus classes
			$(elem)
			.css("opacity", 0)
			//.hide()
			.focus(function(){
				thediv.addClass("focus");
			})
			.blur(function(){
				thediv.removeClass("focus");
			})
			.click(function(){
				if(!$(elem).attr("checked")){
					//box was just unchecked, uncheck span
					thespan.removeClass("checked");	
				}else{
					//box was just checked, check span
					thespan.addClass("checked");
				}
			});

			//handle defaults
			if($(elem).attr("checked")){
				//box is checked by default, check our box
				thespan.addClass("checked");	
			}

		});
		
		//get gravatar
		
		$("#email").getGravatar({
			avatarSize: 30,
			start: function(){
				$("#working").fadeIn("fast");
			},
			stop: function(){
				$("#working").fadeOut("fast");
			},
			url: '/includes/get-gravatar.php',
			fallback: 'http://pixelmatrixdesign.com/img/default-avatar.png'
		});
		
		$("#working").hide();
		
});