/// <reference path="jquery-1.4.2.min.js" />

(function ($) {

    $.fn.flipBoard = function (params) {

        var defaults = {};
        config = $.extend(defaults, params);
        container = $(this);

        var finishedAnimationCallback;
        var queuedAnimations = new Array();

        // Private methods - Begin
        var log = function (msg) {
            if (window.console && console.log) {
                console.log(msg);
            }
        };

        var runNextAnimation = function (index) {
            var animation = queuedAnimations[index];

            if (animation.type == 'bubble') {
                return showBubble(animation.settings, index + 1);
            }

            container = $('#animationContainer');

            var freeze = false;
            if (animation.settings.freeze != '' && animation.settings.freeze == '1') {
                freeze = true;
            }

            var img;
            if (animation.settings.useFreezed != '') {
                container.children().remove();

                var id = animation.settings.useFreezed.replace('{', '').replace('}', '');
                var freezedContainer = $('#' + id);

                // Append the "freezed" image to the main container.
                // [PP]
                $('img', freezedContainer).appendTo(container);

                // Remove the "freezed" container from the DOM.
                // [PP]
                freezedContainer.remove();
            }
            else {
                container.children().remove();
            }

            for (var i = 0; i < animation.images.length; i++) {
                var image = $('<img style="display: none;" />');
                image.attr('id', 'img_' + i);
                image.attr('src', animation.images[i].href);
                image.css('width', animation.images[i].width);
                image.css('height', animation.images[i].height);

                image.appendTo(container);
            }

            $('img:first', container).css('display', 'block');

            if (animation.images.length > 1) {
                var toggleImageInterval = window.setInterval(function () {

                    $('img', container).toggle();

                }, animation.settings.flipDelay);

            }

            // Set initial position for the current animation.
            // [PP]
            container.css({
                top: getPosition(animation.settings.startPositionY, "height"),
                left: getPosition(animation.settings.startPositionX, "width")
            });

            container.animate({
                top: getPosition(animation.settings.endPositionY, "height"),
                left: getPosition(animation.settings.endPositionX, "width")
            }, animation.settings.sequenceDuration, "linear", function () {
                clearInterval(toggleImageInterval);

                if (freeze)
                    freezeFrame(animation.settings.id.replace('{', '').replace('}', ''));

                if (index + 1 < queuedAnimations.length) {
                    runNextAnimation(index + 1);
                }
                else {
                    finishedAnimationCallback();
                }
            });
        };

        var freezeFrame = function (id) {
            var freezedContainer = container.clone().attr("id", id).addClass("freezedContainer");
            freezedContainer.insertBefore(container);
        };

        var showBubble = function (settings, index) {
            $('div#animationBubble div.bubbleContent').html(settings.text);
            $('div#animationBubble')
                .show()
                .css({
                    top: $('div#animationContainer').position().top - ($('div#animationBubble').height() + 10),
                    left: $('div#animationContainer').position().left
                });

            window.setTimeout(
                function () {
                    $('div#animationBubble').hide();
                    runNextAnimation(index);
                }, settings.duration);
        };

        var getPosition = function (value, dimension) {
            switch (dimension) {
                case "height":
                    return (value * $(window).height()) / 100 - (container.height() / 2);
                case "width":
                    return (value * $(window).width()) / 100 - (container.width() / 2);
            }
        };
        // Private methods - End

        // Public methods - Begin
        this.initialize = function () {
            return this;
        };

        this.queueAnimation = function (type, images, settings) {
            queuedAnimations.push({ type: type, images: images, settings: settings });
        };

        this.start = function (cb) {
            finishedAnimationCallback = cb;
            runNextAnimation(0);
        }; // Public methods - End

        return this.initialize();
    };
})(jQuery);
