packageAd = Class.create();
packageAd.prototype = {
	
	initialize: function(url, fullscreenurl, posterurl, container, width, height) {
		// get the container node
		this.container = $(container);
		
		if (!AC.Detector.isQTInstalled()) {
			// if we don't have QT, display the download stuff
			this.container.getElementsByClassName('noqt')[0].style.display = 'block';
            return;
		}
		
		this.url = url;
		this.width = width;
		this.height = height;
		this.fullscreenurl = fullscreenurl;
		this.posterurl = posterurl;
		
		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("endmovie");
		if (endStates.length === 0) {
            return;
		}
		
		this.endmovie = $(endStates[0]);
		
		$(this.endmovie).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.endmovie).addClassName("hidden");
				this.playMovie();
			}
		}.bind(this));
	},
	
	playMovie: function() {
		
		this.endmovie.parentNode.removeChild(this.endmovie);
		
		//ensure movie is visible
		this.container.removeClassName('loading');
		
		// package the movie
		this.movie = AC.Quicktime.packageMovie('ruhressentials', this.url, {
			src: this.posterurl,
			width: this.width,
			height: this.height,
			href: this.fullscreenurl,
			target: 'quicktimeplayer',
			kioskmode: true,
			bgcolor: '#3C3A3A',
			autoplay: true,
			controller: false,
			volume: 50,
			cache: true
		});
		this.container.appendChild(this.movie);
		this.attachController();
	},
	
	attachController: function() {
		
		if (!this.movieController) {
		    return;
	    }
	    
		this.movieController.attachToMovie(this.movie, {
		    onMoviePlayable: this.movieController.monitorMovie.bind(this.movieController),
			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.endmovie.setOpacity(0);
		
		this.container.appendChild(this.endmovie);
        $(this.endmovie).removeClassName("hidden");
        new Effect.Appear(this.endmovie, {duration: 2});
    }
}

/*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;
		});
	}
});*/
