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

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

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

(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.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);
