/* Banner slideshow with controls */
function SlideShow(images, container) {
    /* Array of images that will become slides */
    this.images = images;
    /* Slides container */
    this.slides = [];
    /* Element that will contain slides */
    this.container = container;
    /* Active slide number container */
    this.active_slide = 0;
    /* Interval handler */
    this.interval = null;
    /* Cache window dimensions */
    this.window_dimensions = {width: $(window).width(), height: $(window).height()};
    this.window_ratio = this.window_dimensions.width / this.window_dimensions.height;

    /* Create an img tag and adopt it to slides container */
    this.adoptImage = function(index) {
        var image_src = this.images[index];
        var image = $(document.createElement('img'));
        image.attr({
            src: image_src
        });
        image.css({
            display: 'none'
        });
        var self = this;
        image.bind('load', function(){
            self.resizeImage(image);
            self.slides.push(image);
            if (index == 0) {
                image.fadeIn(500);
            } else if (index == 1) {
                self.runTimer();
            }
            if (self.images.length > index + 1) {
                self.adoptImage(index + 1);
            }
        });
        this.container.append(image);
    }

    /* Changes active slide to new_slide. new_slide is INT */
    this.changeSlide = function(new_slide) {
        if(this.active_slide == new_slide) return false;
        this.slides[this.active_slide].fadeOut(900);
        this.active_slide = new_slide;
        this.slides[this.active_slide].fadeIn(900);
        return true;
    }

    /* Change to next */
    this.changeToNext = function() {
        var next_slide = self.active_slide + 1;
        if(next_slide == self.slides.length) next_slide = 0;
        self.changeSlide(next_slide);
    }

    /* Timed executioner */
    this.runTimer = function() {
        if(this.slides.length < 2) return false;
        self = this;
        this.interval = setInterval(self.changeToNext, 7000);
        return true;
    }

    /* Apply height of image to slides container */
    this.applyDimensions = function() {
        this.window_dimensions = {width: $(window).width(), height: $(window).height()};
        this.window_ratio = this.window_dimensions.width / this.window_dimensions.height;
        // Apply size to container
        this.container.css({
            width: this.window_dimensions.width + 'px',
            height: this.window_dimensions.height + 'px'
        });
        var self = this;
        $.each(this.slides, function(index, image){
            self.resizeImage(image);
        });
    }

    this.resizeImage = function(image) {
        var image_dimensions = {width: image.width(), height: image.height()};
        var image_ratio = image_dimensions.width / image_dimensions.height;
        if (image_ratio == this.window_ratio) {
            image.css({
                width: this.window_dimensions.width + 'px',
                height: this.window_dimensions.height + 'px'
            });
        } else if (image_ratio > this.window_ratio) {
            image.css({
                width: (this.window_dimensions.width * image_ratio / this.window_ratio) + 'px',
                height: this.window_dimensions.height + 'px'
            });
        } else if (image_ratio < this.window_ratio) {
            image.css({
                width: this.window_dimensions.width + 'px',
                height: (this.window_dimensions.height * this.window_ratio / image_ratio) + 'px'
            });
        }
    }

    /* Initialize */
    this.applyDimensions();
    this.adoptImage(0);
    var self = this;
    $(window).bind('resize', function() {
        self.applyDimensions();
    });
}
