﻿/// <reference path="jquery-1.5.min.js" />


var ss = {
    totalImages: 0,
    currentImage: 0,
    AreControlsVisible: false,
    timeoutID: 0,
    timerSeconds: 6000,
    fadeOutSeconds: 1000,
    fadeInSeconds: 4000,
    captionSpanId: "",
    fadeCaption: true,



    prev: function () {

        if (this.currentImage > 0) {
            this.display(this.currentImage - 1);
        }
        else {
            this.display(this.totalImages);
        }
        window.clearInterval(this.timeoutID);
    },


    next: function () {

        if (this.currentImage < this.totalImages) {
            this.display(this.currentImage + 1)
        }
        else {
            this.display(0);
        }

        window.clearInterval(this.timeoutID);
    },



    hideControls: function () {
        $(".controls").stop().animate({ opacity: 0 }, 500);
        this.AreControlsVisible = false;
    },


    showControls: function () {
        if (!this.AreControlsVisible) {
            $(".controls").stop().animate({ opacity: 1 }, 500);
        }
        this.AreControlsVisible = true;
    },

    display: function (num) {

        //the first time this is run - the images will all be hidden - to achieve the fadeIn effect on page load - to account for this we need to
        $(".large-images li").stop().animate({ opacity: 0 }, (this.fadeOutSeconds));
        var workingLi = $(".large-images li[value='" + num + "']'");
        workingLi.stop().delay(300).animate({ opacity: 1 }, (this.fadeInSeconds));


        //If a the ID for the caption element is set,
        //then the alt element of the image is set into that element
        if (ss.captionSpanId != '') {
            var captionText = $(workingLi).find('img:first').attr('alt');
            if(this.fadeCaption){
                $(ss.captionSpanId).stop().delay(300).animate({ opacity: -0.5 }, (this.fadeOutSeconds), 'swing', function () {
                    $(ss.captionSpanId).html(captionText);}
                );
                $(ss.captionSpanId).stop().delay(300).animate({ opacity: 1 }, (this.fadeInSeconds));
            }
            else{
                $(ss.captionSpanId).html(captionText);
            } 
        }

        this.currentImage = num;

    }
};

$(window).load(function () {

    //$('#innerContent').bind("mouseenter", ss.showControls).bind("mouseleave", ss.hideControls);

    //$(".prev").bind("click", function () { ss.prev(); });
    //$(".next").bind("click", function () { ss.next() });

    ss.totalImages = ($(".large-images li").length) - 1;
    ss.currentImage = parseInt($(".large-images li:visible").attr('value'));
    $(".large-images li").first().fadeIn(ss.fadeInSeconds);
    ss.timeoutID = window.setInterval('displayFromTimer()', ss.timerSeconds);

    ss.captionSpanId = '#navText';

});

function displayFromTimer() {
    if (ss.currentImage < ss.totalImages) {
        ss.display(ss.currentImage + 1)
    }
    else {
        ss.display(0);
    }
}

