﻿$(function () {
    // setup homepage rotator
    Clickfarm.HomePageRotator.Setup(".rotator img", { duration: 3, fadeTime: 600, loop:true });
});


// ensure the clickfarm namespace is available
if (!window.Clickfarm) {
    Clickfarm = new function () {
        this.Name = "Clickfarm";
        this.Origin = "HomeRotator.js";
        this.Version = "1.0";
    };
}

// ensure utility namespace is available
if (!Clickfarm.Util) {
    Clickfarm.Util = new function () {
        this.Name = "Util";
        this.Origin = "HomeRotator.js";
        this.Version = "1.0";
    };
}

// ensure the time namespace is avalable
if (!Clickfarm.Util.Time) {
    Clickfarm.Util.Time = new function () {
        this.Name = "Time";
        this.Origin = "HomeRotator.js";
        this.Version = "1.0";

        this.ReasonableInterval = function (interval) {
            if (interval < 50)
                return interval * 1000; // probably seconds
            else
                return interval; // probably milliseconds
        }
    };
}

// create rotator object
if (!Clickfarm.HomePageRotator) {
    Clickfarm.HomePageRotator = new function () {
        this.Name = "HomePageRotator";
        this.Origin = "HomeRotator.js";
        this.Version = "1.0";
        this.Selector = "";
        this.Items = null;
        this.ActiveItem = -1;
        this.Running = false;
        this.Timer = null;
        this.Interval = 5;         // parameter: duration - duration of each slide
        this.Repeat = true;        // parameter: loop - should slideshow loop
        this.TransitionTime = 300; // parameter: fadeTime - duration of transition

        this.Setup = function (selector, params) {
            this.Selector = selector;
            this.Items = $(this.Selector);
            this.Reset();

            if (params != null) {
                if (params.duration != null) {
                    this.Interval = params.duration;
                }
                if (params.loop != null) {
                    this.Repeat = params.loop;
                }
                if (params.fadeTime != null) {
                    this.TransitionTime = params.fadeTime;
                }
            }

            this.Start();
        }

        this.Reset = function () {
            if (this.Items != null) {
                if (this.Items.length < 1)
                    this.ActiveItem = -1;
                else
                    this.ActiveItem = 0;
                this.Running = false;
                if (this.Timer != null) {
                    clearInterval(this.Timer);
                    this.Timer = null;
                }
                if (this.Items.length > 0) {
                    this.Items.hide();
                    $(this.Items[0]).show();
                }
            }
        }

        this.Start = function () {
            if (!this.Running) {
                this.Running = true;
                this.Timer = setInterval("Clickfarm.HomePageRotator.Next()", Clickfarm.Util.Time.ReasonableInterval(this.Interval));
            }
        }

        this.Next = function () {
            if (this.Items == null || this.Items.length < 2) {
                this.Stop();
                return;
            }

            var nextItem = this.ActiveItem + 1;
            if (nextItem > this.Items.length - 1)
                nextItem = 0;

            var looped = false;
            if (nextItem == 0) {
                if (!this.Repeat) {
                    this.Stop();
                    return;
                }
                else
                    looped = true;
            }

            if (looped)
                $(this.Items[nextItem]).fadeIn(0, function () { Clickfarm.HomePageRotator.HideNotActive(nextItem, looped); });
            else
                $(this.Items[nextItem]).fadeIn(this.TransitionTime, function () { Clickfarm.HomePageRotator.HideNotActive(nextItem, looped); });
        }

        this.HideNotActive = function (nextItem, looped) {
            if (looped) {
                for (var x = 1; x < this.Items.length - 1; x++)
                    $(this.Items[x]).hide();
                $(this.Items[this.ActiveItem]).fadeOut(this.TransitionTime);
            }

            this.ActiveItem = nextItem;
        }

        this.Stop = function () {
            if (this.Running) {
                clearInterval(this.Timer);
                this.Running = false;
            }
        }

    };
}


