/* 汎用ホバーアクション */
(function($){
	$.fn.jshover = function(config){
		var defaults = {
		}
		var options = $.extend(defaults, config);
		return this.each(function(_i){
			var tagname = this.tagName.toLowerCase();
			if(tagname === 'a' || tagname === 'input' || tagname === 'button') {
				$(this).hover(
					function() {
						$(this)
							.stop()
							.animate({opacity : .5}, 'fast');
					},
					function() {
						$(this)
							.stop()
							.animate({opacity : 1});
					}
				);
				$(this).click(function() {
					$(this).trigger('mouseout');
				});
			} else if(tagname === 'img') {
				var outsrc = $(this).attr('src');
				var onsrc = $(this).attr('src').replace(/(.*)(\.(gif|jpg|jpeg|png)+)/, '$1_on$2');
				$.data(this, 'outsrc', outsrc);
				$.data(this, 'onsrc', onsrc);
				$(this).hover(
					function() {
						$(this).attr('src', $.data(this, 'onsrc'));
					},
					function() {
						$(this).attr('src', $.data(this, 'outsrc'));
					}
				);
				$(this).parent('a').find('span').hover(
					function(_e) {
						_e.preventDefault();
						$(this).parent('a').find('img.hover').trigger('mouseover');
					},
					function(_e) {
						_e.preventDefault();
						$(this).parent('a').find('img.hover').trigger('mouseout');
					}
				);
				$(this).parent('a').click(function() {
					$('img', this).trigger('mouseout');
					$('span', this).trigger('mouseout');
				});
				//画像プリロード
				$('#preloader').append('<img src="' + onsrc + '" alt="" />');
			}
		});
	};
})(jQuery);

/* 21の秘密ポップアップ */
function popup() {
	$('#sidebar-secrets21').click(function() {
		var newwin = window.open(this.href, '21secrets', 'width=920,height=528,resizable=yes,status=yes,location=yes,toolbar=no,titlebar=yes,scrollbars=yes');
		newwin.focus();
		return false;
	});
}

/* サイドバー現在地表示 */
function sidebar_active() {
	var current = location.pathname.replace('index.html');
	$('#sidebar a').each(function() {
		var href = $(this).attr('href');
		href = href.replace(location.domain, '');
		href = href.replace('://', '');
		href = href.replace('index.html', '');
		if(current.indexOf(href) !== -1) {
			var newsrc = $.data($('img.hover', this).get(0), 'onsrc');
			$('img.hover', this)
				.attr('src', newsrc)
				.unbind('mouseover, mouseout');
		}
	});
}

$(function() {
	$('a.hover, img.hover').jshover();
	popup();
	sidebar_active();
});

