/**
 * Carousel
 */
var Carousel = function(length, options){
    this.length = length || 0;
    this.current = 0;
    this.locked = false;
    this.timer = null;
    options = options || {};
    this.interval = options.interval || 0;
};
Carousel.prototype.next = function(){
    this.move(+1);
};
Carousel.prototype.prev = function(){
    this.move(-1);
};
Carousel.prototype.move = function(d){
    if (this.locked){
        return;
    }
    this.locked = true;
    var last = this.current;
    this.current = (this.current + d) % this.length;
    if (this.current < 0){
        this.current += this.length;
    }
    this.effect(last, this.current, d);
};
Carousel.prototype.onEffectEnd = function(){
    this.locked = false;
};
Carousel.prototype.start = function(){
    if (this.timer){
        return;
    }
    var self = this;
    this.timer = setInterval(function(){
        self.next();
    }, this.interval);
};
Carousel.prototype.stop = function(){
    if (this.timer == null){
        return;
    }
    clearInterval(this.timer);
    this.timer = null;
};


/**
 * MainCarousel
 */
var MainCarousel = function(slides){
    this.base = Carousel;
    this.base(slides.length, {
        interval: 6000
    });
    this.slides = slides;
};
MainCarousel.prototype = new Carousel();
MainCarousel.prototype.effect = function(last, current, d){
    var duration = 700;
    var self = this;
    $(this.slides[last]).css({
        zIndex: 0
    });
    $(this.slides[current]).css({
        opacity: 0,
        zIndex: 10
    }).animate({
        opacity: 1
    }, {
        duration: duration,
        complete: function(){
            $(self.slides[last]).css({
                opacity: 0
            });
            self.onEffectEnd();
        }
    });
};

/**
 * AppealCarousel
 */
var AppealCarousel = function(slide_base, slides){
    slide_base = $(slide_base);
    slides = $(slides);
    this.base = Carousel;
    this.base(slides.length, {
        interval: 10000
    });
    this.slide_base = slide_base;
    this.slides = slides;
    slide_base.append(slides.clone());
    slide_base.append(slides.clone());
};
AppealCarousel.prototype = new Carousel();
AppealCarousel.prototype.effect = function(last, current, d){
    var self = this;
    var pos = this.slides.length + last;
    this.slide_base.css({
        left: pos * -308
    }).animate({left: (pos + d) * -308}, {
        duration: 500,
        complete: function(){
            self.onEffectEnd();
        }
    });
};

(function(){
    var main_carousel = new MainCarousel($("#main_visual .slide"));
    $("#main_visual .prev").click(function(event){
        event.preventDefault();
        main_carousel.prev();
    });
    $("#main_visual .next").click(function(event){
        event.preventDefault();
        main_carousel.next();
    });
    $("#main_visual").mouseenter(function(){
        main_carousel.stop();
    }).mouseleave(function(){
        main_carousel.start();
    });
    $(window).load(function(){
        main_carousel.start();
    });
})();

(function(){
    var appeal_carousel = new AppealCarousel("#appeal_slide_base", "#appeal_slide_base .slide");
    $("#appeal .prev").click(function(event){
        event.preventDefault();
        appeal_carousel.prev();
    });
    $("#appeal .next").click(function(event){
        event.preventDefault();
        appeal_carousel.next();
    });
    $("#appeal").mouseenter(function(){
        appeal_carousel.stop();
    }).mouseleave(function(){
        appeal_carousel.start();
    });
    $(window).load(function(){
        appeal_carousel.start();
    });
})();

