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

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

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

(function($){
/*----------------------------------------------------------------
    画像切り替え
----------------------------------------------------------------*/
    $.fn.imgchange = 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.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 + "px",
                            marginTop:  - its.load_h + "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: "0px",
                                left: "0px",
                                marginLeft: "0px",
                                marginTop: "0px"
                            }).attr("src", thum).animate({
                                opacity: "1"
                            },{
                                duration: its.speed, easing: "linear"
                            });
                        }
                    });
                }
                return false;
            });
        });
        return this;
    }


/*----------------------------------------------------------------
    画像スライダー
----------------------------------------------------------------*/
    $.fn.imgsliding = function(options){
        //初期値設定
        var s = $.extend({
            parent: "#inner ul",
            item:   "#inner li",
            next:   "#next",
            prev:   "#prev",
            start:  0,
            number: 5,
            speed:  500
        }, options);

        return this.each(function(){
            if (!this) return false;
            //初期値設定
            var itemBox  = $(s.item).innerWidth();
            var itemh    = $(s.item).innerHeight();
            var itemNum  = $(s.item).length;
            var maxWidth = itemBox * itemNum;
            var maxLeft  = - (maxWidth - ( itemBox * s.number ));
            var box      = $(s.parent);
            box.css({
                position: "absolute",
                left: s.start + "px",
                top: "0px",
                width: maxWidth + "px"
            }).parent().css("height", itemh + "px");
            //前へ戻るアニメーション処理
            $(s.prev).click(function(){
                var position = box.css("left").substring(0, box.css("left").length - 2);
                if(position >= s.start) {
                    var moveLeft = maxLeft + "px";
                } else {
                    if(position < maxLeft) {
                        var moveLeft = Number(position) + (itemBox * s.number) + "px";
                    } else {
                        var moveLeft = s.start + "px";
                    }
                }
                box.animate({ left: moveLeft}, { duration: s.speed, easing: "swing"});
            });
            //次へ進むアニメーション処理
            $(s.next).click(function(){
                var position = box.css("left").substring(0, box.css("left").length - 2);
                if( - (itemBox * s.number) < maxLeft && position >= s.start) {
                    var moveLeft = maxLeft + "px";
                } else {
                    if(position <= maxLeft) {
                        var moveLeft = s.start + "px";
                    } else if(Number(position) - (itemBox * s.number) < maxLeft) {
                        var moveLeft = maxLeft + "px";
                    } else {
                        var moveLeft = Number(position) - (itemBox * s.number) + "px";
                    }
                }
                box.animate({ left: moveLeft}, { duration: s.speed, easing: "swing"});
            });
        });
        return this;
    }

})(jQuery);
