(function(window) {
    
function Slideshow(canvas, slides, titles, subtitles, title, subtitle, buttons, ui) {
    
    var that, timer, buttonLeft, buttonRight;
    
    that = this;
    this.canvas = canvas;
    this.slides = slides;
    this.titles = titles;
    this.subtitles = subtitles;
    this.title = title[0];
    this.subtitle = subtitle[0];
    this.buttons = buttons;
    
    $(this.buttons[0]).click(function() {
        that.index = (that.index > 0) ? that.index - 1 : that.slides.length - 1;
        that.showSlide(4, true);
    });
    
    $(this.buttons[1]).click(function() {
        that.index = (that.index < that.slides.length - 1) ? that.index + 1 : 0;
        that.showSlide(4, true);
    });
    
    $(ui).hoverIntent(function() {
        that.clearTimer();
        $(that.buttons).fadeIn();
    }, function() {
        that.setTimer();
        $(that.buttons).fadeOut();
    });
    
    Slideshow.SPACER_X = (this.canvas.width - (Slideshow.BLOCK_WIDTH * Slideshow.NUM_COLS));
    Slideshow.SPACER_Y = (this.canvas.height - (Slideshow.BLOCK_HEIGHT * Slideshow.NUM_ROWS));
    
    if ($.browser.msie && parseFloat($.browser.version) < 9) {
        that.initLegacy();
    } else {
        Loader.onload = function() {
            that.initCanvas();
        }
        Loader.add(this.slides).load();
    }   
}

var r = Slideshow.prototype;

Slideshow.SPACER_X;
Slideshow.SPACER_Y;
Slideshow.NUM_COLS = 8;
Slideshow.NUM_ROWS = 3;
Slideshow.NUM_BLOCKS = 24;
Slideshow.BLOCK_WIDTH = 88;
Slideshow.BLOCK_HEIGHT = 88;

r.blocks = [];
r.stage;
r.loader;
r.index = 0;
r.slides = [];
r.canvas;
r.oldSlide = null;

/*r.start = function() {
    this.blocks = this.createBlocks();
    this.showSlide();
    this.setTimer();
    Ticker.addListener(this.stage);
}*/

r.initCanvas = function() {
    this.stage = new Stage(this.canvas);
    this.blocks = this.createBlocks();
    this.showSlide();
    this.setTimer();
    Ticker.addListener(this.stage);
}

r.initLegacy = function() {
    this.showSlide();
    this.setTimer();
}

r.setTimer = function() {
    
    var that = this;
    
    this.timer = setInterval(function() {
        that.nextSlide();
        that.showSlide();
    }, 8000);
}

r.clearTimer = function() {
    clearInterval(this.timer);
}

r.createBlocks = function() {
    
    var blocks, block, col, row;
    
    blocks = [];
    col = 0;
    row = 0;
    
    for(var i = 0; i < Slideshow.NUM_BLOCKS; i++) {
        block = new RotatePlane(row, col);
        this.stage.addChild(block);
        blocks.push(block);
        col++;
        if (col == Slideshow.NUM_COLS) {
            col = 0;
            row++;
        }
    }
    return blocks;
}

r.nextSlide = function() {
    this.index = (this.index > this.slides.length - 2) ? 0 : this.index + 1;
}

r.prevSlide = function() {
    this.index = (this.index > 0) ? this.index - 1 : this.slides.length - 1;
}

r.showSlide = function(m, fast) {
    
    var canvas, that, delay, multiplier;
    
    that = this;
    delay = 0;
    multiplier = m || 10;
    
    $(this.title).clearQueue();
    $(this.subtitle).clearQueue();
    Tween._tweens = [];
    
    for(var i = 0, block; i < this.blocks.length; i++) {
        block = this.blocks[i];
        block.slide(this.slides[this.index], m, fast);
    }
    
    if ($.browser.msie && parseFloat($.browser.version) < 9) {
        if (this.oldSlide != null) $(this.slides[this.oldSlide]).fadeOut(multiplier * 300);
        $(this.slides[this.index]).fadeIn(multiplier * 300);
    }
    
    delay = multiplier * 300;
    
    var titleoptions = { opacity: 1 };
    var titlebackoptions = { opacity: 0 };
    var subtitleoptions = { opacity: 1 };
    var subtitlebackoptions = { opacity: 0 };
    
    if ($(that.titles[that.oldSlide]).hasClass("right")) {
        titlebackoptions.right = "-195px";
        subtitlebackoptions.right = "-195px";
    } else {
        titlebackoptions.left = "-207px";
        subtitlebackoptions.left = "-207px";
    }
    
    if ($(that.titles[that.index]).hasClass("right")) {
        subtitleoptions.right = "29px";
        titleoptions.right = "29px";
    } else {
        subtitleoptions.left = "17px";
        titleoptions.left = "17px";
    }
    
    if (this.oldSlide != null) {
        $(this.title).animate(titlebackoptions, multiplier * 100, "easeInOutExpo");
        $(this.title).queue(function(next) {
            $(this).css({ left: "", right: "" });
            if ($(that.titles[that.index]).hasClass("right")) {
                $(this).addClass("right");
            } else {
                $(this).removeClass("right");
            }
            next();
        });
        $(this.subtitle).animate(subtitlebackoptions, multiplier * 100, "easeInElastic");
        $(this.subtitle).queue(function(next) {
            $(this).css({ left: "", right: "" });
            if ($(that.subtitles[that.index]).hasClass("right")) {
                $(this).addClass("right");
            } else {
                $(this).removeClass("right");
            }
            next();
        });
    }
    
    $(this.title).queue(function(next) {
        $(this).html("");
        $(this).append($(that.titles[that.index]));
        setTimeout(next, delay);
    });
    
    $(this.subtitle).queue(function(next) {
        $(this).html("");
        $(this).append($(that.subtitles[that.index]));
        setTimeout(next, delay + 1000);
    });
    
    $(this.title).animate(titleoptions, multiplier * 100, "easeInOutExpo");
    $(this.subtitle).animate(subtitleoptions, multiplier * 100, "easeOutElastic");
    this.oldSlide = this.index;
}

window.Slideshow = Slideshow;
}(window));
