﻿/// <reference path="jquery-1.4.2-vsdoc.js" />

var isFirstSlide = true;
var timer; var isTimerActive = 0;
var currentSlideIndex;
var settings;
var interval;
var videoCode = '<div class="slideshowVideo" style="position:absolute;"><div class=".slideMask" style="text-align:right;padding:5px; cursor:pointer;" onclick="stopVideo()"><span style="color:#ffffff;font-weight:bold;">Close</span><div>##embedCode##</div>';
var slideshowQuote;

jQuery.fn.slideshow = function(config) {
    settings = config;
    if (settings.interval) { interval = settings.interval; }
    else { interval = 9000; }

    var parentDiv = $(this);
    parentDiv.css({ width: settings.width, height: settings.height, overflow: 'hidden', position: 'relative' });

    $.each(settings.slides, function(i, slide) { $('<img src="' + slide.image + '" style="display:none; float:left; position:absolute; border:none; z-index:600;" />').appendTo(parentDiv); });

    $('<div class="slideshowQuote" style="display:none; position:relative; z-index:650; height:90px;width:520px;"></div>').appendTo(parentDiv);
    slideshowQuote = $('.slideshowQuote');
    slideshowQuote.width(settings.quoteWidth);
    $('img:first', parentDiv).load(showNextSlide);
    $(document).keyup(function(e) { if (e.keyCode == 27) { stopVideo(); } })
}


function showNextSlide() {
    clearTimer();
    var currentSlide = $('img.currentSlide');
    var nextSlide;
    var parentDiv = slideshowQuote.parent();

    if (currentSlide.size() > 0) {
        nextSlide = $('img.currentSlide').next('img');
        if (nextSlide.size() == 0) {
            nextSlide = $('img:first', currentSlide.parent());
            nextSlide.addClass('currentSlide');
            currentSlideIndex = 1;
        }
        else { currentSlideIndex++; }
    }
    else {
        nextSlide = $('img:first', parentDiv);
        nextSlide.addClass('currentSlide');
        currentSlideIndex = 1;
        if ($('img', parentDiv).size() > 1) { nextSlide.stop().fadeIn(setTimer); }
        else { nextSlide.stop().fadeIn(); }
        populateQuote(parentDiv);
        return;
    }

    if (currentSlide.size() > 0) {
        currentSlide.removeClass('currentSlide').stop().fadeOut(1000);
        slideshowQuote.stop().fadeOut(1000);
    }
    nextSlide.addClass('currentSlide').stop().fadeIn(1000, function() { populateQuote(parentDiv); setTimer(); });

}

function populateQuote(parentDiv) {

    var slideData = settings.slides[currentSlideIndex - 1];
    var link = '';

    if (slideData.source && slideData.source.length > 0) {
        if (slideData.isVideo == '1') { link = '<div style="text-align:right;"><a href="#" onclick="playVideo(); return false;">' + slideData.sourceText + '</a></div>'; }
        else { link = '<div class="heroCTA"><a href="' + slideData.source + '">' + slideData.sourceText + '</a></div>'; }
    }
    slideshowQuote.html(link + '<div class="contentContainer">' + slideData.quote + '</div>');

    var quoteWidth = slideshowQuote.width();
    
    // mds: added easing to the animation.
    if (isFirstSlide) {
        window.setTimeout(function() { slideshowQuote.css({ left: '-' + quoteWidth + 'px', display: 'block', opacity: '.99' }).stop().animate({ left: '0' }, { duration: 'slow', easing: 'easeOutCubic' }); }, 1000);
        isFirstSlide = false;
    }
    else {
        slideshowQuote.css({ left: '-' + quoteWidth + 'px', display: 'block', opacity: '.99' }).stop().animate({ left: '0' }, { duration: 'slow', easing: 'easeOutCubic' });
    }

}

