﻿//Procedural code style used

function InitFlashSpots()
{
    var parameters = { wmode: 'transparent' };
    //init all flash movies
    var init = function()
    {
        var self = $(this);
        var flashfile = self.find("input[name='flash']");
        if (flashfile.length == 1)
        {
            var link = self.find('a.link');
            if (link.length == 1)
            {
                self.parent().click(function()
                {
                    location.href = link.attr("href");
                });
            }

            self.flash(flashfile.val(),
            {
                klass: "flash",
                paremeters: parameters,
                flashvars: self.find("input[name='text'],input[name='link'],input[name='image']").JSONSerialize()
            });
            self = null;
            flashfile = null;
            paramtext = null;
            paramlink = null;
        }
    }

    $(".flash div").each(init);
}

function InitRotatingSpots()
{
    $(".spot.rotate").Rotate();
}

(function($)
{
    $.fn.JSONSerialize = function()
    {
        var hash = {};
        this.each(function()
        {
            hash[this.name] = this.value;
        });
        return hash;
    };

})(jQuery);

(function($)
{
    var settings = {
        waittime: 2100,
        fade: "normal"
    };

    $.fn.Rotate = function(options)
    {
        settings = $.extend(settings, options);

        //filter out the ones widt 1 child
        var selected = this.filter(function() { return $(this).children().length > 1 });
        var count = selected.length;
        var current = 0;

        var rotate = function()
        {
            selected.eq(current).children(":visible").fadeOut(settings.fade, function()
            {
                var self = $(this);

                current++;
                if (current >= count)
                {
                    current = 0;
                }

                var next = (!self.is(":last-child") ? self.next() : self.parent().children(":first-child"));
                next.fadeIn(settings.fade, function()
                {
                    self.removeClass("hide");
                    setTimeout(rotate, settings.waittime);

                });

            });
        };

        if (count > 0)
        {
            setTimeout(rotate, settings.waittime);
        }

    };
})(jQuery);

$(function()
{
    InitFlashSpots();
    InitRotatingSpots();
});