packageAd = Class.create();
packageAd.prototype = {
	controller: null,
	
	initialize: function(size, sizes, container, controller) {
		// get the container node
		this.container = $(container);
		this.size = size;
		this.sizes = sizes;
						
		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.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, this.sizes[size].url, {
				width: this.sizes[size].width,
				height: this.sizes[size].height,
				autoplay: true,
				controller: controllerstatus,
				cache: true,
				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.setActiveState(movieController.GetMovieSize());
				}.bind(this)});
				movieController.controllerPanel.addClassName('active');
			}
			
			this.setShare(movieController);
		}
	},
	
	setActiveState: function(bytes) {
		// first remove active class if its on
		$$('.controllerwrap .sizes a').each(function(link) {
			if(link.hasClassName('active')) link.removeClassName('active');
		});
		if(bytes < this.sizes.lowerLimit) {
			document.getElementsByClassName('small')[0].addClassName('active');
		} else if(bytes < this.sizes.upperLimit) {
			document.getElementsByClassName('medium')[0].addClassName('active');
		} else
			document.getElementsByClassName('large')[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.shareId = 103;
				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);

		this.track('End');
	},
	
	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.track('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);
		}
	},

	track: function(state) {
		if (state == 'Replay') {
			AC.Tracking.trackPage({
				pageName: AC.Tracking.pageName(),
				prop4: this.sizes[this.size].url,
				prop6: AC.Tracking.pageName(),
				prop13: AC.Tracking.pageName() + ' - '+ state
			});
		} else if (state == 'End') {
			AC.Tracking.trackClick({
				prop13: AC.Tracking.pageName() + ' - '+ state
			}, this, 'o', AC.Tracking.pageName() + ' - '+ state);
		}
	}
}
