jQuery.fn.extend({
	textfill: function(e) {
		var txt = e.attr('title');
		e.focus(function() {
			if ($.trim(e.val()) == txt) {
				e.val('').css('color', '#333');
			}
		});
		e.blur(function() {
			if ($.trim(e.val()) == '' || $.trim(e.val()) == txt) {
				e.val(txt).css('color', '#999');
			}
		});
		e.parents('form:first').submit(function() {
			if ($.trim(e.val()) == txt) {
				e.triggerHandler('focus');
			}
		});
		e.blur();
	}
});

$(function() {
	// scroller
	$('#totop a, .totop a').click(function() {
		$(this).blur();
		$('html, body').animate({ scrollTop: 0 }, 'fast');
		return false;
	});

	// modal box
	$('a.lb').nyroModal({
		galleryCounts: null
	});

	// form error
	$('div.error-message').each(function() {
		$(this).css({
			position: 'absolute',
			top: 0,
			width: 514,
			height: '100%',
			lineHeight: 1.75,
			textIndent: '0.5em',
			border: '1px solid red',
			backgroundColor: '#EEE'
		}).hover(function() {
			$(this).fadeOut('fast');
		});
	});

	// rollover
	$(function() {
//		var c = location.pathname.split('/');
		$('#menu li a').each(function() {
			if ($(this).attr('href') == $('#topicpath a:last-child').attr('href')) {
				$(this).addClass('current');
			}
		});
		if (location.pathname.split('/').pop() == 'links') {
			$('#links h2 a').addClass('current');
		}
	});

	$('#menu li a[class!="current"]').hover(
		function() {
			$(this).animate({
				opacity: 0.5,
				duration: 'slow',
			})
		},
		function() {
			$(this).animate({
				opacity: 1.0,
				duration: 'slow'
			})
		}
	);

	$('a.ro img, #blog-main a img').hover(
		function() { $(this).css('opacity', 0.7) },
		function() { $(this).css('opacity', 1.0) }
	);

	$('body .box .button a').hover(
		function() {
			$(this).css({
				opacity: 0.3,
				backgroundColor: '#FFF'
			});
		},
		function() {
			$(this).css({
				opacity: 1.0,
				backgroundColor: 'transparent'
			});
		}
	);

	// image size fixed
	$('.blog-body img').each(function() {
		if ($(this).width() > 535) {
			$(this).css({ width: 535, height: 'auto' });
		}
	});

	$('form input[title]').each(function() {
		$(this).textfill($(this));
	});

	$('form textarea[title]').each(function() {
		$(this).textfill($(this));
	});

	// New icon
	$(function() {
		var n = new Date().getTime();
		$('#body .box.col2 h3').each(function() {
			if (Date.parse($(this).text().replace(/\./g, '/')) > n - (1000*60*60*24*14)) {
				$(this).addClass('new');
			}
		});
	});

	$('p.totop:last').css({ marginTop: 0, visibility: 'hidden' });
	$('.blog-article:last').css({ marginBottom: 15 });
});

