﻿/// <reference path="jquery-1.5.1-vsdoc.js" />

(function ($) {
    var settings = new Object();
    var timers = new Object();

    $.fn.rotate = function (callerSettings) {
        var $this = $(this);
        var index = $this.attr('id');
        settings[index] = $.extend({
            interval: 7000,
            rotator: '/Home/Banner/',
            hideDuration: 2000,
            hideEasing: 'swing',
            showDuration: 'slow',
            showEasing: 'swing',
            foreground: true,
            simultaneous: false,
            indicator: false,
            indicatorUrl: '',
            indicatorBgColor: '#FFFFFF',
            indicatorHeight: '60px',
            indicatorWidth: '468px',
            indicatorZIndex: '40',
            imageSrc: '../../Content/header02.png',
            imageAlt: 'Florian Strümpf'
        }, callerSettings || {});
        if (settings[index].indicator) createIndicator();
        if (settings[index].foreground) createImage();
        startTimer();

        function createIndicator() {
            if (settings[index].indicatorUrl != '')
                $('<div></div>')
                    .insertBefore($this)
                    .css({
                        'background-color': settings[index].indicatorBgColor,
                        'background-image': 'url("' + settings[index].indicatorUrl + '")',
                        'background-position': 'center center',
                        'background-repeat': 'no-repeat',
                        'height': settings[index].indicatorHeight,
                        'left': '10px',
                        'position': 'absolute',
                        'top': '10px',
                        'width': settings[index].indicatorWidth,
                        'z-index': settings[index].indicatorZIndex
                    });
        }

        function createImage() {
            $('<img id="topmost" />')
                .attr({ src: settings[index].imageSrc, alt: settings[index].imageAlt })
                .insertAfter($this)
                .css({
                    'left': '0px',
                    'position': 'absolute',
                    'top': '0px',
                    'z-index': '10'
                });
        }

        function startTimer() {
            timers[index] = setInterval(startRotation, settings[index].interval);
        }

        function startRotation() {
            clearInterval(timers[index]);
            if (settings[index].foreground) {
                getRotation(function () {
                    $('img#topmost').animate({ opacity: 0 }, settings[index].hideDuration, settings[index].hideEasing,
                        function () {
                            $(this).attr({ 'src': $this.attr('src'), 'alt': $this.attr('alt') }).css({ 'opacity': 1 });
                            startTimer();
                        });
                });
            }
            else {
                $this.animate({
                    opacity: 0
                },
                    settings[index].hideDuration,
                    settings[index].hideEasing,
                    function () { getRotation(); });
            }
        }

        function getRotation(callback) {
            var image;
            if (settings[index].foreground)
                image = $('img#topmost').attr('src').split('/');
            else {
                var urlRegex = /^url\("http:\/\/(.+)"\)$/i;
                var match = urlRegex.exec($this.css('background-image'));
                image = match[1].split('/');
            }
            var id = image[image.length - 1];
            $.getJSON(
                settings[index].rotator + id,
                function (rotation) { endRotation(image, rotation, callback); });
        }

        function endRotation(image, rotation, callback) {
            image.pop();
            image.push(rotation.Id);
            if (settings[index].foreground) {
                $this.attr({ 'src': image.join('/'), 'alt': rotation.Alt });
                callback();
            }
            else {
                $this.css('background-image', 'url("http://' + image.join('/') + '")');
                $this.animate({ opacity: 1 }, settings[index].showDuration, settings[index].showEasing, function () { startTimer(); });
            }
        }
    };
})(jQuery);

//var playerTimer = null;
//var playerCounter = 1;

//$(document).ready(function() {
//    playerTimer = setInterval(hidePlayer, 7000);
//});

//function hidePlayer() {
//    clearInterval(playerTimer);
//    $("div#player").animate({ opacity: 0 }, 2000, "swing", showPlayer);
//}

//function showPlayer() {
//    var player = $(this).css("background-image");
//    if (playerCounter == 1) player = player.replace("1.jpg", "2.jpg");
//    if (playerCounter == 2) player = player.replace("2.jpg", "3.jpg");
//    if (playerCounter == 3) player = player.replace("3.jpg", "1.jpg");
//    $(this).css("background-image", player);
//    $(this).animate({ opacity: 1 }, "slow", "swing");
//    playerCounter += 1;
//    if (playerCounter == 4) playerCounter = 1;
//    playerTimer = setInterval(hidePlayer, 7000);
//}

