﻿/// <reference path="jquery-1.3.1.js">

$(document).ready(function() {
    anContentRotatorFadeIn.globalSlidePause = 10;
    anContentRotatorFadeIn.globalTitleTransition = 0.4;
    anContentRotatorFadeIn.init();
});

// This content rotator class allows for items to be 'flown in' from the sides
//  of the pages
anContentRotatorFadeIn = {
    globalSlidePause: 8500,
    globalTitleTransition: 400,
    onclick: null,
    isAnimating: false,
    transitionTimeoutId: null,
    currentItem: null,
    firstTime: true,
    init: function() {
        var rotator = document.getElementById("contentRotator");
        var rotator = {
            currentContentItem: $("#currentContentItem", rotator),
            mainArtLink: $("#currentContentItem a.mainArt", rotator),
            mainArtImg: $("#currentContentItem a.mainArt img", rotator),
            caption: $("#currentText .caption", rotator),
            title: null,
            deck: null,
            titlePause: 250,
            currentIndex: 0,
            mainNav: $("#navigation .ctrl", rotator),
            slideNavigators: $("#navigation .slides", rotator),
            slideItems: $("#slideSection .slides", rotator),
            back: $(".ctrl #back", rotator),
            next: $(".ctrl #next", rotator),
            isPaused: false,
            slides: []
        };
        this.currentIndex = rotator.currentIndex;
        this.loadSlides(rotator);
        this.globalSlidePause = this.globalSlidePause * 1000;
        this.globalTitleTransition = this.globalTitleTransition * 1000;
        this.previousHandler(rotator);
        this.nextHandler(rotator);
        this.start(rotator);
        this.goTo(0, rotator);
    },
    clearInterval: function(A) {
        window.clearTimeout(this.transitionTimeoutId);
        this.transitionTimeoutId = null
    },
    fadeOutSlide: function(A) {

        if (this.firstTime == false)
        {
            $(A.mainArtImg).fadeOut('slow');

            $(A.caption).fadeOut('slow', function() {
                anContentRotatorFadeIn.setSlideContent(A);
                $(A.mainArtImg).fadeIn('slow');
                $(A.caption).fadeIn('slow');
            });
        }
        else
        {
            $(A.mainArtImg).hide();
            $(A.caption).hide();
            anContentRotatorFadeIn.setSlideContent(A);
            $(A.mainArtImg).fadeIn('slow');
            $(A.caption).fadeIn('slow');
            this.firstTime = false;
        }

        anContentRotatorFadeIn.slideTransition(A);
        anContentRotatorFadeIn.isAnimating = false;
    },
    goNext: function(A) {
        if (A.currentIndex == (A.slides.length - 1))
        {
            anContentRotatorFadeIn.goTo(0, A)
        } else
        {
            anContentRotatorFadeIn.goTo(null, A)
        }
    },
    goTo: function(A, B) {
        anContentRotatorFadeIn.clearInterval(B);
        if (A != null)
        {
            $("li", B.slideItems).slice(B.currentIndex, B.currentIndex + 1).children("a").removeClass("selected");
            $("div", B.slideNavigators).slice(B.currentIndex, B.currentIndex + 1).removeClass("navcircleSelected")
            if (A !== 0)
            {
                $("li", B.slideItems).slice(A, A + 1).children("a").addClass("selected")
                $("div", B.slideNavigators).slice(A, A + 1).addClass("navcircleSelected");
            } else
            {
                $("li a", B.slideItems).slice(B.slides.length - 1, B.slides.length).removeClass("selected");
                $("li", B.slideItems).slice(A, A + 1).children("a").addClass("selected")
                $("div", B.slideNavigators).slice(B.slides.length - 1, B.slides.length).removeClass("navcircleSelected");
                $("div", B.slideNavigators).slice(A, A + 1).addClass("navcircleSelected");
            }
            B.currentIndex = A
        } else
        {
            $("li", B.slideItems).slice(B.currentIndex, B.currentIndex + 1).children("a").removeClass("selected");
            $("li", B.slideItems).slice(B.currentIndex + 1, B.currentIndex + 2).children("a").addClass("selected");
            $("div", B.slideNavigators).slice(B.currentIndex, B.currentIndex + 1).removeClass("navcircleSelected").addClass("navcircle");
            $("div", B.slideNavigators).slice(B.currentIndex + 1, B.currentIndex + 2).addClass("navcircleSelected");
            B.currentIndex++
        }
        this.currentItem = B;
        this.fadeOutSlide(B);
    },
    loadSlides: function(A) {
        $("li", A.slideItems).each(function(D) {
            var C = this;
            C = {
                url: $("div.slidecontent a.mainArtLink", this).attr("href"),
                title: $("div.slidecontent h1.title a", this).text(),
                summary: $("div.slidecontent p.summary", this).text(),
                imageSrc: $("div.slidecontent a.mainArtLink img", this).attr("src")
            };
            var F = new Image();
            F.src = C.imageSrc;
            A.slides.push(C)
        })
    },
    nextHandler: function(A) {
        $(A.next).click(
        function() {
            if (anContentRotatorFadeIn.isAnimating)
            {
                return
            }
            anContentRotatorFadeIn.isAnimating = true;
            anContentRotatorFadeIn.showNext(A);
            return false
        })
    },
    previousHandler: function(A) {
        $(A.back).click(
        function() {
            if (anContentRotatorFadeIn.isAnimating)
            {
                return
            }
            anContentRotatorFadeIn.isAnimating = true;
            anContentRotatorFadeIn.showPrevious(A);
            return false
        })
    },
    setContentHTML: function(B) {
        var A = '<h1 class="title"><a href="' + B.slides[B.currentIndex].url + '">' + B.slides[B.currentIndex].title + "</a></h1>";
        A += '<p class="summary"><a href="' + B.slides[B.currentIndex].url + '">' + B.slides[B.currentIndex].summary + "</a></p>";
        return A
    },
    setMainArtContent: function(A) {
        $(A.mainArtLink).attr("href", A.slides[A.currentIndex].url);
        if ($(A.mainArtLink).children().length == 0)
        {
            $(A.mainArtLink).append('<img src="' + A.slides[A.currentIndex].imageSrc + '" alt="' + A.slides[A.currentIndex].title + '" />')
        } else
        {
            $(A.mainArtImg).attr({
                src: A.slides[A.currentIndex].imageSrc,
                alt: A.slides[A.currentIndex].title
            })
        }
    },
    setNextTransition: function(A) {
        if (A.slides.length == 1)
        {
            return
        }
        if (!A.isPaused)
        {
            this.transitionTimeoutId = window.setTimeout(function() {
                anContentRotatorFadeIn.goNext(A)
            },
            anContentRotatorFadeIn.globalSlidePause)
        }
    },
    setSlideContent: function(A) {
        var B = anContentRotatorFadeIn.setContentHTML(A);
        $(A.caption).html(B);
        anContentRotatorFadeIn.setMainArtContent(A);
    },
    showNext: function(A) {
        if (A.currentIndex == (A.slides.length - 1))
        {
            this.goTo(0, A)
        } else
        {
            this.goTo(A.currentIndex + 1, A)
        }
    },
    showPrevious: function(A) {
        if (A.currentIndex == 0)
        {
            this.goTo(A.slides.length - 1, A)
        } else
        {
            this.goTo(A.currentIndex - 1, A)
        }
    },
    showSelected: function(A, B) {
        this.goTo(A, B)
    },
    slideTransition: function(A) {
        anContentRotatorFadeIn.setNextTransition(A)
    },
    start: function(A) {
        // remove the selected tag for both the circles and the li's
        $("li", A.slideItems).slice(A.currentIndex, A.currentIndex + 1).children("a").removeClass("selected");
        $("div", A.slideNavigators).slice(A.currentIndex, A.currentIndex + 1).removeClass("navcircleSelected").addClass("navcircle");
        this.setMainArtContent(A);
        var B = this.setContentHTML(A);
        $(A.caption).html(B);
        if (this.onClick != null)
        {
            $(A.currentcontentItem).find("a").click(function() {
                anContentRotatorFadeIn.onClick()
            })
        }
        $(A.mainArtLink).fadeIn(500, function() { anContentRotatorFadeIn.slideTransition(A) })
    }
}
