/**
 * jQuery - Config file
 */
(function($) {
	SyntaxHighlighter.defaults['toolbar'] = false;

	var RewishApply = (function(context) {
		$('a[href^="http://"], a[href^="https://"]', context)
			.not('a[href^="http://'+ location.host +'"]')
			.not('.downLoad').addClass('exLink').winOpen();
		$('div.readMore a img', context).rollover();

		$('a[href^="#"]').click(function(e) {
			if (e.preventDefault) e.preventDefault();
			else window.event.returnValue = false;
			$($(this).attr('href')).smoothScroll();
		});

		$('tr:even', $('div.textBody', context)).addClass('even');

		var codeTaggleLabel = ['Code View', 'Plain Text View'];
		$('pre[class^="brush"]', context).each(function() {
			var self = $(this), plainView = self.clone();
			self.before(plainView.attr('class', 'plain').hide());
			SyntaxHighlighter.highlight(null, this);
			var codeView = plainView.next('div.syntaxhighlighter');
			var status = true;
			var codeToggle = $('<a/>', {
				text: codeTaggleLabel[+status],
				href: 'javascript:void(0)',
				'class': 'codeTaggle',
				click: function() {
					status = !status;
					plainView.toggle();
					codeView.toggle();
					codeToggle.text(codeTaggleLabel[+status]);
				}
			});
			plainView.before(codeToggle);
		});
		return arguments.callee;
	})();

	$('#trackbacks input').click(function() {
		 $(this).select();
	});

	var supportPlaceholder = 'placeholder' in document.createElement('input');
	$('input[type="search"]').each(function() {
		var self = $(this);
		if (self.innerWidth() < 154) {
			self.width(154);
		}
		if (supportPlaceholder) {
			return;
		}
		var position = self.position();
		var placeholder = $('<div/>', {
			text: self.attr('placeholder'),
			css: {
				position: 'absolute',
				top: position.top,
				left: position.left,
				width: self.css('width'),
				height: self.css('height'),
				paddingLeft: self.css('paddingLeft'),
				lineHeight: self.css('height'),
				color: '#999',
				cursor: 'text'
			},
			click: function() {
				$(this).hide();
				self.focus();
			}
		});
		self.after(placeholder)
			.focus(function() {
				placeholder.hide();
			}).blur(function() {
				if ($(this).val() === '') {
					placeholder.show();
				}
			});
	});

	// Hatena Star
	Hatena.Star.Token = '60dfc0240b9e4ab07aa8f9930a02387d0644bf23';
	Hatena.Star.SiteConfig = {
		entryNodes: {
			'article > header': {
				uri: 'h1 a',
				title: 'h1',
				container: 'h1'
			}
		}
	};
	if (window.addEventListener) {
		var StarConfig = Hatena.Star.SiteConfig.entryNodes;
		NodeInsert = function(evt) {
			Hatena.Star.SiteConfig.entryNodes = {
				'> header': StarConfig['article > header']
			};
			RewishApply(evt.target);
			Hatena.Star.EntryLoader.loadNewEntries(evt.target);
		};
		window.addEventListener('AutoPagerize_DOMNodeInserted', NodeInsert, false);
		window.addEventListener('AutoPatchWork.DOMNodeInserted', NodeInsert, false);
	}

	window.awasete_yomitai = function(data) {
		var awasete = ['<ul>'];
		for (var i = 0, len = 5; i < len; ++i) {
			awasete[awasete.length] = [
				'<li><a href="', data[i]['url'], '"',
				'style="background-image: url(', data[i]['favicon'], ');">',
				data[i]['title'], '</a></li>'
			].join('');
		}
		awasete[awasete.length] = '</ul>';
		$('section.awasete a').after(awasete.join(''));
	};
	$.getScript("http://api.awasete.com/showjson.phtml?u=http%3A%2F%2Frewish.org%2F");

})(jQuery)
