packageAd = Class.create({

    sizes: {
        small: { width:320, height:200 },
        medium: { width:640, height:400 },
        large: { width:848, height:528 }
    },

    initialize: function(size, url, container, controllerContainerId) {
        // get the container node
        this.container = $(container);
        this.setShare();

        if (!AC.Detector.isQTInstalled()) {
            // if we don't have QT, display the download stuff
            this.container.getElementsByClassName('noqt')[0].style.display = 'block';
            return;
        }

        this.size = this.sizes[size];
        this.url = url;

        if (!controllerContainerId) {
            var controllerContainerId = 'moviecontroller';
        }

        this.movieController = new AC.QuicktimeController();
        this.movieController.render(controllerContainerId);
        this.movieController.controllerPanel.addClassName('active');

        this.initEndState();
        this.playMovie();
    },

    initEndState: function() {
        var endStates = this.container.getElementsByClassName("endstate");
        if (endStates.length === 0) {
            return;
        }

        this.endstate = $(endStates[0]);

        $(this.endstate).observe('click', function(evt) {
            var trigger = evt.element();

            while (trigger.tagName != 'A' && trigger.tagName != 'BODY') {
                trigger = $(trigger.parentNode);
            }

            if (trigger.hasClassName('replay')) {
                evt.preventDefault();
                $(this.endstate).addClassName("hidden");

                var movieName = 'V@R: ' + AC.Tracking.pageName();
		        AC.Tracking.trackClick({prop13: movieName}, this, 'o', movieName);
		
				this.playMovie();
            }
        }.bind(this));
    },

    playMovie: function() {

        this.endstate.parentNode.removeChild(this.endstate);

        //ensure movie is visible
        this.container.removeClassName('loading');

        // package the movie
        this.movie = AC.Quicktime.packageMovie('ad-'+this.size.width, this.url, {
            width: this.size.width,
            height: this.size.height + 16,
            autoplay: true,
            controller: false,
            cache: true
        });
        this.container.appendChild(this.movie);
        this.attachController();

    },

    attachController: function() {

        if (!this.movieController) {
            return;
        }
		
        this.movieController.attachToMovie(this.movie, {
            onMoviePlayable: function() {
				this.movieController.monitorMovie.bind(this.movieController);
				if (!this._hasPlayed) {
					this._hasPlayed = true;
				}
			}.bind(this),
            onMovieFinished: this._showEndState.bind(this)
        });
    },

    _showEndState: function() {

        this.movieController.Stop();
        this.movieController.detachFromMovie();
        this.container.removeChild(this.movie)
        this.container.innerHTML = '';
        this.movie = null;

        // filling content
        this.endstate.setOpacity(0);

        var buttons = this.endstate.getElementsByClassName("pillbutton");

        for (var i = buttons.length - 1; i >= 0; i--){
            $(buttons[i]).setOpacity(0);
        }

        this.container.appendChild(this.endstate);
        $(this.endstate).removeClassName("hidden");
        new Effect.Appear(this.endstate, {duration: 2, afterFinish: function() {
            for (var i = 0; i < buttons.length; i++) {
                new Effect.Appear(buttons[i]);
            }
        }});


        movieName = "V@E: " + AC.Tracking.pageName();
        AC.Tracking.trackClick({prop13: movieName}, this, 'o', movieName);
    },

    setShare: function() {
        var backToVideo = $$('#backtovideo .toggleshare')[0];
        if (backToVideo.hasClassName('replay')) {
            backToVideo.removeClassName('replay');
        }

        var shareToggles = document.getElementsByClassName('toggleshare');
        for (var i = 0; i < shareToggles.length; i++) {
            var toggle = $(shareToggles[i]);

            toggle.observe('click', function(evt) {
                Event.stop(evt);
                Share.toggleVideo({
                    controller: this.movieController,
                    sharetoggles: shareToggles
                });

                if (!!Share.shown) {
                    //hide the current movie
                    if (this.movieController) {
                        this.container.addClassName('loading');
                        this.movieController.controllerPanel.hide();
                    }

                    var endState = $$('#moviecontainer .endstate');
                    if (endState[0]){
                        endState[0].hide();
                    }

                    var extras = document.getElementsByClassName("controllerwrap");
                    if (extras[0]) {
                        $(extras[0]).hide();
                    }

                    new Effect.Appear('sharecontainer');
                } else {
                    new Effect.Fade('sharecontainer', {
                        afterFinish: function() {
                            if (this.movieController) {
                                this.container.removeClassName('loading');
                                this.movieController.controllerPanel.show();
                            }
                            var endState = $$('#moviecontainer .endstate');
                            if (endState[0]){
                                endState[0].show();
                            }

                            var extras = document.getElementsByClassName("controllerwrap");
                            if (extras[0]) {
                                $(extras[0]).show();
                            }
                            if (this.movieController) {
                                setTimeout(this.movieController.Play.bind(this.movieController), 100);
                            }
                        }.bind(this)
                    });
                }

            }.bind(this));
        }
    }
});

Event.observe(window, 'load', function() {
    if ($('adsthumbs')) {
        // redirect the page to size we're on, instead of just the index...
        var match = window.location.pathname.match(/([^\/]+\.html)$/);
        var vidPage = (match == null || match[0] == "index.html" ? "" : match[0]);
        $A($('adsthumbs').getElementsByTagName("a")).each(function(anchor) {
            anchor.href = anchor.href + vidPage;
        });
    }
});
