IndexAd = Class.create();
IndexAd.prototype = {
	initialize: function() {
		// add a class to facilitate noscript
		Element.addClassName('main', 'hasjs');
	
		// initial DOM stuff
		this.content = $('hero_content');
		this.trigger = $('hero_adlink');
		this.target = $('hero_ad');
		this.controllerContainer = $('controller');
		this.hasQuicktime = AC.Detector.isQTInstalled();

		Element.hide(this.target);
		this.getMovieInfo();

		if (AC.Detector.isiPhone()) {
			this.phone();
			return;
		}

		// add the event to the trigger
		this.setTrigger();

		// determine if we need to set the movie or the content
		if (this.isAdRequested()) {
			this.showMovieContainer();
		} else {
			this.showContent();
		}

	},
	
	isAdRequested: function() {
		var string = document.location.hash;
		
		if (string) {
			string = string.toString().replace('#', '');
			if (string == 'ad') return true;
		}
		return false;
	},

	getMovieInfo: function() {
		this.movieUrl = this.trigger.href;
		this.moviePoster = this.trigger.getElementsBySelector('.posterframe')[0].innerHTML.match(/src="(.*)"/)[1];
	},

	setTrigger: function() {
		this.trigger.href = '#ad';
		Event.observe(this.trigger, 'click', this.showMovieContainer.bind(this));
	},
	
	showMovieContainer: function() {
		this.trackAd();

		Effect.Fade(this.content, { duration:.5,
			afterFinish: function() {
				Element.show(this.target);
				this.showMovie();
			}.bind(this)
		});
	},
	
	showMovie: function() {

		var movie = AC.Quicktime.packageMovie('admovie', this.movieUrl, {
			width: 848,
			height: 480,
			autoplay: true,
			showlogo: false,
			controller: false,
			cache: true
		});
		var controller = new AC.QuicktimeController(movie, {
			onMoviePlayable: function() {
				controller.monitorMovie();
			},
			onMovieFinished: function() {
				this.trackEnd();
				this.showContent();
				this.replayed = true;
			}.bind(this)
		});
		this.controller = controller;
		this.target.getElementsByClassName('ad_view')[0].appendChild(movie);
		this.controller.render(this.controllerContainer);
		
		
		//if show movie was requested give option to manually close movie
		var hasCloseButton = !!(this.target.getElementsBySelector('.close')[0]);
		
		if ((this.isAdRequested() || this.replayed) && !hasCloseButton) {
			var closeButton = Builder.node('a', { className:'close', href:'#closead' }, 'Close');
			
			Event.observe(closeButton, 'click', this.showContent.bind(this));
			
			this.target.appendChild(closeButton);
		}
		
		movie = null;
	},
	
	showContent: function() {
		this.trackContent();

		if (this.controller) {
			this.controller.Stop();
			var movie = this.controller.movie;
			this.controller.detachFromMovie();
			this.controllerContainer.innerHTML = '';
			
			// ensure movie is hidden in IE
			movie.style.display = 'none';
			movie.parentNode.removeChild(movie);
			movie = null;
		}
		
		this.target.hide();
		new Effect.Appear(this.content);
	},

	phone: function() {
		var movie = AC.Quicktime.packageMovie('admovie', this.movieUrl, {
			width: 160,
			height: 66,
			posterFrame: this.moviePoster,
			autoplay: false,
			controller: false,
			cache: true
		});

		this.trigger.up().replaceChild(movie, this.trigger);
	},

	trackAd: function() {
		document.title = document.title + ' - Watch the Ad';

		AC.Tracking.trackPage({
			pageName: 'V@S: ' + AC.Tracking.pageName(),
			prop4: this.movieUrl,
			prop13: 'V@S: ' + AC.Tracking.pageName()
		});
	},

	trackEnd: function() {
		AC.Tracking.trackClick({
			prop13: 'V@E: ' + AC.Tracking.pageName()
		}, this, 'o', 'Green Notebook Ad - End');
	},

	trackContent: function() {
		document.title = document.title.replace(' - Watch the Ad', '');
	}
};






Event.onDOMReady(function() {
	new IndexAd();
});
