jQuery(function($){

	// Multiple Backgrounds
	/*if(!$.browser.safari){
		$('body>header').before(Array(
			'<div class="bg_wrap_c"></div>',
			'<div class="bg_wrap_b"></div>',
			'<div class="bg_wrap_a"></div>'
		).join(''));
	}*/

	// Activate easingScroll
	// @ 2009-07-18
	$('a[href*="#"]').filter(function(){
		return !$(this).parent().parent().hasClass('tab');
	}).filter(function(){
		return !$(this).parent().hasClass('reply');
	}).click(function(){
		$(this).blur();
	}).easingScroll({
		easing: 'easeInOutQuart',
		duration: 500
	});
		
	// Activate tabSwitch and inputSearch
	// @ 2009-07-18	
	//$('div.sub > nav').tabSwitch();
	$('input#s').inputSearch();
	
	// Remove ^[\t\n]* from pre elms
	// generated by WP-Syntaxhighlighter plugin
	// and Activate PrettyPrint
	// @ 2009-07-17
	$('pre[name="code"]').each(function(){
		var $this = $(this),
			text = $this.text(),
			reg = /^[\n\t]*/;
			
		if(text.match(reg)){
			$this.text(text.replace(reg,''));
		}
	});
	$('pre').addClass('prettyprint');
	prettyPrint();
	
	// Reply for comment form
	// move $('#respond') to target parent "li"
	// show cancel, and create the frame
	// @ 2009-07-18
	var replyComments = function(commentId,id){
		var parent = $('#'+commentId),
			comment_form = $('#respond'),
			cancel = $('#cancel-comment-reply-link'),
			hiddenParam = $('#comment_parent'),
			scroll_target = $.support.boxModel ? navigator.appName.match(/Opera/) ? 'html' : 'html,body' : 'body';
		
		if($('#comment_form_holder').length<1){
			comment_form.replaceWith('<div id="comment_form_holder"></div>');
		}
		parent.append(comment_form);
		hiddenParam.val(id);
		$('#author').focus();
		
		var respond_offset = comment_form.offset();
		
		$(scroll_target).animate({
			scrollTop: respond_offset.top,
			scrollLeft: respond_offset.left
		},{
			easing: 'easeInOutCirc',
			duration: 500
		});		
		
		cancel.show().bind('click',function(){
			hiddenParam.val(0);
			$('#comment_form_holder').replaceWith(comment_form);
			$(this).hide().unbind('click');
			
			return false;
		});
	}
	$('a','section#commentsArea div.reply').click(function(){
		var replyId = this.id.replace('reply-','');
		var id = replyId.replace('comment-','');
		
		replyComments(replyId,id);
		
		return false;
	});	
	
	/*$('#hougentimer div.box').click(function(){
		pageTracker._trackPageview('/hougentimer/');
	});*/
});

function awasete_yomitai(data){

	$('#info ul.tab').append([
		'<li>',
			'<a href="#awasete">Awasete</a>',
		'<li>'
	].join(''));
	
	$('#info').append([
		'<section id="awasete">',
			'<h2><a href="http://awasete.com/show.phtml?u=http%3A%2F%2Fmoto-mono.net%2F">あわせて読みたい</a></h2>',
		'</section>'
	].join(''));
	
	var awasete = $('#awasete').append('<ul/>');
	$(data).each(function() {				
		$('ul',awasete).append([
			'<li>',
				'<a href="'+this.url+'" style="background-image: url('+this.favicon+')">'+this.title+'</a>',
			'</li>'
		].join(''));
	});
	
	$('ul>li>a',awasete).click(function() {
		if( pageTracker ) {
			pageTracker._trackEvent('あわせて読みたい', $(this).text());
		}
	});
	
	awasete.append([
		'<div id="awaseteLink">',
			'5509と',
			'<a href="http://awasete.com/show.phtml?u=http%3A%2F%2Fmoto-mono.net%2F">',
				'あわせて読みたい',
			'</a>',
		'</div>'
	].join(''));
	
	// tabswitch init
	$('div.sub > nav').tabSwitch();
}
