﻿
    /******************************************************************************/
    /*  Indigo Carousel                                                           */
    /******************************************************************************/
    var carouselWaitingForFlash = null;

    function indigoCarousel(options, carouselElementID, carouselSlides, websiteRoot)
    {
        var _this = this;
        this.defaults =
        {
            speed: 1000,
            interval: 5000,
            prevText: "<span>&laquo;</span>",
            nextText: "<span>&raquo;</span>"
        };

        this.options = $.extend(this.defaults, options);
        this.carouselElementID = carouselElementID;
        this.carouselSlides = (carouselSlides ? carouselSlides : []);
        this.websiteRoot = websiteRoot;
        this.videoObject = null;
        this.videoPlaying = false;

        $(function () { _this.initialise(); });
    }
    indigoCarousel.prototype.initialise = function ()
    {
        var 
            carouselElement = document.getElementById(this.carouselElementID);

        if (carouselElement)
        {
            var 
                _this = this,
                carousel = $(carouselElement),
                viewport = $(".ViewPort", carouselElement),
                flashVideoSlide = $(".FlashVideoSlide", carouselElement),
                itemContainer = $(".Items", carouselElement),
                items = null,
                textPanel = $(".TextPanel", carouselElement),
                textPanelCorner = $(".TextPanelCorner", carouselElement),
                contentLink = $(".ContentLink", carouselElement),
                playVideoLink = $(".PlayVideoLink", carouselElement),
                textPanelContent = $(".TextPanelContent", carouselElement),
                textPanelContentPs = $("p", textPanelContent),
                totalItemWidth = 0,
                current = 0,
                timer = 0,
                running = false,
                moving = false,
                wasAuto = true;

            // Remove the placeholder and non-js class
            carousel.children().remove("img");
            carousel.removeClass("Carousel-Static");

            viewport.css("display", "block");

            flashVideoSlide.height(viewport.height());
            flashVideoSlide.animate({ opacity: 0 }, 0);
            flashVideoSlide.css("display", "none");

            textPanelContent.animate({ opacity: 0 }, 0);
            textPanelContent.css("display", "");

            contentLink.animate({ opacity: 0 }, 0);

            playVideoLink.animate({ opacity: 0 }, 0);
            playVideoLink.click(function (evt)
            {
                evt.preventDefault();
                if (!moving)
                {
                    moving = true;

                    wasAuto = timer != 0;
                    stop();

                    $(".Previous").css("display", "none");
                    $(".Next").css("display", "none");

                    function checkFlashConnection()
                    {
                        if (!_this.connected && new Date() - _this.connectStartTime > 2000)
                        {
                            fadeBackFromVideo(function ()
                            {
                                moving = false;
                            });
                        }
                        else
                            setTimeout(checkFlashConnection, 1000);
                    }

                    function embedVideo()
                    {
                        _this.connected = false;
                        _this.connectStartTime = new Date();

                        var 
                            flashvars = {},
                            params = {},
                            attributes = {};

                        flashvars.skip = "0";
                        flashvars.youTubeId = _this.carouselSlides[current].youTubeID;
                        params.wmode = "transparent";
                        params.allowScriptAccess = "always";
                        params.allowFullScreen = "true";

                        carouselWaitingForFlash = _this;
                        flashVideoSlide.css("display", "block");
                        swfobject.embedSWF(_this.websiteRoot + "templates/swf/videoplayer.swf", "divFlashVideoSlideInner", viewport.width(), viewport.height(), "10.0.0", "expressInstall.swf", flashvars, params, attributes);

                        setTimeout(checkFlashConnection, 1000);
                    }

                    if ($.browser.msie && $.browser.version <= 8)
                    {
                        viewport.css("visibility", "hidden");
                        embedVideo();
                    }
                    else
                    {
                        viewport.animate({ opacity: 0 }, 250, null, function ()
                        {
                            embedVideo();
                        });
                    }
                }
            });

            function flashReady()
            {
                this.connected = true;
                setTimeout(function ()
                {
                    fadeToVideo();
                }, 1);
            }
            _this.flashReady = flashReady;

            function fadeToVideo(callBack)
            {
                _this.videoObject = swfobject.getObjectById("divFlashVideoSlideInner");
                if (_this.videoObject && _this.videoObject.playVideo)
                {
                    _this.videoObject.playVideo();
                    _this.videoPlaying = true;

                    flashVideoSlide.animate({ opacity: 1 }, 500, null, function ()
                    {
                        moving = false;
                        if (callBack)
                            callBack();
                    });
                }
                else
                {
                    fadeBackFromVideo(callBack);
                }
            }

            function fadeBackFromVideo(callBack)
            {
                if (_this.videoObject && _this.videoObject.stopVideo)
                    _this.videoObject.stopVideo();

                flashVideoSlide.stop().animate({ opacity: 0 }, 500, null, function ()
                {
                    flashVideoSlide.css("display", "none");
                    swfobject.removeSWF("divFlashVideoSlideInner");
                    flashVideoSlide.html("<div id=\"divFlashVideoSlideInner\" class=\"FlashVideoSlideInner\"></div>");

                    function _showViewPort()
                    {
                        $(".Previous").css("display", "block");
                        $(".Next").css("display", "block");
                        _this.videoPlaying = false;
                        if (callBack)
                            callBack();
                    }

                    if ($.browser.msie && $.browser.version <= 8)
                    {
                        viewport.css("visibility", "visible");
                        _showViewPort();
                    }
                    else
                    {
                        viewport.stop().animate({ opacity: 1 }, 1000, null, function ()
                        {
                            _showViewPort();
                        });
                    }
                });
            }

            function videoFinished()
            {
                fadeBackFromVideo(function ()
                {
                    if (wasAuto)
                        start();
                });
            }
            _this.videoFinished = videoFinished;

            for (var i = 0; i < _this.carouselSlides.length; i++)
            {
                if (_this.carouselSlides[i].youTubeID == "" && _this.carouselSlides[i].linkURL != "")
                    itemContainer.append("<div style=\"width:" + viewport.width() + "px;height:" + viewport.height() + "px;overflow:hidden;float:left;\"><a href=\"" + _this.carouselSlides[i].linkURL + "\"><img alt=\"\" src=\"" + _this.carouselSlides[i].imageFilePath + "\" /></a></div>");
                else
                    itemContainer.append("<div style=\"width:" + viewport.width() + "px;height:" + viewport.height() + "px;overflow:hidden;float:left;\"><img alt=\"\" src=\"" + _this.carouselSlides[i].imageFilePath + "\" /></div>");
            }

            items = itemContainer.children();

            // Create the controls.
            if ($(items).length > 1)
            {
                $(carousel).append("<a class=\"Play\" href=\"#play\">Pause</a>");
                $(".Play", carousel).click(function (evt)
                {
                    evt.preventDefault();

                    if (running)
                        stop();
                    else
                    {
                        if (current == $(items).length - 1)
                        {
                            $(itemContainer).css({ "left": $(carousel).width() });
                            current = 0;
                        }
                        else
                            current++;

                        scrollTo(current);

                        start();
                    }
                });

                $(items).first().clone().prependTo($(viewport)).css({ "position": "absolute", "top": 0, "left": 0 });   // Set the background image.
                $(itemContainer).hide().css({ "left": -1 * $(viewport).outerWidth() });
                $(items).last().clone().prependTo(itemContainer);

                $(carousel).append("<a class=\"Previous\" href=\"#previous\">" + _this.options.prevText + "</a>");
                $(".Previous", carousel).click(function (evt) { evt.preventDefault(); previous(); });

                $(carousel).append("<a class=\"Next\" href=\"#next\">" + _this.options.nextText + "</a>");
                $(".Next", carousel).click(function (evt) { evt.preventDefault(); next(); });
            }

            $(carousel).append("<div class=\"Controls\"><ul></ul></div>");
            var itemControls = $(".Controls ul", carousel);

            $(items).each(function (i)
            {
                var item = $(document.createElement("li"))
            		.html("<a href=\"#" + (i + 1) + "\">" + (i + 1) + "</a>")
            		.appendTo($(itemControls))
            		.click(function (evt)
            		{
            		    evt.preventDefault();

            		    if (!moving)
            		    {
            		        if (current == i)
            		            return;

            		        moving = true;

            		        if (_this.videoPlaying)
            		        {
            		            fadeBackFromVideo(function ()
            		            {
            		                moving = false;
            		                _gaq.push(['_trackEvent', 'slideshow', 'transition ' + (i + 1), PAGE_NAME]);
            		                scrollTo(i, true);
            		                stop();
            		            });
            		        }
            		        else
            		        {
            		            moving = false;
            		            _gaq.push(['_trackEvent', 'slideshow', 'transition ' + (i + 1), PAGE_NAME]);
            		            scrollTo(i, true);
            		            stop();
            		        }
            		    }
            		});

                if (i == 0)
                {
                    $(item).addClass("Start");
                    $("a", item).addClass("Selected");
                }

                if (i == $(items).length - 1)
                    $(item).addClass("End");

                if (i < $(items).length - 1)
                    totalItemWidth = totalItemWidth + $(this).width();
            });

            if (($.browser.msie && $.browser.version == 7) || ($.browser.msie && $.browser.version == 6))
                $(".Controls li", carousel).css({ "display": "inline", "zoom": "1" });
            else
                $(".Controls li", carousel).css({ "display": "inline-block" });


            $(".Next, .Previous", carousel).hover(function ()
            {
                if (!$.browser.msie)
                    $(this).children().fadeIn(250);
                else
                    $(this).children().css({ "display": "block" });
            }, function ()
            {
                if (!$.browser.msie)
                    $(this).children().fadeOut(250);
                else
                    $(this).children().css({ "display": "none" });
            });

            function fadeOutPanel(callBack)
            {
                playVideoLink.animate({ opacity: 0 }, 250);
                contentLink.animate({ opacity: 0 }, 250);

                textPanelContent.animate({ opacity: 0 }, 250, null, function ()
                {
                    textPanelCorner.animate({ borderWidth: 0 }, 250, null, function ()
                    {
                        playVideoLink.css("display", "none");
                        contentLink.css("display", "none");
                        if (callBack)
                            callBack();
                    });
                });
            }

            function fadeInPanel(callBack)
            {
                if
                (
                    (_this.carouselSlides[current].youTubeID != "") ||
                    (_this.carouselSlides[current].youTubeID == "" && (_this.carouselSlides[current].linkURL != "" || _this.carouselSlides[current].title != "" || _this.carouselSlides[current].text != ""))
                )
                {
                    if (_this.carouselSlides[current].youTubeID == "" && _this.carouselSlides[current].linkURL != "")
                    {
                        $(textPanelContentPs[0]).html("<a class=\"TitleLink\" href=\"" + _this.carouselSlides[current].linkURL + "\">" + _this.carouselSlides[current].title + "</a>");
                        $(textPanelContentPs[1]).html("<a href=\"" + _this.carouselSlides[current].linkURL + "\">" + _this.carouselSlides[current].text + "</a>");
                    }
                    else
                    {
                        $(textPanelContentPs[0]).html(_this.carouselSlides[current].title);
                        $(textPanelContentPs[1]).html(_this.carouselSlides[current].text);
                    }

                    textPanelCorner.animate({ borderWidth: 300 }, 250, null, function ()
                    {

                        if (_this.carouselSlides[current].slideType.toLowerCase() == "video")
                        {
                            playVideoLink.css("display", "block");
                            playVideoLink.animate({ opacity: 1 }, 250);
                        }
                        else
                        {
                            if (_this.carouselSlides[current].linkURL != "")
                            {
                                contentLink.attr("href", _this.carouselSlides[current].linkURL);
                                contentLink.css("display", "block");
                                contentLink.animate({ opacity: 1 }, 250);
                            }
                            else
                                contentLink.attr("href", "#");
                        }

                        textPanelContent.animate({ opacity: 1 }, 250, null, function ()
                        {
                            if (callBack)
                                callBack();
                            moving = false;
                        });
                    });
                }
                else
                {
                    if (callBack)
                        callBack();
                    moving = false;
                }
            }

            function scrollTo(index, scrollDirect)
            {
                if (moving)
                    return;
                else
                {
                    moving = true;

                    var 
                        lastIndex = current;

                    current = index;
                    itemContainer.show();

                    function scrollToPosition(callBack)
                    {
                        var scrollPosition = 0;

                        if (current == -1)     // Previous clicked from position 0.
                        {
                            scrollPosition = 0;
                            current = $(items).length - 1;
                        }
                        else
                        {
                            var currentItem = $(items).get(index);
                            scrollPosition = -1 * $(currentItem).position().left;
                        }

                        setSelected(current);

                        //                        if (!scrollDirect)
                        //                        {
                        //                            if (current == 0)
                        //                                $(itemContainer).css({ "left": 0 });
                        //                        }

                        $(itemContainer).stop().animate({ "left": scrollPosition }, _this.options.speed, function ()
                        {
                            if (current == $(items).length - 1)
                                $(itemContainer).css({ "left": 0 });
                            
                            if (callBack)
                                callBack();
                        });
                    }

                    fadeOutPanel(function ()
                    {
                        scrollToPosition(function ()
                        {
                            fadeInPanel(function ()
                            {
                            });
                        })
                    });

                }
            }

            function setSelected(index)
            {
                $(".Controls li a", carousel).removeClass("Selected");
                var c = $(".Controls ul", carousel).children();
                $("a", c[index]).addClass("Selected");
            }

            function advance()
            {
                if (!moving)
                {
                    if (current == $(items).length - 1)
                        current = 0;
                    else
                        current++;

                    scrollTo(current);
                }
            }

            function next()
            {
                if (!moving)
                {
                    if (current == $(items).length - 1)
                    {
                        current = 0;
                    }
                    else
                        current++;

                    scrollTo(current);
                }
                stop();
            }

            function previous()
            {
                if (!moving)
                {
                    // Reset the item container to the last item so the scrolling back is smooth.
                    if (current == 0)   // Scroll back from the first item.
                    {
                        current = -1;
                    }
                    else if (current == $(items).length - 1)    // Scroll back from the last item.
                    {
                        var i = $(items).get(current);
                        $(itemContainer).css({ "left": -1 * $(i).position().left });
                        current--;
                    }
                    else
                        current--;

                    scrollTo(current);
                }
                stop();
            }

            function start()
            {
                if (!running)
                {
                    running = true;
                    timer = setInterval(function () { advance(); }, _this.options.interval);
                    $(".Play", carousel).html("Pause");
                }
            }

            function stop()
            {
                if (running)
                {
                    clearInterval(timer);
                    timer = 0;
                    $(".Play", carousel).html("Play");
                    running = false;
                }
            }

            setTimeout(function ()
            {
                fadeInPanel();

                // Init
                if ($(items).length > 1)
                    start();
            }, 1000);
        }
    };
    
    function flashReady(e)
    {
        if (carouselWaitingForFlash)
            carouselWaitingForFlash.flashReady();
    }

    function videoFinished()
    {
        if (carouselWaitingForFlash)
            carouselWaitingForFlash.videoFinished();
    }
