/*
 * Copyright (C) 2007 ichi
 *
 * MIT License
 * http://www.opensource.org/licenses/mit-license.php
 */


/*いろいろ関数。パクリだらけ*/
myFunc = {
	
	//URI関連
	URI: function(url){
		this.originalPath = url;
		
		//絶対パスを取得
		this.getAbsolutePath = function(path){
			var img = new Image();
			img.src = path;
			path = img.src;
			img.src = '#';
			return path;
		};
	
		this.absolutePath = this.getAbsolutePath(url);
	
		//同じ文書にリンクしているかどうか
		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;
		}
	},
	
	
	//日付関連
	date: function(d){
		this.d = d ? new Date(d) : new Date();
		yy = this.d.getYear();
		mm = this.d.getMonth() + 1;
		dd = this.d.getDate();
		
		this.year = yy < 2000 ? yy + 1900 : yy;
		this.month = mm < 10 ? mm = "0" + mm : mm;
		this.day = dd <10 ? dd = "0" + dd : dd;
		
		this.date = function(sep){
			sep = sep || "";
			return this.year + sep + this.month + sep + this.day;
		};
	}
	
};


//MSIE判別（簡易）
var isMSIE = /*@cc_on!@*/false; 


//ポップアップウィンドウ
function popup(file, name, option){
	var defaultOp = "location=no,menubar=no,status=no,toolbar=no";
	var opAfter = defaultOp ? defaultOp + "," : "";
	
	if(option){
		var opArr2 = [];
		
		opArr = option.split(",");
		for(i in opArr){
			opArr2[i] = opArr[i].split("=");
			//スクロールするかどうかチェック
			if(opArr2[i][0] == "scrollbars")
				var doScroll = opArr2[i][1] == "yes" ? true : false;
		}
		
		//数値修正
		for(i in opArr2){
			switch(opArr2[i][0]){
				case "width":
					opArr2[i][1] -= 0;
					opArr2[i][1] += doScroll ? 15 : 0;
					break;
				case "height":
					opArr2[i][1] -= 0;
					opArr2[i][1] += doScroll ? 20 : 0;
					break;
			}
			opArr2[i] = opArr2[i][0] + "=" + opArr2[i][1];
			if(i == 0){
				opAfter = opArr2[i];
			}else{
				opAfter += "," + opArr2[i];
			}
		}
	}
	
	window.open(file, name, opAfter);
}

//flash表示
function swf(path, w, h, flashvars){
	document.open();
	document.writeln('<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'
		+' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,19,0"'
		+' width="'+ w +'"'
		+' height="'+ h +'">');
	document.writeln('<param name="movie" value="'+ path +'" />');
	document.writeln('<param name="quality" value="high" />');
	if(flashvars){
		document.writeln('<param name="FlashVars" value="'+ flashvars +'" />');
	}
	
	
	embedFlashvars = flashvars ? ' FlashVars="'+ flashvars +'"' : "" ;
	document.writeln('<embed'
	+' pluginspage="http://www.macromedia.com/go/getflashplayer"'
	+' type="application/x-shockwave-flash"'
	+' src="'+ path +'"'
	+' width="'+ w +'"'
	+' height="'+ h +'"'
	+embedFlashvars
	+' quality="high"'
	+'></embed>');
	document.writeln('</object>');
	document.close();
}


//jQuery dump関数
function jquery_dump($obj) {
	var dumphtml = [];
	if($.browser.msie) {
		for(var i = 0; i < $obj.length; i++) {
			dumphtml.push('[' + i + '] ');
			dumphtml.push($obj[i].outerHTML.replace(/^[\r\n\t]+/, ''));
			dumphtml.push("\n");
		}
	} else {
		for(var i = 0; i < $obj.length; i++) {
			dumphtml.push('[' + i + '] '
			+ '<' + $obj[i].nodeName.toLowerCase());
			for(var j = 0; j < $obj[i].attributes.length; j++) {
				dumphtml.push(' ' + $obj[i].attributes[j].nodeName + '="'
				+ $obj[i].attributes[j].nodeValue + '"');
			}
			dumphtml.push('>' + $obj[i].innerHTML);
			dumphtml.push('<\/' + $obj[i].nodeName.toLowerCase() + '>');
			dumphtml.push("\n");
		}
	}
	alert(dumphtml.join(''));
}

/*** 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);


