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

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.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: ".downnavi"
		}, 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.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;
	}


/*----------------------------------------------------------------
    画像フェードイン・フェードアウト
----------------------------------------------------------------*/
    $.fn.imgThumShow = function(options) {
        var its = $.extend({
             img:   "#productImg img",
             thum:  ".thum a",
             img_w: 550,
             img_h: 398,
             speed: "slow",
             thum_o: "0.8",
             load:  "/site/img/works/loading.gif",
             load_w: 45,
             load_h: 45
        }, options);

        return this.each(function(){
            if (!this) return false;

            //初期設定
            var img  = $(its.img);
            var thum = $(its.thum);
            img.parent().css({
                width:  its.img_w + "px",
                height: its.img_h + "px",
                position: "relative",
                overflow: "hidden"
            });

            //サムネイルホバー
            thum.hover(function(){
                $(this).children().animate({ opacity: its.thum_o},{ duration: its.speed});
            }, function(){
                $(this).children().animate({ opacity: "1"},{ duration: its.speed});
            });

            //クリック後の処理
            thum.click(function(){
                var url  = $(this).attr("href");
                var thum = url  +  "&dummy=" + (new Date()).getTime();
                var loading = its.load;
                loading = loading + "?t=" + (new Date()).getTime();
                //ローカル確認用
                //var thum    = url;
                //var loading = its.load;
                var thumImg = new Image();
                thumImg.src = thum;

                //フェードアウト・ローディング画像に切り替え
                img.animate({
                    opacity: "0"
                },{
                    duration: its.speed, easing: "linear",
                    complete: function(){
                        img.css({
                            position: "absolute",
                            top:        "50%",
                            left:       "50%",
                            marginLeft: - (its.load_w / 2) + "px",
                            marginTop:  - (its.load_h / 2) + "px"
                        }).attr("src", loading).animate({
                            opacity: "1"
                        },{
                            duration: its.speed, easing: "linear"
                        });
                    }
                });

                //読み込み処理・フェードイン
                thumImg.onload = function() {
                    img.animate({
                        opacity: "0"
                    },{
                        duration: its.speed, easing: "linear",
                        complete: function(){
                            img.css({
                                top: "5px",
                                left: "5px",
                                marginLeft: "0px",
                                marginTop: "0px"
                            }).attr("src", thum).animate({
                                opacity: "1"
                            },{
                                duration: its.speed, easing: "linear"
                            });
                        }
                    });
                }
                return false;
            });
        });
        return this;
    }



})(jQuery);
