//--------------------------------------------------------------------
// JavaScript
// トップページ、カルーセルギャラリーで利用するjavaScript

//--------------------------------------------------------------------
// 変数宣言
var vars = {};
vars.c          = {};
vars.o          = {};
vars.b          = {};
vars.nextspeed  = 800;
vars.prevspeed  = 600;
vars.nexteasing = 'easeOutBounceGC';
vars.preveasing = 'easeInBackGC';
vars.movable    = false;
vars.interval   = 8000;
vars.timer;


//--------------------------------------------------------------------
// イージング追加
// jQuery Easing Pluginより
// http://gsgd.co.uk/sandbox/jquery/easing/
$.extend({easing: {
	easeInQuad: function (x, t, b, c, d) {
		return c*(t/=d)*t + b;
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInOutQuad: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t + b;
		return -c/2 * ((--t)*(t-2) - 1) + b;
	},
	easeInCubic: function (x, t, b, c, d) {
		return c*(t/=d)*t*t + b;
	},
	easeOutCubic: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t + 1) + b;
	},
	easeInOutCubic: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	},
	easeInQuart: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t + b;
	},
	easeOutQuart: function (x, t, b, c, d) {
		return -c * ((t=t/d-1)*t*t*t - 1) + b;
	},
	easeInOutQuart: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
		return -c/2 * ((t-=2)*t*t*t - 2) + b;
	},
	easeInQuint: function (x, t, b, c, d) {
		return c*(t/=d)*t*t*t*t + b;
	},
	easeOutQuint: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t*t*t + 1) + b;
	},
	easeInOutQuint: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
		return c/2*((t-=2)*t*t*t*t + 2) + b;
	},
	easeInSine: function (x, t, b, c, d) {
		return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
	},
	easeOutSine: function (x, t, b, c, d) {
		return c * Math.sin(t/d * (Math.PI/2)) + b;
	},
	easeInOutSine: function (x, t, b, c, d) {
		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
	},
	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;
	},
	easeInCirc: function (x, t, b, c, d) {
		return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
	},
	easeOutCirc: function (x, t, b, c, d) {
		return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
	},
	easeInOutCirc: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
		return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
	},
	easeInElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
	},
	easeOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
	},
	easeInOutElastic: function (x, t, b, c, d) {
		var s=1.70158;var p=0;var a=c;
		if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
		if (a < Math.abs(c)) { a=c; var s=p/4; }
		else var s = p/(2*Math.PI) * Math.asin (c/a);
		if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
		return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
	},
	easeInBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*(t/=d)*t*((s+1)*t - s) + b;
	},
	easeOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
	},
	easeInOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		if (s == undefined) s = 10;
		if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
		return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
	},
	easeInBounce: function (x, t, b, c, d) {
		return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
	},
	easeOutBounce: function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
	},
	easeInOutBounce: function (x, t, b, c, d) {
		if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
		return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
	},

//グルコム追加分
	easeOutExpoGC: function (x, t, b, c, d) {
		return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
	},
	easeOutBounceGC: function (x, t, b, c, d) {
		if ((t/=d) < (1/2.75)) {
			return c*(7.5625*t*t) + b;
		} else if (t < (2/2.75)) {
			return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
		} else if (t < (2.5/2.75)) {
			return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
		} else {
			return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
		}
	},
	easeInBackGC: function (x, t, b, c, d, s) {
		if (s == undefined) s = 4;
		return c*(t/=d)*t*((s+1)*t - s) + b;
	}


}});


//--------------------------------------------------------------------
// ギャラリーオブジェクト

// コンストラクタ
function Carousel(area_id) {
    this.container  = $('<ul></ul>');
    this.sources    = [];
    this.images     = [];
    this.position   = 0;
    this.nextspeed  = vars.nextspeed;
    this.prevspeed  = vars.prevspeed;
    this.nexteasing = vars.nexteasing;
    this.preveasing = vars.preveasing;
    this.movable    = vars.movable;
    this.interval   = vars.interval;

    $('#' + area_id).append(this.container);
}

// 初期化
// ※画像URLをsourcesプロパティに格納してから呼び出す
Carousel.prototype.init = function() {
    var that = this;

    for (i = 0; i < that.sources.length; i += 1) {
        var li = $('<li></li>');
        if (that.anchors[i]) {
            var a = $('<a></a>');
            $(a).attr('href', that.anchors[i]);
            $(a).attr('target', '_blank');
            $(a).append(that.loadImages[i]);
            $(li).append(a);
        }
        else {
            $(li).append(that.loadImages[i]);
        }
        that.images[i] = li;
    }

    $(that.container).append(that.images[that.position]);
    that.allowMove();
};

// 画像送り
Carousel.prototype.next = function(that) {
    if (that.isMovable()) {
        that.denyMove();
        clearInterval(vars.timer);

        var move_start = $(that.container).attr('clientWidth');

        var now_img = that.images[that.position];
        this.position += 1;
        if (that.position >= that.images.length) {
            that.position = 0;
        }
        var next_img = that.images[that.position];
        $(next_img).css({opacity: '1', left: move_start});

        $(that.container).append(next_img);

        $(next_img).animate({left: 0, opacity: '1'}, that.nextspeed, that.nexteasing, function() {
            $(now_img).remove();
            that.allowMove();
            vars.setIntervalTimer();
        });
    }
};

