﻿var BTN_OPACITY = 0.6;
var BTN_IN = 400;
var BTN_OUT = 600;

var easing;
var currentPosition = -1;
var slides = new Array();
var thumbs = new Array();

function setupSlideShow() {
    setupSlideShow('easeOutCubic');
}

function setupSlideShow(ease) {
    easing = ease;

    slides = $("div.slide").css({ 'position': 'absolute', 'top': 0 })
                           .each(function (i) {
                               $(this).css('left', ($(this).width() + 30) * i);
                           });

    thumbs = $("ul.slides a").mouseenter(function (event) {
        $(this).stop(true, true);
        $(this).fadeTo(BTN_IN, 1.0);
    });

    thumbs.each(function (i) {
        $(this)
        .click(function (event) {
            event.preventDefault();
            slideTo(i);
            $(this).blur();
        })
        .mouseleave(function (event) {
            if (i == currentPosition) return;
            $(this).stop(true, true);
            $(this).fadeTo(BTN_OUT, BTN_OPACITY);
        });

        if (i == currentPosition) return;
        $(this).fadeTo(BTN_OUT, BTN_OPACITY);
    });

    $(".slideshow a.button")
    .mouseleave(function (event) {
        $(this).stop(true, true);
        $(this).fadeTo(BTN_OUT, BTN_OPACITY);
    })
    .mouseenter(function (event) {
        $(this).stop(true, true);
        $(this).fadeTo(BTN_IN, 1.0);
    })
    .fadeTo(BTN_OUT, BTN_OPACITY);

    $(".slideshow #next").click(function (event) {
        event.preventDefault();
        slideTo(currentPosition + 1);
        $(this).blur();
    });

    $(".slideshow #previous").click(function (event) {
        event.preventDefault();
        slideTo(currentPosition-1);
        $(this).blur();
    });

    slideTo(Math.floor(Math.random() * slides.length));
}

function slideTo(position) {
    if (slides.length == 0 || position == currentPosition) return;
    if (position < 0 || position >= slides.length) return;

    $("#slides").animate({ 'left': ($(slides[position]).width() + 30) * position * -1 },
                         400 + (200 * (Math.abs(currentPosition - position) - 1)),
                         easing,
                         function () {
                             $("#slideswrapper").animate({ 'height': $(slides[position]).outerHeight(false) }, 'slow', easing, null);
                         });

    $(thumbs[position]).fadeTo(BTN_IN, 1.0);
    $(thumbs[currentPosition]).fadeTo(BTN_OUT, BTN_OPACITY);

    currentPosition = position;
}

