/**
 *jquery.fun.js
 *頻繁に使われるJS処理をまとめたもの
 *author: actcube
 *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,time);
 *  @target: スクロール終点要素 default="html,body"
 *  @time: スクロール時間 default=500
 *
 *ドロップダウン
 * $(element).dropDown(time);
 *  @time: default=250
 **/

var onNameStr;
$.fn.rollover = function(onName) {
	if(!onName) {
		onName = "_on";
	}
	onNameStr = onName;
	jQuery.each($(this),function() {
		var tagName = this.tagName.toLowerCase();
		$(this).unbind("mouseover");
		$(this).unbind("mouseout");
		//
		if (tagName == 'img'||tagName == 'input') {
			$(this).bind("mouseover",function() {
				rover($(this));
			});
			
			$(this).bind("mouseout",function() {
				rover($(this));
			});
		} else {
			var img = $(this).find('img,input');
			$(this).bind("mouseover",function(){
				rover(img);
			});
			$(this).bind("mouseout",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 val = 0;
		if (target != 'html,body') {
			val = $(target).offset().top;
			if (val != 0) {
				val -= $(target).height();
			}
		}
		$('html,body').animate({ scrollTop:val  }, time, 'quart');
		return false;
	});
}
jQuery.easing.quart = function (x, t, b, c, d) {
	return -c * ((t=t/d-1)*t*t*t - 1) + b;
};

//dropDown
$.fn.dropDown = function(time) {
	if (!time) {
		time = 250;
	}
	/*
	function dropMove(target){
		$(target).find(".drop").animate(
			{
				height:"toggle",
				opacity:"toggle"
			},"fast","easeOutCirc"
		);
	}
	*/
	$(this).hover(function() {
		//dropMove(this);
		$(this).find(".drop").slideDown(200);
		var timer = $(this).data("timer");
		if (timer) {
			clearTimeout(timer);
		}
	},function() {
		var target = this;
		$(this).data("timer",setTimeout(function(){
			//dropMove(target);
			$(target).find(".drop").slideUp(200);
		},250));
	});
};
//easing dropDown
jQuery.extend(jQuery.easing,
	{
		easeInQuart: function (x, t, b, c, d) {
			return c*(t/=d)*t*t*t + b;
		},
		easeOutQuart: function (x, t, b, c, d) {
			return -c * ((t=t/d-1)*t*t*t - 1) + b;
		},
		easeOutExpo: function (x, t, b, c, d) {
			return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
		},
		easeOutCirc: function (x, t, b, c, d) {
			return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
		}
	}
);
