(function ($) {
    $.ux.behavior('Slider', {
        initialize: function () {
            this._pauseRotate = 0;
            this._currentPage = 1;
            this.prevButton = $(".prev", this.element).css("visibility", "hidden");
            this.nextButton = $(".next", this.element);
            if (this.options.lazyLoad) this.imagesLoaded = false;
            if (this.options.updateItemNumbers) {
                this.currentPage = $(".current", this.element);
                this.totalPages = $(".total", this.element);
            }
            this.updateCounts();
            this._currentPageItem = 1;
            if (this.options.slideType == 'fade') {
                $.fn.reverse = [].reverse;
                $("ul.imageSlider", this.element).html($("li", this.element).reverse());
                $("ul li", this.element).show();
            }
            if (this.options.lazyLoad) {
                $("li img:last", this.element).lazyImage({
                    callback: function () {
                        $(this).imageContainer({
                            useParentContainer: true
                        })
                    }
                });
            }
            var $this = this;
            UserAction.add(function () {
                if ($this.options.autoAdvance) {
                    $this.setDelay();
                }
            });
        },
        onclick: $.delegate({
            '.prev': function (element, event) {
                this.prev();
            },
            '.next': function (element, event) {
                this.next();
            }
        }),
        onmouseenter: function () {
            this._pauseRotate = 1;
        },
        onmouseleave: function () {
            this._pauseRotate = 0;
        },
        prev: function () {
            if (this.options.autoAdvance) {
                this.setDelay();
            }
            if (this._currentPage > 1 || this.options.autoRotate) {
                this._currentPage--;
                this._currentPageItem--;
            }
            if (this._currentPageItem < 1) {
                this._currentPageItem = this._totalPages;
            }
            if (this.options.autoRotate && this._currentPage <= 1) {
                if (this.options.slideType == 'fade') {} else {
                    $("li:first", this.element).before($("li:last", this.element));
                    $("ul", this.element).animate({
                        "left": '-=' + this.options.elementWidth + 'px'
                    }, 0);
                }
                this._currentPage++;
            }
            this._checkNav();
            this.nextButton.css("visibility", "visible");
            this.slide('left');
            this.dispatchEvent("Prev");
            return false;
        },
        next: function (fromAutoAdvance) {
            var self = this;
            if (!this.imagesLoaded && this.options.lazyLoad) {
                $('li img:eq(' + (this._totalItems - 2) + ')', this.element).lazyImage({
                    callback: function () {
                        $(this).imageContainer({
                            useParentContainer: true
                        });
                        self.imagesLoaded = true;
                        self.next(fromAutoAdvance);
                    }
                });
            }
            if (this.options.autoAdvance) {
                this.setDelay();
                if (fromAutoAdvance && this._pauseRotate) {
                    return false;
                }
            }
            if (this.imagesLoaded || !this.options.lazyLoad) {
                if (this._currentPage < this._totalPages || this.options.autoRotate) {
                    this._currentPage++;
                    this._currentPageItem++;
                }
                if (this._currentPageItem > this._totalPages) {
                    this._currentPageItem = 1;
                }
                if (this.options.autoRotate && this._currentPage > this._totalItems) {
                    if (this.options.slideType == 'fade') {} else {
                        $("li:last", this.element).after($("li:first", this.element));
                        $("ul", this.element).animate({
                            "left": '+=' + this.options.elementWidth + 'px'
                        }, 0);
                    }
                    this._currentPage--;
                }
                this._checkNav();
                this.prevButton.css("visibility", "visible");
                this.slide('right');
                this.dispatchEvent("Next");
                this.imagesLoaded = false;
            }
            return false;
        },
        _checkNav: function () {
            if (this._currentPage == 1 && !this.options.autoRotate) {
                this.prevButton.css("visibility", "hidden");
            } else {
                this.prevButton.css("visibility", "visible");
            }
            if (this._totalPages <= (this._currentPage) && !this.options.autoRotate) {
                this.nextButton.css("visibility", "hidden");
            } else {
                this.nextButton.css("visibility", "visible");
            }
            if (this.options.updateItemNumbers) {
                this._updateItemCounts();
            }
        },
        _updateItemCounts: function () {
            this.currentPage.html(this._currentPageItem);
        },
        setDelay: function () {
            var self = this;
            this.clearDelay();
            this._slideTimer = setTimeout(function () {
                self.next(true)
            }, this.options.slideDelay);
        },
        clearDelay: function () {
            clearTimeout(this._slideTimer);
        },
        slide: function (dir) {
            var self = this;
            if (dir == "right") {
                if (this.options.slideType == 'fade') {
                    $("ul li:last", this.element).fadeOut('slow', function () {
                        $("li:first", self.element).before($("li:last", self.element));
                        $("li:first", self.element).show();
                    });
                } else {
                    $("ul", this.element).animate({
                        "left": '-=' + (this.options.elementWidth * this.options.numberPerPage) + 'px'
                    });
                }
            } else {
                if (this.options.slideType == 'fade') {
                    $("li:last", this.element).after($("li:first", this.element));
                    $("li:last", this.element).hide().fadeIn('slow', function () {});
                } else {
                    $("ul", this.element).animate({
                        "left": '+=' + (this.options.elementWidth * this.options.numberPerPage) + 'px'
                    });
                }
            }
        },
        updateCounts: function () {
            this._totalItems = $("li", this.element).length;
            this._totalPages = Math.ceil(this._totalItems / this.options.numberPerPage);
            if (this.options.updateItemNumbers) {
                this.totalPages.html(this._totalItems);
            }
            this._checkNav();
        }
    }, {
        autoRotate: true,
        autoAdvance: true,
        slideDelay: 5000,
        updateItemNumbers: false,
        elementWidth: 100,
        numberPerPage: 1,
        slideType: 'slide',
        lazyLoad: true
    });
})(jQuery);
$(document).ready(function () {
    $('.featuredImageWindow').attach('Slider', {
        autoRotate: true,
        elementWidth: 470,
        updateItemNumbers: true,
        slideDelay: 8000,
        slideType: 'fade'
    });
    (function () {
        var scrollWindowOptions = {
            itemList: $("#topicGuidesSlider").find(".guideSlider"),
            marginWidth: 0,
            itemsPerView: 1,
            viewIndicator: $("#topicGuidesSlider .viewIndicator"),
            navLeft: $("#topicGuidesSlider .goLeft"),
            navRight: $("#topicGuidesSlider .goRight")
        };
        dlabs.objects.scrollWindow = new scrollWindow(scrollWindowOptions);
        setTimeout('dlabs.objects.scrollWindow.initAutoRotate($("#topicGuidesSlider"))', 8000);
    })();
    var calloutOptions = {
        style: '2',
        childClassToPad: '.Heading1a'
    };
    (function () {
        var scrollWindow2Options = {
            itemList: $("#featuredIDidThis").find(".featuredIDTList"),
            marginWidth: 0,
            itemsPerView: 1,
            navLeft: $("#featuredIDidThis .goLeft"),
            navRight: $("#featuredIDidThis .goRight")
        };
        if ($('body').hasClass('US')) {
            dlabs.objects.scrollWindow2 = new scrollWindow(scrollWindow2Options);
            setTimeout('dlabs.objects.scrollWindow2.initAutoRotate($("#featuredIDidThis"))', 8000);
        }
    })();
});
(function ($) {
    $.phader = function (element, options) {
        var self = this;
        self.options = $.extend($.phader.defaults, options || {});
        var debug = this.options.debug;
        $.extend(self, {
            element: element,
            isMoving: false,
            isPaused: false,
            isStopped: false,
            sideStage: $('.jsPhaderSideStage', this.element),
            mainStage: $('.jsPhaderMainStage', this.element),
            sideStageHeight: function () {
                return (($('li', self.sideStage).length - self.options.sideStageItemsShown) * self.options.sideStageItemSize.height);
            },
            reverseStages: function () {
                var _tmp = $.fn.reverse;
                $.fn.reverse = [].reverse;
                $("ul", this.mainStage).html($("ul li", this.mainStage).reverse());
                $("ul", this.sideStage).html($("ul li", this.sideStage).reverse());
                $.fn.reverse = _tmp;
            },
            setupTooltips: function () {
                if (self.options.useTooltips) {
                    try {
                        $('.jsPhaderContainer .jsPhaderSideStage li a').tooltip(self.options.tooltipOptions);
                    } catch (e) {}
                }
            },
            setupSideStage: function () {
                $('ul li:last', this.sideStage).insertBefore($('ul li:first', this.sideStage));
                $("ul", this.sideStage).css({
                    bottom: this.sideStageHeight()
                });
                this.setupTooltips();
            },
            setupMainStage: function () {
                var self = this;
                $("li.video a", this.mainStage).bind("click", function () {
                    var $parent = $(this).parent().parent();
                    $(".videoThumbnail", $parent).hide();
                    $(".videoPlayer", $parent).show();
                    self.isStopped = true;
                    $(self.element).trigger("phaderVideoPlaying");
                    return false;
                });
            },
            resetVideos: function () {
                $("li.video .videoThumbnail", this.mainStage).show();
                $("li.video .videoPlayer", this.mainStage).hide();
            },
            _setTimeout: function () {
                $this = this;
                this._main = setTimeout(function () {
                    $this.main()
                }, this.options.delay);
            },
            _clearTimeout: function () {
                clearTimeout(this._main);
            },
            advance: function (forced) {
                if (this.isMoving || (this.isPaused && !forced) || (this.isStopped && !forced)) return;
                this._clearTimeout();
                this.isMoving = true;
                this.imageLoaded = false;
                $this = this;
                $("ul", this.sideStage).animate({
                    bottom: '-=' + this.options.sideStageItemSize.height + 'px'
                }, function () {
                    $("li:last", this).prependTo(this);
                    $(this).css({
                        bottom: $this.sideStageHeight()
                    });
                    $this.isMoving = false;
                    $this._setTimeout();
                    if (self.isStopped) {
                        self.resetVideos();
                        self.isStopped = false;
                    }
                });
                $("ul li:last", this.mainStage).fadeOut('slow', function () {
                    $("li:first", $this.mainStage).before($("li:last", $this.mainStage));
                    $("li:first", $this.mainStage).show();
                });
                this.setupTooltips();
                this.options.onSwitch(forced);
                $(this.element).trigger("phaderAdvance");
                $(this.element).trigger("phaderChanged");
            },
            reverse: function (forced) {
                if (this.isMoving || (this.isPaused && !forced) || (this.isStopped && !forced)) return;
                this._clearTimeout();
                this.isMoving = true;
                this.imageLoaded = false;
                $this = this;
                if (this.isStopped) {
                    this.resetVideos();
                    this.isStopped = false;
                }
                $('ul li:first', this.sideStage).clone().insertAfter($('ul li:last', this.sideStage));
                $("ul", this.sideStage).animate({
                    bottom: '+=' + this.options.sideStageItemSize.height + 'px'
                }, function () {
                    $("li:first", this).remove();
                    $(this).css({
                        bottom: $this.sideStageHeight()
                    });
                    $this.isMoving = false;
                    $this._setTimeout();
                    if (self.videoPlaying) {
                        self.resetVideos();
                        self.videoPlaying = false;
                    }
                });
                $("li:last", this.mainStage).after($("li:first", this.mainStage));
                $("li:last", this.mainStage).hide().fadeIn('slow', function () {});
                this.setupTooltips();
                this.options.onSwitch(forced);
                $(this.element).trigger("phaderReverse");
                $(this.element).trigger("phaderChanged");
            },
            main: function () {
                this.advance(false);
                this._setTimeout();
            }
        });
        self.reverseStages();
        self.setupSideStage();
        self.setupMainStage();
        $(window).load(function () {
            $("img.lazy", self.element).lazyImage({
                callback: function () {
                    $(this).imageContainer({
                        useParentContainer: true
                    });
                },
                completeCallback: function () {
                    self._setTimeout();
                }
            });
            $(self.element).bind("mouseenter", function () {
                self.isPaused = true;
            });
            $(self.element).bind("mouseleave", function () {
                self.isPaused = false;
            });
        });
        $('.jsPhaderAdvance').click(function () {
            self.advance(true);
            return false;
        });
        $('.jsPhaderReverse').click(function () {
            self.reverse(true);
            return false;
        });
    };
    $.phader.defaults = {
        debug: false,
        delay: 5000,
        sideStageItemSize: {
            height: 75,
            width: 98
        },
        sideStageItemsShown: 4,
        useTooltips: true,
        tooltipOptions: {
            offset: [-13, 0],
            useLayout: true
        },
        onSwitch: function () {}
    };
    $.fn.phader = function (options) {
        return this.each(function () {
            var element = $(this);
            if (element.data('phader')) return;
            var phader = new $.phader(element, options);
            element.data('phader', phader);
        });
    }
})(jQuery);
(function ($) {
    $.tooltip = function (element, options) {
        var self = this;
        self.options = $.extend({}, $.tooltip.defaults, options || {});
        $.extend(self, {
            element: element,
            currentTooltip: $(this.options.layout),
            content: '',
            getTooltipContent: function () {
                var content = "";
                if (self.options.selector != '') {
                    if (self.options.useLayout) {
                        content = $(self.options.selector).html();
                    } else {
                        content = self.options.selector;
                    }
                }
                if (content == "" && self.element.attr('name') != "") {
                    if (self.options.useLayout) {
                        content = $("#" + self.element.attr('name')).html();
                    } else {
                        content = "#" + self.element.attr('name');
                    }
                }
                if (self.options.useLayout) {
                    if (content == "" && self.element.attr('title') != "" && self.options.useTitle) {
                        content = self.element.attr('title');
                        self.element.attr("title", "");
                        self.content = content;
                    }
                }
                return content;
            },
            getElementLocation: function () {
                return this.element.offset();
            },
            removeTooltip: function () {
                this.currentTooltip.fadeOut(0, function () {
                    if (self.options.useLayout) {
                        $(this).remove();
                    } else {
                        $(this).hide();
                    }
                    if (self.options.useTitle) self.element.attr('title', self.content);
                });
            },
            createTooltip: function () {
                var content = this.getTooltipContent();
                if (content == "") return false;
                if (this.options.useLayout) {
                    var tooltip = $(this.currentTooltip).addClass(this.options.tipClass).html(content).hide().appendTo('body');
                } else {
                    var tooltip = $(content);
                    self.currentTooltip = tooltip;
                }
                if (self.options.useLayout) {
                    var offsets = {
                        left: self.getElementLocation().left + self.options.offset[0],
                        top: self.getElementLocation().top + self.options.offset[1]
                    };
                    if (self.options.offsetSelfWidth) {
                        offsets.left = offsets.left - tooltip.width();
                    }
                    tooltip.css({
                        position: 'absolute',
                        left: offsets.left,
                        top: offsets.top
                    });
                } else {
                    tooltip.css({
                        left: self.options.offset[0],
                        top: self.options.offset[1]
                    })
                }
                tooltip.show();
            }
        });
        if (self.options.bindParent) {
            var bindElement = element.parent();
        } else {
            var bindElement = element;
        }
        bindElement.hover(function () {
            self._timeout = setTimeout(function () {
                self.createTooltip()
            }, self.options.delay);
        }, function () {
            self.removeTooltip();
            clearTimeout(self._timeout);
        });
    };
    $.tooltip.defaults = {
        offset: [0, 0],
        offsetSelfWidth: true,
        selector: '',
        layout: '<div/>',
        tipClass: 'Tooltip',
        useTitle: false,
        delay: 500,
        useLayout: true,
        bindParent: false
    };
    $.fn.tooltip = function (options) {
        return this.each(function () {
            var element = $(this);
            if (element.data('tooltip')) return;
            var tooltip = new $.tooltip(element, options);
            element.data('tooltip', tooltip);
        });
    };
})(jQuery);
(function ($) {
    $(function () {
        $('.jsPhaderContainer').phader({
            sideStageItemSize: {
                height: 75,
                width: 98
            }
        });
    });
})(jQuery);