// 画像戻し
Carousel.prototype.prev = function() {
    var that = this;

    if (that.isMovable()) {
        that.denyMove();
        clearInterval(vars.timer);

        var move_end = $(that.container).attr('clientWidth');

        var now_img = that.images[that.position];
        that.position -= 1;
        if (that.position < 0 ) {
            that.position = that.images.length - 1;
        }
        var prev_img = that.images[that.position];
        $(prev_img).css({opacity: '1', left: 0});

        $(that.container).prepend(prev_img);

        $(now_img).animate({left: move_end, opacity: '0'}, that.prevspeed, that.preveasing, function() {
            $(now_img).remove();
            that.allowMove();
            vars.setIntervalTimer();
        });
    }
};

// 動作中判定
Carousel.prototype.isMovable = function() {
    return this.movable;
}

// 動作拒否
Carousel.prototype.denyMove = function() {
    this.movable = false;
}

// 動作許可
Carousel.prototype.allowMove = function() {
    this.movable = true;
}


//--------------------------------------------------------------------
// 画像マーカ

// コンストラクタ
function OrderMarker(area_id) {
    this.container  = $('<ol></ol>');
    this.marker     = $('<div class="marker"></div>');
    this.nextspeed  = vars.nextspeed;
    this.prevspeed  = vars.prevspeed;
    this.nexteasing = vars.nexteasing;
    this.preveasing = vars.preveasing;
    this.movable    = vars.movable;
    this.interval   = vars.interval;

    $('#' + area_id).append(this.marker);
    $('#' + area_id).append(this.container);
}

// 初期化
// ※初期化済みのギャラリーオブジェクトインスタンスを受け取る
OrderMarker.prototype.init = function(carousel) {
    this.carousel = carousel;
    this.num      = carousel.images.length;
    this.start    = 10;
    this.span     = 26;
    this.end      = ((this.num -1) * this.span) + this.start;

    for (i = 0; i < this.num; i += 1) {
        var j = i + 1;
        var li = $('<li>' + j + '</li>');
        $(li).attr('number', i);
        $(li).click(function(ev) {
            var num = $(ev.target).attr('number');
            if (num > vars.c.position) {
                vars.c.position = num - 1;
                vars.c.next(vars.c);
                vars.o.next(vars.o);
                return false;
            }
            else if (num < vars.c.position) {
                vars.c.position = num - 1;
                vars.c.next(vars.c);
                vars.o.next(vars.o);
                return false;
            }
            else {
                return false;
            }
        });
        $(this.container).append(li);
    }
};

// 画像送り
OrderMarker.prototype.next = function(that) {
    var end = that.start + (that.carousel.position * that.span);
    $(that.marker).animate({left: end}, that.nextspeed, that.nexteasing);
};

OrderMarker.prototype.prev = function(that) {
    var end = that.start + (that.carousel.position * that.span);
    $(that.marker).animate({left: end}, that.prevspeed, that.preveasing);
}

//--------------------------------------------------------------------
// トリガボタンオブジェクト

// コンストラクタ
function NaviButton(area_id) {
    this.carousel = {};
    this.next = $('<div class="next">次へ</div>');
    this.prev = $('<div class="prev">前へ</div>');
    $('#' + area_id).append(this.next);
    $('#' + area_id).append(this.prev);
}

// 初期化
// ※初期化済みのギャラリーオブジェクトインスタンスを受け取る
NaviButton.prototype.init = function(carousel, marker) {
    this.carousel = carousel;
    this.marker   = marker;
    this.bind();
};

// クリックイベントのバインド
NaviButton.prototype.bind = function() {
    var that = this;

    $(that.next).bind('click', function(event) {
        that.carousel.next(that.carousel);
        that.marker.next(that.marker);
        return false;
    });

    $(that.next).hover(
        function() {
            $(this).css('background-position', '-66px -22px');
        },
        function() {
            $(this).css('background-position', '-66px 0');
        }
    );

    $(that.prev).bind('click', function(event) {
        that.carousel.prev(that.carousel);
        that.marker.prev(that.marker);
        return false;
    });

    $(that.prev).hover(
        function() {
            $(this).css('background-position', '0 -22px');
        },
        function() {
            $(this).css('background-position', '0 0');
        }
    );

};


//--------------------------------------------------------------------
// readyイベント

$(document).ready(function() {
    $('#carousel').removeClass('noscript').addClass('loading');

    var loadComplete = 0;

    var cb = function() {
        if (loadComplete >= vars.c.sources.length) {
            $('#carousel').removeClass('loading').css({
                'background-image' : 'none',
                'background-color' : '#2e0000'
            });
            vars.c.init();
            vars.o.init(vars.c);
            vars.b.init(vars.c, vars.o);

            vars.setIntervalTimer = function() {
                vars.timer = setInterval(function() {
                    vars.c.next(vars.c);
                    vars.o.next(vars.o);
                }, vars.interval);
            }
            vars.setIntervalTimer();
        }
    };

    vars.c.loadImages = [];
    for (var i = 0; i < vars.c.sources.length; i += 1) {
        vars.c.loadImages[i]        = new Image();
        vars.c.loadImages[i].src    = vars.c.sources[i];
        vars.c.loadImages[i].width  = 538;
        vars.c.loadImages[i].height = 318;
        if ($(vars.c.loadImages[i]).attr('complete')) {
            loadComplete += 1;
            cb();
        }
        else {
            $(vars.c.loadImages[i]).load(function() {
                loadComplete += 1;
                cb();
            });
        }
    }
});

// インスタンス生成
vars.c = new Carousel('carousel');
vars.o = new OrderMarker('carousel');
vars.b = new NaviButton('carousel');

