/*----------------------------------------------------------------
	各種機能プラグイン
----------------------------------------------------------------*/

jQuery.extend( jQuery.easing,
{
	def: 'easeOutExpo',
	easeInExpo: function (x, t, b, c, d) {
		return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
	},
	easeOutExpo: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	},
	easeInOutExpo: function (x, t, b, c, d) {
		if (t==0) return b;
		if (t==d) return b+c;
		if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
		return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
	}
});

(function($){
/*----------------------------------------------------------------
	画像切り替え
----------------------------------------------------------------*/
	$.fn.imgover = function(options){
		//初期値設定
		var m = $.extend({
			name:  "_over"
		}, options);
		return this.each(function(){
			if (!this) return false;
			//ファイル名取得
			var src	= $(this).attr("src");
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var hsrc  = src.replace(ftype, m.name + ftype);
			//オーバー画像読込み
			var img = new Image();
			img.src = hsrc;
			//ロールオーバー処理
			$(this).hover(function (){
				$(this).attr("src",hsrc);
			}, function(){
				$(this).attr("src",src);
			});
		});
		return this;
	}

/*----------------------------------------------------------------
	マウスオーバー透過
----------------------------------------------------------------*/
	$.fn.imghover = function () {
		return this.each(function(){
			$(this).hover(
				function(){ $(this).stop().animate({ opacity: 0.7 }, 200 ); },
				function(){ $(this).stop().animate({ opacity: 1 }, 200 ); }
			);
		});
	}


/*----------------------------------------------------------------
	イメージ透過切り替え
----------------------------------------------------------------*/
	$.fn.imgchange = function(options){
		var m = $.extend({
			name:  "-o",
			reverse: false
		}, options);
		return this.each(function(){
			if (!this) return false;
			var src	 = $(this).attr("src");
			var hheight = $(this).attr("height");
			var hwidth  = $(this).attr("width");
			var ftype = src.substring(src.lastIndexOf('.'), src.length);
			var hsrc  = src.replace(ftype, m.name + ftype);
			var img = new Image();
			img.src = hsrc;
			if(!hheight)
				hheight = img.height;
			if(!hwidth)
				hwidth  = img.width;
			$(this).parent()
			.css({
				display: 'block',
				backgroundImage: 'url(' + img.src + ')',
				backgroundRepeat: 'no-repeat',
				//margin: 0,
				padding: 0,
				lineHeight: 0,
				fontSize: 0,
				width: hwidth + 'px',
				height: hheight + 'px'
			});
			if(!m.reverse){
					$(this).parent().parent().hover(
						function(){ $(this).children("a").children("img").stop().animate({ opacity: 0 }, 300 ); },
						function(){ $(this).children("a").children("img").stop().animate({ opacity: 1 }, 300 ); }
					);
				}
				else {
					$(this).parent().parent().css({ opacity: 0 });
					$(this).hover(
						function(){ $(this).children("a").children("img").stop().animate({ opacity: 1 }, 300 ); },
						function(){ $(this).children("a").children("img").stop().animate({ opacity: 0 }, 300 ); }
					);
				}
		});
		//return this;
	}


/*----------------------------------------------------------------
	プルダウンメニュー
----------------------------------------------------------------*/
	$.fn.menudown = function(options){
		var md = $.extend({
			target: ".downnavi",
			name: "-o"
		}, options);
		return this.each(function(){
			if (!this) return false;
			var h = $(this).parent().find(md.target).innerHeight();
			$(this).next().css({
				height: "0px",
				display: "none"
			});
			$(this).parent().hover(function(){
				$(this).find(md.target).css("display","block").stop().animate({height: h + "px"},300,'linear');
			},function(){
				$(this).find(md.target).stop().animate({height: "0px"},{ duration: 300, easing: 'linear', complete: function(){ $(this).css("display", "none")}});
			});
		});
		return this;
	}
	$.fn.menuChild = function(options){
		return this.each(function(){
			if (!this) return false;
			var target = $(this).children("ul");
			var mp = (target.parent().length + 2) * 28;
			var h  = target.children("li").length * 28;
			var w  = "200";
			var mh = mp + h;
			target.css({
				height: "0px",
				width: "0px",
				display: "none"
			});
			$(this).hover(function(){
				$(".downnavi").css({
					width: "400px",
					height: mh + "px"
				});
				target.css("display","block").stop().animate({height: h + "px", width: w + "px"},300,'linear');
			},function(){
				target.stop().animate({height: "0px", width: "0px"},{ duration: 300, easing: 'linear', complete: function(){ target.css("display", "none");}});
			});
		});
		return this;
	}


/*----------------------------------------------------------------
	ニューススクロール
----------------------------------------------------------------*/
	$.fn.newscroll = function(options){
		var ns = $.extend({
			speed: 500,
			loop:  10000
		}, options);
	
		return this.each(function(){
			if (!this) return false;
			var list = $(this).find("li");
			var max  = list.length - 1;
			count    = 0;

			//CSS初期設定
			list.css({
				position: "absolute",
				left: "0px",
				top: "20px",
				opacity: 0,
				display: "none"
			});
			list.eq(count).css({
				top: "0px",
				opacity: 1,
				display: "block"
			});
			setInterval(function(){
				if(max) {
					list.eq(count).stop().animate({ top: "-20px", opacity: 0},{ duration: ns.speed, easing: "linear", complete: function(){ $(this).css("display", "none");}});
					(count < max) ? count++ : count = 0;
					list.eq(count).css({ top: "20px"});
					list.eq(count).stop().css("display", "block").animate({ top: "0px", opacity: 1},{ duration: ns.speed, easing: "linear"});
				}
			}, ns.loop);
		});
	}

/*----------------------------------------------------------------
	アコーディオン
----------------------------------------------------------------*/
	$.fn.accordion = function(options){
		return this.each(function(){
			if (!this) return false;
			var target = $(this);
			target.next().hide();
			$(".current").find("ul").slideDown();
			target.click(function(){
				target.next().slideUp();
				if ($(this).next().is(":hidden")) {
					$(this).next().slideDown();
				}
				return false;
			});
		});
		return this;
	}

})(jQuery);
