﻿$(document).ready(function(){
	PageStart();
	Content();
	Comment();
	SideBar();
	Footer();
	Search();
});

$(window).load(function(){
	//var $CufonAim=$('#content>div[id^="post"]>h2');
	//Cufon.replace($CufonAim);
});

function PageStart(){
try{
	var $Url=$('#header>div>h1>a').attr('href'),
		$aRel=$('a[rel^="external"],#barLink>ol>li>a'),
		$Img=$('div[id^="post"]>div[class$="entry"] img,li[id^="comment"]>div[class="says"] img'),
		$Title=$('div[id^="post"]>h2>a>img'),
		$Object=$('object');
	$aRel.click(function(){
		this.target='_blank';
	});
	$Img.lazyload({
		placeholder:$Url+'/wp-content/themes/onlyroom/style/blank.gif',
		effect:'fadeIn',
		threshold:1
	});
	$Title.hover(function(){
		$(this).stop().fadeTo('fast',0.5);
	},function(){
		$(this).stop().fadeTo('fast',1);
	});
	$Object.focus(function(){
		$(this).blur();
	});	
}catch(e){};
};

function Content(){
try{
	var $ContentSmall=$('#content>div[id^="post"]>small>a'),
		$ContentH2Edit=$('#content>div[id^="post"]>h2>a[class="post-edit-link"]');
	$ContentSmall.removeAttr('title').removeAttr('rel');
	$ContentH2Edit.removeAttr('title');
}catch(e){};
};

function Comment(){
try{
	var $CommentListLi=$('#commentlist>li'),
		$CommentEditLink=$('#commentlist>li[id^="comment"]>p[class="card"]>a[class="comment-edit-link"]'),
		$CommentTextarea=$('#comment');
	$CommentEditLink.removeAttr('title');
	$CommentListLi.each(function(){
		var $CommentListChildrenLiSays=$(this).find('ul[class="children"]>li>div[class="says"]');
		$(this).find('div[class="says"]>a[class="comment-reply-link"]').clone().appendTo($CommentListChildrenLiSays);
	});
	$(function(){
		var $CommentReply=$('#commentlist>li>.says>a[class="comment-reply-link"]'),
			$CommentListChildrenLiSaysReply=$('#commentlist>li>.children>li>div[class="says"]>a[class="comment-reply-link"]');
		$CommentListChildrenLiSaysReply.click(function(){
			var $ReName=$(this).parents('#commentlist>li>.children>li').find('p[class="card"]>cite').text();
			$CommentTextarea.val('<strong>@'+$ReName+'</strong> ').focus();
			//return false;
		});
		$CommentReply.click(function(){
			$CommentTextarea.val('').focus();
			//return false;
		});
	});
	if($('#respond')[0]){
		var $CommentNum=$('#content>div[id^="post"]>small[class$="comnum"]'),
			$Respond=$('#respond'),
			$RespondTitle=$('#respond>div[class="title"]'),
			$CommentTipWait=$('#respond>p[class$="wait"]'),
			$CommentTipSend=$('#respond>div[class="title"]>p[class$="send"]'),
			$CommentTipError=$('#respond>p[class$="error"]'),
			$CommentForm=$('#commentform'),
			$CommentCancelReply=$('#cancel-comment-reply-link'),
			$CommentSubmit=$('#commentform>p[class="submit"]>input[type="submit"]'),
			$FaceBtn=$('#commentform>p[class^="textarea"]>ins>a'),
			$Url=$('#header>div>h1>a').attr('href'),
			$Num=parseInt($CommentNum.text().match(/\d+/));
		$FaceBtn.click(function(){
			$CommentTextarea.val($CommentTextarea.val()+' '+$(this).attr('class')+' ').focus();
		});
		$CommentSubmit.focus(function(){
			$(this).blur();
		});
		$CommentCancelReply.click(function(){
			$CommentTextarea.empty();
		});
		$CommentForm.find(':input').removeAttr('disabled');
		$CommentForm.submit(function(){
			$.ajax({
				type:$(this).attr('method'),
				url:$Url+'/wp-content/themes/onlyroom/ajax.php',
				data:$(this).serialize(),
				beforeSend:function(){
					$CommentTipError.hide();
					$RespondTitle.show();
					$CommentTipSend.show();
					$CommentForm.find(':input').attr('disabled',true);
				},
				success:function(data){
					$CommentTipError.hide().empty();
					$CommentTipSend.hide();
					$CommentTextarea.empty();
					$Respond.before(data);
					$CommentNum.text($CommentNum.text().replace($Num,$Num+1));
					$Respond.find('div[class="title"],form').fadeOut(1500,function(){
						$CommentTipWait.fadeIn('normal');
					});
					setTimeout(function(){
						$CommentForm.find(':input').removeAttr('disabled');
						$CommentTipWait.fadeOut('normal',function(){
							$Respond.find('div[class="title"],form').fadeIn(1500);
						});
					},15000);
				},
				error:function(request){
					$RespondTitle.hide();
					$CommentTipError.show().html(request.responseText);
					$CommentForm.find(':input').removeAttr('disabled');
				}
			});
			return false;
		});
	}
}catch(e){};
};

function SideBar(){
try{
	var $categoryLi=$('#barCategory>ol>li'),
		$_categoryLi=$('#barCategory>ol>li>a'),
		$_archiveLi=$('#barArchive>ol>li>a'),
		$_linkLi=$('#barLink>ol>li>a[title]'),
		$barMetaLoginID=$('#loginform>p[class$="input"]:eq(0)'),
		$barMetaLoginSN=$('#loginform>p[class$="input"]:eq(1)'),
		$barMetaLoginOK=$('#loginform>p[class="submit"]>input[type="submit"]');
	$categoryLi.removeAttr('class');
	$_categoryLi.removeAttr('title');
	$_archiveLi.removeAttr('title');
	$_linkLi.removeAttr('title');
	$barMetaLoginID.focusin(function(){
		$(this).attr('class','_input');
	}).focusout(function(){
		$(this).attr('class','input');
	});
	$barMetaLoginSN.focusin(function(){
		$(this).attr('class','_input');
	}).focusout(function(){
		$(this).attr('class','input');
	});
	$barMetaLoginOK.focus(function(){
		$(this).blur();
	});
}catch(e){};
};

function Footer(){
try{
	var $ToTop=$('#toTop>div>a'),
		$ContentSmallComnum=$('#content>div[id^="post"]>small[class="_comnum"]>a'),
		$CommentForm=$('#respond'),
		$Root=(window.opera)?(document.compatMode=='CSS1Compat'?$('html'):$('body')):$('html,body');
	$ToTop.click(function(){
		if(!$Root.is(':animated')){
			$Root.animate({scrollTop:'0px'},'normal','swing');
		}
		return false;
	});
	$ContentSmallComnum.click(function(){
		if(!$Root.is(':animated')){
			$Root.animate({scrollTop:$CommentForm.offset().top},'normal','swing');
		}
		return false;
	});
}catch(e){};
};

function Search(){
try{
	if($('#searchform')[0]){
		var $SearchInput=$('#s'),
			$SearchSubmit=$('#searchsubmit');
		$SearchInput.blur(function(){
			if($(this).val()==''){
				$(this).attr('class','s');
			}else{
				$(this).attr('class','_s');
			}
		}).focus(function(){
			$(this).attr('class','_s');
		});
		$SearchSubmit.focus(function(){
			$(this).blur();
		});
	}
}catch(e){};
};
