﻿/// <reference path="/jquery-1.5-vsdoc.js" />

/* SETTINGS */
var Timing = 5;  // Seconds between animation
var SlideSpeed = 1; // Seconds animation duration

var StartingOpacity = 0;
var EndingOpacity = 100;

var EasingType = 'easeOutQuint';
var FadeType = 'easeInQuad';
var FadeOutType = 'easeOutQuint';

var SliderWrapper = '.slider-wrapper';
var ImageItem = '.banner-slider';


/* FUNCTIONS */
Timing = SlideSpeed + Timing * 1000;  // Convert to milliseconds
SlideSpeed = SlideSpeed * 1000;
StartingOpacity = StartingOpacity / 100;
EndingOpacity = EndingOpacity / 100;
var WrapperWidth = 0; // First, let's set the total width of the wrapper DIV
var StopPosition = new Array(); // Also find each stopping position
var CurrentPosition = 0;
var ItemCount = 0;
var IntervalFunction = 'SlideLeft(true)';
var IntervalObject = setInterval(IntervalFunction, Timing);
//var StepBack = false; // Used when we're moving back from position 0

// Check IE
var Browser = getInternetExplorerVersion();
if (Browser == -1) Browser = 100;

$(window).load(function () {

    // First to make the sliding seamless, we need
    // to get the first item and add it again to the end
    ItemCount = $(ImageItem).size();
    $(SliderWrapper + ' ' + ImageItem + ':last').after($(SliderWrapper).html());

    // Get the width of all the image items
    $(ImageItem).each(function () {
        // Sets the Left value of the item
        StopPosition.push(WrapperWidth);
        WrapperWidth += $(this).width();
    });

    // Fade out all but the first item
    if (Browser > 7)
        $(ImageItem + ':not('+ ImageItem +':first)').css("opacity", StartingOpacity);

});

function SlideToPosition(ItemNumber) {

    if (Browser > 7) {
        // Fade-in new item
        $(ImageItem + ':nth-child(' + (ItemNumber + 1) + ')').animate({
            opacity: EndingOpacity
        }, SlideSpeed, FadeType, function () { });

        // Fade-out old item
        if (ItemNumber > 0) {
            $(ImageItem + ':nth-child(' + (ItemNumber) + ')').animate({
                opacity: StartingOpacity
            }, SlideSpeed, FadeOutType, function () { });
        }
    }

    $(SliderWrapper).animate({
        left: (-StopPosition[ItemNumber] - ItemNumber * 2) + 'px'
    }, SlideSpeed, EasingType, function () {
        // Check if it's the last item, if so,
        // set the item back to the start
        if (StopPosition.length - (ItemCount) == CurrentPosition) {
            if (Browser > 7) {
                $(ImageItem + ":first").css("opacity", EndingOpacity);
                $(ImageItem + ':not(' + ImageItem + ':first)').css("opacity", StartingOpacity);
            }
            $(SliderWrapper).css("left", StopPosition[0] + 'px');
            CurrentPosition = 0;
        }
    });
    CurrentPosition = ItemNumber;
}

function SlideLeft(AutoSlide) {
    SlideToPosition(CurrentPosition + 1);
    if (AutoSlide === undefined) {
        clearInterval(IntervalObject);
        IntervalObject = setInterval(IntervalFunction, Timing);
    }
}

function SlideRight() {
    if (CurrentPosition == 0) {
        CurrentPosition = StopPosition.length - (ItemCount);
        if (Browser > 7) $(ImageItem + ':eq(' + (StopPosition.length - (ItemCount)) + ')').css("opacity", 1);
        $(SliderWrapper).css("left", (-StopPosition[StopPosition.length - (ItemCount)] - 12) + 'px');
        $(SliderWrapper).queue(function () {
            SlideToPosition(CurrentPosition - 1);
            $(this).dequeue();
        })
        
    } else {
        SlideToPosition(CurrentPosition - 1);
    }
    clearInterval(IntervalObject);
    IntervalObject = setInterval(IntervalFunction, Timing);
}


/* IE Detection */
function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
    var rv = -1; // Return value assumes failure.
    if (navigator.appName == 'Microsoft Internet Explorer') {
        var ua = navigator.userAgent;
        var re = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
            rv = parseFloat(RegExp.$1);
    }
    return rv;
}
