(function($) {
    $.hss = function(i, c, t, r) {
        this.container = $(i);
        this.templates = t || $.hss.t;
        this.timer = null;
        this.statusBar = null;
        this.display = null;
        this.content = c;
        this.statusText = null;
        this.nextButton = null;
        this.animating = false;
        this.pauseTimeout = null;
		this.rootUrl = r;

        this.init_();
    }

    $.hss.prototype.init_ = function() {
        this.initDisplay_();
        this.initStatus_();
    }

    $.hss.prototype.goToCurrentUrl = function() {
        if (!this.animating) {
            var s = this.display.scroller.children()[0];
            if (s != null) {
                this.clearPauseTimeout();
                this.timer.reset();

                var json = $(s).data("json");

                if (json.target != null) {
                    window.open(json.url, json.target);
                } else {
                    document.location.href = json.url;
                }
            }
        }
    }

    $.hss.prototype.initDisplay_ = function() {
        this.display = new $.hss.display(this.templates, $.proxy(this, "goToCurrentUrl"), this.rootUrl)
        this.container.append(this.display.element);

        for (var i in this.content) {
            var obj = this.content[i];
            this.display.appendImage(obj);
        }
    }

    $.hss.prototype.initStatus_ = function() {
        this.statusBar = this.templates.statusBar(this.rootUrl);

        var h = new $.hss.timer(5000, this.templates, this.rootUrl);
        var obj = this;
        h.complete = function(o) {
            obj.animating = true;
            obj.display.next(function(o) {
                obj.animating = false;
                obj.setContent(o);
                h.start();
            });
        };

        this.timer = h;

        this.statusText = new $.hss.statusText(this.templates, $.proxy(this, "goToCurrentUrl"));
        this.nextButton = new $.hss.nextButton(this.templates, $.proxy(this, "advance"), this.rootUrl);

        this.statusBar.append(this.templates.statusItem(this.timer.element, 51, 50, 0, 0, "first"));
        this.statusBar.append(this.templates.statusItem(this.statusText.element, 865, 50, 51, 0));
        this.statusBar.append(this.templates.statusItem(this.nextButton.element, 44, 50, 916, 0, "last"));
        this.container.append(this.statusBar);
    }

    $.hss.prototype.advance = function() {
        if (!this.animating) {
            this.clearPauseTimeout();

            this.animating = true;
            this.timer.reset();
            var obj = this;
            this.display.next(function(o) {
                obj.animating = false;
                obj.setContent(o);
                obj.pauseTimeout = setTimeout(function() {
                    obj.timer.start();
                }, 2000)
            });
        }
    }

    $.hss.prototype.clearPauseTimeout = function() {
        if (this.pauseTimeout != null) {
            clearTimeout(this.pauseTimeout);
            this.pauseTimeout = null;
        }
    }

    $.hss.prototype.setContent = function(o) {
        this.statusText.setText(o.title);
    }

    $.hss.prototype.start = function() {
        if (this.content.length > 0)
            this.setContent(this.content[0]);

		if (this.content.length > 1)
            this.timer.start();
		else
			this.nextButton.element.css("display", "none");
    }

    $.hss.nextButton = function(t, cb, rootUrl) {
        this.templates = t || $.hss.t;
        this.element = this.templates.nextButton(cb, rootUrl);
    }

    $.hss.statusText = function(t, cb) {
        this.templates = t || $.hss.t;
        this.element = this.templates.statusText(cb);
    }

    $.hss.statusText.prototype.setText = function(t) {
        this.element.text(t);
    }

    $.hss.display = function(t, cb, rootUrl) {
        this.templates = t || $.hss.t;
        this.element = this.templates.display(cb, rootUrl);
        this.scrollContainer = this.element.find(".step-display-scrolling-container").first();
        this.scroller = this.element.find(".step-display-scroller").first();
    }

    $.hss.display.prototype.next = function(cb) {
        var obj = this;

        $.each(this.scroller.children(), function(k, v) {
            $(v).animate({ "left": "-=" + obj.scrollContainer.width() }, 500, "swing", function() {

                if (k == 0) {
                    obj.scroller.remove(v);
                    obj.scroller.append(v);
                    $(v).css("left", obj.scrollContainer.width() * (obj.scroller.children().length - 1));
                    if (cb != null) cb($(obj.scroller.children()[0]).data("json"));
                }
            });
        });
    }

    $.hss.display.prototype.appendImage = function(obj) {
        var img = this.templates.image(obj);
        img.css("left", this.scroller.children().length * this.scrollContainer.width());
        img.data("json", obj);
        this.scroller.append(img);
    }

    $.hss.timer = function(d, t, r) {
        this.duration = d;
        this.templates = t || $.hss.t;
        this.complete = null;
        this.animation = $.hss.timer.animation.interval;
        this.clearAnimation = $.hss.timer.animation.clear_interval;
        this.animationData = null;

        this.element = this.templates.timer(r);
        this.interval = null;
    }

    $.hss.timer.animation = {
        clear_smooth: function(obj) {
        },

        smooth: function(obj) {
            var base = obj.duration / 8;
            var count = obj.element.children().length;
            var obj = obj;
            $.each(obj.element.children(), function(k, v) {
                var func = null;

                if (k + 1 == count) func = obj.complete;

                $(v).fadeIn((k + 1) * base, func);
            });

            return null;
        },

        clear_interval: function(obj) {
            for (var n in obj) {
                clearTimeout(obj[n]);
                obj[n] = null;
            }
        },

        interval: function(obj) {
            var base = obj.duration / 8;
            var count = obj.element.children().length;
            var obj = obj;
            var timeouts = [];
            $.each(obj.element.children(), function(k, v) {
                var func = null;

                if (k + 1 == count) func = obj.complete;

                var t = setTimeout(function() {
                    $(v).fadeIn(base, func);
                }, k * base);
                timeouts[timeouts.length] = t;
            });

            return timeouts;
        },

        clear_longInterval: function(obj) {
            for (var n in obj) {
                clearTimeout(obj[n]);
                obj[n] = null;
            }
        },

        longInterval: function(obj) {
            var base = obj.duration / 8;
            var count = obj.element.children().length;
            var obj = obj;
            var timeouts = [];
            $.each(obj.element.children(), function(k, v) {
                var func = null;

                if (k + 1 == count) func = obj.complete;

                var t = setTimeout(function() {
                    $(v).fadeIn(base * 2, func);
                }, k * base);

                timeouts[timeouts.length] = t;
            });

            return timeouts;
        }
    }

    $.hss.timer.prototype.reset = function() {
        this.clearAnimation(this.animationData);
        this.animationData = null;

        $.each(this.element.children(), function(k, v) {
            $(v).stop(true);
            $(v).css({ "display": "none", "opacity": 1.0 });
        });
    }

    $.hss.timer.prototype.start = function() {
        this.reset();
        this.animationData = this.animation(this);
    }

    /* Home Step Scroller Templates */
    $.hss.t = {
        nextButton: function(cb, rootUrl) {
            var c = $("<div>").addClass("next-button").css({ "background-image": "url(" + rootUrl + "Images/Home/StepScroller/next-button.png)", "background-repeat": "no-repeat", "width": 43, "height": 50, "cursor": "pointer" });
            c.mouseover(function() {
                var pos = c.css("background-position");
                if (pos == 'undefined' || pos == null) {
                    $(this).css("background-position-x", "-43px");
                } else {
                    $(this).css("background-position", "(-43px, 0px)");
                }
            }).mouseout(function() {
                var pos = c.css("background-position");
                if (pos == 'undefined' || pos == null) {
                    $(this).css("background-position-x", "0px");
                } else {
                    $(this).css("background-position", "0px, 0px");
                }
            }).click(function() {
                if (cb != null) cb();
            });
            return c;
        },

        statusText: function(cb) {
            var c = $("<div>").addClass("status-text").css({ "width": 863, "height": 50, "overflow": "hidden" });
            c.css({ "font-family": "Helvetica Neue, Helvetica, Arial, sans-serif", "color": "#FFF", "font-size": "18px", "line-height": "50px", "padding-left": "10px", "cursor": "pointer" })
            c.click(function() {
                if (cb != null) cb();
            });
            return c;
        },

        image: function(obj) {
            var c = $("<img>").addClass("step-image").attr({ "src": obj.src, "width": 960, "height": 400 }).css({ "display": "block", "position": "absolute", "top": 0, "left": 0, "width": 960, "height": 400 });

            return c;
        },

        display: function(cb, rootUrl) {
            var c = $("<div>").addClass("step-display").css({ "position": "absolute", "width": 960, "height": 400, "top": 0, "left": 0, "background": "#000" });
            var sc = $("<div>").addClass("step-display-scrolling-container").css({ "position": "absolute", "width": 960, "height": 400, "top": 0, "left": 0, "overflow": "hidden" });
            c.append(sc.append($("<div>").addClass("step-display-scroller").css({ "position": "absolute", "top": 0, "left": 0 })));
            var ht = $("<div>").addClass("step-display-hit-test").css({ "position": "absolute", "width": 960, "height": 400, "top": 0, "left": 0, "background": "transparent url(" + rootUrl + "Images/Home/StepScroller/display-background.png) no-repeat", "cursor": "pointer" });
            ht.click(function() {
                if (cb != null) cb();
            });
            return c.append(ht);
        },

        statusBar: function(rootUrl) {
            return $("<div>").addClass("status-bar").css({ "position": "absolute", "width": 960, "height": 50, "background": "#98ad1b url(" + rootUrl + "Images/Home/StepScroller/status-bar-background.png) no-repeat", "top": 400, "left": 0 });
        },

        statusItem: function(child, width, height, x, y, position) {
            var borders = 2;
            if (position == 'first') borders = 1;
            else if (position == 'last') borders = 1;

            var si = $("<div>").addClass("status-item").css({ "position": "absolute", "width": width - borders, "height": height, "left": x, "top": y });

            if (position == 'first') si.css("border-right", "solid 1px #86993b");
            else if (position == 'last') si.css("border-left", "solid 1px #b3c366");
            else si.css({ "border-right": "solid 1px #86993b", "border-left": "solid 1px #b3c366" });

            if (child != null) si.append(child);

            return si;
        },

        timer: function(rootUrl) {
            var t = $("<div>").addClass("timer").css({ "position": "absolute", "background": "transparent url(" + rootUrl + "Images/Home/StepScroller/background.png) no-repeat", "height": 34, "width": 34, "top": 8, "left": 8 });
            var p = [{ x: 18, y: 5 }, { x: 21, y: 9 }, { x: 21, y: 18 }, { x: 18, y: 21 }, { x: 9, y: 21 }, { x: 5, y: 18 }, { x: 5, y: 9 }, { x: 9, y: 5}]

            $.each(p, function(k, v) {
                var c = $("<div>").css({ "display": "none", "background": "transparent url(" + rootUrl + "Images/Home/StepScroller/" + (k + 1) + ".png) no-repeat", "height": 25, "width": 25, "position": "absolute", "top": v.y, "left": v.x });
                t.append(c);
            });

            return t;
        }
    }
})(jQuery);
