$(document).ready(function(){
	var currentSite="http://www.fwallstreet.com";
	function fadeBackground(){
		$("div#blogInstructionsBackground")
			.css({
				"height": $(window).height(),
				"left": "0px",
				"position":"fixed",
				"top": "0px",
				"width": $(window).width(),
				"z-index": 1000
			})
			.fadeTo(1,0.85)
			.fadeIn("fast",function(){
			$("body, html").css({"overflow":"hidden"});
		});
	}
	function resetCommentFields(){
		$("div.commentFormRight input[type=text]").each(function(){
			if($(this).attr("default")==""){
				$(this).css({"color":"#666666"}).val($(this).attr("title"));
			} else {
				$(this).css({"color":"#000000"}).val($(this).attr("default"));
			}
		});
		$("input[name=blogid]").val($("input[name=blogid]").attr("title"));
	}
	$("div.commentFormJavascriptWarning").hide();
	$("div.commentFormLeft, div.commentFormRight").show(function(){
		resetCommentFields();
	});
	$("input[name=fbEmail], div.rbgbottom form table tr td input[name=email]")
		.css({"color":"#666666"})
		.val("me@email.com")
		.focus(function(){
			$(this).css({"color":"#000000"})
			if(jQuery.trim($(this).val())=="me@email.com"){
				$(this).val("");
			}
		})
		.blur(function(){
			if(jQuery.trim($(this).val())==""){
				$(this).css({"color":"#666666"})
				$(this).val("me@email.com");
			}
		});
	$("input[name=q]")
		.css({"color":"#666666"})
		.val("intrinsic value, walmart")
		.focus(function(){
			$(this).css({"color":"#000000"})
			if(jQuery.trim($(this).val())=="intrinsic value, walmart"){
				$(this).val("");
			}
		})
		.blur(function(){
			if(jQuery.trim($(this).val())==""){
				$(this).css({"color":"#666666"})
				$(this).val("intrinsic value, walmart");
			}
		});
	$("li#indexLatestArticle a").addClass("indexLatestArticle");
	$("div.indexLatestArticle").show();
	$("ul#indexbuttons li a").click(function(){
		var theClass=$(this).parent().attr("id");
		$(this).parent().parent().each(function(){
			$(this).children("li").each(function(){
				$(this).children("a").removeClass($(this).attr("id"));
			});
		});
		$(this).addClass($(this).parent().attr("id"));
		$(this).parent().parent().parent().parent().children("div#indexsnippet").children().hide();
		$("div."+theClass).fadeIn("fast");
	});
	$("td.cformInput input[id=yn], td.cformInput input[id=ye], td.cformInput textarea").focus(function(){
		if(jQuery.trim($(this).val())=="" || jQuery.trim($(this).val())==$(this).attr('title')){
			$(this).val("").css({"color":"#000000"});
		}
	});
	$("form#cform").submit(function(ev){
		$("td.cformInput input[id=yn], td.cformInput input[id=ye], td.cformInput textarea").each(function(){
			if(jQuery.trim($(this).val())=="" || jQuery.trim($(this).val())==$(this).attr('title')){
				$(this).css({"color":"#990000"}).val($(this).attr('title'));
				ev.preventDefault();
			}
		});
	});
	$("div#archiveDate dd, div#archiveCategory dd").hide();
	$("div#archiveDate dt a, div#archiveCategory dt a").click(function(ev){
		if($(this).parent().next().is(":hidden")){
			$("div#archiveDate dd, div#archiveCategory dd").hide();
			$(this).parent().next().css({"padding-bottom":"10px"}).show("slow");
			ev.preventDefault();
			return false;
		}
	});
	$("div.commentFormRight input, div.commentFormRight textarea").each(function(){
		$(this).css({"color":"#666666"}).val($(this).attr("title"));
	});
	$("div.commentFormRight input, div.commentFormRight textarea").focus(function(){
		if(jQuery.trim($(this).val())==$(this).attr("title") || jQuery.trim($(this).val())==$(this).attr("warning") || jQuery.trim($(this).val())==""){
			$(this).val("").css({"color":"#000000"});
		}
	});
	$("div.commentFormRight input, div.commentFormRight textarea").blur(function(){
		if(jQuery.trim($(this).val())==$(this).attr("title") || jQuery.trim($(this).val())==""){
			$(this).css({"color":"#666666"}).val($(this).attr("title"));
		}
	});
	$("div.commentFormRightInput input[name=cname]").blur(function(){
		$("span#commentFormYourName").text($(this).val());
	});
	$("div.commentFormRightInput input[name=cemail]").blur(function(){
		$("div.commentFormLeft img").attr("src","http://www.gravatar.com/avatar/"+$.md5($(this).val())+"?s=80&d="+escape(currentSite+"/images/gravatar_default.jpg"));
	});
	$("div.submitComment a, div.submitReply a").click(function(){
		var hasError=false;
		if($("input[name=cname]").val()==$("input[name=cname]").attr("title") || $("input[name=cname]").val()==$("input[name=cname]").attr("warning")){
			$("input[name=cname]").css({"color":"#990000"}).val($("input[name=cname]").attr("warning"));
			hasError=true;
		}
		if($("textarea[name=ccomment]").val()==$("textarea[name=ccomment]").attr("title") || $("textarea[name=ccomment]").val()==$("textarea[name=ccomment]").attr("warning")){
			$("textarea[name=ccomment]").css({"color":"#990000"}).val($("textarea[name=ccomment]").attr("warning"));
			hasError=true;
		}
		if(hasError){
			return false;
		} else {
			fadeBackground();
			$("div#submittingComment").css({"left":$(window).width()/2-$("div#submittingComment").width()/2,"top":$(window).height()/2-$("div#submittingComment").height()/2})
				.fadeIn("fast",function(){
								var dataString="&rand="+Math.random();
								dataString+="&name="+encodeURIComponent($("input[name=cname]").val());
								dataString+="&email="+encodeURIComponent($("input[name=cemail]").val());
								dataString+="&url="+encodeURIComponent($("input[name=curl]").val());
								dataString+="&comment="+encodeURIComponent($("textarea[name=ccomment]").val());
								if($("input[name=crm]").is(":checked")){
									dataString+="&rememberme=yes";
								}
								dataString+="&replyto="+$("input[name=ReplyTo]").val();
								dataString+="&blogid="+$("input[name=blogid]").val();
								$.ajax({
										type: "POST",
										url: currentSite+"/catfire2010.php",
										cache: false,
										data: dataString,
										success: function(data){
											if(data=="ok"){
												$("div#submittingComment").fadeOut("fast",function(){
													$("div#thanksComment").css({"left":$(window).width()/2-$("div#thanksComment").width()/2,"top":$(window).height()/2-$("div#thanksComment").height()/2}).fadeIn("slow");
												});
											}
										},
										error: function(objAJAXRequest, strError){
											alert(objAJAXRequest.responseText);
										}
								});
							});
		}
	});
	$("a.reply").click(function(){
		$("input[name=cname]").css({"color":"#666666"}).val($("input[name=cname]").attr("title"));
		$("input[name=cemail]").css({"color":"#666666"}).val($("input[name=cemail]").attr("title"));
		$("input[name=curl]").css({"color":"#666666"}).val($("input[name=curl]").attr("title"));
		$("input[name=ReplyTo]").val($(this).parent().parent().parent().parent().attr("id"));
		$("textarea[name=ccomment]").css({"color":"#666666"}).val($("textarea[name=ccomment]").attr("title"));
		$("div.submitReply, div.cancelReply").show();
		$("div#commentFormMove").remove();
		$(this).parent().parent().parent().parent().append("<div id=\"commentFormMove\"></div>");
		var replyPosition=$("div#commentFormMove").position();
		$("div.commentFormLeft, div.commentFormRight, div.submitComment").hide();
		$("div#commentFormMove").animate({
			height: ($("div.commentFormRight").height()+15)+"px"
		});
		$("div.commentFormRight").css({
			"left":(replyPosition.left+108)+"px",
			"position":"absolute",
			"top":(replyPosition.top+15)+"px"
		}).fadeIn("slow");
		$("html, body").animate({
			scrollTop: (replyPosition.top-50)+"px"
		},1000);
		resetCommentFields();
	});
	$("div.cancelReply a").click(function(){
		$("input[name=cname]").css({"color":"#666666"}).val($("input[name=cname]").attr("title"));
		$("input[name=cemail]").css({"color":"#666666"}).val($("input[name=cemail]").attr("title"));
		$("input[name=curl]").css({"color":"#666666"}).val($("input[name=curl]").attr("title"));
		$("input[name=ReplyTo]").val("");
		$("textarea[name=ccomment]").css({"color":"#666666"}).val($("textarea[name=ccomment]").attr("title"));
		var replyToId=$("div#commentFormMove").parent().attr("id");
		var lastReplyPosition=$("div#"+replyToId).position();
		$("div#commentFormMove").remove();
		$("div.submitReply, div.cancelReply").hide();
		$("div.commentFormLeft, div.commentFormRight, div.submitComment").hide();
		$("div.commentFormRight").css({
			"left":"",
			"position":"",
			"top":""
		}).fadeIn("slow");
		$("div.commentFormLeft, div.commentFormRight, div.submitComment").show();
		$("html, body").animate({
			scrollTop: (lastReplyPosition.top-15)+"px"
		},1000);
		resetCommentFields();
	});
	$("a#showBlogInstructions").click(function(){
		fadeBackground();
	});
});