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

    画像切り替え
    フッター下付け処理
    画像スライダー

----------------------------------------------------------------*/

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:  "-o"
        }, 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.footerBottom = function(options){

        return this.each(function(){
            if (!this) return false;
            var maxh  = $(window).height();
            var thish = $(this).innerHeight();
            var thisw = $(this).innerWidth();
            if(maxh > thish) {
                $("#main").css("height", maxh + "px");
                $("#footer").css({
                    position: "absolute",
                    bottom: "0px",
                    left: "0px",
                    width: thisw + "px"
                })
            }
        });
        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).hover(
						function(){ $(this).stop().animate({ opacity: 0 }, 300 ); },
						function(){ $(this).stop().animate({ opacity: 1 }, 300 ); }
					);
				}
				else {
					$(this).css({ opacity: 0 });
					$(this).hover(
						function(){ $(this).stop().animate({ opacity: 1 }, 300 ); },
						function(){ $(this).stop().animate({ opacity: 0 }, 300 ); }
					);
				}
		});
		//return this;
	}

/*----------------------------------------------------------------
    ページスクロール
----------------------------------------------------------------*/
	$.fn.scrollPage = function (){
		var _url  = location.href.split('#')[0];
		var _hash = location.hash;
		$('a', this ).each(function() {
			if (this.href.indexOf(_url + '#') == 0) {
				var _id = this.href.split('#')[1];
				$(this).click(function(){
						var _top = $('#' + _id).offset().top;
						$($.browser.safari?'body':'html').animate({scrollTop:_top-20},1500,'easeInOutExpo');
				});
	
				if(!($.browser.msie && $.browser.version==6))
					$(this).removeAttr('href');
				$(this).css('cursor','pointer');
			}
	
		});
		if(_hash != ('undefined'|''|null)){
			var _target = $(_hash);
			if(_target.length){
				var _top = _target.offset().top;
				$($.browser.safari?'body':'html').animate({scrollTop:_top-20},1500,'easeInOutExpo');
			}
		}
	}


/*----------------------------------------------------------------
    プルダウンメニュー
----------------------------------------------------------------*/
	$.fn.menudown = function(options){
		var md = $.extend({
			target: ".subnavi"
		}, options);
		return this.each(function(){
			if (!this) return false;
			var h = $(this).parent().find(md.target).height();
			$(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.footNavi = function(options){
        return this.each(function(){
            if (!this) return false;
            var t = $(this);
            var s = t.offset().top;
            var h = t.innerHeight();
            var bottom_flag = false;
            t.hide();
            $(window).scroll(function () {
                var document_y = document.documentElement.scrollHeight || document.body.scrollHeight;
                var scroll_y = document.documentElement.scrollTop || document.body.scrollTop;
                var window_y = 0;
                var isSafari = (navigator.appVersion.toLowerCase().indexOf('safari')+1?1:0);
                var isOpera = (navigator.userAgent.toLowerCase().indexOf('opera')+1?1:0);
                if (isOpera) isIE = false;
                if (!isSafari && !isOpera) {
                    window_y = document.documentElement.clientHeight || document.body.clientHeight || document.body.scrollHeight;
                } else {
                    window_y = window.innerHeight;
                }
                if(bottom_flag && (document_y > scroll_y + window_y + h)){
                    t.slideUp('slow');
                    bottom_flag = false;
                }
                if(bottom_flag){
                    return;
                }
                if(document_y < scroll_y + window_y + 1){
                    t.slideDown('slow');
                    bottom_flag = true;
                    $($.browser.safari?'body':'html').stop().animate({ scrollTop: s }, 'slow' );
                }
            });
        });
        return this;
    }

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

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


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

/*----------------------------------------------------------------
    クリック画像切り替え
----------------------------------------------------------------*/
	$.fn.clickimg = function(options){
		var ci = $.extend({
			target: "",
			w: "320px",
			h: "245px"
		}, options);

		return this.each(function(){
            if (!this) return false;
            $(this).click(function(){
                var img = $(this).attr("href");
				var target = $(ci.target);
				target.append('<img src="' + img + '" width="' + ci.w + '" height="' + ci.h + '" />').children()
				.css({
					position: "absolute",
					opacity: 0
				}).stop().animate({ opacity: 1}, {duration: "slow", easing: "linear"}).prev().prev().remove();
				
                return false;
            });
        });
        return this;
	}

})(jQuery);
