var slider = Class.create();
Object.extend(slider.prototype, Event.Listener);
Object.extend(Object.extend(slider.prototype, AC.Slider.prototype), {
    createArrows: function() {
        this.prevArrow = Builder.node("a", {
            "class": "ACSliderPreviousArrow"
        },
        "&lt;");
        Event.observe(this.prevArrow, "click", function(a) {
            Event.stop(a);
            if (this.currentPage != 1) {
                this.getPrevious()
            }
        }.bind(this));
        this.container.appendChild(this.prevArrow);
        this.nextArrow = Builder.node("a", {
            "class": "ACSliderNextArrow"
        },
        "&gt;");
        Event.observe(this.nextArrow, "click", function(a) {
            Event.stop(a);
            if (this.currentPage != this.totalPages) {
                this.getNext()
            }
        }.bind(this));
        this.container.appendChild(this.nextArrow)
    },
    populate: function() {
        var a = $$(".slideritem");
        for (var d = 0; d < a.length; d++) {
            var c = a[d].down();
            var b = new AC.SliderItem(c);
            this.items.push(b);
            a[d].remove()
        }
        this.render(7)
    },
    scrolltoPageNumber: function(g, f) {
        if (this.currentPage != g && !this.isAnimating) {
            if (!f) {
                var f = this.currentPage - g
            }
            var c = (f == 1) ? "Left Arrow": "Right Arrow";
            AC.Tracking.trackClick({
                prop3: AC.Tracking.pageName() + " - Slider Button - " + c
            },
            this, "o", AC.Tracking.pageName());
            this.isAnimating = true;
            this.currentPage = g;
            this.resetPages();
            var e = this.pageNav.getElementsByTagName("a");
            for (var d = 0, b; b = e[d]; d++) {
                if (Element.hasClassName(b, "active")) {
                    Element.removeClassName(b, "active")
                }
                if (b.innerHTML == this.currentPage) {
                    Element.addClassName(b, "active")
                }
            }
            var a = {
                duration: 0.5,
                queue: {
                    position: "end",
                    scope: "sliderQueue",
                    limit: 1
                },
                afterFinish: this.afterScroll.bind(this)
            };
            if (!this.maskInnerDimension) {
                this.positionWithinMask(this.container)
            }
            if (this.orientation == "horizontal") {
                new Effect.MoveBy(this.list, 0, this.maskInnerDimension * f, a)
            } else {
                new Effect.MoveBy(this.list, this.maskInnerDimension * f, 0, a)
            }
        } else {
            this.resetArrows()
        }
    },
    resetArrows: function() {
        this.prevArrow.removeClassName("inactive");
        this.nextArrow.removeClassName("inactive");
        if (this.currentPage == 1) {
            this.prevArrow.addClassName("inactive")
        } else {
            if (this.currentPage == this.totalPages) {
                this.nextArrow.addClassName("inactive")
            }
        }
    },
    resetPages: function() {
        this.resetArrows();
        if (this.currentPage == 0) {
            this.currentPage = this.totalPages;
            this.positionOffset = -this.maskInnerDimension * this.totalPages + "px";
            this.prevArrow.addClassName("inactive")
        } else {
            if (this.currentPage == 1) {
                this.currentPage = 1;
                this.positionOffset = -this.maskInnerDimension + "px"
            } else {
                if (this.currentPage == this.totalPages + 1) {
                    this.currentPage = 1;
                    this.positionOffset = -this.maskInnerDimension + "px";
                    this.nextArrow.addClassName("inactive")
                } else {
                    this.positionOffset = false
                }
            }
        }
    }
});
Event.onDOMReady(function() {
    var d = {
        trackingNameForSection: function(n, m, o) {
            n.count++;
            if (m.match("design_overlay")) {
                return false
            }
            if (m.match("Step")) {
                return false
            }
            if (n.count > 1) {
                return m
            }
            return false
        },
        sectionDidChange: function(p, n, m, q, o) {
            o.prop25 = o.pageName;
            return o
        },
        QTdidBegin: function(n, m) {
            m.prop25 = m.pageName;
            return m
        }
    };
    var k = new AC.ViewMaster.Tracker("click");
    k.setDelegate(d);
    $$("a.slideshow-action").each(function(m) {
        m.observe("mousedown", function(n) {
            AC.Tracking.trackClick({
                prop3: AC.Tracking.pageName() + " - Play Sildeshow Button (JP)"
            },
            this, "o", AC.Tracking.pageName() + " - Play Slideshow Button")
        })
    });
    var h = function(n) {
        var o = n.event_data.data.sender;
        var m = n.event_data.data.incomingView;
        if (o && typeof(o.setOverlayShadowImageSrc) == "function" && m) {
            if (m.id == "designvideo") {
                o.setOverlayShadowImageSrc("http://images.apple.com/macbookpro/images/overlay-videobg20081014.png")
            } else {
                AC.Tracking.trackPage({
                    pageName: AC.Tracking.pageName() + " - Overlay (JP)"
                });
                o.setOverlayShadowImageSrc("http://images.apple.com/macbookpro/images/overlay-designbg20081014.png")
            }
        }
    };
    Event.Listener.listenForEvent(AC.ViewMaster, "ViewMasterWillShowNotification", false, h);
    var f = $$("#design_overlay > ul > li > a.overlaythumb");
    var a = new AC.ViewMaster.SlideshowViewer(f, "designSwapView", "overlaythumb", null, {
        parentSectionId: "design_overlay",
        parentTriggerClassName: "OverlayPanel",
        silentTriggers: true,
        delay: 6000,
        willEnd: true,
        triggerEvent: ["overlay:clicked", "click"]
    });
    var b = {
        willShow: function(o, n, m) {
            if (m && m.id === "unibody") {}
        }
    };
    a.setDelegate(b);
    var l = new slider("slider");
    l.direction = -1;
    var j = {
        isGalleryViewMasterSlideshowPlaying: false,
        isGalleryViewMasterSlideshowPaused: false,
        isMotionPicturePlaying: false,
        motionPicturePlaying: null,
        shouldStartGalleryViewMasterSlideshow: false,
        _stopSlideShow: function() {
            this.isGalleryViewMasterSlideshowPlaying = false;
            this.shouldStartGalleryViewMasterSlideshow = false;
            if (this.isMotionPicturePlaying) {
                this.motionPicturePlaying = null;
                this.isMotionPicturePlaying = false
            }
            a.slideshow.stop();
            this.slideShowTrigger.removeClassName("playing")
        },
        sliderClicked: function(m) {
            if (this.isGalleryViewMasterSlideshowPlaying === true) {
                this._stopSlideShow()
            }
        },
        _slideShowProgressionNavDisplay: null,
        startStopSlideShow: function(m) {
            if (!this.isGalleryViewMasterSlideshowPlaying == true) {
                this.isGalleryViewMasterSlideshowPlaying = true;
                if (this.isMotionPicturePlaying) {
                    this.shouldStartGalleryViewMasterSlideshow = true
                } else {
                    if (this.isGalleryViewMasterSlideshowPlaying === true) {
                        this.isGalleryViewMasterSlideshowPaused = true;
                        a.slideshow.start()
                    }
                }
                this.slideShowTrigger.addClassName("playing");
                this._slideShowProgressionNavDisplay = j.slideShowProgressionNav.style.display;
                j.slideShowProgressionNav.style.display = "none"
            } else {
                if (this.isMotionPicturePlaying) {
                    this.shouldStartGalleryViewMasterSlideshow = false
                } else {
                    a.slideshow.stop()
                }
                this.isGalleryViewMasterSlideshowPlaying = false;
                this.slideShowTrigger.removeClassName("playing");
                j.slideShowProgressionNav.style.display = this._slideShowProgressionNavDisplay
            }
        },
        slideShowNextAction: function(m) {
            m.stop();
            AC.Tracking.trackClick({
                prop3: AC.Tracking.pageName() + " - Next Button"
            },
            this, "o", AC.Tracking.pageName());
            this.prepareForManualNavigationWhilePlaying();
            a.next()
        },
        prepareForManualNavigationWhilePlaying: function() {
            if (this.isGalleryViewMasterSlideshowPlaying == true) {
                a.slideshow.stop();
                if (this.isMotionPicturePlaying) {
                    this.motionPicturePlaying = null;
                    this.isMotionPicturePlaying = false
                }
                a.slideshow.start()
            }
        },
        slideShowPreviousAction: function(m) {
            m.stop();
            AC.Tracking.trackClick({
                prop3: AC.Tracking.pageName() + " - Back Button"
            },
            this, "o", AC.Tracking.pageName());
            l.direction = 1;
            this.prepareForManualNavigationWhilePlaying();
            a.previous()
        },
        motionPictureDidStart: function(m) {
            var n = m.event_data.data;
            if (n === a.slideshow) {
                this.isGalleryViewMasterSlideshowPaused = true;
                if (this.isMotionPicturePlaying) {
                    this.shouldStartGalleryViewMasterSlideshow = true;
                    a.slideshow.stop()
                }
            } else {
                this.isMotionPicturePlaying = true;
                this.motionPicturePlaying = n;
                if (this.isGalleryViewMasterSlideshowPaused) {
                    this.shouldStartGalleryViewMasterSlideshow = true;
                    a.slideshow.stop()
                }
            }
        },
        motionPictureDidEnd: function(m) {
            var n = m.event_data.data;
            if (n === a.slideshow) {
                if (a.getNextSection() == a.getFirstSection()) {
                    this.isGalleryViewMasterSlideshowPaused = false;
                    this._stopSlideShow();
                    a.reset()
                }
            } else {
                if (this.motionPicturePlaying === n) {
                    this.isMotionPicturePlaying = false;
                    this.motionPicturePlaying = null;
                    if (this.isGalleryViewMasterSlideshowPlaying && this.shouldStartGalleryViewMasterSlideshow) {
                        this.shouldStartGalleryViewMasterSlideshow = false;
                        if (m.event_data.event_name === "didFinishMovie") {
                            a.slideshow.setProgress(a.slideshow.delay() * 2 / 3)
                        } else {
                            a.slideshow.setProgress(a.slideshow.delay())
                        }
                        a.slideshow.start()
                    }
                }
            }
        }
    };
    Object.extend(j, Event.Listener);
    window.pageController = j;
    j.listenForEvent(AC.ViewMaster, "ViewMasterDidShowNotification", false, function(m) {
        var o = m.event_data.data,
        n;
        if (a === o.sender) {
            n = o.sender.indexOfSection(o.incomingView);
            l.scrolltoPageNumber(Math.floor((n / 7) + 1), l.direction);
            if (l.direction === 1) {
                l.direction = -1
            }
        }
    });
    j.listenForEvent(document.event, "didStart", false, j.motionPictureDidStart.bind(j));
    j.listenForEvent(document.event, "didEnd", false, j.motionPictureDidEnd.bind(j));
    j.listenForEvent(document.event, "didFinishMovie", false, j.motionPictureDidEnd.bind(j));
    j.slideShowTrigger = $("play_slideshow");
    j.slideShowProgressionNav = $("progression_nav");
    if (j.slideShowTrigger) {
        Event.observe(j.slideShowTrigger, "click", j.startStopSlideShow.bindAsEventListener(j))
    }
    if ($("slideshow-previous")) {
        Event.observe($("slideshow-previous"), "click", j.slideShowPreviousAction.bindAsEventListener(j))
    }
    if ($("slideshow-next")) {
        Event.observe($("slideshow-next"), "click", j.slideShowNextAction.bindAsEventListener(j))
    }
    Event.observe($("slider"), "click", j.sliderClicked.bindAsEventListener(j));
    var e = window.location.hash.split("#")[1];
    var g = document.getElementsByClassName("overlaythumb"),
    c;
    for (i = 0, c;
    (c = g[i]); i++) {
        if (c.getAttribute("href").match(new RegExp("#" + e + "(?![_w-])"))) {
            window.setTimeout(function() {
                c.fire("overlay:clicked")
            },
            300);
            return
        }
    }
});
