/*
 * Copyright (C) 2007 ichi
 *
 * MIT License
 * http://www.opensource.org/licenses/mit-license.php
 */


/*いろいろ関数。パクリだらけ*/
myFunc = {
	
	//URI関連
	URI: function(arg){
		this.originalPath = arg;
		
		//絶対パスを取得
		this.getAbsolutePath = function(path){
			var img = new Image();
			img.src = path;
			path = img.src;
			img.src = '#';
			return path;
		};
	
		this.absolutePath = this.getAbsolutePath(arg);
	
		//同じ文書にリンクしているかどうか
		this.isSelfLink = (this.absolutePath == location.href);
	},
	
	//preLoad
	preLoad: {
		preLoadImg: [],
		load: function(path){
			var pImg = this.preLoadImg;
			var l = pImg.length;
			pImg[l] = new Image;
			pImg[l].src = path;
		}
	}
};




/*** imgover ***
■解説
[対象Object]内にあるリンク画像(<a>タグに囲まれている<img>)の
マウスオーバー時に画像が「画像名_[引数:postfix].拡張子」に切り替わる。（foo.gif→foo_o.gif）
もちろんマウスアウトで元の画像に戻る。
リンクでは無い画像や、class="[引数:exclude]"を付与された画像ではマウスオーバー時の切替しない。

[引数:isSelf]をtrueにしたら、現在ページとリンク先が同じ場合に、
・画像を「画像名_[引数:aPostfix].拡張子」にする。
・classに[引数:exclude]を付与。（[引数:isSelfOver]がfalseの場合のみ）

■例
<div class="imgover">
	<a><img src="foo.gif"></a>　←反応する
	<a><img src="bar.gif"></a>　←反応する
	<a><img src="foobar.gif" class="imgactive"></a>　←反応しない
	<img src="foo2.gif">　←反応しない
</div>

■引数
.imgover({
	exclude: "imgactive",
	postfix: "o",
	aPostfix: "", //指定なしならpostfixと同じ
	isSelf: false,
	isSelfOver: false
});
*/
(function(){
	jQuery.fn.imgover = function(config){
		
		config = jQuery.extend({
			exclude: "imgactive",
			postfix: "o",
			aPostfix: "",
			isSelf: false,
			isSelfOver: false
		},config);
		
		var tObj = this;
		tObj.find("a").each(function(){
			
			//自身のページかチェックする？
			if(config.isSelf){
				var href = new myFunc.URI(jQuery(this).attr("href"));
			}
			
			jQuery(this).find("img").each(function(){
				var src = jQuery(this).attr("src");
				var ftype = src.substring(src.lastIndexOf('.'), src.length);
				
				//自身のページだったら
				if(href && href.isSelfLink){
					config.aPostfix = config.aPostfix ? config.aPostfix : config.postfix ;
					
					var asrc = src.replace(ftype, "_"+config.aPostfix + ftype);
					jQuery(this).attr("src", asrc);
					src = jQuery(this).attr("src");
					
					//自身の場合のマウスオーバー可否
					if(!config.isSelfOver){
						jQuery(this).addClass(config.exclude);
					}
				}
				
				var hsrc = src.replace(ftype, "_"+config.postfix + ftype);
				jQuery(this).attr("hsrc", hsrc);
				
				myFunc.preLoad.load(hsrc);
				
				jQuery(this).filter("img:not(."+ config.exclude +")")
					.hover(function(){
						jQuery(this).attr("src", jQuery(this).attr("hsrc"));
					},function(){
						jQuery(this).attr("src", jQuery(this).attr("src").replace("_"+config.postfix + ftype, ftype));
					});
			});
		});
	};
})(jQuery);


/*** 画像切替 ***
■解説
[対象Object]のリンクにマウスオーバーで[引数:targetAttr]=""に指定したIDの画像が
[引数:targetAttr]=""が指定されてなかったら自分自身の画像が
「画像名_[引数:postfix].拡張子」に切り替わる。（foo.gif→foo_o.gif）
もちろんマウスアウトで元の画像に戻るよ。

■例
ターゲットが別オブジェクトの場合
<a class="chimg" chimg="hogehoge">text</a>
<img src="foo.gif" id="hogehoge">

ターゲットがオブジェクト自身の場合
<img src="hogehoge.gif" class="chimg" />
<input type="image" src="hogehoge.gif" class="chimg" />

<a href="" chimg="hogehoge"><img src="hogehoge.gif" id="hogehoge" /></a>

■引数
.chimg({
	postfix: "o",
	targetAttr: "chimg"
});
*/
(function(){
	jQuery.fn.chimg = function(config){
		
		config = jQuery.extend({
			postfix: "o",
			targetAttr: "chimg"
		},config);
		
		var tObj = this;
		tObj.each(function(){
			var chimg = jQuery(this).attr(config.targetAttr);
			var target = (chimg == null) ? jQuery(this) : jQuery("#"+chimg) ;
			
			//postfix属性があれば
			var postfix = jQuery(this).attr("postfix");
			var postfix = (postfix == null) ? config.postfix : postfix ;
			
			var src = target.attr("src");
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var hsrc = src.replace(ftype, "_"+postfix+ftype);
			target.attr("hsrc", hsrc);
			
			myFunc.preLoad.load(hsrc);
			
			
			jQuery(this).hover(function(){
				target.attr("src", target.attr("hsrc"));
			},function(){
				target.attr("src", target.attr("src").replace("_"+postfix+ftype, ftype));
			});
		});
	};
})(jQuery);


/*** tableColor ***
■解説
[対象Object]内のtrかliタグに交互に
class="[引数:oddClass]"とclass="[引数:evenClass]"を付与する。
cssで色をつければ交互色に。

■例
<table class="coloredRow">
	<tr class="odd">
		<th></th><td></td>
	</tr>
	<tr class="even">
		<th></th><td></td>
	</tr>
	<tr class="odd">
		<th></th><td></td>
	</tr>
</table>

■TODO
ul,ol,dlあたりでもできるように。
	→liできたよー。

■引数
.alternate({
	evenClass: "even",
	oddClass: "odd",
	onlyTbody: true
});
*/
(function(){
	jQuery.fn.alternate = function(config){
		
		config = jQuery.extend({
			evenClass: "even",
			oddClass: "odd",
			onlyTbody: true
		},config);
		
		var tObj = this;
		
		var tTable = config.onlyTbody ? tObj.filter("table").find("tbody") : tObj.filter("table") ;
		tTable.each(function(){
			jQuery(this).find("tr:odd").addClass(config.oddClass);
			jQuery(this).find("tr:even").addClass(config.evenClass);
		}).end();
		
		tObj.filter("ul,ol").each(function(){
			jQuery(this).find("li:odd").addClass(config.oddClass);
			jQuery(this).find("li:even").addClass(config.evenClass);
		}).end();
	};
})(jQuery);


