var kwTheme = {
	config: ('undefined' != typeof(_config)) ? _config : {}
};

(function($){
	$(document).ready(function() {
		var config = kwTheme.config;
		var themePath = config.themePath || 'http://www.makotokw.com/theme/makotokw-200908/';
		kwTheme.path = themePath; // shorthand
		
		// google custom search
		if (config.useCustomSearch) {
			try {
				var f = document.getElementById('searchbox_003122074834180947232:omul0da14we');
				if (!f) {
					f = document.getElementById('searchbox_demo');
				}
				if (f && f.q) {
					var q = f.q, n = navigator, l = location;
					if (n.platform == 'Win32') {
						q.style.cssText = '';
					}
					var b = function() {
						if (q.value == '') {
							q.style.background = '#fff url(http:\x2F\x2Fwww.google.com\x2Fcoop\x2Fintl\x2Fen\x2Fimages\x2Fgoogle_custom_search_watermark.gif) left no-repeat';
						}
					};
					var f = function() {
						q.style.background = '#fff';
					};
					q.onfocus = f;
					q.onblur = b;
					if (!/[&?]q=[^&]/.test(l.search)) {
						b();
					}
					new CSEOverlay("003122074834180947232:omul0da14we", 
						document.getElementById("searchbox_003122074834180947232:omul0da14we"), 
						document.getElementById("results_003122074834180947232:omul0da14we"))
						;
				}
			} catch (e) {}
		}
		// pretyprint
		if ('function' == typeof(prettyPrint)) {
			prettyPrint();
		}
		
		// lightbox
		$('a[rel=lightbox]').lightBox({
				imageLoading:			themePath+'images/lightbox-ico-loading.gif',		// (string) Path and the name of the loading icon
				imageBtnPrev:			themePath+'images/lightbox-btn-prev.gif',			// (string) Path and the name of the prev button image
				imageBtnNext:			themePath+'images/lightbox-btn-next.gif',			// (string) Path and the name of the next button image
				imageBtnClose:			themePath+'images/lightbox-btn-close.gif',		// (string) Path and the name of the close btn
				imageBlank:				themePath+'images/lightbox-blank.gif'			// (string) Path and the name of a blank image (one pixel)
		});
	});
})(jQuery);

// add function
(function($) {
	function trim(s) {
		if (s) {
			s = s.replace(/\t|\n|\r\n/g, '');
			s = s.replace(/^[ ]+|[ ]+$/g, '');
		}
		return s;
	}
	function reply(authorId, commentId, commentBox) {
		var author = $('#'+authorId).html();
		var s = '<a href="#' + commentId + '">@' + trim(author) + ' </a> \n';
		appendReply(s, commentBox);
	}
	
	function quote(authorId, commentId, commentBodyId, commentBox) {
		var author = $('#'+authorId).html();
		var comment = $('#'+commentBodyId).html();
	
		var s = '<blockquote cite="#' + commentBodyId + '">';
		s += '\n<strong><a href="#' + commentId + '">' + trim(author) + '</a> :</strong>';
		s += comment.replace(/\t/g, "");
		s += '</blockquote>\n';
		insertQuote(s, commentBox);
	}
	
	function appendReply(s, id) {
		var $field = $('#'+id);
		var v = $field.val() || '';
		if (v.indexOf(s) > -1) {
			alert("You've already appended this reply!");
			return false;
		}
	
		if (v.replace(/\s|\t|\n/g, "") == '') {
			$field.val(s);
		} else {
			$field.val(v.replace(/[\n]*$/g, "") + '\n\n' + s);
		}
		$field.focus();
	}
	
	function insertQuote(s, id) {
		var $field = $('#'+id);
		var v = $field.val();
		$field.val(v+s);
		$field.focus();
	}
	
	this.reply = reply;
	this.quote = quote;

}).apply(kwTheme,[jQuery]);

if (kwTheme.config.useAwaseteYomitai) {
	window['awasete_yomitai'] = function awasete_yomitai(blogs) {
		// var blogs = [{titile:,url:,favicon:,more:},...];
		var el = document.getElementById('awasete-blog-list');
		if (el) {
			var max = 10, a = [], len = Math.min(max,blogs.length);
			for (var i=0; i<len; i++) {
				var b = blogs[i];
				a.push('<li style="background:transparent url('+b.favicon+') no-repeat left"><a href='+b.url+'>'+b.title+'<\/a><\/li>');
			}
			el.innerHTML = a.join('');
		}
	}
}
