packageAd = Class.create();
packageAd.prototype = {
	controller: null,
	
	sizes: {
		small: { width:320, height:200 },
		medium: { width:480, height:300, bytes:70007542 },
		large: { width:848, height:480, bytes:95786383 }
	},

	initialize: function(size, url, container, controller) {
		// get the container node
		this.container = $(container);
		if (!this.controller) this.controller = 'moviecontroller';

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

		} else {
			// if we're not opera use the custom controller
			if (!AC.Detector.isOpera()) {
				var controllerstatus = false;
				var height = this.sizes[size].height;
				var movieController = new AC.QuicktimeController();
				movieController.render(this.controller);
			} else {
				var controllerstatus = true;
				var height = this.sizes[size].height+16;
			}

			// package the movie
			var movie = AC.Quicktime.packageMovie('ad-'+size, url, {
				width: this.sizes[size].width,
				height: height,
				autoplay: true,
				controller: controllerstatus,
				cache: true,
				bgcolor: '#000000',
				showlogo: false
			});
			
			//this.container.appendChild(movie);
			this.container.insert({top: movie});
			
			// if we're doing the controller, let's attach it
			if (movieController) {
				movieController.attachToMovie(movie, {onMoviePlayable: function() { 
					setTimeout(movieController.Play.bind(movieController), 100);
					movieController.monitorMovie();
					this.setActiveSize(movieController.GetMovieSize());	
				}.bind(this)});
				movieController.controllerPanel.addClassName('active');
				
			}
			
			this.setShare(movieController);
		}
	},
	
	setActiveSize: function(bytes) {
		switch(bytes) {
			case this.sizes.large.bytes:
				$$('.controllerwrap .sizes a.large')[0].addClassName('active');
				break;
			case this.sizes.medium.bytes:
				$$('.controllerwrap .sizes a.medium')[0].addClassName('active');
				break;
			default:
				$$('.controllerwrap .sizes a.small')[0].addClassName('active');
		} 
	},

	setShare: function(movieController) {
		// For Share Video
		var controller = this.controller;
		
		var backToVideo = $$('#backtovideo .toggleshare')[0];
		if(backToVideo.hasClassName('replay')) backToVideo.removeClassName('replay');
		
		// in case the movie needs to be played again
		clearTimeout(this.waitingToLookForFinished);

		// give the movie some time to load before asking if it's finished
		/*var movieCallback = function() {
			return Share.toggleVideo({
				controller: movieController,
				sharetoggles: $$('.sharetoggle')
			});
		}*/

		var movieCallback = function() {
			return this.showSectionEnd(movieController);
		}.bind(this);

		var lookForFinished = function(controller, callback) {
			return function() {
				// try {console.debug("now waiting for finished");} catch(e) {}
				controller.options.onMovieFinished = callback;
			}
		}

		$$('.toggleshare').each(function(toggleshare) {
			Event.observe(toggleshare, 'click', function(evt, movieController) {
				Event.stop(evt);
				Share.toggleVideo({
					controller: movieController,
					sharetoggles: $$('.sharetoggle')
				});
			}.bindAsEventListener(this, movieController))
		});
		
		this.waitingToLookForFinished = setTimeout(lookForFinished(movieController, movieCallback), 10000);
	},
	
	showSectionEnd: function(controller) {
		
		//assuming the user may have jogged to/past the end
		//prevent them from jogging back into movie once controller is hidden
		var endState = this.container.getElementsByClassName('endstate')[0];
		
		this.container.addClassName('loading');
		controller.controllerPanel.removeClassName('active');
		
		controller.Stop();
		controller.hardPaused = true;
		
		controller.Rewind();
		
		new Effect.Appear(endState);
		
		var backToVideo = $$('#backtovideo .toggleshare')[0];
		if(!backToVideo.hasClassName('replay')) backToVideo.addClassName('replay');
		
		if(!this.refreshSet) this.setRefresh(controller);
	},
	
	setRefresh: function(controller) {		
		var replayButtons = document.getElementsByClassName('replay');
		if (replayButtons.length > 0) {
			replayButtons.each(function(btn, i) {
				Event.observe(btn, 'click', function(evt) {
					var el = Event.element(evt);
					if(el.hasClassName('replay') || el.up().hasClassName('replay')) {
						this.refreshDisplay(controller, true);
					}
					Event.stop(evt);
				}.bindAsEventListener(this, controller));
			}.bind(this))
		}
		
		this.refreshSet = true;	
	},

	refreshDisplay: function(controller, replaySection) {
		var endState = $(this.container).getElementsByClassName('endstate')[0];
		endState.hide();
		
		this.container.removeClassName('loading');
		controller.controllerPanel.addClassName('active');

		//if replaying, assume cached don't delay waiting for finished
		clearTimeout(this.waitingToLookForFinished);

		if (typeof(replaySection) != 'undefined' && replaySection) {
			
			controller.Rewind.bind(controller);
			setTimeout(controller.Play.bind(controller), 100);
			
			var backToVideo = $$('#backtovideo .toggleshare')[0];
			if(backToVideo.hasClassName('replay')) backToVideo.removeClassName('replay');
			
		} else {
			this.setShare(controller);
		}

	}
}
