/**
 *jquery.fun.js
 *頻繁に使われるJS処理をまとめたもの
 *author: actcube
 *update: 09/12/7
 *
 *ロールオーバー
 * jQuery(element).rollover(onName);
 *  @onName: マウスオーバー時src末尾につけるテキスト default="_on"
 *
 *要素の高さ・幅を揃える
 * jQuery(element).sync(direction);
 *  @direction: "auto","width","height" 揃える属性(autoはheightとwidth両方) default="auto"
 *
 *スムーススクロール
 * jQuery(element).smoothScroll(target,time);
 *  @target: スクロール終点要素 default="html,body"
 *  @time: スクロール時間 default=500
 *
 *フォローメニュー
 * jQuery(element).follow(time);
 *  @time: フォロー速度 default=0
 
 *ドロップダウン
 * $(element).dropDown(time);
 *  @time: default=250
 **/
 
 //mootoolsとの共存
jQuery.noConflict();

var onNameStr;
jQuery.fn.rollover = function(onName) {
	if(!onName) {
		onName = "_on";
	}
	onNameStr = onName;
	jQuery.each(jQuery(this),function() {
		var tagName = this.tagName.toLowerCase();
		jQuery(this).unbind("mouseover");
		jQuery(this).unbind("mouseout");
		//
		if (tagName == 'img'||tagName == 'input') {
			jQuery(this).bind("mouseover",function() {
				rover(jQuery(this));
			});
			
			jQuery(this).bind("mouseout",function() {
				rover(jQuery(this));
			});
		} else {
			var img = jQuery(this).find('img,input');
			jQuery(this).bind("mouseover",function(){
				rover(img);
			});
			jQuery(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);
}

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

jQuery.fn.smoothScroll = function(target,time) {
	if(!target) {
		target='html,body';
	}
	if(!time) {
		time = 500;
	}
	jQuery(this).click(function () {
		var val = 0;
		if (target != 'html,body') {
			val = jQuery(target).offset().top;
			if (val != 0) {
				val -= jQuery(target).height();
			}
		}
		jQuery('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;
};

jQuery.fn.follow = function(time) {
	if (!time) {
		time = 500;
	}
	var follower = jQuery(this);
	var parent = follower.parent();
	var padding = follower.css("paddingTop");
	padding = padding.replace("px","");
	var maxHeight = parent.height()-follower.height();
	jQuery(function() {
		following(follower,parent,padding,maxHeight,time);
	});
	jQuery(window).scroll(function () {
		following(follower,parent,padding,maxHeight,time);
	});
}

function following(follower,parent,padding,maxHeight,time) {
	follower.stop();
	if (jQuery(window).height() >= follower.height()) {
		var st = jQuery(window).scrollTop();
		var t = parent.offset().top;
		var val = 0;
		if (st-t > 0) {
			val = st - t;
		}
		if (st-t > maxHeight-padding*6) {
			val = maxHeight-padding*6;
		}
		follower.animate({top:val},time, 'quart');
	} else {
		follower.css("top",0);
	}
}
//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;
		},
		easeInOutSine: function (x, t, b, c, d) {
			return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
		}
	}
);
//dropDown
jQuery.fn.dropDown = function(time) {
	if (!time) {
		time = 250;
	}
	jQuery(this).hover(function() {
		jQuery(this).find(".drop").slideDown(200);
		var timer = jQuery(this).data("timer");
		if (timer) {
			clearTimeout(timer);
		}
	},function() {
		var target = this;
		jQuery(this).data("timer",setTimeout(function(){
			jQuery(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;
		}
	}
);