function playVideo() {
    var parentDiv = slideshowQuote.parent().parent().parent().parent().parent().parent();
    clearTimer();
    var slideData = settings.slides[currentSlideIndex - 1];
    var formattedVideoCode = videoCode.replace('##embedCode##', slideData.source);

    $('<div class="slideMask" style="display:none; z-index:1000; float:left; position:absolute; top:0px; left:0px;">' + formattedVideoCode + '</div>').insertAfter(parentDiv);
    var slideMask = $('.slideMask');
    slideMask.height($(document).height()).width($(window).width());

    var slideshowVideo = $('.slideshowVideo');
    var flashObj = $('.slideMask object');
    // mds: Original version assumed a certain page layout.
    // slideshowVideo.css('top', (slideshowVideo.parent().parent().parent().height() / 2) - (flashObj.attr('height') / 2));
    // mds: To make the video display in the vertical center, I used the window height.
    slideshowVideo.css('top', ($(window).height() / 2) - (flashObj.attr('height') / 2));
    slideshowVideo.css('left', (slideshowVideo.parent().parent().width() / 2) - (flashObj.attr('width') / 2));
    slideMask.stop().fadeTo(1000, .85);

    return false;
}

function stopVideo() {
    var slideMask = $('.slideMask');

    if (slideMask.size() > 0) {
        $('.slideshowVideo').stop().fadeOut(200);
        slideMask.stop().fadeOut(1000, function() { slideMask.remove(); });
    }

    setTimer();
}

function setTimer() {
    clearTimer();
    timer = setInterval(showNextSlide, interval);
    isTimerActive = 1; 
}

function clearTimer() {
    var parentDiv = slideshowQuote.parent();
    clearInterval(timer);
    isTimerActive = 0;
}

(function($) {
    if (!document.defaultView || !document.defaultView.getComputedStyle) { // IE6-IE8
        var oldCurCSS = jQuery.curCSS;
        jQuery.curCSS = function(elem, name, force) {
            if (name === 'background-position') { name = 'backgroundPosition'; }
            if (name !== 'backgroundPosition' || !elem.currentStyle || elem.currentStyle[name]) { return oldCurCSS.apply(this, arguments); }
            var style = elem.style;
            if (!force && style && style[name]) { return style[name]; }
            return oldCurCSS(elem, 'backgroundPositionX', force) + ' ' + oldCurCSS(elem, 'backgroundPositionY', force);
        };
    }

    var oldAnim = $.fn.animate;
    $.fn.animate = function(prop) {
        if ('background-position' in prop) {
            prop.backgroundPosition = prop['background-position'];
            delete prop['background-position'];
        }
        if ('backgroundPosition' in prop) {
            prop.backgroundPosition = '(' + prop.backgroundPosition;
        }
        return oldAnim.apply(this, arguments);
    };

    function toArray(strg) {
        strg = strg.replace(/left|top/g, '0px');
        strg = strg.replace(/right|bottom/g, '100%');
        strg = strg.replace(/([0-9\.]+)(\s|\)|$)/g, "$1px$2");
        var res = strg.match(/(-?[0-9\.]+)(px|\%|em|pt)\s(-?[0-9\.]+)(px|\%|em|pt)/);
        return [parseFloat(res[1], 10), res[2], parseFloat(res[3], 10), res[4]];
    }

    $.fx.step.backgroundPosition = function(fx) {
        if (!fx.bgPosReady) {
            var start = $.curCSS(fx.elem, 'backgroundPosition');

            if (!start) {//FF2 no inline-style fallback
                start = '0px 0px';
            }

            start = toArray(start);

            fx.start = [start[0], start[2]];

            var end = toArray(fx.options.curAnim.backgroundPosition);
            fx.end = [end[0], end[2]];

            fx.unit = [end[1], end[3]];
            fx.bgPosReady = true;
        }
        //return;
        var nowPosX = [];
        nowPosX[0] = ((fx.end[0] - fx.start[0]) * fx.pos) + fx.start[0] + fx.unit[0];
        nowPosX[1] = ((fx.end[1] - fx.start[1]) * fx.pos) + fx.start[1] + fx.unit[1];
        fx.elem.style.backgroundPosition = nowPosX[0] + ' ' + nowPosX[1];

    };
})(jQuery);
