/**
 *jquery.fun.js
 *頻繁に使われるJS処理をまとめたもの
 *author: yuu
 *update: 09/12/7
 *
 *ロールオーバー
 * $(element).rollover(onName);
 *  @onName: マウスオーバー時src末尾につけるテキスト default="_on"
 *
 *要素の高さ・幅を揃える
 * $(element).sync(direction);
 *  @direction: "auto","width","height" 揃える属性(autoはheightとwidth両方) default="auto"
 *
 *スムーススクロール
 * $(element).smoothScroll(target);
 *  @target: スクロール終点要素 default="html,body"
 *
 **/

var onNameStr;
$.fn.rollover = function(onName) {
	if(!onName) {
		onName = "_on";
	}
	onNameStr = onName;
	jQuery.each($(this),function() {
		var tagName = this.tagName.toLowerCase();
		if (tagName == 'img') {
			$(this).hover(function() {
				rover($(this));
			},
			function() {
				rover($(this));
			});
		} else {
			var img = $(this).find('img');
			$(this).hover(function(){
				rover(img);
			},
			function() {
				rover(img);
			});
		}
	});
}

function getExt(str) {
	return str.match(/\.\w+$/);
}

function geteName(str,ext){
	var string = str.substring(str.lastIndexOf("/")+1);
	if (!ext) {
		string = str.substring(0,str.lastIndexOf("."));
	}
	return string;
}

function rover(jElm) {
	var src = jElm.attr("src");
	var fname = geteName(src);
	var ext = getExt(src);
	if (fname.indexOf(onNameStr) == -1) {
		fname += onNameStr;
	} else {
		fname = fname.replace(onNameStr,"");
	}
	//src = src.substr(0,src.lastIndexOf('/')+1);
	jElm.attr("src",fname+ext);
}

$.fn.sync = function(direction) {
	if (!direction) {
		direction = "auto";
	}
	var maxHeight = -1;
	var maxWidth = -1;
	jQuery.each($(this),function(i) {
		if (direction == "auto" || direction == "height") {
			$(this).css("height","");
			if(maxHeight < $(this).height()) {
				maxHeight = $(this).height();
			}
		}
		if (direction == "auto" || direction == "width") {
			$(this).css("width","");
			if(maxWidth < $(this).width()) {
				maxWidth = $(this).width();
			}
		}
	});
	if (maxHeight != 0) {
		$(this).css("height",maxHeight);
	}
	if (maxWidth != 0) {
		$(this).css("width",maxWidth);
	}
}

$.fn.smoothScroll = function(target,time) {
	if(!target) {
		target='html,body';
	}
	if(!time) {
		time = 500;
	}
	$(this).click(function () {
		var top = $(target).offset().top;
		if (top != 0) {
			$("html,body").animate({ scrollTop: top }, time, 'quart');
		}
		return false;
	});
}
jQuery.easing.quart = function (x, t, b, c, d) {
	return -c * ((t=t/d-1)*t*t*t - 1) + b;
};
