/*-------------------------------------------------------------------- 
 * JQuery Plugin : "ReplaceImg"
 *
 * version 1.0
 *
 * Author : Hiromu Hasegawa
 * Copyright (c) 2008 THE HAM MEDIA (http://h2ham.seesaa.net)
 * Licensed under the MIT License:
 * http://www.opensource.org/licenses/mit-license.php
 *
 * Since:     2008-12-03
 *
 * jQuery 1.2.6
--------------------------------------------------------------------*/

(function($){

//画像置換（HTML）
$.fn.replaceImg = function(src,settings) {
	c = $.extend({
		selector: this,
		rollover: '.over',
		postfix:'_on',
		borderSize:0,
		imgTitle:true
	}, settings);
		
	$(c.selector).each(function(){
		var imgAlt = imgtitle = (c.imgTitle)? $(this).text() : '';
		$(this).html('<img src="'+src+'" alt="'+imgAlt+'" style="border:'+c.borderSize+'" />')
			.attr('title',imgtitle);
	});

	$(c.selector).filter(c.rollover).children().each(function(){
		//srcセット
		$(this).addClass('overImg').css({border:c.borderSize});
	});

	$('.overImg').each(function(){
		rolloverSrc = $(this).attr('src').replace(/(\.gif|\.jpg|\.png)$/, c.postfix+"$1");

		//イメージセット
		$(this).clone().insertAfter(this)
			.attr('src',rolloverSrc)
			.css('display','none');

		//ロールオーバー(マウスオン時)
		$(this).mouseover(function(){
			$(this).hide();
			$(this).next().show();
		});

		//ロールオーバー(マウスアウト時)
		$(this).next().mouseout(function(){
			$(this).hide();
			$(this).prev().show();
		});
	});
};

//画像置換（CSS background-image）
$.fn.replaceBgImg = function(url,width,height,settings) {
	c = $.extend({
		selector: this,
		rollover: '.over',
		postfix:'_on',
		borderSize:0,
		bgdisplay:'block',
		bgposition:'0 0',
		bgrepeat:'no-repeat',
		imgTitle:true
	}, settings);
		
	$(c.selector).each(function(){
		var imgtitle  = (c.imgTitle)? $(this).text() : '';
		$(this).css({
			display:c.bgdisplay,
			width:width,
			height:height,
			backgroundImage:'url('+url+')',
			backgroundPosition:c.bgposition,
			backgroundRepeat:c.bgrepeat
		}).text('').attr('title',imgtitle);
		
		rolloverUrl = url.replace(/(\.gif|\.jpg|\.png)$/, c.postfix+"$1");
		
		//イメージセット
		$(this).filter(c.rollover).clone().insertAfter(this)
		.text(imgtitle)
			.css({
				backgroundImage:'url('+rolloverUrl+')',
				display:'none'
			});
		
		//ロールオーバー(マウスオン時)
		$(this).filter(c.rollover).mouseover(function(){
			$(this).hide();
			$(this).next().show().css({display:'block'}).text('');
		});
		//ロールオーバー(マウスアウト時)
		$(this).filter(c.rollover).next().mouseout(function(){
			$(this).hide();
			$(this).prev().show();
		});
	});
};
})(jQuery);